Blame view

lib/webhook-sender.js 2.9 KB
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  const MODULE_NAME = 'WEBHOOK-SENDER';
  
  const axios = require('axios');
  const moment = require('moment');
  const fs = require('fs');
  const path = require('path');
  const stringify = require('json-stringify-pretty-compact');
  const config = require('komodo-sdk/config');
  const logger = require('tektrans-logger');
  
  const DEFAULT_MAX_RETRY = 10;
  const DEFAULT_SLEEP_BEFORE_RETRY_MS = 10 * 1000;
  
  const maxRetry = Number(config.webhook && config.webhook.max_retry)
      || DEFAULT_MAX_RETRY;
  const sleepBeforeRetryMs = Number(config.webhook && config.webhook.sleep_before_retry_ms)
      || DEFAULT_SLEEP_BEFORE_RETRY_MS;
  
  const baseDumpDir = path.join('dump', 'webhook-sender');
  if (!fs.existsSync(baseDumpDir)) {
      fs.mkdirSync(baseDumpDir, { recursive: true });
  }
e7bcc28f4   Adhidarma Hadiwinoto   Use await on call...
23
  const lastDumpFileName = path.join(baseDumpDir, 'last.json');
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
24

5e27716d8   Adhidarma Hadiwinoto   sleepMs promise
25
26
27
28
29
  const sleepMs = (ms) => new Promise((resolve) => {
      setTimeout(() => {
          resolve();
      }, ms);
  });
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
30
31
32
33
  const dumper = async (xid, webhookType, body) => {
      if (!config.webhook || !config.webhook.dump) {
          return;
      }
62fcc778c   Adhidarma Hadiwinoto   Debug
34
      try {
e7bcc28f4   Adhidarma Hadiwinoto   Use await on call...
35
          const filename = [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_');
62fcc778c   Adhidarma Hadiwinoto   Debug
36
          await fs.promises.writeFile(
e7bcc28f4   Adhidarma Hadiwinoto   Use await on call...
37
              path.join(baseDumpDir, `${filename}.json`),
62fcc778c   Adhidarma Hadiwinoto   Debug
38
39
40
41
42
43
44
45
46
47
48
49
50
51
              stringify({ webhookType, body }),
          );
  
          await fs.promises.writeFile(
              lastDumpFileName,
              stringify({ webhookType, body }),
          );
      } catch (e) {
          logger.warn(`${MODULE_NAME} D3EF00D9: Exception on dumper`, {
              xid,
              eCode: e.code,
              eMessage: e.message || e.toString(),
          });
      }
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  };
  
  const sender = async (xid, webhookType, body, retry) => {
      if (!config.webhook || !config.webhook.url) {
          return;
      }
  
      try {
          logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, {
              xid,
              webhookType,
              partner: config.webhook.url,
              trxId: body.transaction_id,
              request_id: body.request_id,
          });
1182d0fd6   Adhidarma Hadiwinoto   Perbaikan url web...
67
          await axios.post(config.webhook.url, {
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
68
69
70
71
72
73
74
75
76
              webhookType,
              body,
          });
  
          await dumper(xid, webhookType, body);
  
          logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, {
              xid,
              webhookType,
1182d0fd6   Adhidarma Hadiwinoto   Perbaikan url web...
77
              partner: config.webhook.url,
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
78
79
80
81
          });
      } catch (e) {
          logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, {
              xid,
485b7af4b   Adhidarma Hadiwinoto   Debug webhooksend...
82
              httpStatusCode: e.response && e.response.status,
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
83
84
85
86
87
88
89
90
91
92
93
94
95
              eCode: e.code,
              eMessage: e.message || e.toString(),
              retried: retry || 0,
              maxRetry,
          });
  
          if ((retry || 0) >= maxRetry) {
              logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, {
                  xid,
              });
  
              return;
          }
5e27716d8   Adhidarma Hadiwinoto   sleepMs promise
96
          await sleepMs(sleepBeforeRetryMs);
62fcc778c   Adhidarma Hadiwinoto   Debug
97
          await sender(xid, webhookType, body, (retry || 0) + 1);
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
98
99
100
      }
  };
  module.exports = sender;