Commit 3b7eab0dcfa729511950c4c5388de675f33cd6de
1 parent
48e051d3f3
Exists in
master
Change some logs
Showing 1 changed file with 3 additions and 0 deletions Inline Diff
lib/transport.js
1 | const MODULE_NAME = 'TRANSPORT'; | 1 | const MODULE_NAME = 'TRANSPORT'; |
2 | 2 | ||
3 | const axios = require('axios').default; | 3 | const axios = require('axios').default; |
4 | const uniqid = require('uniqid'); | 4 | const uniqid = require('uniqid'); |
5 | const urljoin = require('url-join'); | 5 | const urljoin = require('url-join'); |
6 | const config = require('komodo-sdk/config'); | 6 | const config = require('komodo-sdk/config'); |
7 | const logger = require('tektrans-logger'); | 7 | const logger = require('tektrans-logger'); |
8 | 8 | ||
9 | const axiosConfig = { | 9 | const axiosConfig = { |
10 | headers: { | 10 | headers: { |
11 | 'content-type': 'application/json', | 11 | 'content-type': 'application/json', |
12 | }, | 12 | }, |
13 | timeout: 60 * 1000, | 13 | timeout: 60 * 1000, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | const send = async (partner, msg, callerXid) => { | 16 | const send = async (partner, msg, callerXid) => { |
17 | const xid = callerXid || uniqid(); | 17 | const xid = callerXid || uniqid(); |
18 | |||
18 | const modemApiServer = config.modem; | 19 | const modemApiServer = config.modem; |
19 | if (!modemApiServer || !modemApiServer.url) { | 20 | if (!modemApiServer || !modemApiServer.url) { |
20 | logger.warn(`${MODULE_NAME} 4111A9BA: Missing modem`, { xid, partner, msg }); | 21 | logger.warn(`${MODULE_NAME} 4111A9BA: Missing modem`, { xid, partner, msg }); |
21 | return; | 22 | return; |
22 | } | 23 | } |
23 | 24 | ||
24 | const endpointUrl = urljoin( | 25 | const endpointUrl = urljoin( |
25 | modemApiServer.url, | 26 | modemApiServer.url, |
26 | '/sms/send', | 27 | '/sms/send', |
27 | ); | 28 | ); |
28 | 29 | ||
29 | if (!partner || typeof partner !== 'string' || !partner.trim()) { | 30 | if (!partner || typeof partner !== 'string' || !partner.trim()) { |
30 | logger.verbose(`${MODULE_NAME} E5308BDD: Missing destination`); | 31 | logger.verbose(`${MODULE_NAME} E5308BDD: Missing destination`); |
31 | return; | 32 | return; |
32 | } | 33 | } |
33 | 34 | ||
34 | if (!msg || typeof msg !== 'string' || !msg.trim()) { | 35 | if (!msg || typeof msg !== 'string' || !msg.trim()) { |
35 | logger.verbose(`${MODULE_NAME} EC8FF515: Missing msg`); | 36 | logger.verbose(`${MODULE_NAME} EC8FF515: Missing msg`); |
36 | return; | 37 | return; |
37 | } | 38 | } |
38 | 39 | ||
39 | const destination = (partner || '') | 40 | const destination = (partner || '') |
40 | .trim() | 41 | .trim() |
41 | .replace(/^0/, '+62') | 42 | .replace(/^0/, '+62') |
42 | .replace(/^62/, '+62'); | 43 | .replace(/^62/, '+62'); |
43 | 44 | ||
44 | const payload = { | 45 | const payload = { |
45 | destination, | 46 | destination, |
46 | message: (msg || '').trim(), | 47 | message: (msg || '').trim(), |
47 | }; | 48 | }; |
48 | 49 | ||
49 | logger.verbose(`${MODULE_NAME} 8D4F8A3A: Sending message to modem apiserver`, { | 50 | logger.verbose(`${MODULE_NAME} 8D4F8A3A: Sending message to modem apiserver`, { |
50 | xid, | 51 | xid, |
52 | httpMethod: 'POST', | ||
51 | endpointUrl, | 53 | endpointUrl, |
54 | requestContentType: axiosConfig.headers['content-type'], | ||
52 | payload, | 55 | payload, |
53 | }); | 56 | }); |
54 | 57 | ||
55 | try { | 58 | try { |
56 | const response = await axios.post(endpointUrl, payload, axiosConfig); | 59 | const response = await axios.post(endpointUrl, payload, axiosConfig); |
57 | 60 | ||
58 | logger.verbose(`${MODULE_NAME} 6D71CC75: Message sent to modem apiserver`, { | 61 | logger.verbose(`${MODULE_NAME} 6D71CC75: Message sent to modem apiserver`, { |
59 | xid, | 62 | xid, |
60 | partner, | 63 | partner, |
61 | msg, | 64 | msg, |
62 | endpointUrl, | 65 | endpointUrl, |
63 | httpStatus: response.status, | 66 | httpStatus: response.status, |
64 | responseBody: response.data, | 67 | responseBody: response.data, |
65 | }); | 68 | }); |
66 | } catch (e) { | 69 | } catch (e) { |
67 | logger.warn(`${MODULE_NAME} C0124404: Exception on send`, { | 70 | logger.warn(`${MODULE_NAME} C0124404: Exception on send`, { |
68 | xid, | 71 | xid, |
69 | eCode: e.code, | 72 | eCode: e.code, |
70 | eMessage: e.message, | 73 | eMessage: e.message, |
71 | httpStatus: e.response && e.response.status, | 74 | httpStatus: e.response && e.response.status, |
72 | responseBody: e.response && e.response.data, | 75 | responseBody: e.response && e.response.data, |
73 | }); | 76 | }); |
74 | } | 77 | } |
75 | }; | 78 | }; |
76 | exports.send = send; | 79 | exports.send = send; |
77 | 80 |