From f2be872ef8e628fd0e04e9de67eee13492ac1221 Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <adhisimon@gmail.com>
Date: Thu, 10 Nov 2022 10:55:19 +0700
Subject: [PATCH] config.terminals_with_location

---
 config.sample.json                    |  2 ++
 lib/partner-listener/routers/topup.js | 41 +++++++++++++++++++++++++----------
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/config.sample.json b/config.sample.json
index 151bd2f..933c14f 100644
--- a/config.sample.json
+++ b/config.sample.json
@@ -26,6 +26,8 @@
         "max_retry": 10
     },
 
+    "terminals_with_location": [],
+
     "# cluster": "Isi dengan boolean atau angka jumlah anak yang akan dibuat. Jika diisi boolean true, jumlah anak akan dihitung otomatis",
     "cluster": false
 }
\ No newline at end of file
diff --git a/lib/partner-listener/routers/topup.js b/lib/partner-listener/routers/topup.js
index fef8eca..f157a30 100644
--- a/lib/partner-listener/routers/topup.js
+++ b/lib/partner-listener/routers/topup.js
@@ -12,6 +12,12 @@ const dumper = require('../dumper');
 const router = express.Router();
 module.exports = router;
 
+const terminalsWithLocation = Array.isArray(config.terminals_with_location)
+    ? config.terminals_with_location
+        .filter((item) => typeof item === 'string')
+        .map((item) => (item.trim().toLowerCase()))
+    : [];
+
 function onInvalidParameter(missingParameter, req, res) {
     logger.verbose(`${MODULE_NAME} 1536D577: Undefined ${missingParameter} parameter`, {
         xid: res.locals.xid,
@@ -60,22 +66,35 @@ async function pageIndex(req, res) {
 
     const terminalName = `${req.body.terminal_name || req.query.terminal_name}@${req.ip.replace(/^::ffff:/, '')}`;
 
+    const qs = {
+        terminal_name: terminalName,
+        password: req.body.password || req.query.password,
+        request_id: req.body.request_id || req.query.request_id,
+        product_name: req.body.product_name || req.query.product_name,
+        destination: req.body.destination || req.query.destination,
+        origin: config.name || 'HTTPGETX',
+        report_ip: config.listener.core.ip || null,
+        report_port: config.listener.core.port,
+        reverse_url: req.body.reverse_url || req.query.reverse_url || null,
+    };
+
+    if (terminalsWithLocation.indexOf(terminalName.toLowerCase()) >= 0) {
+        const location = req.body.location
+            || req.body.location_id
+            || req.query.location
+            || req.query.location_id;
+
+        if (location) {
+            qs.location = location;
+        }
+    }
+
     matrix.core.sent += 1;
 
     const [err, coreResponse] = await coreapi({
         xid,
         path: '/prepaid/buy',
-        qs: {
-            terminal_name: terminalName,
-            password: req.body.password || req.query.password,
-            request_id: req.body.request_id || req.query.request_id,
-            product_name: req.body.product_name || req.query.product_name,
-            destination: req.body.destination || req.query.destination,
-            origin: config.name || 'HTTPGETX',
-            report_ip: config.listener.core.ip || null,
-            report_port: config.listener.core.port,
-            reverse_url: req.body.reverse_url || req.query.reverse_url || null,
-        },
+        qs,
     });
 
     if (err || !coreResponse) {
-- 
1.9.0