@driveflux/api-functions 0.0.7-next.0 → 0.0.7-next.10

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.
Files changed (93) hide show
  1. package/dist/auth/confirm.d.ts +3 -17
  2. package/dist/auth/confirm.js +66 -325
  3. package/dist/auth/emails.js +42 -210
  4. package/dist/auth/formatter.js +9 -9
  5. package/dist/auth/otp.js +120 -374
  6. package/dist/auth/register.js +95 -383
  7. package/dist/auth/register.js.map +1 -1
  8. package/dist/auth/tokens.js +114 -426
  9. package/dist/auth/verifications.d.ts +2 -8
  10. package/dist/auth/verifications.js +176 -581
  11. package/dist/constants.js +4 -4
  12. package/dist/mailjet/calls/manage-contacts-in-list.d.ts +2 -2
  13. package/dist/mailjet/calls/manage-contacts-in-list.d.ts.map +1 -1
  14. package/dist/mailjet/calls/manage-contacts-in-list.js +22 -167
  15. package/dist/mailjet/calls/manage-contacts-in-list.js.map +1 -1
  16. package/dist/mailjet/calls/manage-subscription-status.d.ts +2 -2
  17. package/dist/mailjet/calls/manage-subscription-status.d.ts.map +1 -1
  18. package/dist/mailjet/calls/manage-subscription-status.js +13 -154
  19. package/dist/mailjet/calls/manage-subscription-status.js.map +1 -1
  20. package/dist/mailjet/calls/request-service.js +18 -182
  21. package/dist/mailjet/refresh-email-preferences.js +21 -221
  22. package/dist/mailjet/set-contact.d.ts +2 -2
  23. package/dist/mailjet/set-contact.d.ts.map +1 -1
  24. package/dist/mailjet/set-contact.js +21 -213
  25. package/dist/mailjet/set-contact.js.map +1 -1
  26. package/dist/mailjet/types.d.ts +2 -2
  27. package/dist/mailjet/types.d.ts.map +1 -1
  28. package/dist/mailjet/utils/convert-to-array.d.ts +1 -1
  29. package/dist/mailjet/utils/convert-to-array.d.ts.map +1 -1
  30. package/dist/mailjet/utils/convert-to-array.js +9 -56
  31. package/dist/mailjet/utils/extract-email-preferences.d.ts +1 -1
  32. package/dist/mailjet/utils/extract-email-preferences.d.ts.map +1 -1
  33. package/dist/mailjet/utils/extract-email-preferences.js +37 -214
  34. package/dist/mailjet/utils/lists.js +28 -248
  35. package/dist/mailjet/utils/update-email-references.d.ts +2 -2
  36. package/dist/mailjet/utils/update-email-references.d.ts.map +1 -1
  37. package/dist/mailjet/utils/update-email-references.js +27 -207
  38. package/dist/mailjet/utils/update-email-references.js.map +1 -1
  39. package/dist/notion/client.js +50 -196
  40. package/dist/notion/helpful.js +25 -169
  41. package/dist/notion/schemas/block.js +1 -1
  42. package/dist/notion/schemas/common.d.ts +13 -10
  43. package/dist/notion/schemas/common.js +7 -9
  44. package/dist/notion/schemas/database.js +68 -100
  45. package/dist/notion/schemas/emoji.d.ts +1 -7
  46. package/dist/notion/schemas/emoji.js +1 -1
  47. package/dist/notion/schemas/file.d.ts +7 -34
  48. package/dist/notion/schemas/file.js +1 -1
  49. package/dist/notion/schemas/kb.js +3 -3
  50. package/dist/notion/schemas/page.js +69 -103
  51. package/dist/notion/schemas/parent.d.ts +4 -28
  52. package/dist/notion/schemas/parent.js +4 -4
  53. package/dist/notion/schemas/user.d.ts +13 -67
  54. package/dist/notion/schemas/user.js +2 -2
  55. package/dist/reservation/agree.d.ts +1 -1
  56. package/dist/reservation/agree.d.ts.map +1 -1
  57. package/dist/reservation/agree.js +17 -157
  58. package/dist/reservation/checks.d.ts +1 -1
  59. package/dist/reservation/checks.d.ts.map +1 -1
  60. package/dist/reservation/checks.js +21 -177
  61. package/dist/reservation/display-vehicle.js +122 -500
  62. package/dist/reservation/display-vehicle.js.map +1 -1
  63. package/dist/reservation/fetch-or-create.d.ts +2 -2
  64. package/dist/reservation/fetch-or-create.d.ts.map +1 -1
  65. package/dist/reservation/fetch-or-create.js +193 -478
  66. package/dist/reservation/fetch-or-create.js.map +1 -1
  67. package/dist/reservation/invoice.d.ts +3 -3
  68. package/dist/reservation/invoice.d.ts.map +1 -1
  69. package/dist/reservation/invoice.js +182 -492
  70. package/dist/reservation/invoice.js.map +1 -1
  71. package/dist/reservation/payer.d.ts +1 -1
  72. package/dist/reservation/payer.d.ts.map +1 -1
  73. package/dist/reservation/payer.js +25 -175
  74. package/dist/reservation/reserve.d.ts +1 -1
  75. package/dist/reservation/reserve.d.ts.map +1 -1
  76. package/dist/reservation/reserve.js +35 -196
  77. package/dist/reservation/reserve.js.map +1 -1
  78. package/dist/reservation/types.d.ts +1 -0
  79. package/dist/reservation/types.d.ts.map +1 -1
  80. package/dist/reservation/vehicle.d.ts +1 -1
  81. package/dist/reservation/vehicle.d.ts.map +1 -1
  82. package/dist/reservation/vehicle.js +23 -188
  83. package/dist/reservation/vehicle.js.map +1 -1
  84. package/dist/slack.js +57 -268
  85. package/dist/validation.js +38 -98
  86. package/dist/vehicle/vehicle-pricing/constants.d.ts +1 -1
  87. package/dist/vehicle/vehicle-pricing/constants.d.ts.map +1 -1
  88. package/dist/vehicle/vehicle-pricing/constants.js +16 -16
  89. package/dist/vehicle/vehicle-pricing/index.d.ts +1 -1
  90. package/dist/vehicle/vehicle-pricing/index.d.ts.map +1 -1
  91. package/dist/vehicle/vehicle-pricing/index.js +87 -259
  92. package/dist/vehicle/vehicle-pricing/index.js.map +1 -1
  93. package/package.json +12 -12
@@ -1,4 +1,4 @@
1
- var knownCountryCodes = [
1
+ const knownCountryCodes = [
2
2
  '1',
3
3
  '376',
4
4
  '93',
@@ -223,17 +223,17 @@ var knownCountryCodes = [
223
223
  '692',
224
224
  '383'
225
225
  ];
226
- export var cleanupPhoneNumber = function(phoneNumber) {
226
+ export const cleanupPhoneNumber = (phoneNumber)=>{
227
227
  // Remove all characters except numbers and +
228
- var cleaned = phoneNumber.replace(/[^\d+]/g, '');
228
+ const cleaned = phoneNumber.replace(/[^\d+]/g, '');
229
229
  // If it doesn't start with +, add it
230
- var withPlus = cleaned.startsWith('+') ? cleaned : "+".concat(cleaned);
230
+ const withPlus = cleaned.startsWith('+') ? cleaned : `+${cleaned}`;
231
231
  // Find the country code by checking against known codes
232
- var countryCode = '';
233
- var remainingNumber = '';
232
+ let countryCode = '';
233
+ let remainingNumber = '';
234
234
  // Try to find the country code by checking the longest matches first
235
- for(var i = 4; i >= 1; i--){
236
- var potentialCode = withPlus.substring(1, 1 + i) // Skip the + and take i digits
235
+ for(let i = 4; i >= 1; i--){
236
+ const potentialCode = withPlus.substring(1, 1 + i) // Skip the + and take i digits
237
237
  ;
238
238
  if (knownCountryCodes.includes(potentialCode)) {
239
239
  countryCode = potentialCode;
@@ -243,7 +243,7 @@ export var cleanupPhoneNumber = function(phoneNumber) {
243
243
  }
244
244
  // If we found a country code and the remaining number starts with 0, remove it
245
245
  if (countryCode && remainingNumber.startsWith('0')) {
246
- return "+".concat(countryCode).concat(remainingNumber.substring(1));
246
+ return `+${countryCode}${remainingNumber.substring(1)}`;
247
247
  }
248
248
  return withPlus;
249
249
  };
package/dist/auth/otp.js CHANGED
@@ -1,148 +1,3 @@
1
- function _array_like_to_array(arr, len) {
2
- if (len == null || len > arr.length) len = arr.length;
3
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
- return arr2;
5
- }
6
- function _array_without_holes(arr) {
7
- if (Array.isArray(arr)) return _array_like_to_array(arr);
8
- }
9
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
- try {
11
- var info = gen[key](arg);
12
- var value = info.value;
13
- } catch (error) {
14
- reject(error);
15
- return;
16
- }
17
- if (info.done) {
18
- resolve(value);
19
- } else {
20
- Promise.resolve(value).then(_next, _throw);
21
- }
22
- }
23
- function _async_to_generator(fn) {
24
- return function() {
25
- var self = this, args = arguments;
26
- return new Promise(function(resolve, reject) {
27
- var gen = fn.apply(self, args);
28
- function _next(value) {
29
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
- }
31
- function _throw(err) {
32
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
33
- }
34
- _next(undefined);
35
- });
36
- };
37
- }
38
- function _iterable_to_array(iter) {
39
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
40
- }
41
- function _non_iterable_spread() {
42
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
43
- }
44
- function _to_consumable_array(arr) {
45
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
46
- }
47
- function _unsupported_iterable_to_array(o, minLen) {
48
- if (!o) return;
49
- if (typeof o === "string") return _array_like_to_array(o, minLen);
50
- var n = Object.prototype.toString.call(o).slice(8, -1);
51
- if (n === "Object" && o.constructor) n = o.constructor.name;
52
- if (n === "Map" || n === "Set") return Array.from(n);
53
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
54
- }
55
- function _ts_generator(thisArg, body) {
56
- var f, y, t, _ = {
57
- label: 0,
58
- sent: function() {
59
- if (t[0] & 1) throw t[1];
60
- return t[1];
61
- },
62
- trys: [],
63
- ops: []
64
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
65
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
66
- return this;
67
- }), g;
68
- function verb(n) {
69
- return function(v) {
70
- return step([
71
- n,
72
- v
73
- ]);
74
- };
75
- }
76
- function step(op) {
77
- if (f) throw new TypeError("Generator is already executing.");
78
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
79
- 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;
80
- if (y = 0, t) op = [
81
- op[0] & 2,
82
- t.value
83
- ];
84
- switch(op[0]){
85
- case 0:
86
- case 1:
87
- t = op;
88
- break;
89
- case 4:
90
- _.label++;
91
- return {
92
- value: op[1],
93
- done: false
94
- };
95
- case 5:
96
- _.label++;
97
- y = op[1];
98
- op = [
99
- 0
100
- ];
101
- continue;
102
- case 7:
103
- op = _.ops.pop();
104
- _.trys.pop();
105
- continue;
106
- default:
107
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
108
- _ = 0;
109
- continue;
110
- }
111
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
112
- _.label = op[1];
113
- break;
114
- }
115
- if (op[0] === 6 && _.label < t[1]) {
116
- _.label = t[1];
117
- t = op;
118
- break;
119
- }
120
- if (t && _.label < t[2]) {
121
- _.label = t[2];
122
- _.ops.push(op);
123
- break;
124
- }
125
- if (t[2]) _.ops.pop();
126
- _.trys.pop();
127
- continue;
128
- }
129
- op = body.call(thisArg, _);
130
- } catch (e) {
131
- op = [
132
- 6,
133
- e
134
- ];
135
- y = 0;
136
- } finally{
137
- f = t = 0;
138
- }
139
- if (op[0] & 5) throw op[1];
140
- return {
141
- value: op[0] ? op[1] : void 0,
142
- done: true
143
- };
144
- }
145
- }
146
1
  import { ROLES } from '@driveflux/auth/authorization/constants';
147
2
  import { config } from '@driveflux/config/backend';
148
3
  import { prisma } from '@driveflux/db';
@@ -154,23 +9,21 @@ import { z } from 'zod';
154
9
  import { sendEmailChangedEmail } from './emails.js';
155
10
  import { clearToken, createSMSToken, verifyToken } from './tokens.js';
156
11
  import { sendVerificationSMS } from './verifications.js';
157
- var SendVerificationSMSBody = z.object({
12
+ const SendVerificationSMSBody = z.object({
158
13
  phoneNumber: z.string(),
159
14
  checkDuplication: z.boolean().optional().nullable(),
160
15
  includeUser: z.boolean().optional().nullable(),
161
16
  userId: z.string().optional().nullable()
162
17
  });
163
- var VerifyOtpBody = z.object({
18
+ const VerifyOtpBody = z.object({
164
19
  scope: z.enum([
165
20
  'verify-email',
166
21
  'verify-phone'
167
22
  ]),
168
- email: z.email().optional().transform(function(email) {
169
- return email === null || email === void 0 ? void 0 : email.toLowerCase().trim();
170
- }),
23
+ email: z.email().optional().transform((email)=>email?.toLowerCase().trim()),
171
24
  phoneNumber: z.string().optional(),
172
25
  code: z.string()
173
- }).refine(function(d) {
26
+ }).refine((d)=>{
174
27
  if (d.scope === 'verify-email' && !d.email) {
175
28
  return false;
176
29
  }
@@ -180,7 +33,7 @@ var VerifyOtpBody = z.object({
180
33
  'email'
181
34
  ],
182
35
  message: 'Email is required'
183
- }).refine(function(d) {
36
+ }).refine((d)=>{
184
37
  if (d.scope === 'verify-phone' && !d.phoneNumber) {
185
38
  return false;
186
39
  }
@@ -191,234 +44,127 @@ var VerifyOtpBody = z.object({
191
44
  ],
192
45
  message: 'Phone number is required'
193
46
  });
194
- var INVALID_TOKEN_PROBLEM = makeProblem(PROBLEM_INVALID_DATA, 'Unable to verify token. It could have been expired.');
195
- var VerifyOtpOnlyBody = z.object({
47
+ const INVALID_TOKEN_PROBLEM = makeProblem(PROBLEM_INVALID_DATA, 'Unable to verify token. It could have been expired.');
48
+ const VerifyOtpOnlyBody = z.object({
196
49
  phoneNumber: z.string().optional(),
197
50
  code: z.string()
198
51
  });
199
- export var handleVerifyOTPOnly = function(param) {
200
- var phoneNumber = param.phoneNumber, code = param.code;
201
- return _async_to_generator(function() {
202
- var tokenResult;
203
- return _ts_generator(this, function(_state) {
204
- switch(_state.label){
205
- case 0:
206
- return [
207
- 4,
208
- verifyToken(code, {
209
- scope: 'verify-phone'
210
- })
211
- ];
212
- case 1:
213
- tokenResult = _state.sent();
214
- if (tokenResult.err) {
215
- console.log('Error verifying token', tokenResult.val);
216
- return [
217
- 2,
218
- new Err(INVALID_TOKEN_PROBLEM)
219
- ];
220
- }
221
- // delete the previous token
222
- // TODO maybe: verify this token when registering one more time then clear it
223
- // await clearToken(tokenResult.val.id)
224
- return [
225
- 2,
226
- new Ok({
227
- phoneNumber: phoneNumber,
228
- phoneNumberVerified: true
229
- })
230
- ];
231
- }
232
- });
233
- })();
52
+ export const handleVerifyOTPOnly = async ({ phoneNumber, code })=>{
53
+ const tokenResult = await verifyToken(code, {
54
+ scope: 'verify-phone'
55
+ });
56
+ if (tokenResult.err) {
57
+ console.log('Error verifying token', tokenResult.val);
58
+ return new Err(INVALID_TOKEN_PROBLEM);
59
+ }
60
+ // delete the previous token
61
+ // TODO maybe: verify this token when registering one more time then clear it
62
+ // await clearToken(tokenResult.val.id)
63
+ return new Ok({
64
+ phoneNumber,
65
+ phoneNumberVerified: true
66
+ });
234
67
  };
235
- export var handleVerifyOTP = function(b) {
236
- return _async_to_generator(function() {
237
- var _tokenResult_val_user, _VerifyOtpBody_parse, scope, email, preFormattedPhoneNumber, code, phoneNumber, tokenResult, previousUser, userUpdate, userGroups, user, token;
238
- return _ts_generator(this, function(_state) {
239
- switch(_state.label){
240
- case 0:
241
- _VerifyOtpBody_parse = VerifyOtpBody.parse(b), scope = _VerifyOtpBody_parse.scope, email = _VerifyOtpBody_parse.email, preFormattedPhoneNumber = _VerifyOtpBody_parse.phoneNumber, code = _VerifyOtpBody_parse.code;
242
- phoneNumber = "+".concat(preFormattedPhoneNumber === null || preFormattedPhoneNumber === void 0 ? void 0 : preFormattedPhoneNumber.replace(/[^0-9]/g, ''));
243
- return [
244
- 4,
245
- verifyToken(code, {
246
- scope: scope,
247
- metadata: {
248
- email: email,
249
- phoneNumber: phoneNumber
250
- }
251
- }, {
252
- includeUser: true
253
- })
254
- ];
255
- case 1:
256
- tokenResult = _state.sent();
257
- if (tokenResult.err) {
258
- return [
259
- 2,
260
- new Err(INVALID_TOKEN_PROBLEM)
261
- ];
262
- }
263
- if (!tokenResult.val.user) {
264
- return [
265
- 2,
266
- new Err(INVALID_TOKEN_PROBLEM)
267
- ];
268
- }
269
- previousUser = tokenResult.val.user;
270
- userUpdate = scope === 'verify-email' ? {
271
- emailVerified: true,
272
- email: email
273
- } : {
274
- phoneNumberVerified: true,
275
- phoneNumber: phoneNumber
276
- };
277
- userUpdate.temporary = false;
278
- userUpdate.temporaryEmail = null;
279
- userGroups = new Set((_tokenResult_val_user = tokenResult.val.user) === null || _tokenResult_val_user === void 0 ? void 0 : _tokenResult_val_user.groups);
280
- userGroups.add(ROLES.MEMBER);
281
- userUpdate.groups = _to_consumable_array(userGroups);
282
- return [
283
- 4,
284
- prisma.user.update({
285
- where: {
286
- id: tokenResult.val.user.id
287
- },
288
- data: userUpdate
289
- })
290
- ];
291
- case 2:
292
- user = _state.sent();
293
- // delete the previous token
294
- return [
295
- 4,
296
- clearToken(tokenResult.val.id)
297
- ];
298
- case 3:
299
- _state.sent();
300
- return [
301
- 4,
302
- prisma.token.create({
303
- data: {
304
- id: generateId('Token'),
305
- user: {
306
- connect: {
307
- id: user === null || user === void 0 ? void 0 : user.id
308
- }
309
- },
310
- expiresAt: addDays(new Date(), 365),
311
- scope: 'all'
312
- }
313
- })
314
- ];
315
- case 4:
316
- token = _state.sent();
317
- if (!(previousUser.email !== user.email)) return [
318
- 3,
319
- 6
320
- ];
321
- return [
322
- 4,
323
- sendEmailChangedEmail(user.id)
324
- ];
325
- case 5:
326
- _state.sent();
327
- _state.label = 6;
328
- case 6:
329
- return [
330
- 2,
331
- new Ok({
332
- accessToken: token.id,
333
- expiresAt: token.expiresAt,
334
- user: user
335
- })
336
- ];
337
- }
338
- });
339
- })();
68
+ export const handleVerifyOTP = async (b)=>{
69
+ const { scope, email, phoneNumber: preFormattedPhoneNumber, code } = VerifyOtpBody.parse(b);
70
+ const phoneNumber = `+${preFormattedPhoneNumber?.replace(/[^0-9]/g, '')}`;
71
+ const tokenResult = await verifyToken(code, {
72
+ scope,
73
+ metadata: {
74
+ email,
75
+ phoneNumber
76
+ }
77
+ }, {
78
+ includeUser: true
79
+ });
80
+ if (tokenResult.err) {
81
+ return new Err(INVALID_TOKEN_PROBLEM);
82
+ }
83
+ if (!tokenResult.val.user) {
84
+ return new Err(INVALID_TOKEN_PROBLEM);
85
+ }
86
+ const previousUser = tokenResult.val.user;
87
+ const userUpdate = scope === 'verify-email' ? {
88
+ emailVerified: true,
89
+ email
90
+ } : {
91
+ phoneNumberVerified: true,
92
+ phoneNumber
93
+ };
94
+ userUpdate.temporary = false;
95
+ userUpdate.temporaryEmail = null;
96
+ const userGroups = new Set(tokenResult.val.user?.groups);
97
+ userGroups.add(ROLES.MEMBER);
98
+ userUpdate.groups = [
99
+ ...userGroups
100
+ ];
101
+ const user = await prisma.user.update({
102
+ where: {
103
+ id: tokenResult.val.user.id
104
+ },
105
+ data: userUpdate
106
+ });
107
+ // delete the previous token
108
+ await clearToken(tokenResult.val.id);
109
+ // Login the user
110
+ const token = await prisma.token.create({
111
+ data: {
112
+ id: generateId('Token'),
113
+ user: {
114
+ connect: {
115
+ id: user?.id
116
+ }
117
+ },
118
+ expiresAt: addDays(new Date(), 365),
119
+ scope: 'all'
120
+ }
121
+ });
122
+ if (previousUser.email !== user.email) {
123
+ await sendEmailChangedEmail(user.id);
124
+ }
125
+ return new Ok({
126
+ accessToken: token.id,
127
+ expiresAt: token.expiresAt,
128
+ user: user
129
+ });
340
130
  };
341
- export var handleSendVerificationSMS = function(b) {
342
- return _async_to_generator(function() {
343
- var _SendVerificationSMSBody_parse, phoneNumber, checkDuplication, userId, formattedPhoneNumber, user, code, verificationResult;
344
- return _ts_generator(this, function(_state) {
345
- switch(_state.label){
346
- case 0:
347
- _SendVerificationSMSBody_parse = SendVerificationSMSBody.parse(b), phoneNumber = _SendVerificationSMSBody_parse.phoneNumber, checkDuplication = _SendVerificationSMSBody_parse.checkDuplication, userId = _SendVerificationSMSBody_parse.userId;
348
- formattedPhoneNumber = "+".concat(phoneNumber.replace(/[^0-9]/g, ''));
349
- if (!checkDuplication) return [
350
- 3,
351
- 2
352
- ];
353
- return [
354
- 4,
355
- prisma.user.findFirst({
356
- where: {
357
- OR: [
358
- {
359
- phoneNumber: formattedPhoneNumber
360
- },
361
- {
362
- phoneNumber: phoneNumber
363
- }
364
- ],
365
- phoneNumberVerified: true
366
- }
367
- })
368
- ];
369
- case 1:
370
- user = _state.sent();
371
- if (user) {
372
- return [
373
- 2,
374
- new Err(makeProblem(PROBLEM_CONFLICT, 'This phone number is already registered'))
375
- ];
376
- }
377
- _state.label = 2;
378
- case 2:
379
- if (!(!config.isProd && !config.sendSMSOutsideOfProd)) return [
380
- 3,
381
- 4
382
- ];
383
- return [
384
- 4,
385
- createSMSToken(userId || null, formattedPhoneNumber)
386
- ];
387
- case 3:
388
- code = _state.sent();
389
- if (!(code === null || code === void 0 ? void 0 : code.value)) {
390
- return [
391
- 2,
392
- new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid Data'))
393
- ];
394
- }
395
- return [
396
- 2,
397
- new Ok({
398
- success: true,
399
- code: code.value
400
- })
401
- ];
402
- case 4:
403
- return [
404
- 4,
405
- sendVerificationSMS(userId || null, phoneNumber)
406
- ];
407
- case 5:
408
- verificationResult = _state.sent();
409
- if (verificationResult.err) {
410
- return [
411
- 2,
412
- verificationResult
413
- ];
131
+ export const handleSendVerificationSMS = async (b)=>{
132
+ const { phoneNumber, checkDuplication, userId } = SendVerificationSMSBody.parse(b);
133
+ const formattedPhoneNumber = `+${phoneNumber.replace(/[^0-9]/g, '')}`;
134
+ if (checkDuplication) {
135
+ const user = await prisma.user.findFirst({
136
+ where: {
137
+ OR: [
138
+ {
139
+ phoneNumber: formattedPhoneNumber
140
+ },
141
+ {
142
+ phoneNumber: phoneNumber
414
143
  }
415
- return [
416
- 2,
417
- new Ok({
418
- success: true
419
- })
420
- ];
144
+ ],
145
+ phoneNumberVerified: true
421
146
  }
422
147
  });
423
- })();
148
+ if (user) {
149
+ return new Err(makeProblem(PROBLEM_CONFLICT, 'This phone number is already registered'));
150
+ }
151
+ }
152
+ if (!config.isProd && !config.sendSMSOutsideOfProd) {
153
+ const code = await createSMSToken(userId || null, formattedPhoneNumber);
154
+ if (!code?.value) {
155
+ return new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid Data'));
156
+ }
157
+ return new Ok({
158
+ success: true,
159
+ code: code.value
160
+ });
161
+ }
162
+ // Send the sms verification
163
+ const verificationResult = await sendVerificationSMS(userId || null, phoneNumber);
164
+ if (verificationResult.err) {
165
+ return verificationResult;
166
+ }
167
+ return new Ok({
168
+ success: true
169
+ });
424
170
  };