From c7ef5eaccc40a7fb5d3bc73c57137c7dda91e7ad Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <me@adhisimon.org>
Date: Sun, 1 Dec 2019 17:00:59 +0700
Subject: [PATCH] Ubah 0 di depan menjadi pada nama terminal baru saat
 pembuatan downline baru jika phone number

---
 lib/command-handler/adddownline.js |  6 ++++++
 lib/common.js                      |  2 +-
 test/common.js                     | 15 ++++++++++-----
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/lib/command-handler/adddownline.js b/lib/command-handler/adddownline.js
index b9008dc..642e3ad 100644
--- a/lib/command-handler/adddownline.js
+++ b/lib/command-handler/adddownline.js
@@ -49,6 +49,7 @@ function execute(tokens, params, cb) {
         coreParams.generate_password = 1;
     }
 
+    /*
     if (typeof coreParams.new_terminal_name === 'string' && coreParams.new_terminal_name.indexOf('@') < 0) {
         let suffix = '';
         if (common.isPhoneNumber(coreParams.new_terminal_name)) {
@@ -64,6 +65,11 @@ function execute(tokens, params, cb) {
     }
 
     coreapi(coreEndpoint, coreParams, 'GET', cb);
+    */
+
+    if (typeof coreParams.new_terminal_name === 'string' && common.isPhoneNumber(coreParams.new_terminal_name) && coreParams.new_terminal_name.indexOf('0') === 0) {
+        coreParams.new_terminal_name = coreParams.new_terminal_name.replace(/^0/, '62');
+    }
 }
 
 module.exports = execute;
\ No newline at end of file
diff --git a/lib/common.js b/lib/common.js
index a15ff27..a1b0cb4 100644
--- a/lib/common.js
+++ b/lib/common.js
@@ -14,7 +14,7 @@ function isPhoneNumber(terminalName) {
         return false;
     }
 
-    if (!terminalName) return false;
+    if (terminalName.trim().length < 7) return false;
 
     return Boolean(terminalName.replace(/^\+/, '').match(/^\d+$/));
 }
diff --git a/test/common.js b/test/common.js
index ba97a7f..e1e3765 100644
--- a/test/common.js
+++ b/test/common.js
@@ -17,17 +17,22 @@ describe('#common', () => {
 
     describe('#isPhoneNumber', () => {
         it('should detect correctly', () => {
-            common.isPhoneNumber('0818').should.be.ok();
-            common.isPhoneNumber('62818').should.be.ok();
-            common.isPhoneNumber('+62818').should.be.ok();
+            common.isPhoneNumber('0818').should.not.be.ok();
+            common.isPhoneNumber('62818').should.not.be.ok();
+            common.isPhoneNumber('+62818').should.not.be.ok();
+
+            common.isPhoneNumber('081808180').should.be.ok();
+            common.isPhoneNumber('62818818').should.be.ok();
+            common.isPhoneNumber('+62818818').should.be.ok();
+
             common.isPhoneNumber('ada').should.not.be.ok();
         });
     });
 
     describe('#indonesiaIntlNumberToZeroPrefix', () => {
         it('should transform correctly', () => {
-            common.indonesiaIntlNumberToZeroPrefix('+62818').should.equal('0818');
-            common.indonesiaIntlNumberToZeroPrefix('0818').should.equal('0818');
+            common.indonesiaIntlNumberToZeroPrefix('+6281812341234').should.equal('081812341234', '+6281812341234');
+            common.indonesiaIntlNumberToZeroPrefix('081808180').should.equal('081808180', '081808180');
             common.indonesiaIntlNumberToZeroPrefix('ada').should.equal('ada');
         });
     })
-- 
1.9.0