1 | 'use strict';
|
2 |
|
3 | var constants = require('../constants');
|
4 | var config = require('../config/ErrorObject');
|
5 |
|
6 | var store = {};
|
7 |
|
8 | store.createResponse = function (module_name, code, data, error, callback) {
|
9 | var response = {};
|
10 |
|
11 | if ((code == null || code == undefined) && (error == null || error == undefined)) {
|
12 | throw new Error("Please Send code or error message");
|
13 | }
|
14 | else if (code == null || code == undefined) {
|
15 | if (error.name === constants.SEQUELIZE_DATABASE_ERROR_NAME)
|
16 | code = constants.SEQUELIZE_DATABASE_ERROR;
|
17 | else if (error.name === constants.SEQUELIZE_VALIDATION_ERROR_NAME)
|
18 | code = constants.SEQUELIZE_VALIDATION_ERROR;
|
19 | else if (error.name === constants.SEQUELIZE_FOREIGN_KEY_CONSTRAINT_ERROR_NAME)
|
20 | code = constants.SEQUELIZE_FOREIGN_KEY_CONSTRAINT_ERROR;
|
21 | else if (error.name === constants.CASSANDRA_VALIDATION_ERROR_NAME)
|
22 | code = constants.CASSANDRA_VALIDATION_ERROR;
|
23 | else
|
24 | code = constants.UNDEFINED_DATABASE_ERROR;
|
25 | }
|
26 |
|
27 | response.code = module_name + '-' + code;
|
28 | response.message = config.getMessage(code);
|
29 |
|
30 | if (error != null) response.error = error;
|
31 |
|
32 | if (data != null) {
|
33 | var isValidFlag = true;
|
34 | if (isValidFlag) {
|
35 | if (!data.hasOwnProperty('rows')) {
|
36 | response.data = data;
|
37 | }
|
38 | else {
|
39 | response.count = data.count;
|
40 | response.data = data.rows;
|
41 | }
|
42 | }
|
43 | else {
|
44 | code = constants.INVALID_JSON_OBJECT;
|
45 | response.message = config.getMessage(code);
|
46 | response.code = module_name + '-' + code;
|
47 | }
|
48 | }
|
49 | callback(response);
|
50 | }
|
51 |
|
52 | store.isValidJson=function (item) {
|
53 | item = typeof item !== "string"
|
54 | ? JSON.stringify(item)
|
55 | : item;
|
56 |
|
57 | try {
|
58 | item = JSON.parse(item);
|
59 | } catch (e) {
|
60 | return false;
|
61 | }
|
62 |
|
63 | if (typeof item === "object" && item !== null) {
|
64 | return true;
|
65 | }
|
66 |
|
67 | return false;
|
68 | }
|
69 |
|
70 | module.exports = store; |
\ | No newline at end of file |