Commit 5ea032d89522555f301c7c459973019dad2f615a
1 parent
58dcc0ba12
Exists in
master
and in
1 other branch
Product price
Showing 5 changed files with 177 additions and 20 deletions Side-by-side Diff
lib/partner-listener/index.js
... | ... | @@ -14,6 +14,7 @@ const routerPay = require('./routers/pay'); |
14 | 14 | const routerTopup = require('./routers/topup'); |
15 | 15 | const routerTrxStatus = require('./routers/trx-status'); |
16 | 16 | const routerProductTree = require('./routers/product-tree'); |
17 | +const routerProductPrice = require('./routers/product-price'); | |
17 | 18 | |
18 | 19 | const app = express(); |
19 | 20 | |
... | ... | @@ -50,6 +51,7 @@ app.use('/inquiry', routerInquiry); |
50 | 51 | app.use('/pay', routerPay); |
51 | 52 | app.use('/topup', routerTopup); |
52 | 53 | app.use('/trx-status', routerTrxStatus); |
54 | +app.use('/product-price', routerProductPrice); | |
53 | 55 | app.use('/product-tree', routerProductTree); |
54 | 56 | app.use('/', routerTopup); |
55 | 57 |
lib/partner-listener/routers/product-price.js
... | ... | @@ -0,0 +1,99 @@ |
1 | +const MODULE_NAME = 'PARTNER-LISTENER.ROUTERS.PRODUCT-PRICE'; | |
2 | + | |
3 | +const axios = require('axios').default; | |
4 | +const express = require('express'); | |
5 | +const urlJoin = require('join-path'); | |
6 | + | |
7 | +const coreUrl = require('komodo-sdk/core-url'); | |
8 | +const logger = require('tektrans-logger'); | |
9 | +const { orderBy } = require('natural-orderby'); | |
10 | + | |
11 | +const ipv6ToIpv4 = require('../../ipv6-to-ipv4'); | |
12 | +const getFromBodyQsParams = require('../../get-from-body-qs-params'); | |
13 | + | |
14 | +const CORE_ENDPOINT = urlJoin(coreUrl, '/product-tree'); | |
15 | + | |
16 | +const router = express.Router(); | |
17 | +module.exports = router; | |
18 | + | |
19 | +const traverse = (data, productType) => { | |
20 | + const products = []; | |
21 | + | |
22 | + if (Array.isArray(data)) { | |
23 | + data.forEach((brandCategory) => { | |
24 | + if (Array.isArray(brandCategory.brands)) { | |
25 | + brandCategory.brands.forEach((brand) => { | |
26 | + if (Array.isArray(brand.product_categories)) { | |
27 | + brand.product_categories.forEach((productCategory) => { | |
28 | + if (Array.isArray(productCategory.products)) { | |
29 | + productCategory.products.forEach((product) => { | |
30 | + products.push({ | |
31 | + product: product.name, | |
32 | + description: product.description, | |
33 | + type: productType, | |
34 | + brandName: brand.name, | |
35 | + productCategory: productCategory.name, | |
36 | + price: product.price, | |
37 | + }); | |
38 | + }); | |
39 | + } | |
40 | + }); | |
41 | + } | |
42 | + }); | |
43 | + } | |
44 | + }); | |
45 | + } | |
46 | + | |
47 | + return products; | |
48 | +}; | |
49 | + | |
50 | +const mainHandler = async (req, res) => { | |
51 | + const { xid } = res.locals; | |
52 | + | |
53 | + const terminalNameWithoutIp = (getFromBodyQsParams(req, 'terminal_name') || '').toString().trim(); | |
54 | + const terminalName = `${terminalNameWithoutIp}@${ipv6ToIpv4(req.ip)}`; | |
55 | + const password = getFromBodyQsParams(req, 'password'); | |
56 | + | |
57 | + try { | |
58 | + const result = await axios.get(CORE_ENDPOINT, { | |
59 | + params: { | |
60 | + terminal_name: terminalName, | |
61 | + password, | |
62 | + }, | |
63 | + }); | |
64 | + | |
65 | + if (!result || !result.data) { | |
66 | + const e = new Error(`${MODULE_NAME} 643A50C5: Invalid result from CORE`); | |
67 | + throw e; | |
68 | + } | |
69 | + | |
70 | + const prepaidProducts = traverse(result.data.prepaid, 'PREPAID'); | |
71 | + const postpaidProducts = traverse(result.data.postpaid, 'POSTPAID'); | |
72 | + | |
73 | + res.json({ | |
74 | + httpgetx_xid: xid, | |
75 | + xid: result.data.xid, | |
76 | + ts: new Date(), | |
77 | + terminal_name: result.data.terminal_name, | |
78 | + products: orderBy( | |
79 | + prepaidProducts.concat(postpaidProducts), | |
80 | + (p) => p.product, | |
81 | + 'asc', | |
82 | + ), | |
83 | + }); | |
84 | + } catch (e) { | |
85 | + logger.warn(`${MODULE_NAME} A5414E86: Exception`, { | |
86 | + xid, | |
87 | + eCode: e.code, | |
88 | + eMessage: e.message || e, | |
89 | + }); | |
90 | + | |
91 | + res.json({ | |
92 | + // httpgetx_xid: xid, | |
93 | + ts: new Date(), | |
94 | + error: e.message || e, | |
95 | + }); | |
96 | + } | |
97 | +}; | |
98 | + | |
99 | +router.get('/', mainHandler); |
lib/partner-listener/routers/product-tree.js
... | ... | @@ -2,11 +2,12 @@ const MODULE_NAME = 'PARTNER-LISTENER.ROUTERS.PRODUCT-TREE'; |
2 | 2 | |
3 | 3 | const axios = require('axios').default; |
4 | 4 | const express = require('express'); |
5 | -const urlJoin = require('url-join'); | |
5 | +const urlJoin = require('join-path'); | |
6 | 6 | |
7 | 7 | const coreUrl = require('komodo-sdk/core-url'); |
8 | 8 | const logger = require('tektrans-logger'); |
9 | 9 | |
10 | +const ipv6ToIpv4 = require('../../ipv6-to-ipv4'); | |
10 | 11 | const getFromBodyQsParams = require('../../get-from-body-qs-params'); |
11 | 12 | |
12 | 13 | const CORE_ENDPOINT = urlJoin(coreUrl, '/product-tree'); |
... | ... | @@ -17,7 +18,8 @@ module.exports = router; |
17 | 18 | const mainHandler = async (req, res) => { |
18 | 19 | const { xid } = res.locals; |
19 | 20 | |
20 | - const terminalName = getFromBodyQsParams(req, 'terminal_name'); | |
21 | + const terminalNameWithoutIp = (getFromBodyQsParams(req, 'terminal_name') || '').toString().trim(); | |
22 | + const terminalName = `${terminalNameWithoutIp}@${ipv6ToIpv4(req.ip)}`; | |
21 | 23 | const password = getFromBodyQsParams(req, 'password'); |
22 | 24 | |
23 | 25 | try { |
... | ... | @@ -28,13 +30,13 @@ const mainHandler = async (req, res) => { |
28 | 30 | }, |
29 | 31 | }); |
30 | 32 | |
31 | - if (!result) { | |
33 | + if (!result || !result.data) { | |
32 | 34 | const e = new Error(`${MODULE_NAME} B7555411: Invalid result from CORE`); |
33 | 35 | throw e; |
34 | 36 | } |
35 | 37 | |
36 | 38 | result.httpgetx_xid = xid; |
37 | - res.json(result); | |
39 | + res.json(result.data); | |
38 | 40 | } catch (e) { |
39 | 41 | logger.warn(`${MODULE_NAME} 5AA99A06: Exception`, { |
40 | 42 | xid, |
... | ... | @@ -43,7 +45,7 @@ const mainHandler = async (req, res) => { |
43 | 45 | }); |
44 | 46 | |
45 | 47 | res.json({ |
46 | - xid, | |
48 | + httpgetx_xid: xid, | |
47 | 49 | ts: new Date(), |
48 | 50 | error: e.message || e, |
49 | 51 | }); |
package-lock.json
... | ... | @@ -11,13 +11,14 @@ |
11 | 11 | "dependencies": { |
12 | 12 | "axios": "^0.19.2", |
13 | 13 | "express": "^4.17.1", |
14 | + "join-path": "^1.1.1", | |
14 | 15 | "komodo-sdk": "^1.45.6", |
15 | 16 | "mkdirp": "^1.0.4", |
16 | 17 | "moment": "^2.24.0", |
18 | + "natural-orderby": "^2.0.3", | |
17 | 19 | "request": "^2.88.0", |
18 | 20 | "tektrans-logger": "^1.2.3", |
19 | 21 | "uniqid": "^5.3.0", |
20 | - "url-join": "^5.0.0", | |
21 | 22 | "uuid": "^3.3.3" |
22 | 23 | }, |
23 | 24 | "devDependencies": { |
... | ... | @@ -244,6 +245,11 @@ |
244 | 245 | "node": ">=0.10.0" |
245 | 246 | } |
246 | 247 | }, |
248 | + "node_modules/as-array": { | |
249 | + "version": "2.0.0", | |
250 | + "resolved": "https://registry.npmjs.org/as-array/-/as-array-2.0.0.tgz", | |
251 | + "integrity": "sha512-1Sd1LrodN0XYxYeZcN1J4xYZvmvTwD5tDWaPUGPIzH1mFsmzsPnVtd2exWhecMjtZk/wYWjNZJiD3b1SLCeJqg==" | |
252 | + }, | |
247 | 253 | "node_modules/asap": { |
248 | 254 | "version": "2.0.6", |
249 | 255 | "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", |
... | ... | @@ -1996,6 +2002,21 @@ |
1996 | 2002 | "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", |
1997 | 2003 | "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" |
1998 | 2004 | }, |
2005 | + "node_modules/join-path": { | |
2006 | + "version": "1.1.1", | |
2007 | + "resolved": "https://registry.npmjs.org/join-path/-/join-path-1.1.1.tgz", | |
2008 | + "integrity": "sha512-jnt9OC34sLXMLJ6YfPQ2ZEKrR9mB5ZbSnQb4LPaOx1c5rTzxpR33L18jjp0r75mGGTJmsil3qwN1B5IBeTnSSA==", | |
2009 | + "dependencies": { | |
2010 | + "as-array": "^2.0.0", | |
2011 | + "url-join": "0.0.1", | |
2012 | + "valid-url": "^1" | |
2013 | + } | |
2014 | + }, | |
2015 | + "node_modules/join-path/node_modules/url-join": { | |
2016 | + "version": "0.0.1", | |
2017 | + "resolved": "https://registry.npmjs.org/url-join/-/url-join-0.0.1.tgz", | |
2018 | + "integrity": "sha512-H6dnQ/yPAAVzMQRvEvyz01hhfQL5qRWSEt7BX8t9DqnPw9BjMb64fjIRq76Uvf1hkHp+mTZvEVJ5guXOT0Xqaw==" | |
2019 | + }, | |
1999 | 2020 | "node_modules/js-tokens": { |
2000 | 2021 | "version": "4.0.0", |
2001 | 2022 | "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", |
... | ... | @@ -2710,6 +2731,14 @@ |
2710 | 2731 | "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", |
2711 | 2732 | "dev": true |
2712 | 2733 | }, |
2734 | + "node_modules/natural-orderby": { | |
2735 | + "version": "2.0.3", | |
2736 | + "resolved": "https://registry.npmjs.org/natural-orderby/-/natural-orderby-2.0.3.tgz", | |
2737 | + "integrity": "sha512-p7KTHxU0CUrcOXe62Zfrb5Z13nLvPhSWR/so3kFulUQU0sgUll2Z0LwpsLN351eOOD+hRGu/F1g+6xDfPeD++Q==", | |
2738 | + "engines": { | |
2739 | + "node": "*" | |
2740 | + } | |
2741 | + }, | |
2713 | 2742 | "node_modules/negotiator": { |
2714 | 2743 | "version": "0.6.2", |
2715 | 2744 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", |
... | ... | @@ -4199,14 +4228,6 @@ |
4199 | 4228 | "punycode": "^2.1.0" |
4200 | 4229 | } |
4201 | 4230 | }, |
4202 | - "node_modules/url-join": { | |
4203 | - "version": "5.0.0", | |
4204 | - "resolved": "https://registry.npmjs.org/url-join/-/url-join-5.0.0.tgz", | |
4205 | - "integrity": "sha512-n2huDr9h9yzd6exQVnH/jU5mr+Pfx08LRXXZhkLLetAMESRj+anQsTAh940iMrIetKAmry9coFuZQ2jY8/p3WA==", | |
4206 | - "engines": { | |
4207 | - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" | |
4208 | - } | |
4209 | - }, | |
4210 | 4231 | "node_modules/util-deprecate": { |
4211 | 4232 | "version": "1.0.2", |
4212 | 4233 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", |
... | ... | @@ -4235,6 +4256,11 @@ |
4235 | 4256 | "integrity": "sha512-usZBT3PW+LOjM25wbqIlZwPeJV+3OSz3M1k1Ws8snlW39dZyYL9lOGC5FgPVHfk0jKmjiDV8Z0mIbVQPiwFs7g==", |
4236 | 4257 | "dev": true |
4237 | 4258 | }, |
4259 | + "node_modules/valid-url": { | |
4260 | + "version": "1.0.9", | |
4261 | + "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", | |
4262 | + "integrity": "sha512-QQDsV8OnSf5Uc30CKSwG9lnhMPe6exHtTXLRYX8uMwKENy640pU+2BgBL0LRbDh/eYRahNCS7aewCx0wf3NYVA==" | |
4263 | + }, | |
4238 | 4264 | "node_modules/validate-npm-package-license": { |
4239 | 4265 | "version": "3.0.4", |
4240 | 4266 | "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", |
... | ... | @@ -4789,6 +4815,11 @@ |
4789 | 4815 | "resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.3.2.tgz", |
4790 | 4816 | "integrity": "sha1-qJS3XUvE9s1nnvMkSp/Y9Gri1Cg=" |
4791 | 4817 | }, |
4818 | + "as-array": { | |
4819 | + "version": "2.0.0", | |
4820 | + "resolved": "https://registry.npmjs.org/as-array/-/as-array-2.0.0.tgz", | |
4821 | + "integrity": "sha512-1Sd1LrodN0XYxYeZcN1J4xYZvmvTwD5tDWaPUGPIzH1mFsmzsPnVtd2exWhecMjtZk/wYWjNZJiD3b1SLCeJqg==" | |
4822 | + }, | |
4792 | 4823 | "asap": { |
4793 | 4824 | "version": "2.0.6", |
4794 | 4825 | "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", |
... | ... | @@ -6208,6 +6239,23 @@ |
6208 | 6239 | "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", |
6209 | 6240 | "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" |
6210 | 6241 | }, |
6242 | + "join-path": { | |
6243 | + "version": "1.1.1", | |
6244 | + "resolved": "https://registry.npmjs.org/join-path/-/join-path-1.1.1.tgz", | |
6245 | + "integrity": "sha512-jnt9OC34sLXMLJ6YfPQ2ZEKrR9mB5ZbSnQb4LPaOx1c5rTzxpR33L18jjp0r75mGGTJmsil3qwN1B5IBeTnSSA==", | |
6246 | + "requires": { | |
6247 | + "as-array": "^2.0.0", | |
6248 | + "url-join": "0.0.1", | |
6249 | + "valid-url": "^1" | |
6250 | + }, | |
6251 | + "dependencies": { | |
6252 | + "url-join": { | |
6253 | + "version": "0.0.1", | |
6254 | + "resolved": "https://registry.npmjs.org/url-join/-/url-join-0.0.1.tgz", | |
6255 | + "integrity": "sha512-H6dnQ/yPAAVzMQRvEvyz01hhfQL5qRWSEt7BX8t9DqnPw9BjMb64fjIRq76Uvf1hkHp+mTZvEVJ5guXOT0Xqaw==" | |
6256 | + } | |
6257 | + } | |
6258 | + }, | |
6211 | 6259 | "js-tokens": { |
6212 | 6260 | "version": "4.0.0", |
6213 | 6261 | "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", |
... | ... | @@ -6778,6 +6826,11 @@ |
6778 | 6826 | "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", |
6779 | 6827 | "dev": true |
6780 | 6828 | }, |
6829 | + "natural-orderby": { | |
6830 | + "version": "2.0.3", | |
6831 | + "resolved": "https://registry.npmjs.org/natural-orderby/-/natural-orderby-2.0.3.tgz", | |
6832 | + "integrity": "sha512-p7KTHxU0CUrcOXe62Zfrb5Z13nLvPhSWR/so3kFulUQU0sgUll2Z0LwpsLN351eOOD+hRGu/F1g+6xDfPeD++Q==" | |
6833 | + }, | |
6781 | 6834 | "negotiator": { |
6782 | 6835 | "version": "0.6.2", |
6783 | 6836 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", |
... | ... | @@ -7932,11 +7985,6 @@ |
7932 | 7985 | "punycode": "^2.1.0" |
7933 | 7986 | } |
7934 | 7987 | }, |
7935 | - "url-join": { | |
7936 | - "version": "5.0.0", | |
7937 | - "resolved": "https://registry.npmjs.org/url-join/-/url-join-5.0.0.tgz", | |
7938 | - "integrity": "sha512-n2huDr9h9yzd6exQVnH/jU5mr+Pfx08LRXXZhkLLetAMESRj+anQsTAh940iMrIetKAmry9coFuZQ2jY8/p3WA==" | |
7939 | - }, | |
7940 | 7988 | "util-deprecate": { |
7941 | 7989 | "version": "1.0.2", |
7942 | 7990 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", |
... | ... | @@ -7958,6 +8006,11 @@ |
7958 | 8006 | "integrity": "sha512-usZBT3PW+LOjM25wbqIlZwPeJV+3OSz3M1k1Ws8snlW39dZyYL9lOGC5FgPVHfk0jKmjiDV8Z0mIbVQPiwFs7g==", |
7959 | 8007 | "dev": true |
7960 | 8008 | }, |
8009 | + "valid-url": { | |
8010 | + "version": "1.0.9", | |
8011 | + "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", | |
8012 | + "integrity": "sha512-QQDsV8OnSf5Uc30CKSwG9lnhMPe6exHtTXLRYX8uMwKENy640pU+2BgBL0LRbDh/eYRahNCS7aewCx0wf3NYVA==" | |
8013 | + }, | |
7961 | 8014 | "validate-npm-package-license": { |
7962 | 8015 | "version": "3.0.4", |
7963 | 8016 | "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", |
package.json
... | ... | @@ -31,13 +31,14 @@ |
31 | 31 | "dependencies": { |
32 | 32 | "axios": "^0.19.2", |
33 | 33 | "express": "^4.17.1", |
34 | + "join-path": "^1.1.1", | |
34 | 35 | "komodo-sdk": "^1.45.6", |
35 | 36 | "mkdirp": "^1.0.4", |
36 | 37 | "moment": "^2.24.0", |
38 | + "natural-orderby": "^2.0.3", | |
37 | 39 | "request": "^2.88.0", |
38 | 40 | "tektrans-logger": "^1.2.3", |
39 | 41 | "uniqid": "^5.3.0", |
40 | - "url-join": "^5.0.0", | |
41 | 42 | "uuid": "^3.3.3" |
42 | 43 | } |
43 | 44 | } |