Commit 7cb66aa3cfc02d1bb547697452312cd46b3c2cdc

Authored by Adhidarma Hadiwinoto
1 parent b88ba20016
Exists in master

Async await on add del senders

Showing 1 changed file with 25 additions and 22 deletions Side-by-side Diff

lib/apiserver/router-config-senders.js
... ... @@ -13,12 +13,15 @@ const router = express.Router();
13 13 module.exports = router;
14 14  
15 15 function saveConfig() {
16   - mutexSave.lock(() => {
17   - fs.writeFile(global.KOMODO_SDK_CONFIG_FILENAME, JSON.stringify(config, null, 4), (err) => {
18   - if (err) {
19   - logger.warn(`APISERVER: ERROR writing config file. ${err.toString()}`);
20   - }
21   - mutexSave.unlock();
  16 + return new Promise((resolve) => {
  17 + mutexSave.lock(() => {
  18 + fs.writeFile(global.KOMODO_SDK_CONFIG_FILENAME, JSON.stringify(config, null, 4), (err) => {
  19 + if (err) {
  20 + logger.warn(`APISERVER: ERROR writing config file. ${err.toString()}`);
  21 + }
  22 + mutexSave.unlock();
  23 + resolve();
  24 + });
22 25 });
23 26 });
24 27 }
... ... @@ -51,7 +54,7 @@ function pageIndex(req, res) {
51 54 res.json(config.senders_imsi);
52 55 }
53 56  
54   -function delImsi(data, imsi, cleanup) {
  57 +async function delImsi(data, imsi, cleanup) {
55 58 const idx = data.indexOf(imsi);
56 59 if (idx < 0) return;
57 60  
... ... @@ -69,16 +72,16 @@ function delImsi(data, imsi, cleanup) {
69 72 });
70 73 }
71 74  
72   - saveConfig();
  75 + await saveConfig();
73 76 }
74 77  
75   -function addImsi(data, imsi) {
  78 +async function addImsi(data, imsi) {
76 79 if (data.indexOf(imsi) >= 0) {
77 80 return false;
78 81 }
79 82  
80 83 data.push(imsi);
81   - saveConfig();
  84 + await saveConfig();
82 85  
83 86 return true;
84 87 }
... ... @@ -106,7 +109,7 @@ function addImsiResponse(req, res) {
106 109 * @param {string} req.query.imsi - nomor imsi
107 110 * @param {object} res - Express result object
108 111 */
109   -function pageAddForPrefix(req, res, next) {
  112 +async function pageAddForPrefix(req, res, next) {
110 113 if (!req.query.prefix) {
111 114 res.json({
112 115 status: 'NOT-OK',
... ... @@ -131,11 +134,11 @@ function pageAddForPrefix(req, res, next) {
131 134 config.senders_imsi.prefix_names[req.query.prefix] = [];
132 135 }
133 136  
134   - res.locals.add_imsi_success = addImsi(config.senders_imsi.prefix_names[req.query.prefix], req.query.imsi);
  137 + res.locals.add_imsi_success = await addImsi(config.senders_imsi.prefix_names[req.query.prefix], req.query.imsi);
135 138 next();
136 139 }
137 140  
138   -function pageDelForPrefix(req, res) {
  141 +async function pageDelForPrefix(req, res) {
139 142 if (!req.query.prefix) {
140 143 res.json({
141 144 status: 'NOT-OK',
... ... @@ -154,7 +157,7 @@ function pageDelForPrefix(req, res) {
154 157 return;
155 158 }
156 159  
157   - delImsi(config.senders_imsi.prefix_names[req.query.prefix], req.query.imsi, true);
  160 + await delImsi(config.senders_imsi.prefix_names[req.query.prefix], req.query.imsi, true);
158 161  
159 162 res.json({
160 163 status: 'OK',
... ... @@ -170,7 +173,7 @@ function pageDelForPrefix(req, res) {
170 173 * @param {string} req.query.imsi - nomor imsi
171 174 * @param {object} res - Express result object
172 175 */
173   -function pageAddForUnknownPrefix(req, res, next) {
  176 +async function pageAddForUnknownPrefix(req, res, next) {
174 177 if (!req.query.imsi) {
175 178 res.json({
176 179 status: 'NOT-OK',
... ... @@ -180,11 +183,11 @@ function pageAddForUnknownPrefix(req, res, next) {
180 183 return;
181 184 }
182 185  
183   - res.locals.add_imsi_success = addImsi(config.senders_imsi.unknown_prefix, req.query.imsi);
  186 + res.locals.add_imsi_success = await addImsi(config.senders_imsi.unknown_prefix, req.query.imsi);
184 187 next();
185 188 }
186 189  
187   -function pageDelForUnknownPrefix(req, res) {
  190 +async function pageDelForUnknownPrefix(req, res) {
188 191 if (!req.query.imsi) {
189 192 res.json({
190 193 status: 'NOT-OK',
... ... @@ -194,7 +197,7 @@ function pageDelForUnknownPrefix(req, res) {
194 197 return;
195 198 }
196 199  
197   - delImsi(config.senders_imsi.unknown_prefix, req.query.imsi);
  200 + await delImsi(config.senders_imsi.unknown_prefix, req.query.imsi);
198 201 res.json({
199 202 status: 'OK',
200 203 message: 'IMSI berhasil dihapus',
... ... @@ -209,7 +212,7 @@ function pageDelForUnknownPrefix(req, res) {
209 212 * @param {string} req.query.imsi - nomor imsi
210 213 * @param {object} res - Express result object
211 214 */
212   -function pageAddForDefault(req, res, next) {
  215 +async function pageAddForDefault(req, res, next) {
213 216 if (!req.query.imsi) {
214 217 res.json({
215 218 status: 'NOT-OK',
... ... @@ -219,11 +222,11 @@ function pageAddForDefault(req, res, next) {
219 222 return;
220 223 }
221 224  
222   - res.locals.add_imsi_success = addImsi(config.senders_imsi.default, req.query.imsi);
  225 + res.locals.add_imsi_success = await addImsi(config.senders_imsi.default, req.query.imsi);
223 226 next();
224 227 }
225 228  
226   -function pageDelForDefault(req, res) {
  229 +async function pageDelForDefault(req, res) {
227 230 if (!req.query.imsi) {
228 231 res.json({
229 232 status: 'NOT-OK',
... ... @@ -233,7 +236,7 @@ function pageDelForDefault(req, res) {
233 236 return;
234 237 }
235 238  
236   - delImsi(config.senders_imsi.default, req.query.imsi);
  239 + await delImsi(config.senders_imsi.default, req.query.imsi);
237 240 res.json({
238 241 status: 'OK',
239 242 message: 'IMSI berhasil dihapus',