Source: models/fctmtokens.js

const EventEmitter = require('events');

/**
 * Class represents fcmtokens in ALARMiator Core
 * @class
 * @author Jens Dinstühler
 */
class Fcmtokens extends EventEmitter {

    /**
     * @constructor
     * @param {object} logger logger instance to be used by this class instance.
     */
    constructor(logger) {
        super();
        this.logger = logger;
    }

    /**
     * Returns all FCM Tokens from core database
     * @param {function} callback rows or null
     */
    getAllFCMTokens(callback) {
        let query = "SELECT * FROM fcmtokenStore";

        global.coreDb.all(query, [], (err, rows) => {
            if (err) {
                console.log(err);
                return null;
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * Returns a single token given with id
     * @param {integer} id tokenid
     * @param {function} callback rows or null
     */
    getTokenWithId(id, callback) {
        let query = "SELECT * FROM fcmtokenStore WHERE id = ?";
        global.coreDb.all(query, [id], (err, rows) => {
            if (err) {
                console.log(err);
                return callback(null);
            } else {
                return callback(rows);
            }
        })
    }

    /**
     * Returns all FCM Tokens from core database enriched with device data for UI Listings
     * @param {function} callback rows or null
     */
    getAllFCMTokensForUI(callback) {
        let query = "SELECT fcmtokenStore.*, " +
            "alertdevices.basedataId AS basedataId, " +
            "alertdevices.name AS deviceName, " +
            "alertdevices.assetsId AS assetsId " +
            "FROM fcmtokenStore " +
            "INNER JOIN alertdevices " +
            "ON fcmtokenStore.deviceId = alertdevices.id";

        global.coreDb.all(query, [], (err, rows) => {
            if (err) {
                console.log(err);
                return null;
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * Returns all active FCM Tokens from core database for the given plugin namespace
     * @param {string} pluginNamespace namespace of plugin this tokens belongs to
     * @param {function} callback rows or null 
     */
    getAllFCMTokensForNamespace(pluginNamespace, callback) {
        let query = "SELECT fcmtokenStore.* " +
            "FROM fcmtokenStore " +
            "INNER JOIN alertdevices " +
            "ON fcmtokenStore.deviceId = alertdevices.id " +
            "WHERE fcmtokenStore.pluginNamespace = ? AND " +
            "fcmtokenStore.state = 1 AND " +
            "alertdevices.state = 1";

        global.coreDb.all(query, [
            pluginNamespace
        ], (err, rows) => {
            if (err) {
                console.log(err);
                return null;
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * Returns rows with tokens for basedata and namespace
     * @param {string} pluginNamespace namespace of plugin, tokens shall be returned for
     * @param {*} basedataId basedata id of member the tokens shall be returned for
     * @param {*} callback rows or null
     */
    getAllFCMTokensForBasedataAndNamespace(pluginNamespace, basedataId, callback) {
        let query = "SELECT " +
            "basedata.id AS basedataId, " +
            "alertdevices.id AS deviceId, " +
            "fcmtokenStore.fcmToken AS fcmToken " +
            "FROM basedata " +
            "INNER JOIN alertdevices ON alertdevices.basedataId = basedata.id " +
            "LEFT JOIN fcmtokenStore ON fcmTokenStore.deviceId = alertdevices.id " +
            "WHERE " +
                "basedata.id = ? AND " +
                "alertdevices.state = 1 AND " +
                "fcmtokenStore.pluginNamespace = ?";

        global.coreDb.all(query, [
            basedataId,
            pluginNamespace
        ], (err, rows) => {
            if (err) {
                console.log(err);
                return null;
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * adds a new fcm token to core database
     * @param {integer} deviceId id of device this token comes from
     * @param {integer} state state (1 active, 0 inactive)
     * @param {string} pluginNamespace namespace of plugin this tokens belongs to
     * @param {integer} predecessorTokenId if this is a new token for an existing (so it is replacing an old one) this is the id of the old token
     * @param {string} fcmToken the actal fcm token
     * @param {function} callback success (true or false)
     */
    createFCMToken(deviceId, state, pluginNamespace, predecessorTokenId, fcmToken, callback) {

        let query = "INSERT INTO fcmtokenStore (deviceId, state, pluginNamespace, predecessorTokenId, fcmToken) VALUES (?, ?, ?, ?, ?)";

        global.coreDb.run(query, [
            deviceId,
            state,
            pluginNamespace,
            predecessorTokenId,
            fcmToken
        ], function (err) {
            if (err) {
                console.log('MODELS-FCMTOKENS | Error storing new device to database: ' + err.message);
                callback(false);
            } else {
                callback(true);
            }
        });
    }

    /**
     * Removes a token from database
     * @param {integer} tokenId id of token in core database
     * @param {function} callback success (true or false)
     */
    removeToken(tokenId, callback) {
        let query = "DELETE FROM fcmtokenStore WHERE id = ?";

        global.coreDb.run(query, [
            tokenId
        ], function (err) {
            if (err) {
                callback(false);
            } else {
                callback(true);
            }
        });
    }

    /**
     * Removes all tokens for a given uuid for a device
     * @param {string} uuid 
     * @param {function} callback success (true or false)
     */
    removeTokensForDeviceUUID(uuid, callback) {
        let query = "SELECT * FROM alertdevices WHERE uuid = ?";

        global.coreDb.all(query, [
            uuid
        ], (err, rows) => {
            if (err) {
                console.log(err);
                return callback(null);
            } else {
                if (rows.length > 0) {
                    var deviceId = rows[0].id;
                    let queryTokens = "DELETE FROM fcmtokenStore WHERE deviceId = ?";
                    global.coreDb.run(queryTokens, [
                        deviceId
                    ], function (err) {
                        if (err) {
                            callback(false);
                        } else {
                            callback(true);
                        }
                    });
                }
            }
        });
    }

    /**
     * Removes all tokens from store for a given device id
     * @param {integer} deviceId id of device
     * @param {function} callback success (true/false)
     */
    removeTokensForDeviceID(deviceId, callback) {
        let queryTokens = "DELETE FROM fcmtokenStore WHERE deviceId = ?";
        global.coreDb.run(queryTokens, [
            deviceId
        ], function (err) {
            if (err) {
                callback(false);
            } else {
                callback(true);
            }
        });
    }

    /**
     * Sets the state of an token to active
     * @param {integer} tokenId id of token in coredatabase to set state for
     * @param {function} callback success (true or false)
     */
    setActive(tokenId, callback) {
        let query = "UPDATE fcmtokenStore SET state = ? WHERE id = ?";
        global.coreDb.run(query, [
            1,
            tokenId
        ], function (err) {
            if (err) {
                console.log(err);
                return callback(false);
            } else {
                return callback(true);
            }
        });
    }

    /**
     * Sets the state of an token to inactive
     * @param {integer} tokenId id of token in coredatabase to set state for
     * @param {function} callback success (true or false)
     */
    setInactive(tokenId, callback) {
        let query = "UPDATE fcmtokenStore SET state = ? WHERE id = ?";
        global.coreDb.run(query, [
            0,
            tokenId
        ], function (err) {
            if (err) {
                console.log(err);
                return callback(false);
            } else {
                return callback(true);
            }
        });
    }

    /**
     * Checks, if a token exists in database
     * @param {string} fcmtoken 
     * @param {function} callback null or row of existing device
     */
    doesTokenExist(fcmtoken, callback) {
        let query = "SELECT * FROM fcmtokenStore WHERE fcmtoken = ?";

        global.coreDb.all(query, [
            fcmtoken
        ], (err, rows) => {
            if (err) {
                return null;
            } else {
                return callback(rows);
            }
        });
    }

}

module.exports = Fcmtokens;