@driveflux/api-functions 0.0.6 → 0.0.7-next.1
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/dist/auth/confirm.js +73 -327
- package/dist/auth/emails.js +43 -210
- package/dist/auth/formatter.js +11 -11
- package/dist/auth/otp.js +122 -392
- package/dist/auth/register.js +100 -396
- package/dist/auth/tokens.d.ts +3 -3
- package/dist/auth/tokens.js +115 -430
- package/dist/auth/verifications.d.ts.map +1 -1
- package/dist/auth/verifications.js +191 -514
- package/dist/auth/verifications.js.map +1 -1
- package/dist/constants.js +5 -4
- package/dist/mailjet/calls/manage-contacts-in-list.js +22 -166
- package/dist/mailjet/calls/manage-subscription-status.js +13 -153
- package/dist/mailjet/calls/request-service.d.ts +1 -1
- package/dist/mailjet/calls/request-service.d.ts.map +1 -1
- package/dist/mailjet/calls/request-service.js +18 -183
- package/dist/mailjet/refresh-email-preferences.js +26 -225
- package/dist/mailjet/set-contact.js +23 -214
- package/dist/mailjet/types.js +2 -1
- package/dist/mailjet/utils/convert-to-array.js +9 -58
- package/dist/mailjet/utils/extract-email-preferences.js +41 -217
- package/dist/mailjet/utils/lists.js +30 -249
- package/dist/mailjet/utils/update-email-references.js +27 -208
- package/dist/notion/client.js +48 -197
- package/dist/notion/helpful.js +29 -170
- package/dist/notion/schemas/block.js +49 -43
- package/dist/notion/schemas/common.js +17 -14
- package/dist/notion/schemas/database.js +125 -159
- package/dist/notion/schemas/emoji.js +3 -2
- package/dist/notion/schemas/file.js +10 -10
- package/dist/notion/schemas/kb.js +9 -8
- package/dist/notion/schemas/page.js +126 -171
- package/dist/notion/schemas/parent.js +9 -8
- package/dist/notion/schemas/user.js +21 -20
- package/dist/reservation/agree.js +19 -158
- package/dist/reservation/checks.js +23 -178
- package/dist/reservation/display-vehicle.d.ts +37 -37
- package/dist/reservation/display-vehicle.d.ts.map +1 -1
- package/dist/reservation/display-vehicle.js +142 -514
- package/dist/reservation/fetch-or-create.js +197 -482
- package/dist/reservation/invoice.js +198 -496
- package/dist/reservation/payer.js +28 -177
- package/dist/reservation/reserve.js +31 -191
- package/dist/reservation/types.js +2 -1
- package/dist/reservation/vehicle.js +24 -186
- package/dist/slack.d.ts +2 -2
- package/dist/slack.d.ts.map +1 -1
- package/dist/slack.js +67 -273
- package/dist/validation.js +81 -139
- package/dist/vehicle/vehicle-pricing/constants.js +33 -36
- package/dist/vehicle/vehicle-pricing/index.js +99 -257
- package/dist/vehicle/vehicle-pricing/types.js +2 -1
- package/package.json +12 -12
|
@@ -1,532 +1,209 @@
|
|
|
1
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
-
try {
|
|
3
|
-
var info = gen[key](arg);
|
|
4
|
-
var value = info.value;
|
|
5
|
-
} catch (error) {
|
|
6
|
-
reject(error);
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
if (info.done) {
|
|
10
|
-
resolve(value);
|
|
11
|
-
} else {
|
|
12
|
-
Promise.resolve(value).then(_next, _throw);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function _async_to_generator(fn) {
|
|
16
|
-
return function() {
|
|
17
|
-
var self = this, args = arguments;
|
|
18
|
-
return new Promise(function(resolve, reject) {
|
|
19
|
-
var gen = fn.apply(self, args);
|
|
20
|
-
function _next(value) {
|
|
21
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
-
}
|
|
23
|
-
function _throw(err) {
|
|
24
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
-
}
|
|
26
|
-
_next(undefined);
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function _ts_generator(thisArg, body) {
|
|
31
|
-
var f, y, t, _ = {
|
|
32
|
-
label: 0,
|
|
33
|
-
sent: function() {
|
|
34
|
-
if (t[0] & 1) throw t[1];
|
|
35
|
-
return t[1];
|
|
36
|
-
},
|
|
37
|
-
trys: [],
|
|
38
|
-
ops: []
|
|
39
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
40
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
41
|
-
return this;
|
|
42
|
-
}), g;
|
|
43
|
-
function verb(n) {
|
|
44
|
-
return function(v) {
|
|
45
|
-
return step([
|
|
46
|
-
n,
|
|
47
|
-
v
|
|
48
|
-
]);
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
function step(op) {
|
|
52
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
53
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
54
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
55
|
-
if (y = 0, t) op = [
|
|
56
|
-
op[0] & 2,
|
|
57
|
-
t.value
|
|
58
|
-
];
|
|
59
|
-
switch(op[0]){
|
|
60
|
-
case 0:
|
|
61
|
-
case 1:
|
|
62
|
-
t = op;
|
|
63
|
-
break;
|
|
64
|
-
case 4:
|
|
65
|
-
_.label++;
|
|
66
|
-
return {
|
|
67
|
-
value: op[1],
|
|
68
|
-
done: false
|
|
69
|
-
};
|
|
70
|
-
case 5:
|
|
71
|
-
_.label++;
|
|
72
|
-
y = op[1];
|
|
73
|
-
op = [
|
|
74
|
-
0
|
|
75
|
-
];
|
|
76
|
-
continue;
|
|
77
|
-
case 7:
|
|
78
|
-
op = _.ops.pop();
|
|
79
|
-
_.trys.pop();
|
|
80
|
-
continue;
|
|
81
|
-
default:
|
|
82
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
83
|
-
_ = 0;
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
87
|
-
_.label = op[1];
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
91
|
-
_.label = t[1];
|
|
92
|
-
t = op;
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
if (t && _.label < t[2]) {
|
|
96
|
-
_.label = t[2];
|
|
97
|
-
_.ops.push(op);
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
if (t[2]) _.ops.pop();
|
|
101
|
-
_.trys.pop();
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
op = body.call(thisArg, _);
|
|
105
|
-
} catch (e) {
|
|
106
|
-
op = [
|
|
107
|
-
6,
|
|
108
|
-
e
|
|
109
|
-
];
|
|
110
|
-
y = 0;
|
|
111
|
-
} finally{
|
|
112
|
-
f = t = 0;
|
|
113
|
-
}
|
|
114
|
-
if (op[0] & 5) throw op[1];
|
|
115
|
-
return {
|
|
116
|
-
value: op[0] ? op[1] : void 0,
|
|
117
|
-
done: true
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
1
|
import { config } from '@driveflux/config/backend';
|
|
122
2
|
import { prisma } from '@driveflux/db';
|
|
123
3
|
import { send } from '@driveflux/email';
|
|
124
4
|
import { verificationEmail } from '@driveflux/email-templates/flux/verification';
|
|
125
5
|
import { enhancedFetch } from '@driveflux/fetch';
|
|
126
|
-
import { makeProblem, PROBLEM_CONFLICT, PROBLEM_EXTERNAL, PROBLEM_INTERNAL, PROBLEM_INVALID_DATA, PROBLEM_NOT_FOUND, PROBLEM_NOT_IMPLEMENTED } from '@driveflux/problem';
|
|
6
|
+
import { makeProblem, PROBLEM_CONFLICT, PROBLEM_EXTERNAL, PROBLEM_INTERNAL, PROBLEM_INVALID_DATA, PROBLEM_NOT_FOUND, PROBLEM_NOT_IMPLEMENTED, } from '@driveflux/problem';
|
|
127
7
|
import { reportProblem } from '@driveflux/reporter';
|
|
128
8
|
import { Err, Ok } from '@driveflux/result';
|
|
129
9
|
import { z } from 'zod';
|
|
130
|
-
import { cleanupPhoneNumber } from './formatter.js';
|
|
131
10
|
import { clearToken, createEmailToken, createSMSToken } from './tokens.js';
|
|
132
|
-
|
|
11
|
+
const CheckDuplicationBody = z.object({
|
|
133
12
|
phoneNumber: z.string(),
|
|
134
|
-
email: z.string().transform(
|
|
135
|
-
return email.toLowerCase().trim();
|
|
136
|
-
})
|
|
13
|
+
email: z.string().transform((email) => email.toLowerCase().trim()),
|
|
137
14
|
});
|
|
138
|
-
export
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
if (!temporaryEmail) return [
|
|
174
|
-
3,
|
|
175
|
-
5
|
|
176
|
-
];
|
|
177
|
-
return [
|
|
178
|
-
4,
|
|
179
|
-
verifyEmailUsage(temporaryEmail)
|
|
180
|
-
];
|
|
181
|
-
case 4:
|
|
182
|
-
results = _state.sent();
|
|
183
|
-
if (results === null || results === void 0 ? void 0 : results.err) {
|
|
184
|
-
return [
|
|
185
|
-
2,
|
|
186
|
-
results
|
|
187
|
-
];
|
|
188
|
-
}
|
|
189
|
-
_state.label = 5;
|
|
190
|
-
case 5:
|
|
191
|
-
destinationEmail = temporaryEmail || user.email;
|
|
192
|
-
return [
|
|
193
|
-
4,
|
|
194
|
-
createEmailToken(user.id, destinationEmail)
|
|
195
|
-
];
|
|
196
|
-
case 6:
|
|
197
|
-
token = _state.sent();
|
|
198
|
-
if (!token) {
|
|
199
|
-
return [
|
|
200
|
-
2,
|
|
201
|
-
new Err(makeProblem(PROBLEM_NOT_IMPLEMENTED, 'Token was not created'))
|
|
202
|
-
];
|
|
203
|
-
}
|
|
204
|
-
return [
|
|
205
|
-
4,
|
|
206
|
-
prisma.user.update({
|
|
207
|
-
where: {
|
|
208
|
-
id: user.id
|
|
209
|
-
},
|
|
210
|
-
data: {
|
|
211
|
-
temporaryEmail: destinationEmail
|
|
212
|
-
},
|
|
213
|
-
include: {
|
|
214
|
-
tokens: true
|
|
215
|
-
}
|
|
216
|
-
})
|
|
217
|
-
];
|
|
218
|
-
case 7:
|
|
219
|
-
updatedUser = _state.sent();
|
|
220
|
-
tokens = updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.tokens;
|
|
221
|
-
if (!tokens) return [
|
|
222
|
-
3,
|
|
223
|
-
9
|
|
224
|
-
];
|
|
225
|
-
return [
|
|
226
|
-
4,
|
|
227
|
-
Promise.allSettled(tokens.map(function(userToken) {
|
|
228
|
-
return _async_to_generator(function() {
|
|
229
|
-
return _ts_generator(this, function(_state) {
|
|
230
|
-
switch(_state.label){
|
|
231
|
-
case 0:
|
|
232
|
-
if (!(userToken.scope === 'verify-email' && userToken.id !== token.id)) return [
|
|
233
|
-
3,
|
|
234
|
-
2
|
|
235
|
-
];
|
|
236
|
-
return [
|
|
237
|
-
4,
|
|
238
|
-
clearToken(userToken.id)
|
|
239
|
-
];
|
|
240
|
-
case 1:
|
|
241
|
-
return [
|
|
242
|
-
2,
|
|
243
|
-
_state.sent()
|
|
244
|
-
];
|
|
245
|
-
case 2:
|
|
246
|
-
return [
|
|
247
|
-
2
|
|
248
|
-
];
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
})();
|
|
252
|
-
}))
|
|
253
|
-
];
|
|
254
|
-
case 8:
|
|
255
|
-
_state.sent();
|
|
256
|
-
_state.label = 9;
|
|
257
|
-
case 9:
|
|
258
|
-
// Query object for the link
|
|
259
|
-
query = new URLSearchParams({
|
|
260
|
-
email: destinationEmail,
|
|
261
|
-
token: token.id,
|
|
262
|
-
code: token.value
|
|
263
|
-
});
|
|
264
|
-
if (next) {
|
|
265
|
-
query.append('next', next);
|
|
266
|
-
}
|
|
267
|
-
link = "".concat(config.appUrl, "/verify?").concat(query);
|
|
268
|
-
title = temporaryEmail ? 'Confirm email change' : 'Verify your email address';
|
|
269
|
-
_state.label = 10;
|
|
270
|
-
case 10:
|
|
271
|
-
_state.trys.push([
|
|
272
|
-
10,
|
|
273
|
-
12,
|
|
274
|
-
,
|
|
275
|
-
13
|
|
276
|
-
]);
|
|
277
|
-
// Generate html
|
|
278
|
-
html = verificationEmail({
|
|
279
|
-
user: user.firstName || '',
|
|
280
|
-
link: link,
|
|
281
|
-
title: "".concat(title),
|
|
282
|
-
newEmail: temporaryEmail,
|
|
283
|
-
isChanging: !!temporaryEmail
|
|
284
|
-
});
|
|
285
|
-
return [
|
|
286
|
-
4,
|
|
287
|
-
send({
|
|
288
|
-
to: {
|
|
289
|
-
name: user.firstName || '',
|
|
290
|
-
address: destinationEmail
|
|
291
|
-
},
|
|
292
|
-
subject: "".concat(title),
|
|
293
|
-
html: html
|
|
294
|
-
})
|
|
295
|
-
];
|
|
296
|
-
case 11:
|
|
297
|
-
result = _state.sent();
|
|
298
|
-
if (result.err) {
|
|
299
|
-
console.error('Error sending email:', result.val);
|
|
300
|
-
return [
|
|
301
|
-
2,
|
|
302
|
-
new Err(makeProblem('email_not_sent', 'Unable to send the verification email'))
|
|
303
|
-
];
|
|
304
|
-
}
|
|
305
|
-
return [
|
|
306
|
-
2,
|
|
307
|
-
new Ok({
|
|
308
|
-
success: true,
|
|
309
|
-
link: link
|
|
310
|
-
})
|
|
311
|
-
];
|
|
312
|
-
case 12:
|
|
313
|
-
e = _state.sent();
|
|
314
|
-
return [
|
|
315
|
-
2,
|
|
316
|
-
new Err(makeProblem('email_not_sent', e.message))
|
|
317
|
-
];
|
|
318
|
-
case 13:
|
|
319
|
-
return [
|
|
320
|
-
2
|
|
321
|
-
];
|
|
15
|
+
export const sendVerificationEmail = async (userId, temporaryEmail, next) => {
|
|
16
|
+
const user = typeof userId === 'string'
|
|
17
|
+
? await prisma.user.findUnique({ where: { id: userId } })
|
|
18
|
+
: userId;
|
|
19
|
+
if (!user) {
|
|
20
|
+
return new Err(makeProblem(PROBLEM_NOT_FOUND, `User ${typeof userId === 'string' ? userId : 'unknown'} not found.`));
|
|
21
|
+
}
|
|
22
|
+
if (temporaryEmail) {
|
|
23
|
+
const results = await verifyEmailUsage(temporaryEmail);
|
|
24
|
+
if (results?.err) {
|
|
25
|
+
return results;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const destinationEmail = temporaryEmail || user.email;
|
|
29
|
+
const token = await createEmailToken(user.id, destinationEmail);
|
|
30
|
+
if (!token) {
|
|
31
|
+
return new Err(makeProblem(PROBLEM_NOT_IMPLEMENTED, 'Token was not created'));
|
|
32
|
+
}
|
|
33
|
+
const updatedUser = await prisma.user.update({
|
|
34
|
+
where: {
|
|
35
|
+
id: user.id,
|
|
36
|
+
},
|
|
37
|
+
data: {
|
|
38
|
+
temporaryEmail: destinationEmail,
|
|
39
|
+
},
|
|
40
|
+
include: {
|
|
41
|
+
tokens: true,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
const tokens = updatedUser?.tokens;
|
|
45
|
+
if (tokens) {
|
|
46
|
+
await Promise.allSettled(tokens.map(async (userToken) => {
|
|
47
|
+
if (userToken.scope === 'verify-email' && userToken.id !== token.id) {
|
|
48
|
+
return await clearToken(userToken.id);
|
|
322
49
|
}
|
|
50
|
+
return;
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
// Query object for the link
|
|
54
|
+
const query = new URLSearchParams({
|
|
55
|
+
email: destinationEmail,
|
|
56
|
+
token: token.id,
|
|
57
|
+
code: token.value,
|
|
58
|
+
});
|
|
59
|
+
if (next) {
|
|
60
|
+
query.append('next', next);
|
|
61
|
+
}
|
|
62
|
+
const link = `${config.appUrl}/verify?${query}`;
|
|
63
|
+
const title = temporaryEmail
|
|
64
|
+
? 'Confirm email change'
|
|
65
|
+
: 'Verify your email address';
|
|
66
|
+
try {
|
|
67
|
+
// Generate html
|
|
68
|
+
const html = verificationEmail({
|
|
69
|
+
user: user.firstName || '',
|
|
70
|
+
link,
|
|
71
|
+
title: `${title}`,
|
|
72
|
+
newEmail: temporaryEmail,
|
|
73
|
+
isChanging: !!temporaryEmail,
|
|
323
74
|
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
// TODO: add email regex checking
|
|
333
|
-
if (newEmail.length < 3) {
|
|
334
|
-
return [
|
|
335
|
-
2,
|
|
336
|
-
new Err(makeProblem(PROBLEM_INVALID_DATA, 'Email address needs minimum 3 characters'))
|
|
337
|
-
];
|
|
338
|
-
}
|
|
339
|
-
return [
|
|
340
|
-
4,
|
|
341
|
-
prisma.user.findFirst({
|
|
342
|
-
where: {
|
|
343
|
-
email: newEmail
|
|
344
|
-
}
|
|
345
|
-
})
|
|
346
|
-
];
|
|
347
|
-
case 1:
|
|
348
|
-
existingEmail = _state.sent();
|
|
349
|
-
if (existingEmail) {
|
|
350
|
-
return [
|
|
351
|
-
2,
|
|
352
|
-
new Err(makeProblem(PROBLEM_INVALID_DATA, 'The email address is already in use'))
|
|
353
|
-
];
|
|
354
|
-
}
|
|
355
|
-
return [
|
|
356
|
-
2,
|
|
357
|
-
new Ok({})
|
|
358
|
-
];
|
|
359
|
-
}
|
|
75
|
+
// send email
|
|
76
|
+
const result = await send({
|
|
77
|
+
to: {
|
|
78
|
+
name: user.firstName || '',
|
|
79
|
+
address: destinationEmail,
|
|
80
|
+
},
|
|
81
|
+
subject: `${title}`,
|
|
82
|
+
html,
|
|
360
83
|
});
|
|
361
|
-
|
|
84
|
+
if (result.err) {
|
|
85
|
+
console.error('Error sending email:', result.val);
|
|
86
|
+
return new Err(makeProblem('email_not_sent', 'Unable to send the verification email'));
|
|
87
|
+
}
|
|
88
|
+
return new Ok({ success: true, link });
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
return new Err(makeProblem('email_not_sent', e.message));
|
|
92
|
+
}
|
|
362
93
|
};
|
|
363
|
-
export
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
return
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
2,
|
|
378
|
-
new Err(makeProblem(PROBLEM_NOT_IMPLEMENTED, 'Token was not created'))
|
|
379
|
-
];
|
|
380
|
-
}
|
|
381
|
-
return [
|
|
382
|
-
4,
|
|
383
|
-
sendSms(phoneNumber, token.value)
|
|
384
|
-
];
|
|
385
|
-
case 2:
|
|
386
|
-
return [
|
|
387
|
-
2,
|
|
388
|
-
_state.sent()
|
|
389
|
-
];
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
})();
|
|
94
|
+
export const verifyEmailUsage = async (newEmail) => {
|
|
95
|
+
// TODO: add email regex checking
|
|
96
|
+
if (newEmail.length < 3) {
|
|
97
|
+
return new Err(makeProblem(PROBLEM_INVALID_DATA, 'Email address needs minimum 3 characters'));
|
|
98
|
+
}
|
|
99
|
+
const existingEmail = await prisma.user.findFirst({
|
|
100
|
+
where: {
|
|
101
|
+
email: newEmail,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
if (existingEmail) {
|
|
105
|
+
return new Err(makeProblem(PROBLEM_INVALID_DATA, 'The email address is already in use'));
|
|
106
|
+
}
|
|
107
|
+
return new Ok({});
|
|
393
108
|
};
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
return
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
_state.trys.push([
|
|
401
|
-
0,
|
|
402
|
-
4,
|
|
403
|
-
,
|
|
404
|
-
6
|
|
405
|
-
]);
|
|
406
|
-
body = {
|
|
407
|
-
user: config.esms.apiKey,
|
|
408
|
-
pass: config.esms.apiSecret,
|
|
409
|
-
to: cleanupPhoneNumber(phoneNumber),
|
|
410
|
-
msg: "RM0.00 FLUX verification code: ".concat(code),
|
|
411
|
-
sender: 'FLUX'
|
|
412
|
-
};
|
|
413
|
-
return [
|
|
414
|
-
4,
|
|
415
|
-
enhancedFetch('https://api.esms.com.my/sms/send', {
|
|
416
|
-
method: 'POST',
|
|
417
|
-
body: JSON.stringify(body)
|
|
418
|
-
})
|
|
419
|
-
];
|
|
420
|
-
case 1:
|
|
421
|
-
result = _state.sent();
|
|
422
|
-
if (!result.err) return [
|
|
423
|
-
3,
|
|
424
|
-
3
|
|
425
|
-
];
|
|
426
|
-
_ = Err.bind;
|
|
427
|
-
return [
|
|
428
|
-
4,
|
|
429
|
-
reportProblem(makeProblem({
|
|
430
|
-
type: 'public',
|
|
431
|
-
code: PROBLEM_EXTERNAL,
|
|
432
|
-
message: 'Unable to send sms verification',
|
|
433
|
-
privateMetadata: {
|
|
434
|
-
error: result.val
|
|
435
|
-
}
|
|
436
|
-
}))
|
|
437
|
-
];
|
|
438
|
-
case 2:
|
|
439
|
-
return [
|
|
440
|
-
2,
|
|
441
|
-
new (_.apply(Err, [
|
|
442
|
-
void 0,
|
|
443
|
-
_state.sent()
|
|
444
|
-
]))
|
|
445
|
-
];
|
|
446
|
-
case 3:
|
|
447
|
-
return [
|
|
448
|
-
2,
|
|
449
|
-
new Ok({
|
|
450
|
-
success: true,
|
|
451
|
-
code: code
|
|
452
|
-
})
|
|
453
|
-
];
|
|
454
|
-
case 4:
|
|
455
|
-
e = _state.sent();
|
|
456
|
-
_1 = Err.bind;
|
|
457
|
-
return [
|
|
458
|
-
4,
|
|
459
|
-
reportProblem(makeProblem({
|
|
460
|
-
type: 'public',
|
|
461
|
-
code: PROBLEM_INTERNAL,
|
|
462
|
-
message: 'Unable to send sms verification',
|
|
463
|
-
privateMetadata: {
|
|
464
|
-
error: e
|
|
465
|
-
}
|
|
466
|
-
}))
|
|
467
|
-
];
|
|
468
|
-
case 5:
|
|
469
|
-
return [
|
|
470
|
-
2,
|
|
471
|
-
new (_1.apply(Err, [
|
|
472
|
-
void 0,
|
|
473
|
-
_state.sent()
|
|
474
|
-
]))
|
|
475
|
-
];
|
|
476
|
-
case 6:
|
|
477
|
-
return [
|
|
478
|
-
2
|
|
479
|
-
];
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
})();
|
|
109
|
+
export const sendVerificationSMS = async (userId, phoneNumber) => {
|
|
110
|
+
const token = await createSMSToken(userId, phoneNumber);
|
|
111
|
+
if (!token) {
|
|
112
|
+
return new Err(makeProblem(PROBLEM_NOT_IMPLEMENTED, 'Token was not created'));
|
|
113
|
+
}
|
|
114
|
+
return await sendSms(phoneNumber, token.value);
|
|
483
115
|
};
|
|
484
|
-
|
|
485
|
-
return
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
531
|
-
})
|
|
116
|
+
const formatPhoneNumber = (phoneNumber) => {
|
|
117
|
+
return phoneNumber.replace(/[^0-9]/g, '');
|
|
118
|
+
};
|
|
119
|
+
const sendSms = async (phoneNumber, code) => {
|
|
120
|
+
const isInternational = !formatPhoneNumber(phoneNumber).startsWith('60');
|
|
121
|
+
try {
|
|
122
|
+
const sendSmsResult = isInternational
|
|
123
|
+
? await sendVonageSms(phoneNumber, code)
|
|
124
|
+
: await sendEsmsSms(phoneNumber, code);
|
|
125
|
+
if (sendSmsResult.err) {
|
|
126
|
+
return new Err(await reportProblem(makeProblem({
|
|
127
|
+
type: 'public',
|
|
128
|
+
code: PROBLEM_EXTERNAL,
|
|
129
|
+
message: 'Unable to send sms verification',
|
|
130
|
+
privateMetadata: {
|
|
131
|
+
error: sendSmsResult.val,
|
|
132
|
+
},
|
|
133
|
+
})));
|
|
134
|
+
}
|
|
135
|
+
return new Ok({ success: true, code });
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
return new Err(await reportProblem(makeProblem({
|
|
139
|
+
type: 'public',
|
|
140
|
+
code: PROBLEM_INTERNAL,
|
|
141
|
+
message: 'Unable to send sms verification',
|
|
142
|
+
privateMetadata: {
|
|
143
|
+
error: e,
|
|
144
|
+
},
|
|
145
|
+
})));
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
export const handleCheckDuplication = async (b) => {
|
|
149
|
+
const { phoneNumber, email } = CheckDuplicationBody.parse(b);
|
|
150
|
+
// Check if the user exists
|
|
151
|
+
const checkForEmail = await prisma.user.findFirst({
|
|
152
|
+
where: {
|
|
153
|
+
email: email.toLowerCase().trim(),
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
if (checkForEmail) {
|
|
157
|
+
return new Err(makeProblem(PROBLEM_CONFLICT, 'This email is already registered. Are you trying to sign in?'));
|
|
158
|
+
}
|
|
159
|
+
const checkForMobile = await prisma.user.findFirst({
|
|
160
|
+
where: {
|
|
161
|
+
phoneNumber: phoneNumber.replace(/[\s-]/g, ''),
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
if (checkForMobile) {
|
|
165
|
+
return new Err(makeProblem(PROBLEM_CONFLICT, 'The phone number is already in use. Please try a different one.'));
|
|
166
|
+
}
|
|
167
|
+
return new Ok({ notDuplicate: true });
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* This function runs only for foreign numbers and sends an SMS containing the OTP code to the foreign number
|
|
171
|
+
* @param phoneNumber user's phone number
|
|
172
|
+
* @param code OTP code
|
|
173
|
+
* @returns result of sending SMS to user
|
|
174
|
+
*/
|
|
175
|
+
const sendVonageSms = async (phoneNumber, code) => {
|
|
176
|
+
const body = {
|
|
177
|
+
from: 'FLUX',
|
|
178
|
+
to: formatPhoneNumber(phoneNumber),
|
|
179
|
+
text: `FLUX verification code: ${code}`,
|
|
180
|
+
api_key: config.vonage.apiKey,
|
|
181
|
+
api_secret: config.vonage.apiSecret,
|
|
182
|
+
};
|
|
183
|
+
const result = await enhancedFetch('https://rest.nexmo.com/sms/json', {
|
|
184
|
+
method: 'POST',
|
|
185
|
+
body: JSON.stringify(body),
|
|
186
|
+
});
|
|
187
|
+
return result;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* This function runs only for local numbers (MY) and sends an SMS containing the OTP code to the foreign number
|
|
191
|
+
* @param phoneNumber user's phone number
|
|
192
|
+
* @param code OTP code
|
|
193
|
+
* @returns result of sending SMS to user
|
|
194
|
+
*/
|
|
195
|
+
const sendEsmsSms = async (phoneNumber, code) => {
|
|
196
|
+
const body = {
|
|
197
|
+
user: config.esms.apiKey,
|
|
198
|
+
pass: config.esms.apiSecret,
|
|
199
|
+
to: formatPhoneNumber(phoneNumber),
|
|
200
|
+
msg: `RM0.00 FLUX verification code: ${code}`,
|
|
201
|
+
sender: 'FLUX',
|
|
202
|
+
};
|
|
203
|
+
const result = await enhancedFetch('https://api.esms.com.my/sms/send', {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
body: JSON.stringify(body),
|
|
206
|
+
});
|
|
207
|
+
return result;
|
|
532
208
|
};
|
|
209
|
+
//# sourceMappingURL=verifications.js.map
|