// Logging
var log4js = require('log4js');
log4js.configure({
appenders: { mylogger: { type:"dateFile", filename: "logs/modules/inbound/feedbackchannel/feedbackchannel.log" }, console: { type:"console" } },
categories: { default: { appenders:["mylogger", "console"], level:"ALL" } }
});
const logger = log4js.getLogger("default");
const express = require('express'); // require express
var app = express();
const mysql = require('mysql'); // Required for database connection
var config = require('../../../../config/config.js'); // Read global config
// Port the feedback service should respond to
var port = 3050;
// Create db connection
const db = mysql.createConnection (config.databaseOptions);
db.connect((err) => {
if (err) {
throw err;
}
logger.info(`${svcName} | Connected to database `, config.databaseOptions.database);
});
global.db = db;
// Get arguments main application sent during startup
var args = process.argv.slice(2); // Get arguments main application sent during startup
const orgId = args[1]; // Set organizationID this service instance is active for
const ibSvcId = args[2]; // Set ibSvcId this service instance has in database (needed for reading the instance configuration from database)
// Set Service Name for Logging purposes
const svcName = 'FEEDBACKCHANNEL-'+args[0];
// Send startup-message to console
logger.info(`${svcName} | organization ${orgId} | service instance id ${ibSvcId} | starting up .. `); // Send startup-message to console/log
/**
* Function reads the configuration key/value pairs from database
*
*/
function readModuleConfiguration(callback) {
var funcResult = true;
let query = "SELECT keyword, value FROM services_inbound_config WHERE keyword LIKE 'inbound.feedbackchannel%' AND svcInstanceId = " + ibSvcId;
db.query(query, (err, result) => {
if (err) {
logger.error(`${svcName} | cannot read module configuration from database`);
funcResult = false;
} else {
if (result.length > 0) {
result.forEach(element => {
if (element.keyword === 'inbound.feedbackchannel.port') port = element.value;
});
} else {
funcResult = false;
}
}
callback(funcResult);
});
}
//
//
// Service specific implementation
//
//
// Start feedbackchannel service
function initializeService() {
app.listen(port, () => {
logger.info(`${svcName} | Service running on port ${port}`);
});
//Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded({
extended: true
}));
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
app.post("/setOperationFeedback", (req, res, next) => {
logger.info(JSON.stringify(req.body));
var operationid = req.body.operationid;
var alertdeviceid = req.body.alertdeviceid;
var feedbackstate = req.body.feedbackstate;
persistFeedback(operationid, alertdeviceid, feedbackstate, (success) => {
res.json([operationid, alertdeviceid, feedbackstate, success]);
})
})
}
function persistFeedback(operationid, alertdeviceid, feedbackstate, callback) {
let query = "SELECT id FROM feedbackchannel WHERE operationid = " + operationid + " AND alertdeviceid = '" + alertdeviceid + "'";
db.query(query, (err, result) => {
if(err) {
logger.error(svcName + ' | Error in query checking for existing feedback entry for operation with id ' + operationid + ' and for alertdevice with id ' + alertdeviceid + ' | Error: ' + err);
logger.error(svcName + ' | Query with Error: ' + query);
callback(false);
} else {
let query = '';
if (result.length > 0) {
// there was an entry in database found for this combination --> Update existing entry
query = "UPDATE feedbackchannel SET feedbackstate = " + feedbackstate + " WHERE id = " + result[0].id;
} else {
// there is no entry in database for this combination --> Insert new entry
query = "INSERT INTO feedbackchannel (`alertdeviceid`, `timestampincoming`, `feedbackstate`, `operationid`) "
+"VALUES ('" + alertdeviceid + "', curdate(), '" + feedbackstate + "', '" + operationid + "');"
}
db.query(query, (err2, result) => {
if (err2) {
logger.error(svcName + ' | Error updating / inserting feedback in databse: ' + err2);
logger.error(svcName + ' | Query with Error: ' + query);
callback(false);
} else {
callback(true);
}
})
}
})
}
// Read Configuration from database
// and start the filesystem watcher afterwards
readModuleConfiguration((result) => {
if (result === true) {
logger.info(`${svcName} | successfully read configuration for service instance from database`);
initializeService();
} else {
logger.warn(`${svcName} | Could not read configuration for service instance from database. Please check configuration for this instance. Service not started!`);
//process.exit();
}
});
//
//
// GENERIC Keep-alive Service Functionality (--don't change--)
//
//
process.on('message', (m) => {
if (m == 'stop') {
logger.info(`${svcName} | ${args} | ${m} | Service termination command recieved from host`);
process.exit();
} else if(m == 'healthcheck') {
process.send('{"state":"1", "pid":"' + process.pid + '"}');
}
});