Source: models/core/feedbackstore.js

const EventEmitter = require('events');

class FeedbackStore extends EventEmitter {

    /**
     * @constructor
     */
    constructor(alogger) {
        super();
        this.logger = alogger;
    }


    /**
     * Adds new feedback objects for basedata to core database, ususally caused by an new alarming
     * @param {integer} operationId id of operations object this feedbacks shall be added for
     * @param {array} basedataUUIDArray array of UUIDs of basedata to add, e.g. ['uuid1', 'uuid2']
     * @param {function} callback success (true/false)
     */
    generateFeedbackObjectsForOperation(operationId, basedataUUIDArray, callback) {
        if (Array.isArray(basedataUUIDArray)) {

            // render timestamp
            // Create an instance of the Date class
            var date = new Date();
            // Convert it to an ISO string
            var sqliteDate = date.toISOString();

            let query = "INSERT INTO feedbackStore " +
                "(operationId, basedataUUID, stateId, stateSet) ";
            query += 'VALUES ';

            var counter = basedataUUIDArray.length;
            basedataUUIDArray.forEach((uuid, index) => {
                query += " (" + operationId + ", '" + uuid + "', 0, '" + sqliteDate + "')";
                if ((index + 1) < counter) {
                    query += ", ";
                }
            })

            console.log(query);

            global.coreDb.run(query, [], function (err) {
                if (err) {
                    console.log(`ERROR Adding new feedbackstate objects to core database: ${err.message}`);
                    callback(false);
                } else {
                    callback(true);
                }
            });
        } else {
            // No UUIDs for basedata entires handed over
            return callback(false);
        }
    };


    /**
     * Returns basedata and set feedback state for given operation id
     * @param {integer} operationId id of operation
     * @param {*} callback rows of null
     */
    getFeedbackForOperationWithId(operationId, callback) {
        console.log('collecting feedback for operation with id: ' + operationId);
        let query = "SELECT " +
                    "basedata.id AS basedataId, " +
                    "basedata.firstname, " +
                    "basedata.lastname, " +
                    "basedata.uuid AS basedataUUID, " +
                    "feedbackStore.stateId, " +
                    "feedbackStates.state AS feedbackState, " +
                    "feedbackStates.stateColor AS feedbackStateColor, " +
                    "feedbackStates.stateLabel AS feedbackStateLabel " +
                    "FROM basedata " +
                    "INNER JOIN feedbackStore ON feedbackStore.basedataUUID = basedata.uuid " +
                    "INNER JOIN feedbackStates ON feedbackStates.id = feedbackStore.stateId " +
            "WHERE feedbackStore.operationId = ?";
        global.coreDb.all(query, [
            operationId
        ], (err, rows) => {
            if (err) {
                console.log(err);
                return callback(null);
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * returns the operationId for a given operation uuid
     * @param {string} uuid 
     * @param {function} callback rows or null
     */
    getOperationIdForUUID(uuid, callback) {
        let query = 'SELECT * FROM operationsStore WHERE uuid = ?';
        global.coreDb.all(query, [
            uuid
        ], (err, rows) => {
            if (err) {
                console.log(err);
                return callback(null);
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * Sets a new feedbackstate for given operationUUID and basedataUUID
     * @param {string} basedataUUID uuid of basedata
     * @param {string} operationUUID uuid of operation
     * @param {integer} state integer value from table feedbackStates
     * @param {function} callback success (true/false)
     */
    setFeedback(basedataUUID, operationUUID, state, callback) {
        this.getOperationIdForUUID(operationUUID, (rowOperation) => {
            var operationId = null;
            if (rowOperation !== null) {
                if (rowOperation.length > 0) {
                    operationId = rowOperation[0].id;
                }
            }
            // now set state
            if (operationId !== null) {
                let query = "UPDATE feedbackStore SET stateId = ? WHERE basedataUUID = ? and operationId = ?";
                global.coreDb.run(query, [
                    state,
                    basedataUUID,
                    operationId
                ], function (err) {
                    if (err) {
                        console.log(`ERROR setting new feedbackstate at core database: ${err.message}`);
                        callback(false);
                    } else {
                        callback(true);
                    }
                });
            } else {
                callback(false);
            }
            
        })
    }

}

module.exports = FeedbackStore;