product-price.js
3.87 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
114
115
116
117
118
119
120
const MODULE_NAME = 'PARTNER-LISTENER.ROUTERS.PRODUCT-PRICE';
const RATE_LIMIT_MESSAGE = 'Rate limited. Cobalah satu menit lagi!\n';
const RATE_LIMIT_MAX = 2;
const RATE_LIMIT_WINDOW_MS = 60 * 1000;
const axios = require('axios');
const express = require('express');
const urlJoin = require('join-path');
const expressRateLimit = require('express-rate-limit');
const coreUrl = require('komodo-sdk/core-url');
const logger = require('tektrans-logger');
const { orderBy } = require('natural-orderby');
const ipv6ToIpv4 = require('../../ipv6-to-ipv4');
const getFromBodyQsParams = require('../../get-from-body-qs-params');
const CORE_ENDPOINT = urlJoin(coreUrl, '/product-tree');
const router = express.Router();
module.exports = router;
const rateLimit = expressRateLimit({
windowMs: RATE_LIMIT_WINDOW_MS,
max: RATE_LIMIT_MAX,
message: RATE_LIMIT_MESSAGE,
keyGenerator: (req, res) => res.locals && res.locals.terminalName,
// handler: (req, res, next, opts) => {
// onRateLimited(req, res, 'ip', opts);
// },
});
const traverse = (data, productType) => {
const products = [];
if (Array.isArray(data)) {
data.forEach((brandCategory) => {
if (Array.isArray(brandCategory.brands)) {
brandCategory.brands.forEach((brand) => {
if (Array.isArray(brand.product_categories)) {
brand.product_categories.forEach((productCategory) => {
if (Array.isArray(productCategory.products)) {
productCategory.products.forEach((product) => {
products.push({
product: product.name,
description: product.description,
type: productType,
brandName: brand.name,
productCategory: productCategory.name,
price: product.price,
});
});
}
});
}
});
}
});
}
return products;
};
const extractTerminalName = (req, res, next) => {
const terminalNameWithoutIp = (getFromBodyQsParams(req, 'terminal_name') || '').toString().trim();
res.locals.terminalName = `${terminalNameWithoutIp}@${ipv6ToIpv4(req.ip)}`;
next();
};
const mainHandler = async (req, res) => {
const { xid } = res.locals;
const { terminalName } = res.locals;
const password = getFromBodyQsParams(req, 'password');
try {
const result = await axios.get(CORE_ENDPOINT, {
params: {
terminal_name: terminalName,
password,
},
});
if (!result || !result.data) {
const e = new Error(`${MODULE_NAME} 643A50C5: Invalid result from CORE`);
throw e;
}
const prepaidProducts = traverse(result.data.prepaid, 'PREPAID');
const postpaidProducts = traverse(result.data.postpaid, 'POSTPAID');
res.json({
httpgetx_xid: xid,
xid: result.data.xid,
ts: new Date(),
error: result.data.error,
terminal_name: result.data.terminal_name,
products: orderBy(
prepaidProducts.concat(postpaidProducts),
(p) => p.product,
'asc',
),
});
} catch (e) {
logger.warn(`${MODULE_NAME} A5414E86: Exception`, {
xid,
eCode: e.code,
eMessage: e.message || e,
});
res.json({
// httpgetx_xid: xid,
ts: new Date(),
error: e.message || e,
});
}
};
router.get('/', extractTerminalName, rateLimit, mainHandler);