data.js
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const MODULE_NAME = 'CONFIG.DATA';
const fs = require('fs/promises');
const logger = require('tektrans-logger');
let config = {};
/**
* Get all config
*
* @param {string} xid
*
* @returns <object>
*/
async function all(xid) {
try {
if (Object.keys(config).length === 0) {
const configString = await fs.readFile('config.json');
config = JSON.parse(configString);
}
} catch (e) {
logger.warn(`${MODULE_NAME} ED8D390C: Exception`, {
xid,
eMessage: e.message,
eCode: e.code,
});
}
return config;
}
/**
* Get active product prepaid config
*
* @param {string} xid
*
* @returns <Array>
*/
async function getActiveProductArray(xid) {
try {
if (Object.keys(config).length === 0) {
const configString = await fs.readFile('config.json');
config = JSON.parse(configString);
}
const allProductsArray = Object.values(config.products);
if (Array.isArray(allProductsArray)) {
const activeProductArray = allProductsArray.filter((val) => val.active === 1);
const activeProductNameArray = activeProductArray.map((val) => val.name);
return activeProductNameArray;
}
} catch (e) {
logger.warn(`${MODULE_NAME} 2D49D035: Exception`, {
xid,
eMessage: e.message,
eCode: e.code,
});
}
return [];
}
/**
* Get active product postpaid config
*
* @param {string} xid
*
* @returns <Array>
*/
async function getActiveProductPostpaidArray(xid) {
try {
if (Object.keys(config).length === 0) {
const configString = await fs.readFile('config.json');
config = JSON.parse(configString);
}
const allProductsArray = Object.values(config.postpaid_products);
if (Array.isArray(allProductsArray)) {
const activeProductArray = allProductsArray.filter((val) => val.active === 1);
const activeProductNameArray = activeProductArray.map((val) => val.name);
return activeProductNameArray;
}
} catch (e) {
logger.warn(`${MODULE_NAME} B5414DB0: Exception`, {
xid,
eMessage: e.message,
eCode: e.code,
});
}
return [];
}
/**
* Reload config
*
* @param {string} xid
*
* @returns <object>
*/
async function reload(xid) {
try {
const configString = await fs.readFile('config.json');
config = JSON.parse(configString);
} catch (e) {
logger.warn(`${MODULE_NAME} D005AED3: Exception`, {
xid,
eMessage: e.message,
eCode: e.code,
});
}
return config;
}
exports.all = all;
exports.reload = reload;
exports.getActiveProductArray = getActiveProductArray;
exports.getActiveProductPostpaidArray = getActiveProductPostpaidArray;