@driveflux/api-functions 0.0.7-next.2 → 0.0.7-next.21
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.d.ts +1 -1
- package/dist/auth/register.js +102 -398
- package/dist/auth/register.js.map +1 -1
- package/dist/auth/tokens.js +115 -430
- package/dist/auth/verifications.js +177 -583
- package/dist/constants.js +5 -4
- package/dist/mailjet/calls/manage-contacts-in-list.d.ts +2 -2
- package/dist/mailjet/calls/manage-contacts-in-list.d.ts.map +1 -1
- package/dist/mailjet/calls/manage-contacts-in-list.js +24 -168
- package/dist/mailjet/calls/manage-contacts-in-list.js.map +1 -1
- package/dist/mailjet/calls/manage-subscription-status.d.ts +2 -2
- package/dist/mailjet/calls/manage-subscription-status.d.ts.map +1 -1
- package/dist/mailjet/calls/manage-subscription-status.js +14 -154
- package/dist/mailjet/calls/manage-subscription-status.js.map +1 -1
- 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.d.ts +2 -2
- package/dist/mailjet/set-contact.d.ts.map +1 -1
- package/dist/mailjet/set-contact.js +26 -217
- package/dist/mailjet/set-contact.js.map +1 -1
- package/dist/mailjet/types.d.ts +2 -2
- package/dist/mailjet/types.d.ts.map +1 -1
- package/dist/mailjet/types.js +2 -1
- package/dist/mailjet/utils/convert-to-array.d.ts +1 -1
- package/dist/mailjet/utils/convert-to-array.d.ts.map +1 -1
- package/dist/mailjet/utils/convert-to-array.js +9 -58
- package/dist/mailjet/utils/extract-email-preferences.d.ts +1 -1
- package/dist/mailjet/utils/extract-email-preferences.d.ts.map +1 -1
- 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.d.ts +2 -2
- package/dist/mailjet/utils/update-email-references.d.ts.map +1 -1
- package/dist/mailjet/utils/update-email-references.js +27 -208
- package/dist/mailjet/utils/update-email-references.js.map +1 -1
- 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.d.ts +1 -1
- package/dist/reservation/agree.d.ts.map +1 -1
- package/dist/reservation/agree.js +19 -158
- package/dist/reservation/checks.d.ts +1 -1
- package/dist/reservation/checks.d.ts.map +1 -1
- package/dist/reservation/checks.js +23 -178
- package/dist/reservation/display-vehicle.d.ts +17 -17
- package/dist/reservation/display-vehicle.js +142 -514
- package/dist/reservation/display-vehicle.js.map +1 -1
- package/dist/reservation/fetch-or-create.d.ts +2 -2
- package/dist/reservation/fetch-or-create.d.ts.map +1 -1
- package/dist/reservation/fetch-or-create.js +199 -482
- package/dist/reservation/fetch-or-create.js.map +1 -1
- package/dist/reservation/invoice.d.ts +3 -3
- package/dist/reservation/invoice.d.ts.map +1 -1
- package/dist/reservation/invoice.js +198 -496
- package/dist/reservation/invoice.js.map +1 -1
- package/dist/reservation/payer.d.ts +1 -1
- package/dist/reservation/payer.d.ts.map +1 -1
- package/dist/reservation/payer.js +28 -177
- package/dist/reservation/reserve.d.ts +1 -1
- package/dist/reservation/reserve.d.ts.map +1 -1
- package/dist/reservation/reserve.js +36 -196
- package/dist/reservation/reserve.js.map +1 -1
- package/dist/reservation/types.d.ts +1 -0
- package/dist/reservation/types.d.ts.map +1 -1
- package/dist/reservation/types.js +2 -1
- package/dist/reservation/vehicle.d.ts +1 -1
- package/dist/reservation/vehicle.d.ts.map +1 -1
- package/dist/reservation/vehicle.js +26 -188
- package/dist/reservation/vehicle.js.map +1 -1
- package/dist/slack.d.ts +2 -2
- package/dist/slack.d.ts.map +1 -1
- package/dist/slack.js +67 -273
- package/dist/validation.d.ts +4 -4
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +89 -139
- package/dist/validation.js.map +1 -1
- package/dist/vehicle/vehicle-pricing/constants.d.ts +1 -1
- package/dist/vehicle/vehicle-pricing/constants.d.ts.map +1 -1
- package/dist/vehicle/vehicle-pricing/constants.js +33 -36
- package/dist/vehicle/vehicle-pricing/index.d.ts +1 -1
- package/dist/vehicle/vehicle-pricing/index.d.ts.map +1 -1
- package/dist/vehicle/vehicle-pricing/index.js +101 -259
- package/dist/vehicle/vehicle-pricing/index.js.map +1 -1
- package/dist/vehicle/vehicle-pricing/types.js +2 -1
- package/package.json +21 -21
|
@@ -1,615 +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
10
|
import { clearToken, createEmailToken, createSMSToken } from './tokens.js';
|
|
131
|
-
|
|
11
|
+
const CheckDuplicationBody = z.object({
|
|
132
12
|
phoneNumber: z.string(),
|
|
133
|
-
email: z.string().transform(
|
|
134
|
-
return email.toLowerCase().trim();
|
|
135
|
-
})
|
|
13
|
+
email: z.string().transform((email) => email.toLowerCase().trim()),
|
|
136
14
|
});
|
|
137
|
-
export
|
|
138
|
-
|
|
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
|
-
if (!temporaryEmail) return [
|
|
173
|
-
3,
|
|
174
|
-
5
|
|
175
|
-
];
|
|
176
|
-
return [
|
|
177
|
-
4,
|
|
178
|
-
verifyEmailUsage(temporaryEmail)
|
|
179
|
-
];
|
|
180
|
-
case 4:
|
|
181
|
-
results = _state.sent();
|
|
182
|
-
if (results === null || results === void 0 ? void 0 : results.err) {
|
|
183
|
-
return [
|
|
184
|
-
2,
|
|
185
|
-
results
|
|
186
|
-
];
|
|
187
|
-
}
|
|
188
|
-
_state.label = 5;
|
|
189
|
-
case 5:
|
|
190
|
-
destinationEmail = temporaryEmail || user.email;
|
|
191
|
-
return [
|
|
192
|
-
4,
|
|
193
|
-
createEmailToken(user.id, destinationEmail)
|
|
194
|
-
];
|
|
195
|
-
case 6:
|
|
196
|
-
token = _state.sent();
|
|
197
|
-
if (!token) {
|
|
198
|
-
return [
|
|
199
|
-
2,
|
|
200
|
-
new Err(makeProblem(PROBLEM_NOT_IMPLEMENTED, 'Token was not created'))
|
|
201
|
-
];
|
|
202
|
-
}
|
|
203
|
-
return [
|
|
204
|
-
4,
|
|
205
|
-
prisma.user.update({
|
|
206
|
-
where: {
|
|
207
|
-
id: user.id
|
|
208
|
-
},
|
|
209
|
-
data: {
|
|
210
|
-
temporaryEmail: destinationEmail
|
|
211
|
-
},
|
|
212
|
-
include: {
|
|
213
|
-
tokens: true
|
|
214
|
-
}
|
|
215
|
-
})
|
|
216
|
-
];
|
|
217
|
-
case 7:
|
|
218
|
-
updatedUser = _state.sent();
|
|
219
|
-
tokens = updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.tokens;
|
|
220
|
-
if (!tokens) return [
|
|
221
|
-
3,
|
|
222
|
-
9
|
|
223
|
-
];
|
|
224
|
-
return [
|
|
225
|
-
4,
|
|
226
|
-
Promise.allSettled(tokens.map(function(userToken) {
|
|
227
|
-
return _async_to_generator(function() {
|
|
228
|
-
return _ts_generator(this, function(_state) {
|
|
229
|
-
switch(_state.label){
|
|
230
|
-
case 0:
|
|
231
|
-
if (!(userToken.scope === 'verify-email' && userToken.id !== token.id)) return [
|
|
232
|
-
3,
|
|
233
|
-
2
|
|
234
|
-
];
|
|
235
|
-
return [
|
|
236
|
-
4,
|
|
237
|
-
clearToken(userToken.id)
|
|
238
|
-
];
|
|
239
|
-
case 1:
|
|
240
|
-
return [
|
|
241
|
-
2,
|
|
242
|
-
_state.sent()
|
|
243
|
-
];
|
|
244
|
-
case 2:
|
|
245
|
-
return [
|
|
246
|
-
2
|
|
247
|
-
];
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
})();
|
|
251
|
-
}))
|
|
252
|
-
];
|
|
253
|
-
case 8:
|
|
254
|
-
_state.sent();
|
|
255
|
-
_state.label = 9;
|
|
256
|
-
case 9:
|
|
257
|
-
// Query object for the link
|
|
258
|
-
query = new URLSearchParams({
|
|
259
|
-
email: destinationEmail,
|
|
260
|
-
token: token.id,
|
|
261
|
-
code: token.value
|
|
262
|
-
});
|
|
263
|
-
if (next) {
|
|
264
|
-
query.append('next', next);
|
|
265
|
-
}
|
|
266
|
-
link = "".concat(config.appUrl, "/verify?").concat(query);
|
|
267
|
-
title = temporaryEmail ? 'Confirm email change' : 'Verify your email address';
|
|
268
|
-
_state.label = 10;
|
|
269
|
-
case 10:
|
|
270
|
-
_state.trys.push([
|
|
271
|
-
10,
|
|
272
|
-
12,
|
|
273
|
-
,
|
|
274
|
-
13
|
|
275
|
-
]);
|
|
276
|
-
// Generate html
|
|
277
|
-
html = verificationEmail({
|
|
278
|
-
user: user.firstName || '',
|
|
279
|
-
link: link,
|
|
280
|
-
title: "".concat(title),
|
|
281
|
-
newEmail: temporaryEmail,
|
|
282
|
-
isChanging: !!temporaryEmail
|
|
283
|
-
});
|
|
284
|
-
return [
|
|
285
|
-
4,
|
|
286
|
-
send({
|
|
287
|
-
to: {
|
|
288
|
-
name: user.firstName || '',
|
|
289
|
-
address: destinationEmail
|
|
290
|
-
},
|
|
291
|
-
subject: "".concat(title),
|
|
292
|
-
html: html
|
|
293
|
-
})
|
|
294
|
-
];
|
|
295
|
-
case 11:
|
|
296
|
-
result = _state.sent();
|
|
297
|
-
if (result.err) {
|
|
298
|
-
console.error('Error sending email:', result.val);
|
|
299
|
-
return [
|
|
300
|
-
2,
|
|
301
|
-
new Err(makeProblem('email_not_sent', 'Unable to send the verification email'))
|
|
302
|
-
];
|
|
303
|
-
}
|
|
304
|
-
return [
|
|
305
|
-
2,
|
|
306
|
-
new Ok({
|
|
307
|
-
success: true,
|
|
308
|
-
link: link
|
|
309
|
-
})
|
|
310
|
-
];
|
|
311
|
-
case 12:
|
|
312
|
-
e = _state.sent();
|
|
313
|
-
return [
|
|
314
|
-
2,
|
|
315
|
-
new Err(makeProblem('email_not_sent', e.message))
|
|
316
|
-
];
|
|
317
|
-
case 13:
|
|
318
|
-
return [
|
|
319
|
-
2
|
|
320
|
-
];
|
|
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);
|
|
321
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,
|
|
322
74
|
});
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
// TODO: add email regex checking
|
|
332
|
-
if (newEmail.length < 3) {
|
|
333
|
-
return [
|
|
334
|
-
2,
|
|
335
|
-
new Err(makeProblem(PROBLEM_INVALID_DATA, 'Email address needs minimum 3 characters'))
|
|
336
|
-
];
|
|
337
|
-
}
|
|
338
|
-
return [
|
|
339
|
-
4,
|
|
340
|
-
prisma.user.findFirst({
|
|
341
|
-
where: {
|
|
342
|
-
email: newEmail
|
|
343
|
-
}
|
|
344
|
-
})
|
|
345
|
-
];
|
|
346
|
-
case 1:
|
|
347
|
-
existingEmail = _state.sent();
|
|
348
|
-
if (existingEmail) {
|
|
349
|
-
return [
|
|
350
|
-
2,
|
|
351
|
-
new Err(makeProblem(PROBLEM_INVALID_DATA, 'The email address is already in use'))
|
|
352
|
-
];
|
|
353
|
-
}
|
|
354
|
-
return [
|
|
355
|
-
2,
|
|
356
|
-
new Ok({})
|
|
357
|
-
];
|
|
358
|
-
}
|
|
75
|
+
// send email
|
|
76
|
+
const result = await send({
|
|
77
|
+
to: {
|
|
78
|
+
name: user.firstName || '',
|
|
79
|
+
address: destinationEmail,
|
|
80
|
+
},
|
|
81
|
+
subject: `${title}`,
|
|
82
|
+
html,
|
|
359
83
|
});
|
|
360
|
-
|
|
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
|
+
}
|
|
361
93
|
};
|
|
362
|
-
export
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
2,
|
|
377
|
-
new Err(makeProblem(PROBLEM_NOT_IMPLEMENTED, 'Token was not created'))
|
|
378
|
-
];
|
|
379
|
-
}
|
|
380
|
-
return [
|
|
381
|
-
4,
|
|
382
|
-
sendSms(phoneNumber, token.value)
|
|
383
|
-
];
|
|
384
|
-
case 2:
|
|
385
|
-
return [
|
|
386
|
-
2,
|
|
387
|
-
_state.sent()
|
|
388
|
-
];
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
})();
|
|
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({});
|
|
392
108
|
};
|
|
393
|
-
|
|
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);
|
|
115
|
+
};
|
|
116
|
+
const formatPhoneNumber = (phoneNumber) => {
|
|
394
117
|
return phoneNumber.replace(/[^0-9]/g, '');
|
|
395
118
|
};
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
];
|
|
425
|
-
case 3:
|
|
426
|
-
return [
|
|
427
|
-
4,
|
|
428
|
-
sendEsmsSms(phoneNumber, code)
|
|
429
|
-
];
|
|
430
|
-
case 4:
|
|
431
|
-
_tmp = _state.sent();
|
|
432
|
-
_state.label = 5;
|
|
433
|
-
case 5:
|
|
434
|
-
sendSmsResult = _tmp;
|
|
435
|
-
if (!sendSmsResult.err) return [
|
|
436
|
-
3,
|
|
437
|
-
7
|
|
438
|
-
];
|
|
439
|
-
_ = Err.bind;
|
|
440
|
-
return [
|
|
441
|
-
4,
|
|
442
|
-
reportProblem(makeProblem({
|
|
443
|
-
type: 'public',
|
|
444
|
-
code: PROBLEM_EXTERNAL,
|
|
445
|
-
message: 'Unable to send sms verification',
|
|
446
|
-
privateMetadata: {
|
|
447
|
-
error: sendSmsResult.val
|
|
448
|
-
}
|
|
449
|
-
}))
|
|
450
|
-
];
|
|
451
|
-
case 6:
|
|
452
|
-
return [
|
|
453
|
-
2,
|
|
454
|
-
new (_.apply(Err, [
|
|
455
|
-
void 0,
|
|
456
|
-
_state.sent()
|
|
457
|
-
]))
|
|
458
|
-
];
|
|
459
|
-
case 7:
|
|
460
|
-
return [
|
|
461
|
-
2,
|
|
462
|
-
new Ok({
|
|
463
|
-
success: true,
|
|
464
|
-
code: code
|
|
465
|
-
})
|
|
466
|
-
];
|
|
467
|
-
case 8:
|
|
468
|
-
e = _state.sent();
|
|
469
|
-
_1 = Err.bind;
|
|
470
|
-
return [
|
|
471
|
-
4,
|
|
472
|
-
reportProblem(makeProblem({
|
|
473
|
-
type: 'public',
|
|
474
|
-
code: PROBLEM_INTERNAL,
|
|
475
|
-
message: 'Unable to send sms verification',
|
|
476
|
-
privateMetadata: {
|
|
477
|
-
error: e
|
|
478
|
-
}
|
|
479
|
-
}))
|
|
480
|
-
];
|
|
481
|
-
case 9:
|
|
482
|
-
return [
|
|
483
|
-
2,
|
|
484
|
-
new (_1.apply(Err, [
|
|
485
|
-
void 0,
|
|
486
|
-
_state.sent()
|
|
487
|
-
]))
|
|
488
|
-
];
|
|
489
|
-
case 10:
|
|
490
|
-
return [
|
|
491
|
-
2
|
|
492
|
-
];
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
})();
|
|
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
|
+
}
|
|
496
147
|
};
|
|
497
|
-
export
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
new Err(makeProblem(PROBLEM_CONFLICT, 'This email is already registered. Are you trying to sign in?'))
|
|
518
|
-
];
|
|
519
|
-
}
|
|
520
|
-
return [
|
|
521
|
-
4,
|
|
522
|
-
prisma.user.findFirst({
|
|
523
|
-
where: {
|
|
524
|
-
phoneNumber: phoneNumber.replace(/[\s-]/g, '')
|
|
525
|
-
}
|
|
526
|
-
})
|
|
527
|
-
];
|
|
528
|
-
case 2:
|
|
529
|
-
checkForMobile = _state.sent();
|
|
530
|
-
if (checkForMobile) {
|
|
531
|
-
return [
|
|
532
|
-
2,
|
|
533
|
-
new Err(makeProblem(PROBLEM_CONFLICT, 'The phone number is already in use. Please try a different one.'))
|
|
534
|
-
];
|
|
535
|
-
}
|
|
536
|
-
return [
|
|
537
|
-
2,
|
|
538
|
-
new Ok({
|
|
539
|
-
notDuplicate: true
|
|
540
|
-
})
|
|
541
|
-
];
|
|
542
|
-
}
|
|
543
|
-
});
|
|
544
|
-
})();
|
|
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 });
|
|
545
168
|
};
|
|
546
169
|
/**
|
|
547
170
|
* This function runs only for foreign numbers and sends an SMS containing the OTP code to the foreign number
|
|
548
171
|
* @param phoneNumber user's phone number
|
|
549
172
|
* @param code OTP code
|
|
550
173
|
* @returns result of sending SMS to user
|
|
551
|
-
*/
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
4,
|
|
566
|
-
enhancedFetch('https://rest.nexmo.com/sms/json', {
|
|
567
|
-
method: 'POST',
|
|
568
|
-
body: JSON.stringify(body)
|
|
569
|
-
})
|
|
570
|
-
];
|
|
571
|
-
case 1:
|
|
572
|
-
result = _state.sent();
|
|
573
|
-
return [
|
|
574
|
-
2,
|
|
575
|
-
result
|
|
576
|
-
];
|
|
577
|
-
}
|
|
578
|
-
});
|
|
579
|
-
})();
|
|
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;
|
|
580
188
|
};
|
|
581
189
|
/**
|
|
582
190
|
* This function runs only for local numbers (MY) and sends an SMS containing the OTP code to the foreign number
|
|
583
191
|
* @param phoneNumber user's phone number
|
|
584
192
|
* @param code OTP code
|
|
585
193
|
* @returns result of sending SMS to user
|
|
586
|
-
*/
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
4,
|
|
601
|
-
enhancedFetch('https://api.esms.com.my/sms/send', {
|
|
602
|
-
method: 'POST',
|
|
603
|
-
body: JSON.stringify(body)
|
|
604
|
-
})
|
|
605
|
-
];
|
|
606
|
-
case 1:
|
|
607
|
-
result = _state.sent();
|
|
608
|
-
return [
|
|
609
|
-
2,
|
|
610
|
-
result
|
|
611
|
-
];
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
})();
|
|
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;
|
|
615
208
|
};
|
|
209
|
+
//# sourceMappingURL=verifications.js.map
|