'use strict';
var constants = require('../constants');
var config = require('../config/ErrorObject');
var store = {};
store.createResponse = function (module_name, code, data, error, callback) {
var response = {};
if ((code == null || code == undefined) && (error == null || error == undefined)) {
throw new Error("Please Send code or error message");
}
else if (code == null || code == undefined) {
if (error.name === constants.SEQUELIZE_DATABASE_ERROR_NAME)
code = constants.SEQUELIZE_DATABASE_ERROR;
else if (error.name === constants.SEQUELIZE_VALIDATION_ERROR_NAME)
code = constants.SEQUELIZE_VALIDATION_ERROR;
else Iif (error.name === constants.SEQUELIZE_FOREIGN_KEY_CONSTRAINT_ERROR_NAME)
code = constants.SEQUELIZE_FOREIGN_KEY_CONSTRAINT_ERROR;
else if (error.name === constants.CASSANDRA_VALIDATION_ERROR_NAME)
code = constants.CASSANDRA_VALIDATION_ERROR;
else
code = constants.UNDEFINED_DATABASE_ERROR;
}
response.code = module_name + '-' + code;
response.message = config.getMessage(code);
if (error != null) response.error = error;
if (data != null) {
var isValidFlag = true;//store.isValidJson(data);
Eif (isValidFlag) {
if (!data.hasOwnProperty('rows')) {
response.data = data;
}
else {
response.count = data.count;
response.data = data.rows;
}
}
else {
code = constants.INVALID_JSON_OBJECT;
response.message = config.getMessage(code);
response.code = module_name + '-' + code;
}
}
callback(response);
}
store.isValidJson=function (item) {
item = typeof item !== "string"
? JSON.stringify(item)
: item;
try {
item = JSON.parse(item);
} catch (e) {
return false;
}
if (typeof item === "object" && item !== null) {
return true;
}
return false;
}
module.exports = store; |