corebasic 1.0.141 → 1.0.143

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/libs/auth.js CHANGED
@@ -37,7 +37,8 @@ async function attemptLogin(req, res) {
37
37
  let meta = {company: "GLOBAL", outlet: "GLOBAL"}
38
38
 
39
39
  let expiry = 300000
40
- let phone = req.body.phone.trim()
40
+ let userMob = req.body.mob ?? req.body.phone
41
+ let mob = userMob === '0123456789' ? userMob : Utils.parseMob(userMob)
41
42
  let time = new Date().getTime()
42
43
  let collection = (req.body.app ? req.body.app + '.' : '') + "auth.login"
43
44
  let errMessage = {success: false, message: "Login Server Error"}
@@ -47,31 +48,31 @@ async function attemptLogin(req, res) {
47
48
  throw { ...errMessage, mode: otpValid ? 'verify' : 'login', info: "Invalid Client ID" }
48
49
 
49
50
  if (otpValid) { // verify login
50
- let res = await Dip.query(meta, collection, { _id: phone, otp: req.body.otp, clientId, time: { $gt: time - expiry } })
51
+ let res = await Dip.query(meta, collection, { _id: mob, otp: req.body.otp, clientId, time: { $gt: time - expiry } })
51
52
  if (res.length) {
52
- try {await Dip.update(meta, collection, { _id: phone }, { $set: { otp: '', clientId: '' } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
53
- return {...res[0], mode: 'verify', success: true, userId: res[0].userId, phone: res[0]._id}
53
+ try {await Dip.update(meta, collection, { _id: mob }, { $set: { otp: '', clientId: '', loggedIn: true } }) } catch (err) { throw {...errMessage, mode: 'verify', info: 'Cleanup Failed'} }
54
+ return {...res[0], mode: 'verify', success: true, userId: res[0].userId, phone: res[0]._id, mob: res[0]._id}
54
55
  }
55
56
  throw {...errMessage, mode: 'verify', info: "Invalid/Expired OTP"}
56
57
  } else { // generate login
57
- let otp = phone === '0123456789' ? '1234' : otpGenerator.generate(4, { upperCaseAlphabets: false, specialChars: false, digital: true, lowerCaseAlphabets: false });
58
- if (phone === '0123456789' || await sendOtp(phone, otp, req.body.app)) {
58
+ let otp = mob === '0123456789' ? '1234' : otpGenerator.generate(4, { upperCaseAlphabets: false, specialChars: false, digital: true, lowerCaseAlphabets: false });
59
+ if (mob === '0123456789' || await sendOtp(mob, otp, req.body.app)) {
59
60
  let userId = req.body.userId ?? Utils.uid()
60
61
  let now = Utils.now().toISOString()
61
62
  let data = req.body.data ?? {}
62
63
  let info = { clientId, created: now, updated: now, ...data}
63
- await Dip.update(meta, collection, { _id: phone }, { $set: { otp, clientId, time, updated: now }, $setOnInsert: { _id: phone, otp, time, userId, ...info } }, { upsert: true })
64
+ await Dip.update(meta, collection, { _id: mob }, { $set: { otp, clientId, time, updated: now, attemptLogin: true }, $setOnInsert: { _id: mob, otp, time, userId, ...info } }, { upsert: true })
64
65
 
65
- return {mode: 'login', success: true, userId, expiry, phone}
66
+ return {mode: 'login', success: true, userId, expiry, phone: mob, mob}
66
67
  }
67
68
  throw {...errMessage, mode: 'login', info: 'Generating Info Failed'}
68
69
  }
69
70
  }
70
71
 
71
72
 
72
- async function sendOtp(phone, otp, app) {
73
+ async function sendOtp(mob, otp, app) {
73
74
  try {
74
- let api = process.env.SMS_API.replace(':phone', phone).replace(':otp', otp).replace(':app', app ?? 'app')
75
+ let api = process.env.SMS_API.replace(':phone', mob).replace(':otp', otp).replace(':app', app ?? 'app')
75
76
  await axios.get(api)
76
77
  return true
77
78
  } catch {
@@ -83,11 +84,11 @@ async function sendOtp(phone, otp, app) {
83
84
 
84
85
  // API
85
86
  // ----
86
- // Login => HttpRequest.post('/login').send({app: '', phone: '', userId: 'Optional', data: {//optional} }) // Response => { mode: 'login', success, userId, expiry }
87
- // Verify => HttpRequest.post('/login').send({app: '', clientId: '', phone: '', otp: ''}) // Response => { mode: 'verify', success, userId }
87
+ // Login => HttpRequest.post('/login').send({app: '', mob: '', userId: 'Optional', data: {//optional} }) // Response => { mode: 'login', success, userId, expiry }
88
+ // Verify => HttpRequest.post('/login').send({app: '', clientId: '', mob: '', otp: ''}) // Response => { mode: 'verify', success, userId }
88
89
 
89
90
  // Usage
90
- // Auth.validate((req) => ["Phone1", "Phone2"].includes(req.body.phone.trim()))
91
+ // Auth.validate((req) => ["Phone1", "Phone2"].includes(req.body.mob.trim()))
91
92
  // Auth.start(app, async (req, res, data) => {
92
93
  // try {
93
94
  // let meta = {company: "GLOBAL", outlet: "GLOBAL"}
@@ -0,0 +1,561 @@
1
+ export const codes = {
2
+ AD: { code: 'AD', label: 'Andorra', phone: '376', phoneLength: 6 },
3
+ AE: {
4
+ code: 'AE',
5
+ label: 'United Arab Emirates',
6
+ phone: '971',
7
+ phoneLength: 9
8
+ },
9
+ AF: { code: 'AF', label: 'Afghanistan', phone: '93', phoneLength: 9 },
10
+ AG: {
11
+ code: 'AG',
12
+ label: 'Antigua and Barbuda',
13
+ phone: '1-268',
14
+ phoneLength: 10
15
+ },
16
+ AI: { code: 'AI', label: 'Anguilla', phone: '1-264', phoneLength: 10 },
17
+ AL: { code: 'AL', label: 'Albania', phone: '355', phoneLength: 9 },
18
+ AM: { code: 'AM', label: 'Armenia', phone: '374', phoneLength: 6 },
19
+ AO: { code: 'AO', label: 'Angola', phone: '244', phoneLength: 9 },
20
+ AQ: { code: 'AQ', label: 'Antarctica', phone: '672', phoneLength: 6 },
21
+ AR: {
22
+ code: 'AR',
23
+ label: 'Argentina',
24
+ phone: '54',
25
+ phoneLength: [ 6, 7, 8 ]
26
+ },
27
+ AS: {
28
+ code: 'AS',
29
+ label: 'American Samoa',
30
+ phone: '1-684',
31
+ phoneLength: 10
32
+ },
33
+ AT: {
34
+ code: 'AT',
35
+ label: 'Austria',
36
+ phone: '43',
37
+ phoneLength: [ 10, 11 ]
38
+ },
39
+ AU: {
40
+ code: 'AU',
41
+ label: 'Australia',
42
+ phone: '61',
43
+ suggested: true,
44
+ phoneLength: 9
45
+ },
46
+ AW: { code: 'AW', label: 'Aruba', phone: '297', phoneLength: 7 },
47
+ AX: {
48
+ code: 'AX',
49
+ label: 'Alland Islands',
50
+ phone: '358',
51
+ min: 7,
52
+ max: 10
53
+ },
54
+ AZ: { code: 'AZ', label: 'Azerbaijan', phone: '994', phoneLength: 9 },
55
+ BA: {
56
+ code: 'BA',
57
+ label: 'Bosnia and Herzegovina',
58
+ phone: '387',
59
+ phoneLength: 8
60
+ },
61
+ BB: { code: 'BB', label: 'Barbados', phone: '1-246', phoneLength: 10 },
62
+ BD: { code: 'BD', label: 'Bangladesh', phone: '880', phoneLength: 10 },
63
+ BE: { code: 'BE', label: 'Belgium', phone: '32', phoneLength: 9 },
64
+ BF: { code: 'BF', label: 'Burkina Faso', phone: '226', phoneLength: 8 },
65
+ BG: { code: 'BG', label: 'Bulgaria', phone: '359', phoneLength: 9 },
66
+ BH: { code: 'BH', label: 'Bahrain', phone: '973', phoneLength: 8 },
67
+ BI: { code: 'BI', label: 'Burundi', phone: '257', phoneLength: 8 },
68
+ BJ: { code: 'BJ', label: 'Benin', phone: '229', phoneLength: 8 },
69
+ BL: {
70
+ code: 'BL',
71
+ label: 'Saint Barthelemy',
72
+ phone: '590',
73
+ phoneLength: 9
74
+ },
75
+ BM: { code: 'BM', label: 'Bermuda', phone: '1-441', phoneLength: 10 },
76
+ BN: {
77
+ code: 'BN',
78
+ label: 'Brunei Darussalam',
79
+ phone: '673',
80
+ phoneLength: 7
81
+ },
82
+ BO: { code: 'BO', label: 'Bolivia', phone: '591', phoneLength: 9 },
83
+ BR: { code: 'BR', label: 'Brazil', phone: '55', phoneLength: 11 },
84
+ BS: { code: 'BS', label: 'Bahamas', phone: '1-242', phoneLength: 10 },
85
+ BT: { code: 'BT', label: 'Bhutan', phone: '975', phoneLength: 7 },
86
+ BV: { code: 'BV', label: 'Bouvet Island', phone: '47', phoneLength: 10 },
87
+ BW: { code: 'BW', label: 'Botswana', phone: '267', phoneLength: 7 },
88
+ BY: { code: 'BY', label: 'Belarus', phone: '375', phoneLength: 9 },
89
+ BZ: { code: 'BZ', label: 'Belize', phone: '501', phoneLength: 7 },
90
+ CA: {
91
+ code: 'CA',
92
+ label: 'Canada',
93
+ phone: '1',
94
+ suggested: true,
95
+ phoneLength: 10
96
+ },
97
+ CC: {
98
+ code: 'CC',
99
+ label: 'Cocos (Keeling) Islands',
100
+ phone: '61',
101
+ phoneLength: 10
102
+ },
103
+ CD: {
104
+ code: 'CD',
105
+ label: 'Congo, Democratic Republic of the',
106
+ phone: '243',
107
+ phoneLength: 7
108
+ },
109
+ CF: {
110
+ code: 'CF',
111
+ label: 'Central African Republic',
112
+ phone: '236',
113
+ phoneLength: 8
114
+ },
115
+ CG: {
116
+ code: 'CG',
117
+ label: 'Congo, Republic of the',
118
+ phone: '242',
119
+ phoneLength: 9
120
+ },
121
+ CH: { code: 'CH', label: 'Switzerland', phone: '41', phoneLength: 9 },
122
+ CI: { code: 'CI', label: "Cote d'Ivoire", phone: '225', phoneLength: 8 },
123
+ CK: { code: 'CK', label: 'Cook Islands', phone: '682', phoneLength: 5 },
124
+ CL: { code: 'CL', label: 'Chile', phone: '56', phoneLength: 9 },
125
+ CM: { code: 'CM', label: 'Cameroon', phone: '237', phoneLength: 9 },
126
+ CN: { code: 'CN', label: 'China', phone: '86', phoneLength: 11 },
127
+ CO: { code: 'CO', label: 'Colombia', phone: '57', phoneLength: 10 },
128
+ CR: { code: 'CR', label: 'Costa Rica', phone: '506', phoneLength: 8 },
129
+ CU: { code: 'CU', label: 'Cuba', phone: '53', phoneLength: 8 },
130
+ CV: { code: 'CV', label: 'Cape Verde', phone: '238', phoneLength: 7 },
131
+ CW: { code: 'CW', label: 'Curacao', phone: '599', phoneLength: 7 },
132
+ CX: {
133
+ code: 'CX',
134
+ label: 'Christmas Island',
135
+ phone: '61',
136
+ phoneLength: 9
137
+ },
138
+ CY: { code: 'CY', label: 'Cyprus', phone: '357', phoneLength: 8 },
139
+ CZ: { code: 'CZ', label: 'Czech Republic', phone: '420', phoneLength: 9 },
140
+ DE: {
141
+ code: 'DE',
142
+ label: 'Germany',
143
+ phone: '49',
144
+ suggested: true,
145
+ phoneLength: 10
146
+ },
147
+ DJ: { code: 'DJ', label: 'Djibouti', phone: '253', phoneLength: 10 },
148
+ DK: { code: 'DK', label: 'Denmark', phone: '45', phoneLength: 8 },
149
+ DM: { code: 'DM', label: 'Dominica', phone: '1-767', phoneLength: 10 },
150
+ DO: {
151
+ code: 'DO',
152
+ label: 'Dominican Republic',
153
+ phone: '1-809',
154
+ phoneLength: 10
155
+ },
156
+ DZ: { code: 'DZ', label: 'Algeria', phone: '213', phoneLength: 9 },
157
+ EC: { code: 'EC', label: 'Ecuador', phone: '593', phoneLength: 9 },
158
+ EE: { code: 'EE', label: 'Estonia', phone: '372', phoneLength: 8 },
159
+ EG: { code: 'EG', label: 'Egypt', phone: '20', phoneLength: 10 },
160
+ EH: { code: 'EH', label: 'Western Sahara', phone: '212', phoneLength: 9 },
161
+ ER: { code: 'ER', label: 'Eritrea', phone: '291', phoneLength: 7 },
162
+ ES: { code: 'ES', label: 'Spain', phone: '34', phoneLength: 9 },
163
+ ET: { code: 'ET', label: 'Ethiopia', phone: '251', phoneLength: 9 },
164
+ FI: { code: 'FI', label: 'Finland', phone: '358', min: 9, max: 11 },
165
+ FJ: { code: 'FJ', label: 'Fiji', phone: '679', phoneLength: 7 },
166
+ FK: {
167
+ code: 'FK',
168
+ label: 'Falkland Islands (Malvinas)',
169
+ phone: '500',
170
+ phoneLength: 5
171
+ },
172
+ FM: {
173
+ code: 'FM',
174
+ label: 'Micronesia, Federated States of',
175
+ phone: '691',
176
+ phoneLength: 7
177
+ },
178
+ FO: { code: 'FO', label: 'Faroe Islands', phone: '298', phoneLength: 5 },
179
+ FR: {
180
+ code: 'FR',
181
+ label: 'France',
182
+ phone: '33',
183
+ suggested: true,
184
+ phoneLength: 9
185
+ },
186
+ GA: { code: 'GA', label: 'Gabon', phone: '241', phoneLength: 7 },
187
+ GB: { code: 'GB', label: 'United Kingdom', phone: '44', phoneLength: 10 },
188
+ GD: { code: 'GD', label: 'Grenada', phone: '1-473', phoneLength: 10 },
189
+ GE: { code: 'GE', label: 'Georgia', phone: '995', phoneLength: 9 },
190
+ GF: { code: 'GF', label: 'French Guiana', phone: '594', phoneLength: 9 },
191
+ GG: { code: 'GG', label: 'Guernsey', phone: '44', phoneLength: 10 },
192
+ GH: { code: 'GH', label: 'Ghana', phone: '233', phoneLength: 9 },
193
+ GI: { code: 'GI', label: 'Gibraltar', phone: '350', phoneLength: 8 },
194
+ GL: { code: 'GL', label: 'Greenland', phone: '299', phoneLength: 6 },
195
+ GM: { code: 'GM', label: 'Gambia', phone: '220', phoneLength: 7 },
196
+ GN: { code: 'GN', label: 'Guinea', phone: '224', phoneLength: 9 },
197
+ GP: { code: 'GP', label: 'Guadeloupe', phone: '590', phoneLength: 9 },
198
+ GQ: {
199
+ code: 'GQ',
200
+ label: 'Equatorial Guinea',
201
+ phone: '240',
202
+ phoneLength: 9
203
+ },
204
+ GR: { code: 'GR', label: 'Greece', phone: '30', phoneLength: 10 },
205
+ GS: {
206
+ code: 'GS',
207
+ label: 'South Georgia and the South Sandwich Islands',
208
+ phone: '500',
209
+ phoneLength: 5
210
+ },
211
+ GT: { code: 'GT', label: 'Guatemala', phone: '502', phoneLength: 8 },
212
+ GU: { code: 'GU', label: 'Guam', phone: '1-671', phoneLength: 10 },
213
+ GW: { code: 'GW', label: 'Guinea-Bissau', phone: '245', phoneLength: 9 },
214
+ GY: { code: 'GY', label: 'Guyana', phone: '592', phoneLength: 7 },
215
+ HK: { code: 'HK', label: 'Hong Kong', phone: '852', phoneLength: 8 },
216
+ HM: {
217
+ code: 'HM',
218
+ label: 'Heard Island and McDonald Islands',
219
+ phone: '672',
220
+ phoneLength: 10
221
+ },
222
+ HN: { code: 'HN', label: 'Honduras', phone: '504', phoneLength: 8 },
223
+ HR: { code: 'HR', label: 'Croatia', phone: '385', phoneLength: 9 },
224
+ HT: { code: 'HT', label: 'Haiti', phone: '509', phoneLength: 8 },
225
+ HU: { code: 'HU', label: 'Hungary', phone: '36', phoneLength: 9 },
226
+ ID: { code: 'ID', label: 'Indonesia', phone: '62', phoneLength: 11 },
227
+ IE: { code: 'IE', label: 'Ireland', phone: '353', phoneLength: 9 },
228
+ IL: { code: 'IL', label: 'Israel', phone: '972', phoneLength: 9 },
229
+ IM: { code: 'IM', label: 'Isle of Man', phone: '44', phoneLength: 10 },
230
+ IN: { code: 'IN', label: 'India', phone: '91', phoneLength: 10 },
231
+ IO: {
232
+ code: 'IO',
233
+ label: 'British Indian Ocean Territory',
234
+ phone: '246',
235
+ phoneLength: 7
236
+ },
237
+ IQ: { code: 'IQ', label: 'Iraq', phone: '964', phoneLength: 10 },
238
+ IR: {
239
+ code: 'IR',
240
+ label: 'Iran, Islamic Republic of',
241
+ phone: '98',
242
+ phoneLength: 11
243
+ },
244
+ IS: { code: 'IS', label: 'Iceland', phone: '354', phoneLength: 7 },
245
+ IT: { code: 'IT', label: 'Italy', phone: '39', phoneLength: 10 },
246
+ JE: { code: 'JE', label: 'Jersey', phone: '44', phoneLength: 10 },
247
+ JM: { code: 'JM', label: 'Jamaica', phone: '1-876', phoneLength: 10 },
248
+ JO: { code: 'JO', label: 'Jordan', phone: '962', phoneLength: [ 8, 9 ] },
249
+ JP: { code: 'JP', label: 'Japan', phone: '81', suggested: true },
250
+ KE: { code: 'KE', label: 'Kenya', phone: '254', phoneLength: 10 },
251
+ KG: { code: 'KG', label: 'Kyrgyzstan', phone: '996', phoneLength: 9 },
252
+ KH: { code: 'KH', label: 'Cambodia', phone: '855', phoneLength: 9 },
253
+ KI: { code: 'KI', label: 'Kiribati', phone: '686', phoneLength: 8 },
254
+ KM: { code: 'KM', label: 'Comoros', phone: '269', phoneLength: 7 },
255
+ KN: {
256
+ code: 'KN',
257
+ label: 'Saint Kitts and Nevis',
258
+ phone: '1-869',
259
+ phoneLength: 10
260
+ },
261
+ KP: {
262
+ code: 'KP',
263
+ label: "Korea, Democratic People's Republic of",
264
+ phone: '850',
265
+ phoneLength: [ 4, 6, 7, 13 ]
266
+ },
267
+ KR: {
268
+ code: 'KR',
269
+ label: 'Korea, Republic of',
270
+ phone: '82',
271
+ phoneLength: [ 7, 8 ]
272
+ },
273
+ KW: { code: 'KW', label: 'Kuwait', phone: '965', phoneLength: 8 },
274
+ KY: {
275
+ code: 'KY',
276
+ label: 'Cayman Islands',
277
+ phone: '1-345',
278
+ phoneLength: 7
279
+ },
280
+ KZ: { code: 'KZ', label: 'Kazakhstan', phone: '7', phoneLength: 10 },
281
+ LA: {
282
+ code: 'LA',
283
+ label: "Lao People's Democratic Republic",
284
+ phone: '856',
285
+ phoneLength: [ 8, 9 ]
286
+ },
287
+ LB: { code: 'LB', label: 'Lebanon', phone: '961', phoneLength: [ 7, 8 ] },
288
+ LC: { code: 'LC', label: 'Saint Lucia', phone: '1-758', phoneLength: 7 },
289
+ LI: { code: 'LI', label: 'Liechtenstein', phone: '423', phoneLength: 7 },
290
+ LK: { code: 'LK', label: 'Sri Lanka', phone: '94', phoneLength: 7 },
291
+ LR: { code: 'LR', label: 'Liberia', phone: '231', phoneLength: [ 8, 9 ] },
292
+ LS: { code: 'LS', label: 'Lesotho', phone: '266', phoneLength: 8 },
293
+ LT: { code: 'LT', label: 'Lithuania', phone: '370', phoneLength: 8 },
294
+ LU: { code: 'LU', label: 'Luxembourg', phone: '352', phoneLength: 9 },
295
+ LV: { code: 'LV', label: 'Latvia', phone: '371', phoneLength: 8 },
296
+ LY: { code: 'LY', label: 'Libya', phone: '218', phoneLength: 10 },
297
+ MA: { code: 'MA', label: 'Morocco', phone: '212', phoneLength: 9 },
298
+ MC: { code: 'MC', label: 'Monaco', phone: '377', phoneLength: 8 },
299
+ MD: {
300
+ code: 'MD',
301
+ label: 'Moldova, Republic of',
302
+ phone: '373',
303
+ phoneLength: 8
304
+ },
305
+ ME: { code: 'ME', label: 'Montenegro', phone: '382', phoneLength: 8 },
306
+ MF: {
307
+ code: 'MF',
308
+ label: 'Saint Martin (French part)',
309
+ phone: '590',
310
+ phoneLength: 6
311
+ },
312
+ MG: { code: 'MG', label: 'Madagascar', phone: '261', phoneLength: 7 },
313
+ MH: {
314
+ code: 'MH',
315
+ label: 'Marshall Islands',
316
+ phone: '692',
317
+ phoneLength: 7
318
+ },
319
+ MK: {
320
+ code: 'MK',
321
+ label: 'Macedonia, the Former Yugoslav Republic of',
322
+ phone: '389',
323
+ phoneLength: 8
324
+ },
325
+ ML: { code: 'ML', label: 'Mali', phone: '223', phoneLength: 8 },
326
+ MM: { code: 'MM', label: 'Myanmar', phone: '95', min: 7, max: 10 },
327
+ MN: { code: 'MN', label: 'Mongolia', phone: '976', phoneLength: 8 },
328
+ MO: { code: 'MO', label: 'Macao', phone: '853', phoneLength: 8 },
329
+ MP: {
330
+ code: 'MP',
331
+ label: 'Northern Mariana Islands',
332
+ phone: '1-670',
333
+ phoneLength: 7
334
+ },
335
+ MQ: { code: 'MQ', label: 'Martinique', phone: '596', phoneLength: 9 },
336
+ MR: { code: 'MR', label: 'Mauritania', phone: '222', phoneLength: 8 },
337
+ MS: { code: 'MS', label: 'Montserrat', phone: '1-664', phoneLength: 10 },
338
+ MT: { code: 'MT', label: 'Malta', phone: '356', phoneLength: 8 },
339
+ MU: { code: 'MU', label: 'Mauritius', phone: '230', phoneLength: 8 },
340
+ MV: { code: 'MV', label: 'Maldives', phone: '960', phoneLength: 7 },
341
+ MW: {
342
+ code: 'MW',
343
+ label: 'Malawi',
344
+ phone: '265',
345
+ phoneLength: [ 7, 8, 9 ]
346
+ },
347
+ MX: { code: 'MX', label: 'Mexico', phone: '52', phoneLength: 10 },
348
+ MY: { code: 'MY', label: 'Malaysia', phone: '60', phoneLength: 7 },
349
+ MZ: { code: 'MZ', label: 'Mozambique', phone: '258', phoneLength: 12 },
350
+ NA: { code: 'NA', label: 'Namibia', phone: '264', phoneLength: 7 },
351
+ NC: { code: 'NC', label: 'New Caledonia', phone: '687', phoneLength: 6 },
352
+ NE: { code: 'NE', label: 'Niger', phone: '227', phoneLength: 8 },
353
+ NF: { code: 'NF', label: 'Norfolk Island', phone: '672', phoneLength: 6 },
354
+ NG: { code: 'NG', label: 'Nigeria', phone: '234', phoneLength: 8 },
355
+ NI: { code: 'NI', label: 'Nicaragua', phone: '505', phoneLength: 8 },
356
+ NL: { code: 'NL', label: 'Netherlands', phone: '31', phoneLength: 9 },
357
+ NO: { code: 'NO', label: 'Norway', phone: '47', phoneLength: 8 },
358
+ NP: { code: 'NP', label: 'Nepal', phone: '977', phoneLength: 10 },
359
+ NR: { code: 'NR', label: 'Nauru', phone: '674', phoneLength: 7 },
360
+ NU: { code: 'NU', label: 'Niue', phone: '683', phoneLength: 4 },
361
+ NZ: {
362
+ code: 'NZ',
363
+ label: 'New Zealand',
364
+ phone: '64',
365
+ phoneLength: [ 8, 9 ]
366
+ },
367
+ OM: { code: 'OM', label: 'Oman', phone: '968', phoneLength: 8 },
368
+ PA: { code: 'PA', label: 'Panama', phone: '507', phoneLength: 8 },
369
+ PE: { code: 'PE', label: 'Peru', phone: '51', phoneLength: 9 },
370
+ PF: {
371
+ code: 'PF',
372
+ label: 'French Polynesia',
373
+ phone: '689',
374
+ phoneLength: 8
375
+ },
376
+ PG: {
377
+ code: 'PG',
378
+ label: 'Papua New Guinea',
379
+ phone: '675',
380
+ phoneLength: 8
381
+ },
382
+ PH: { code: 'PH', label: 'Philippines', phone: '63', phoneLength: 10 },
383
+ PK: { code: 'PK', label: 'Pakistan', phone: '92', phoneLength: 10 },
384
+ PL: { code: 'PL', label: 'Poland', phone: '48', phoneLength: 9 },
385
+ PM: {
386
+ code: 'PM',
387
+ label: 'Saint Pierre and Miquelon',
388
+ phone: '508',
389
+ phoneLength: 6
390
+ },
391
+ PN: { code: 'PN', label: 'Pitcairn', phone: '870', phoneLength: 9 },
392
+ PR: { code: 'PR', label: 'Puerto Rico', phone: '1', phoneLength: 10 },
393
+ PS: {
394
+ code: 'PS',
395
+ label: 'Palestine, State of',
396
+ phone: '970',
397
+ phoneLength: 9
398
+ },
399
+ PT: { code: 'PT', label: 'Portugal', phone: '351', phoneLength: 9 },
400
+ PW: { code: 'PW', label: 'Palau', phone: '680', phoneLength: 7 },
401
+ PY: { code: 'PY', label: 'Paraguay', phone: '595', phoneLength: 9 },
402
+ QA: { code: 'QA', label: 'Qatar', phone: '974', phoneLength: 8 },
403
+ RE: { code: 'RE', label: 'Reunion', phone: '262', phoneLength: 10 },
404
+ RO: { code: 'RO', label: 'Romania', phone: '40', phoneLength: 10 },
405
+ RS: { code: 'RS', label: 'Serbia', phone: '381', phoneLength: 9 },
406
+ RU: {
407
+ code: 'RU',
408
+ label: 'Russian Federation',
409
+ phone: '7',
410
+ phoneLength: 10
411
+ },
412
+ RW: { code: 'RW', label: 'Rwanda', phone: '250', phoneLength: 9 },
413
+ SA: { code: 'SA', label: 'Saudi Arabia', phone: '966', phoneLength: 9 },
414
+ SB: {
415
+ code: 'SB',
416
+ label: 'Solomon Islands',
417
+ phone: '677',
418
+ phoneLength: 7
419
+ },
420
+ SC: { code: 'SC', label: 'Seychelles', phone: '248', phoneLength: 7 },
421
+ SD: { code: 'SD', label: 'Sudan', phone: '249', phoneLength: 7 },
422
+ SE: { code: 'SE', label: 'Sweden', phone: '46', phoneLength: 7 },
423
+ SG: { code: 'SG', label: 'Singapore', phone: '65', phoneLength: 8 },
424
+ SH: { code: 'SH', label: 'Saint Helena', phone: '290', phoneLength: 4 },
425
+ SI: { code: 'SI', label: 'Slovenia', phone: '386', phoneLength: 9 },
426
+ SJ: {
427
+ code: 'SJ',
428
+ label: 'Svalbard and Jan Mayen',
429
+ phone: '47',
430
+ phoneLength: 8
431
+ },
432
+ SK: { code: 'SK', label: 'Slovakia', phone: '421', phoneLength: 9 },
433
+ SL: { code: 'SL', label: 'Sierra Leone', phone: '232', phoneLength: 8 },
434
+ SM: { code: 'SM', label: 'San Marino', phone: '378', phoneLength: 10 },
435
+ SN: { code: 'SN', label: 'Senegal', phone: '221', phoneLength: 9 },
436
+ SO: { code: 'SO', label: 'Somalia', phone: '252', phoneLength: [ 8, 9 ] },
437
+ SR: {
438
+ code: 'SR',
439
+ label: 'Suriname',
440
+ phone: '597',
441
+ phoneLength: [ 6, 7 ]
442
+ },
443
+ SS: { code: 'SS', label: 'South Sudan', phone: '211', phoneLength: 7 },
444
+ ST: {
445
+ code: 'ST',
446
+ label: 'Sao Tome and Principe',
447
+ phone: '239',
448
+ phoneLength: 7
449
+ },
450
+ SV: { code: 'SV', label: 'El Salvador', phone: '503', phoneLength: 8 },
451
+ SX: {
452
+ code: 'SX',
453
+ label: 'Sint Maarten (Dutch part)',
454
+ phone: '1-721',
455
+ phoneLength: 10
456
+ },
457
+ SY: {
458
+ code: 'SY',
459
+ label: 'Syrian Arab Republic',
460
+ phone: '963',
461
+ phoneLength: 7
462
+ },
463
+ SZ: { code: 'SZ', label: 'Swaziland', phone: '268', phoneLength: 8 },
464
+ TC: {
465
+ code: 'TC',
466
+ label: 'Turks and Caicos Islands',
467
+ phone: '1-649',
468
+ phoneLength: 10
469
+ },
470
+ TD: { code: 'TD', label: 'Chad', phone: '235', phoneLength: 6 },
471
+ TF: {
472
+ code: 'TF',
473
+ label: 'French Southern Territories',
474
+ phone: '262',
475
+ phoneLength: 10
476
+ },
477
+ TG: { code: 'TG', label: 'Togo', phone: '228', phoneLength: 8 },
478
+ TH: { code: 'TH', label: 'Thailand', phone: '66', phoneLength: 9 },
479
+ TJ: { code: 'TJ', label: 'Tajikistan', phone: '992', phoneLength: 9 },
480
+ TK: { code: 'TK', label: 'Tokelau', phone: '690', phoneLength: 5 },
481
+ TL: { code: 'TL', label: 'Timor-Leste', phone: '670', phoneLength: 7 },
482
+ TM: { code: 'TM', label: 'Turkmenistan', phone: '993', phoneLength: 8 },
483
+ TN: { code: 'TN', label: 'Tunisia', phone: '216', phoneLength: 8 },
484
+ TO: { code: 'TO', label: 'Tonga', phone: '676', phoneLength: 5 },
485
+ TR: { code: 'TR', label: 'Turkey', phone: '90', phoneLength: 11 },
486
+ TT: {
487
+ code: 'TT',
488
+ label: 'Trinidad and Tobago',
489
+ phone: '1-868',
490
+ phoneLength: 7
491
+ },
492
+ TV: { code: 'TV', label: 'Tuvalu', phone: '688', phoneLength: 5 },
493
+ TW: {
494
+ code: 'TW',
495
+ label: 'Taiwan, Province of China',
496
+ phone: '886',
497
+ phoneLength: 9
498
+ },
499
+ TZ: {
500
+ code: 'TZ',
501
+ label: 'United Republic of Tanzania',
502
+ phone: '255',
503
+ phoneLength: 7
504
+ },
505
+ UA: { code: 'UA', label: 'Ukraine', phone: '380', phoneLength: 9 },
506
+ UG: { code: 'UG', label: 'Uganda', phone: '256', phoneLength: 7 },
507
+ US: {
508
+ code: 'US',
509
+ label: 'United States',
510
+ phone: '1',
511
+ suggested: true,
512
+ phoneLength: 10
513
+ },
514
+ UY: { code: 'UY', label: 'Uruguay', phone: '598', phoneLength: 8 },
515
+ UZ: { code: 'UZ', label: 'Uzbekistan', phone: '998', phoneLength: 9 },
516
+ VA: {
517
+ code: 'VA',
518
+ label: 'Holy See (Vatican City State)',
519
+ phone: '379',
520
+ phoneLength: 10
521
+ },
522
+ VC: {
523
+ code: 'VC',
524
+ label: 'Saint Vincent and the Grenadines',
525
+ phone: '1-784',
526
+ phoneLength: 7
527
+ },
528
+ VE: { code: 'VE', label: 'Venezuela', phone: '58', phoneLength: 7 },
529
+ VG: {
530
+ code: 'VG',
531
+ label: 'British Virgin Islands',
532
+ phone: '1-284',
533
+ phoneLength: 7
534
+ },
535
+ VI: {
536
+ code: 'VI',
537
+ label: 'US Virgin Islands',
538
+ phone: '1-340',
539
+ phoneLength: 10
540
+ },
541
+ VN: { code: 'VN', label: 'Vietnam', phone: '84', phoneLength: 9 },
542
+ VU: { code: 'VU', label: 'Vanuatu', phone: '678', phoneLength: 5 },
543
+ WF: {
544
+ code: 'WF',
545
+ label: 'Wallis and Futuna',
546
+ phone: '681',
547
+ phoneLength: 6
548
+ },
549
+ WS: {
550
+ code: 'WS',
551
+ label: 'Samoa',
552
+ phone: '685',
553
+ phoneLength: [ 5, 6, 7 ]
554
+ },
555
+ XK: { code: 'XK', label: 'Kosovo', phone: '383', phoneLength: 8 },
556
+ YE: { code: 'YE', label: 'Yemen', phone: '967', phoneLength: 9 },
557
+ YT: { code: 'YT', label: 'Mayotte', phone: '262', phoneLength: 9 },
558
+ ZA: { code: 'ZA', label: 'South Africa', phone: '27', phoneLength: 9 },
559
+ ZM: { code: 'ZM', label: 'Zambia', phone: '260', phoneLength: 9 },
560
+ ZW: { code: 'ZW', label: 'Zimbabwe', phone: '263', phoneLength: 9 }
561
+ }
package/libs/utils.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as ObjectId from './ObjectId.js'
2
+ import * as Mobile from './mobilecodes.js'
2
3
 
3
4
  // Parse JSON file: fileToJson
4
5
  import {readFile,writeFile} from 'fs/promises';
@@ -211,3 +212,25 @@ export const excludeMiddleware = function(middleware, ...paths) {
211
212
  pathCheck ? next() : middleware(req, res, next);
212
213
  }
213
214
  }
215
+
216
+ // --------------------
217
+ // Parse Mobile Number
218
+ // --------------------
219
+
220
+ export function parseMob(mob, code) {
221
+ code = code ?? "IN"
222
+ if (isEmpty(mob))
223
+ return mob
224
+ mob = mob.trim().replace(/(,| |-|\+)/g, '') // remove comma, space, hyphen and plus
225
+ mob = mob.startsWith("0") ? mob.replace(/^0*/,'') : mob // remove starting n zeroes
226
+
227
+ let prefix = Mobile.codes[code].phone
228
+
229
+ if (!mob.startsWith(prefix)) {
230
+ mob = `${prefix}${mob}`
231
+ } else { // mob.startsWith(prefix)
232
+ if (mob.length < Mobile.codes[code].phoneLength + prefix.length) // mob just happens to start with prefix
233
+ mob = `${prefix}${mob}`
234
+ }
235
+ return mob
236
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.141",
4
+ "version": "1.0.143",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {