@driveflux/api-functions 0.0.7-next.0 → 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.
Files changed (44) hide show
  1. package/dist/auth/confirm.js +73 -327
  2. package/dist/auth/emails.js +43 -210
  3. package/dist/auth/formatter.js +11 -11
  4. package/dist/auth/otp.js +122 -392
  5. package/dist/auth/register.js +100 -396
  6. package/dist/auth/tokens.js +115 -430
  7. package/dist/auth/verifications.js +177 -583
  8. package/dist/constants.js +5 -4
  9. package/dist/mailjet/calls/manage-contacts-in-list.js +22 -166
  10. package/dist/mailjet/calls/manage-subscription-status.js +13 -153
  11. package/dist/mailjet/calls/request-service.js +18 -183
  12. package/dist/mailjet/refresh-email-preferences.js +26 -225
  13. package/dist/mailjet/set-contact.js +23 -214
  14. package/dist/mailjet/types.js +2 -1
  15. package/dist/mailjet/utils/convert-to-array.js +9 -58
  16. package/dist/mailjet/utils/extract-email-preferences.js +41 -217
  17. package/dist/mailjet/utils/lists.js +30 -249
  18. package/dist/mailjet/utils/update-email-references.js +27 -208
  19. package/dist/notion/client.js +48 -197
  20. package/dist/notion/helpful.js +29 -170
  21. package/dist/notion/schemas/block.js +49 -43
  22. package/dist/notion/schemas/common.js +17 -14
  23. package/dist/notion/schemas/database.js +125 -159
  24. package/dist/notion/schemas/emoji.js +3 -2
  25. package/dist/notion/schemas/file.js +10 -10
  26. package/dist/notion/schemas/kb.js +9 -8
  27. package/dist/notion/schemas/page.js +126 -171
  28. package/dist/notion/schemas/parent.js +9 -8
  29. package/dist/notion/schemas/user.js +21 -20
  30. package/dist/reservation/agree.js +19 -158
  31. package/dist/reservation/checks.js +23 -178
  32. package/dist/reservation/display-vehicle.js +142 -514
  33. package/dist/reservation/fetch-or-create.js +197 -482
  34. package/dist/reservation/invoice.js +198 -496
  35. package/dist/reservation/payer.js +28 -177
  36. package/dist/reservation/reserve.js +31 -191
  37. package/dist/reservation/types.js +2 -1
  38. package/dist/reservation/vehicle.js +24 -186
  39. package/dist/slack.js +67 -273
  40. package/dist/validation.js +81 -139
  41. package/dist/vehicle/vehicle-pricing/constants.js +33 -36
  42. package/dist/vehicle/vehicle-pricing/index.js +99 -257
  43. package/dist/vehicle/vehicle-pricing/types.js +2 -1
  44. package/package.json +9 -9
@@ -1,444 +1,129 @@
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 _define_property(obj, key, value) {
31
- if (key in obj) {
32
- Object.defineProperty(obj, key, {
33
- value: value,
34
- enumerable: true,
35
- configurable: true,
36
- writable: true
37
- });
38
- } else {
39
- obj[key] = value;
40
- }
41
- return obj;
42
- }
43
- function _object_spread(target) {
44
- for(var i = 1; i < arguments.length; i++){
45
- var source = arguments[i] != null ? arguments[i] : {};
46
- var ownKeys = Object.keys(source);
47
- if (typeof Object.getOwnPropertySymbols === "function") {
48
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
- }));
51
- }
52
- ownKeys.forEach(function(key) {
53
- _define_property(target, key, source[key]);
54
- });
55
- }
56
- return target;
57
- }
58
- function ownKeys(object, enumerableOnly) {
59
- var keys = Object.keys(object);
60
- if (Object.getOwnPropertySymbols) {
61
- var symbols = Object.getOwnPropertySymbols(object);
62
- if (enumerableOnly) {
63
- symbols = symbols.filter(function(sym) {
64
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
65
- });
66
- }
67
- keys.push.apply(keys, symbols);
68
- }
69
- return keys;
70
- }
71
- function _object_spread_props(target, source) {
72
- source = source != null ? source : {};
73
- if (Object.getOwnPropertyDescriptors) {
74
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
75
- } else {
76
- ownKeys(Object(source)).forEach(function(key) {
77
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
78
- });
79
- }
80
- return target;
81
- }
82
- function _type_of(obj) {
83
- "@swc/helpers - typeof";
84
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
85
- }
86
- function _ts_generator(thisArg, body) {
87
- var f, y, t, _ = {
88
- label: 0,
89
- sent: function() {
90
- if (t[0] & 1) throw t[1];
91
- return t[1];
92
- },
93
- trys: [],
94
- ops: []
95
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
96
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
97
- return this;
98
- }), g;
99
- function verb(n) {
100
- return function(v) {
101
- return step([
102
- n,
103
- v
104
- ]);
105
- };
106
- }
107
- function step(op) {
108
- if (f) throw new TypeError("Generator is already executing.");
109
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
110
- 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;
111
- if (y = 0, t) op = [
112
- op[0] & 2,
113
- t.value
114
- ];
115
- switch(op[0]){
116
- case 0:
117
- case 1:
118
- t = op;
119
- break;
120
- case 4:
121
- _.label++;
122
- return {
123
- value: op[1],
124
- done: false
125
- };
126
- case 5:
127
- _.label++;
128
- y = op[1];
129
- op = [
130
- 0
131
- ];
132
- continue;
133
- case 7:
134
- op = _.ops.pop();
135
- _.trys.pop();
136
- continue;
137
- default:
138
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
139
- _ = 0;
140
- continue;
141
- }
142
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
143
- _.label = op[1];
144
- break;
145
- }
146
- if (op[0] === 6 && _.label < t[1]) {
147
- _.label = t[1];
148
- t = op;
149
- break;
150
- }
151
- if (t && _.label < t[2]) {
152
- _.label = t[2];
153
- _.ops.push(op);
154
- break;
155
- }
156
- if (t[2]) _.ops.pop();
157
- _.trys.pop();
158
- continue;
159
- }
160
- op = body.call(thisArg, _);
161
- } catch (e) {
162
- op = [
163
- 6,
164
- e
165
- ];
166
- y = 0;
167
- } finally{
168
- f = t = 0;
169
- }
170
- if (op[0] & 5) throw op[1];
171
- return {
172
- value: op[0] ? op[1] : void 0,
173
- done: true
174
- };
175
- }
176
- }
177
1
  import { prisma } from '@driveflux/db';
178
2
  import { generateId } from '@driveflux/db/id';
179
- import { makeProblem, PROBLEM_CORRUPT, PROBLEM_EXPIRED, PROBLEM_INVALID_DATA, PROBLEM_NOT_FOUND } from '@driveflux/problem';
3
+ import { makeProblem, PROBLEM_CORRUPT, PROBLEM_EXPIRED, PROBLEM_INVALID_DATA, PROBLEM_NOT_FOUND, } from '@driveflux/problem';
180
4
  import { Err, Ok } from '@driveflux/result';
181
5
  import { addDays } from 'date-fns/addDays';
182
6
  import { addMinutes } from 'date-fns/addMinutes';
183
7
  import { customAlphabet } from 'nanoid';
184
- export var createToken = function(userId, type, value, providedExpiresAt) {
185
- return _async_to_generator(function() {
186
- var alphabet, nanoid, identifier, scope, expiresAt;
187
- return _ts_generator(this, function(_state) {
188
- switch(_state.label){
189
- case 0:
190
- // Generate 6 digits
191
- alphabet = '0123456789';
192
- nanoid = customAlphabet(alphabet, 6);
193
- identifier = !userId ? value : null;
194
- scope = type === 'email' ? 'verify-email' : 'verify-phone';
195
- expiresAt = providedExpiresAt || (type === 'email' ? addDays(new Date(), 1) : addMinutes(new Date(), 30));
196
- if (!userId && !identifier) {
197
- return [
198
- 2
199
- ];
200
- }
201
- return [
202
- 4,
203
- prisma.token.create({
204
- data: _object_spread_props(_object_spread({
205
- id: generateId('Token'),
206
- value: nanoid()
207
- }, userId ? {
208
- user: {
209
- connect: {
210
- id: userId
211
- }
212
- }
213
- } : {}, identifier ? {
214
- identifier: identifier
215
- } : {}), {
216
- expiresAt: expiresAt,
217
- scope: scope,
218
- metadata: _define_property({}, type, value)
219
- })
220
- })
221
- ];
222
- case 1:
223
- return [
224
- 2,
225
- _state.sent()
226
- ];
227
- }
228
- });
229
- })();
8
+ export const createToken = async (userId, type, value, providedExpiresAt) => {
9
+ // Generate 6 digits
10
+ const alphabet = '0123456789';
11
+ const nanoid = customAlphabet(alphabet, 6);
12
+ const identifier = !userId ? value : null;
13
+ const scope = type === 'email' ? 'verify-email' : 'verify-phone';
14
+ const expiresAt = providedExpiresAt ||
15
+ (type === 'email' ? addDays(new Date(), 1) : addMinutes(new Date(), 30));
16
+ if (!userId && !identifier) {
17
+ return;
18
+ }
19
+ return await prisma.token.create({
20
+ data: {
21
+ id: generateId('Token'),
22
+ value: nanoid(),
23
+ ...(userId
24
+ ? {
25
+ user: {
26
+ connect: {
27
+ id: userId,
28
+ },
29
+ },
30
+ }
31
+ : {}),
32
+ ...(identifier ? { identifier } : {}),
33
+ expiresAt,
34
+ scope,
35
+ metadata: {
36
+ [type]: value,
37
+ },
38
+ },
39
+ });
230
40
  };
231
- export var createEmailToken = function(userId, email) {
232
- return _async_to_generator(function() {
233
- return _ts_generator(this, function(_state) {
234
- switch(_state.label){
235
- case 0:
236
- return [
237
- 4,
238
- createToken(userId, 'email', email)
239
- ];
240
- case 1:
241
- return [
242
- 2,
243
- _state.sent()
244
- ];
245
- }
246
- });
247
- })();
41
+ export const createEmailToken = async (userId, email) => {
42
+ return await createToken(userId, 'email', email);
248
43
  };
249
- export var createSMSToken = function(userId, phoneNumber) {
250
- return _async_to_generator(function() {
251
- return _ts_generator(this, function(_state) {
252
- switch(_state.label){
253
- case 0:
254
- return [
255
- 4,
256
- createToken(userId, 'phoneNumber', phoneNumber)
257
- ];
258
- case 1:
259
- return [
260
- 2,
261
- _state.sent()
262
- ];
263
- }
264
- });
265
- })();
44
+ export const createSMSToken = async (userId, phoneNumber) => {
45
+ return await createToken(userId, 'phoneNumber', phoneNumber);
266
46
  };
267
- export var verifyToken = function(tokenIdOrValue, verifications, option) {
268
- return _async_to_generator(function() {
269
- var token, _tmp, _verifications_metadata, tokenPhoneNumber, formattedTokenPhoneNumber, phoneNumber, tokenEmail;
270
- return _ts_generator(this, function(_state) {
271
- switch(_state.label){
272
- case 0:
273
- if (!((typeof tokenIdOrValue === "undefined" ? "undefined" : _type_of(tokenIdOrValue)) === 'object')) return [
274
- 3,
275
- 1
276
- ];
277
- _tmp = tokenIdOrValue;
278
- return [
279
- 3,
280
- 3
281
- ];
282
- case 1:
283
- return [
284
- 4,
285
- prisma.token.findFirst(_object_spread({
286
- where: {
287
- OR: [
288
- {
289
- id: tokenIdOrValue
290
- },
291
- {
292
- value: tokenIdOrValue
293
- }
294
- ]
295
- }
296
- }, (option === null || option === void 0 ? void 0 : option.includeUser) ? {
297
- include: {
298
- user: true
299
- }
300
- } : {}))
301
- ];
302
- case 2:
303
- _tmp = _state.sent();
304
- _state.label = 3;
305
- case 3:
306
- token = _tmp;
307
- if (!token) {
308
- return [
309
- 2,
310
- new Err(makeProblem(PROBLEM_NOT_FOUND, 'Token not found'))
311
- ];
312
- }
313
- if (token.expiresAt && token.expiresAt.getTime() < Date.now()) {
314
- return [
315
- 2,
316
- new Err(makeProblem(PROBLEM_EXPIRED, 'This token has expired'))
317
- ];
318
- }
319
- if (typeof (verifications === null || verifications === void 0 ? void 0 : verifications.scope) !== 'undefined' && token.scope !== verifications.scope) {
320
- return [
321
- 2,
322
- new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid token scope'))
323
- ];
324
- }
325
- if (typeof (verifications === null || verifications === void 0 ? void 0 : verifications.metadata) !== 'undefined') {
326
- if (typeof verifications.metadata.phoneNumber !== 'undefined') {
327
- ;
328
- tokenPhoneNumber = (token === null || token === void 0 ? void 0 : token.metadata) && _type_of(token.metadata) === 'object' && 'phoneNumber' in token.metadata ? token.metadata.phoneNumber : undefined;
329
- formattedTokenPhoneNumber = "+".concat(tokenPhoneNumber === null || tokenPhoneNumber === void 0 ? void 0 : tokenPhoneNumber.replace(/[^0-9]/g, ''));
330
- phoneNumber = (_verifications_metadata = verifications.metadata) === null || _verifications_metadata === void 0 ? void 0 : _verifications_metadata.phoneNumber;
331
- if (verifications.scope === 'verify-phone' && phoneNumber !== formattedTokenPhoneNumber && phoneNumber !== tokenPhoneNumber) {
332
- return [
333
- 2,
334
- new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid token data'))
335
- ];
336
- }
337
- }
338
- if (typeof verifications.metadata.email !== 'undefined') {
339
- tokenEmail = (token === null || token === void 0 ? void 0 : token.metadata) && _type_of(token.metadata) === 'object' && 'email' in token.metadata ? token.metadata.email : undefined;
340
- if (verifications.scope === 'verify-email' && tokenEmail !== verifications.metadata.email) {
341
- return [
342
- 2,
343
- new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid token data'))
344
- ];
345
- }
346
- }
347
- }
348
- // if (typeof verifications?.metadata !== 'undefined') {
349
- // for (const key of Object.keys(verifications.metadata)) {
350
- // if (
351
- // typeof verifications.metadata[key] !== 'undefined' &&
352
- // verifications.metadata[key] !== (token.metadata as any)?.[key] &&
353
- // ((key === 'email' && verifications.scope === 'verify-email') ||
354
- // (key === 'phoneNumber' && verifications.scope === 'verify-phone'))
355
- // ) {
356
- // return new Err(makeProblem(PROBLEM_INVALID_DATA, `Invalid token data`))
357
- // }
358
- // }
359
- // }
360
- if ((option === null || option === void 0 ? void 0 : option.includeUser) && !token.user) {
361
- return [
362
- 2,
363
- new Err(makeProblem(PROBLEM_CORRUPT, 'Token is invalid'))
364
- ];
365
- }
366
- return [
367
- 2,
368
- new Ok(token)
369
- ];
47
+ export const verifyToken = async (tokenIdOrValue, verifications, option) => {
48
+ const token = typeof tokenIdOrValue === 'object'
49
+ ? tokenIdOrValue
50
+ : (await prisma.token.findFirst({
51
+ where: {
52
+ OR: [{ id: tokenIdOrValue }, { value: tokenIdOrValue }],
53
+ },
54
+ ...(option?.includeUser ? { include: { user: true } } : {}),
55
+ }));
56
+ if (!token) {
57
+ return new Err(makeProblem(PROBLEM_NOT_FOUND, 'Token not found'));
58
+ }
59
+ if (token.expiresAt && token.expiresAt.getTime() < Date.now()) {
60
+ return new Err(makeProblem(PROBLEM_EXPIRED, 'This token has expired'));
61
+ }
62
+ if (typeof verifications?.scope !== 'undefined' &&
63
+ token.scope !== verifications.scope) {
64
+ return new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid token scope'));
65
+ }
66
+ if (typeof verifications?.metadata !== 'undefined') {
67
+ if (typeof verifications.metadata.phoneNumber !== 'undefined') {
68
+ const tokenPhoneNumber = token?.metadata &&
69
+ typeof token.metadata === 'object' &&
70
+ 'phoneNumber' in token.metadata
71
+ ? token.metadata.phoneNumber
72
+ : undefined;
73
+ const formattedTokenPhoneNumber = `+${(tokenPhoneNumber)?.replace(/[^0-9]/g, '')}`;
74
+ const phoneNumber = verifications.metadata?.phoneNumber;
75
+ if (verifications.scope === 'verify-phone' &&
76
+ phoneNumber !== formattedTokenPhoneNumber &&
77
+ phoneNumber !== tokenPhoneNumber) {
78
+ return new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid token data'));
370
79
  }
371
- });
372
- })();
373
- };
374
- export var clearToken = function(tokenId) {
375
- return _async_to_generator(function() {
376
- var _e;
377
- return _ts_generator(this, function(_state) {
378
- switch(_state.label){
379
- case 0:
380
- _state.trys.push([
381
- 0,
382
- 2,
383
- ,
384
- 3
385
- ]);
386
- return [
387
- 4,
388
- prisma.token.delete({
389
- where: {
390
- id: tokenId
391
- }
392
- })
393
- ];
394
- case 1:
395
- _state.sent();
396
- return [
397
- 3,
398
- 3
399
- ];
400
- case 2:
401
- _e = _state.sent();
402
- return [
403
- 3,
404
- 3
405
- ];
406
- case 3:
407
- return [
408
- 2
409
- ];
80
+ }
81
+ if (typeof verifications.metadata.email !== 'undefined') {
82
+ const tokenEmail = token?.metadata &&
83
+ typeof token.metadata === 'object' &&
84
+ 'email' in token.metadata
85
+ ? token.metadata.email
86
+ : undefined;
87
+ if (verifications.scope === 'verify-email' &&
88
+ tokenEmail !== verifications.metadata.email) {
89
+ return new Err(makeProblem(PROBLEM_INVALID_DATA, 'Invalid token data'));
410
90
  }
411
- });
412
- })();
91
+ }
92
+ }
93
+ // if (typeof verifications?.metadata !== 'undefined') {
94
+ // for (const key of Object.keys(verifications.metadata)) {
95
+ // if (
96
+ // typeof verifications.metadata[key] !== 'undefined' &&
97
+ // verifications.metadata[key] !== (token.metadata as any)?.[key] &&
98
+ // ((key === 'email' && verifications.scope === 'verify-email') ||
99
+ // (key === 'phoneNumber' && verifications.scope === 'verify-phone'))
100
+ // ) {
101
+ // return new Err(makeProblem(PROBLEM_INVALID_DATA, `Invalid token data`))
102
+ // }
103
+ // }
104
+ // }
105
+ if (option?.includeUser && !token.user) {
106
+ return new Err(makeProblem(PROBLEM_CORRUPT, 'Token is invalid'));
107
+ }
108
+ return new Ok(token);
413
109
  };
414
- export var clearExpiredTokens = function() {
415
- return _async_to_generator(function() {
416
- return _ts_generator(this, function(_state) {
417
- switch(_state.label){
418
- case 0:
419
- return [
420
- 4,
421
- prisma.token.deleteMany({
422
- where: {
423
- OR: [
424
- {
425
- expiresAt: {
426
- lte: new Date()
427
- }
428
- },
429
- {
430
- invalid: true
431
- }
432
- ]
433
- }
434
- })
435
- ];
436
- case 1:
437
- _state.sent();
438
- return [
439
- 2
440
- ];
441
- }
110
+ export const clearToken = async (tokenId) => {
111
+ try {
112
+ await prisma.token.delete({
113
+ where: {
114
+ id: tokenId,
115
+ },
442
116
  });
443
- })();
117
+ }
118
+ catch (_e) {
119
+ // Nothing to for now
120
+ }
121
+ };
122
+ export const clearExpiredTokens = async () => {
123
+ await prisma.token.deleteMany({
124
+ where: {
125
+ OR: [{ expiresAt: { lte: new Date() } }, { invalid: true }],
126
+ },
127
+ });
444
128
  };
129
+ //# sourceMappingURL=tokens.js.map