@driveflux/api-functions 1.0.72 → 1.0.75

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 (47) hide show
  1. package/dist/auth/confirm.js +24 -29
  2. package/dist/auth/emails.js +12 -13
  3. package/dist/auth/formatter.js +5 -5
  4. package/dist/auth/otp.js +66 -50
  5. package/dist/auth/register.js +42 -34
  6. package/dist/auth/tokens.js +58 -55
  7. package/dist/auth/verifications.js +53 -52
  8. package/dist/constants.js +0 -1
  9. package/dist/create-logger.js +1 -2
  10. package/dist/mailjet/calls/manage-contacts-in-list.js +5 -6
  11. package/dist/mailjet/calls/manage-subscription-status.js +4 -5
  12. package/dist/mailjet/calls/request-service.js +7 -6
  13. package/dist/mailjet/refresh-email-preferences.js +11 -12
  14. package/dist/mailjet/set-contact.js +11 -12
  15. package/dist/mailjet/types.js +1 -2
  16. package/dist/mailjet/utils/convert-to-array.js +8 -6
  17. package/dist/mailjet/utils/extract-email-preferences.js +14 -15
  18. package/dist/mailjet/utils/lists.js +7 -8
  19. package/dist/mailjet/utils/update-email-references.js +16 -15
  20. package/dist/notion/client.js +22 -19
  21. package/dist/notion/helpful.js +6 -9
  22. package/dist/notion/schemas/block.js +42 -48
  23. package/dist/notion/schemas/common.js +9 -14
  24. package/dist/notion/schemas/database.js +62 -60
  25. package/dist/notion/schemas/emoji.js +1 -2
  26. package/dist/notion/schemas/file.js +9 -9
  27. package/dist/notion/schemas/kb.js +5 -6
  28. package/dist/notion/schemas/page.js +72 -61
  29. package/dist/notion/schemas/parent.js +4 -5
  30. package/dist/notion/schemas/user.js +18 -19
  31. package/dist/reservation/agree.js +6 -7
  32. package/dist/reservation/checks.js +3 -4
  33. package/dist/reservation/display-vehicle.js +66 -74
  34. package/dist/reservation/ensure-user-billing-address.js +9 -11
  35. package/dist/reservation/fetch-or-create.js +83 -102
  36. package/dist/reservation/invoice.js +76 -125
  37. package/dist/reservation/payer.js +5 -6
  38. package/dist/reservation/payment-intent-sync.js +4 -6
  39. package/dist/reservation/reserve.js +3 -5
  40. package/dist/reservation/types.js +1 -2
  41. package/dist/reservation/vehicle.js +13 -16
  42. package/dist/slack.js +24 -29
  43. package/dist/validation.js +77 -79
  44. package/dist/vehicle/vehicle-pricing/constants.js +22 -19
  45. package/dist/vehicle/vehicle-pricing/index.js +28 -42
  46. package/dist/vehicle/vehicle-pricing/types.js +1 -2
  47. package/package.json +10 -11
@@ -11,34 +11,30 @@ import { sendVerificationEmail } from './verifications.js';
11
11
  const Body = z.object({
12
12
  firstName: z.string(),
13
13
  lastName: z.string(),
14
- phoneNumber: z.string().transform((s) => s.replace(/[\s-]/g, '')),
14
+ phoneNumber: z.string().transform((s)=>s.replace(/[\s-]/g, '')),
15
15
  code: z.string().optional().nullable(),
16
- email: z
17
- .string()
18
- .email()
19
- .transform((s) => s.toLowerCase().trim()),
20
- pageSource: z.string().optional().nullable(),
16
+ email: z.string().email().transform((s)=>s.toLowerCase().trim()),
17
+ pageSource: z.string().optional().nullable()
21
18
  });
22
- export const handleConfirmUser = async (b, user, xForwardedFor) => {
19
+ export const handleConfirmUser = async (b, user, xForwardedFor)=>{
23
20
  const { phoneNumber, email, code, pageSource, ...body } = Body.parse(b);
24
21
  let phoneNumberVerified = false;
25
22
  if (code) {
26
- const tokenResult = await verifyToken(code, { scope: 'verify-phone' });
23
+ const tokenResult = await verifyToken(code, {
24
+ scope: 'verify-phone'
25
+ });
27
26
  if (tokenResult.err) {
28
27
  return new Err(makeProblem(PROBLEM_CONFLICT, 'Invalid OTP token'));
29
28
  }
30
29
  await clearToken(tokenResult.val.id);
31
30
  phoneNumberVerified = true;
32
- }
33
- else {
31
+ } else {
34
32
  phoneNumberVerified = user.phoneNumberVerified;
35
33
  }
36
- const method = isMetadata(user?.metadata)
37
- ? user?.metadata?.signupProvider
38
- : undefined;
34
+ const method = isMetadata(user?.metadata) ? user?.metadata?.signupProvider : undefined;
39
35
  const updatedUser = await prisma.user.update({
40
36
  where: {
41
- id: user.id,
37
+ id: user.id
42
38
  },
43
39
  data: {
44
40
  ...body,
@@ -47,42 +43,41 @@ export const handleConfirmUser = async (b, user, xForwardedFor) => {
47
43
  preferredLocale: 'en',
48
44
  phoneNumberVerified,
49
45
  consented: true,
50
- groups: ['member'],
46
+ groups: [
47
+ 'member'
48
+ ],
51
49
  signupParams: {
52
50
  method,
53
- source: pageSource,
51
+ source: pageSource
54
52
  },
55
53
  consents: {
56
54
  create: {
57
55
  id: generateId('UserConsent'),
58
- ipAddress: Array.isArray(xForwardedFor)
59
- ? xForwardedFor?.[0] || '0.0.0.0'
60
- : xForwardedFor || '0.0.0.0',
56
+ ipAddress: Array.isArray(xForwardedFor) ? xForwardedFor?.[0] || '0.0.0.0' : xForwardedFor || '0.0.0.0',
61
57
  consentType: 'terms',
62
- email: email,
63
- },
58
+ email: email
59
+ }
64
60
  },
65
- registrationComplete: true,
66
- },
61
+ registrationComplete: true
62
+ }
67
63
  });
68
64
  const token = await prisma.token.create({
69
65
  data: {
70
66
  id: generateId('Token'),
71
67
  user: {
72
68
  connect: {
73
- id: updatedUser.id,
74
- },
69
+ id: updatedUser.id
70
+ }
75
71
  },
76
72
  expiresAt: addDays(new Date(), 365),
77
- scope: 'all',
78
- },
73
+ scope: 'all'
74
+ }
79
75
  });
80
76
  // We don't want this to be a task. It should be instant
81
77
  await sendVerificationEmail(updatedUser.id);
82
78
  await refreshEmailPreferences(updatedUser.id);
83
79
  return new Ok({
84
80
  token,
85
- user: updatedUser,
81
+ user: updatedUser
86
82
  });
87
83
  };
88
- //# sourceMappingURL=confirm.js.map
@@ -4,11 +4,11 @@ import { emailChangedEmail } from '@driveflux/email-templates/flux/email-changed
4
4
  import { passwordChangedEmail } from '@driveflux/email-templates/flux/password-changed';
5
5
  import { makeProblem, PROBLEM_NOT_FOUND } from '@driveflux/problem';
6
6
  import { Err } from '@driveflux/result';
7
- export const sendEmailChangedEmail = async (userId) => {
7
+ export const sendEmailChangedEmail = async (userId)=>{
8
8
  const user = await prisma.user.findUnique({
9
9
  where: {
10
- id: userId,
11
- },
10
+ id: userId
11
+ }
12
12
  });
13
13
  if (!user) {
14
14
  return new Err(makeProblem(PROBLEM_NOT_FOUND, `The user ${userId} was not found`));
@@ -17,20 +17,20 @@ export const sendEmailChangedEmail = async (userId) => {
17
17
  subject: 'Email change confirmation',
18
18
  to: {
19
19
  name: user.firstName || '',
20
- address: user.email,
20
+ address: user.email
21
21
  },
22
22
  html: emailChangedEmail({
23
23
  user: user.firstName || '',
24
24
  title: 'Email change confirmation',
25
- newEmail: user.email,
26
- }),
25
+ newEmail: user.email
26
+ })
27
27
  });
28
28
  };
29
- export const sendPasswordChangedEmail = async (userId) => {
29
+ export const sendPasswordChangedEmail = async (userId)=>{
30
30
  const user = await prisma.user.findUnique({
31
31
  where: {
32
- id: userId,
33
- },
32
+ id: userId
33
+ }
34
34
  });
35
35
  if (!user) {
36
36
  return new Err(makeProblem(PROBLEM_NOT_FOUND, `The user ${userId} was not found`));
@@ -40,12 +40,11 @@ export const sendPasswordChangedEmail = async (userId) => {
40
40
  subject: title,
41
41
  to: {
42
42
  name: user.firstName || '',
43
- address: user.email,
43
+ address: user.email
44
44
  },
45
45
  html: passwordChangedEmail({
46
46
  user: user.firstName || '',
47
- title: title,
48
- }),
47
+ title: title
48
+ })
49
49
  });
50
50
  };
51
- //# sourceMappingURL=emails.js.map
@@ -221,9 +221,9 @@ const knownCountryCodes = [
221
221
  '246',
222
222
  '269',
223
223
  '692',
224
- '383',
224
+ '383'
225
225
  ];
226
- export const cleanupPhoneNumber = (phoneNumber) => {
226
+ export const cleanupPhoneNumber = (phoneNumber)=>{
227
227
  // Remove all characters except numbers and +
228
228
  const cleaned = phoneNumber.replace(/[^\d+]/g, '');
229
229
  // If it doesn't start with +, add it
@@ -232,8 +232,9 @@ export const cleanupPhoneNumber = (phoneNumber) => {
232
232
  let countryCode = '';
233
233
  let remainingNumber = '';
234
234
  // Try to find the country code by checking the longest matches first
235
- for (let i = 4; i >= 1; i--) {
236
- const 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
  if (knownCountryCodes.includes(potentialCode)) {
238
239
  countryCode = potentialCode;
239
240
  remainingNumber = withPlus.substring(1 + i); // Everything after the country code
@@ -246,4 +247,3 @@ export const cleanupPhoneNumber = (phoneNumber) => {
246
247
  }
247
248
  return withPlus;
248
249
  };
249
- //# sourceMappingURL=formatter.js.map
package/dist/auth/otp.js CHANGED
@@ -2,7 +2,7 @@ import { ROLES } from '@driveflux/auth/authorization/constants';
2
2
  import { config } from '@driveflux/config/backend';
3
3
  import { prisma } from '@driveflux/db';
4
4
  import { generateId } from '@driveflux/db/id';
5
- import { makeProblem, PROBLEM_CONFLICT, PROBLEM_INVALID_DATA, } from '@driveflux/problem';
5
+ import { makeProblem, PROBLEM_CONFLICT, PROBLEM_INVALID_DATA } from '@driveflux/problem';
6
6
  import { Err, Ok } from '@driveflux/result';
7
7
  import { addDays } from 'date-fns/addDays';
8
8
  import { z } from 'zod';
@@ -13,43 +13,46 @@ const SendVerificationSMSBody = z.object({
13
13
  phoneNumber: z.string(),
14
14
  checkDuplication: z.boolean().optional().nullable(),
15
15
  includeUser: z.boolean().optional().nullable(),
16
- userId: z.string().optional().nullable(),
16
+ userId: z.string().optional().nullable()
17
17
  });
18
- const VerifyOtpBody = z
19
- .object({
20
- scope: z.enum(['verify-email', 'verify-phone']),
21
- email: z
22
- .email()
23
- .optional()
24
- .transform((email) => email?.toLowerCase().trim()),
18
+ const VerifyOtpBody = z.object({
19
+ scope: z.enum([
20
+ 'verify-email',
21
+ 'verify-phone'
22
+ ]),
23
+ email: z.email().optional().transform((email)=>email?.toLowerCase().trim()),
25
24
  phoneNumber: z.string().optional(),
26
- code: z.string(),
27
- })
28
- .refine((d) => {
25
+ code: z.string()
26
+ }).refine((d)=>{
29
27
  if (d.scope === 'verify-email' && !d.email) {
30
28
  return false;
31
29
  }
32
30
  return true;
33
31
  }, {
34
- path: ['email'],
35
- message: 'Email is required',
36
- })
37
- .refine((d) => {
32
+ path: [
33
+ 'email'
34
+ ],
35
+ message: 'Email is required'
36
+ }).refine((d)=>{
38
37
  if (d.scope === 'verify-phone' && !d.phoneNumber) {
39
38
  return false;
40
39
  }
41
40
  return true;
42
41
  }, {
43
- path: ['phoneNumber'],
44
- message: 'Phone number is required',
42
+ path: [
43
+ 'phoneNumber'
44
+ ],
45
+ message: 'Phone number is required'
45
46
  });
46
47
  const INVALID_TOKEN_PROBLEM = makeProblem(PROBLEM_INVALID_DATA, 'Unable to verify token. It could have been expired.');
47
48
  const VerifyOtpOnlyBody = z.object({
48
49
  phoneNumber: z.string().optional(),
49
- code: z.string(),
50
+ code: z.string()
50
51
  });
51
- export const handleVerifyOTPOnly = async ({ phoneNumber, code, }) => {
52
- const tokenResult = await verifyToken(code, { scope: 'verify-phone' });
52
+ export const handleVerifyOTPOnly = async ({ phoneNumber, code })=>{
53
+ const tokenResult = await verifyToken(code, {
54
+ scope: 'verify-phone'
55
+ });
53
56
  if (tokenResult.err) {
54
57
  console.log('Error verifying token', tokenResult.val);
55
58
  return new Err(INVALID_TOKEN_PROBLEM);
@@ -59,13 +62,21 @@ export const handleVerifyOTPOnly = async ({ phoneNumber, code, }) => {
59
62
  // await clearToken(tokenResult.val.id)
60
63
  return new Ok({
61
64
  phoneNumber,
62
- phoneNumberVerified: true,
65
+ phoneNumberVerified: true
63
66
  });
64
67
  };
65
- export const handleVerifyOTP = async (b) => {
66
- const { scope, email, phoneNumber: preFormattedPhoneNumber, code, } = VerifyOtpBody.parse(b);
68
+ export const handleVerifyOTP = async (b)=>{
69
+ const { scope, email, phoneNumber: preFormattedPhoneNumber, code } = VerifyOtpBody.parse(b);
67
70
  const phoneNumber = `+${preFormattedPhoneNumber?.replace(/[^0-9]/g, '')}`;
68
- const tokenResult = await verifyToken(code, { scope, metadata: { email, phoneNumber } }, { includeUser: true });
71
+ const tokenResult = await verifyToken(code, {
72
+ scope,
73
+ metadata: {
74
+ email,
75
+ phoneNumber
76
+ }
77
+ }, {
78
+ includeUser: true
79
+ });
69
80
  if (tokenResult.err) {
70
81
  return new Err(INVALID_TOKEN_PROBLEM);
71
82
  }
@@ -73,25 +84,25 @@ export const handleVerifyOTP = async (b) => {
73
84
  return new Err(INVALID_TOKEN_PROBLEM);
74
85
  }
75
86
  const previousUser = tokenResult.val.user;
76
- const userUpdate = scope === 'verify-email'
77
- ? {
78
- emailVerified: true,
79
- email,
80
- }
81
- : {
82
- phoneNumberVerified: true,
83
- phoneNumber,
84
- };
87
+ const userUpdate = scope === 'verify-email' ? {
88
+ emailVerified: true,
89
+ email
90
+ } : {
91
+ phoneNumberVerified: true,
92
+ phoneNumber
93
+ };
85
94
  userUpdate.temporary = false;
86
95
  userUpdate.temporaryEmail = null;
87
96
  const userGroups = new Set(tokenResult.val.user?.groups);
88
97
  userGroups.add(ROLES.MEMBER);
89
- userUpdate.groups = [...userGroups];
98
+ userUpdate.groups = [
99
+ ...userGroups
100
+ ];
90
101
  const user = await prisma.user.update({
91
102
  where: {
92
- id: tokenResult.val.user.id,
103
+ id: tokenResult.val.user.id
93
104
  },
94
- data: userUpdate,
105
+ data: userUpdate
95
106
  });
96
107
  // delete the previous token
97
108
  await clearToken(tokenResult.val.id);
@@ -101,12 +112,12 @@ export const handleVerifyOTP = async (b) => {
101
112
  id: generateId('Token'),
102
113
  user: {
103
114
  connect: {
104
- id: user?.id,
105
- },
115
+ id: user?.id
116
+ }
106
117
  },
107
118
  expiresAt: addDays(new Date(), 365),
108
- scope: 'all',
109
- },
119
+ scope: 'all'
120
+ }
110
121
  });
111
122
  if (previousUser.email !== user.email) {
112
123
  await sendEmailChangedEmail(user.id);
@@ -114,21 +125,25 @@ export const handleVerifyOTP = async (b) => {
114
125
  return new Ok({
115
126
  accessToken: token.id,
116
127
  expiresAt: token.expiresAt,
117
- user: user,
128
+ user: user
118
129
  });
119
130
  };
120
- export const handleSendVerificationSMS = async (b) => {
131
+ export const handleSendVerificationSMS = async (b)=>{
121
132
  const { phoneNumber, checkDuplication, userId } = SendVerificationSMSBody.parse(b);
122
133
  const formattedPhoneNumber = `+${phoneNumber.replace(/[^0-9]/g, '')}`;
123
134
  if (checkDuplication) {
124
135
  const user = await prisma.user.findFirst({
125
136
  where: {
126
137
  OR: [
127
- { phoneNumber: formattedPhoneNumber },
128
- { phoneNumber: phoneNumber },
138
+ {
139
+ phoneNumber: formattedPhoneNumber
140
+ },
141
+ {
142
+ phoneNumber: phoneNumber
143
+ }
129
144
  ],
130
- phoneNumberVerified: true,
131
- },
145
+ phoneNumberVerified: true
146
+ }
132
147
  });
133
148
  if (user) {
134
149
  return new Err(makeProblem(PROBLEM_CONFLICT, 'This phone number is already registered'));
@@ -141,7 +156,7 @@ export const handleSendVerificationSMS = async (b) => {
141
156
  }
142
157
  return new Ok({
143
158
  success: true,
144
- code: code.value,
159
+ code: code.value
145
160
  });
146
161
  }
147
162
  // Send the sms verification
@@ -149,6 +164,7 @@ export const handleSendVerificationSMS = async (b) => {
149
164
  if (verificationResult.err) {
150
165
  return verificationResult;
151
166
  }
152
- return new Ok({ success: true });
167
+ return new Ok({
168
+ success: true
169
+ });
153
170
  };
154
- //# sourceMappingURL=otp.js.map
@@ -18,32 +18,37 @@ import { sendVerificationEmail } from './verifications.js';
18
18
  const Body = z.object({
19
19
  firstName: z.string(),
20
20
  lastName: z.string(),
21
- phoneNumber: z.string().transform((s) => s.replace(/[\s-]/g, '')),
22
- authMethod: z.enum(['mobile', 'email', 'facebook', 'google', 'apple']),
21
+ phoneNumber: z.string().transform((s)=>s.replace(/[\s-]/g, '')),
22
+ authMethod: z.enum([
23
+ 'mobile',
24
+ 'email',
25
+ 'facebook',
26
+ 'google',
27
+ 'apple'
28
+ ]),
23
29
  code: z.string().optional().nullable(),
24
30
  keepCode: z.boolean().optional().default(false),
25
- email: z
26
- .string()
27
- .email()
28
- .transform((s) => s.toLowerCase().trim()),
29
- dateOfBirth: dateValidation
30
- .refine((d) => {
31
+ email: z.string().email().transform((s)=>s.toLowerCase().trim()),
32
+ dateOfBirth: dateValidation.refine((d)=>{
31
33
  return isAfter(d, subYears(new Date(), 25));
32
34
  }, {
33
- path: ['dateOfBirth'],
34
- message: 'You must be at least 25 years old to signup for the service.',
35
- })
36
- .optional(),
35
+ path: [
36
+ 'dateOfBirth'
37
+ ],
38
+ message: 'You must be at least 25 years old to signup for the service.'
39
+ }).optional(),
37
40
  password: z.string().min(6).optional(),
38
41
  noMarketing: z.boolean().optional(),
39
42
  pageSource: z.string().optional().nullable(),
40
- skipPassword: z.boolean().optional().nullable(),
43
+ skipPassword: z.boolean().optional().nullable()
41
44
  });
42
- export const handleRegister = async (b, xForwardedFor) => {
45
+ export const handleRegister = async (b, xForwardedFor)=>{
43
46
  const { noMarketing, password, authMethod, phoneNumber, code, keepCode, pageSource, skipPassword, ...body } = Body.parse(b);
44
47
  let phoneNumberVerified = false;
45
48
  if (code) {
46
- const tokenResult = await verifyToken(code, { scope: 'verify-phone' });
49
+ const tokenResult = await verifyToken(code, {
50
+ scope: 'verify-phone'
51
+ });
47
52
  if (tokenResult.err) {
48
53
  return new Err(makeProblem(PROBLEM_CONFLICT, 'Invalid OTP token'));
49
54
  }
@@ -57,7 +62,9 @@ export const handleRegister = async (b, xForwardedFor) => {
57
62
  }
58
63
  // Check if the user exists
59
64
  const foundUser = await prisma.user.findFirst({
60
- where: { email: body.email },
65
+ where: {
66
+ email: body.email
67
+ }
61
68
  });
62
69
  if (foundUser) {
63
70
  return new Err(makeProblem(PROBLEM_CONFLICT, 'A user with this email address already exists. Did you mean to login?'));
@@ -75,20 +82,20 @@ export const handleRegister = async (b, xForwardedFor) => {
75
82
  preferredLocale: 'en',
76
83
  phoneNumberVerified,
77
84
  consented: true,
78
- groups: ['member'],
85
+ groups: [
86
+ 'member'
87
+ ],
79
88
  consents: {
80
89
  create: {
81
90
  id: generateId('UserConsent'),
82
- ipAddress: Array.isArray(xForwardedFor)
83
- ? xForwardedFor?.[0] || '0.0.0.0'
84
- : xForwardedFor || '0.0.0.0',
91
+ ipAddress: Array.isArray(xForwardedFor) ? xForwardedFor?.[0] || '0.0.0.0' : xForwardedFor || '0.0.0.0',
85
92
  consentType: 'terms',
86
- email: body.email,
87
- },
93
+ email: body.email
94
+ }
88
95
  },
89
96
  signupParams: {
90
97
  method: authMethod,
91
- source: pageSource,
98
+ source: pageSource
92
99
  },
93
100
  registrationComplete: true,
94
101
  accounts: {
@@ -97,32 +104,33 @@ export const handleRegister = async (b, xForwardedFor) => {
97
104
  object: 'account',
98
105
  type: 'default',
99
106
  provider: 'credentials',
100
- providerAccountId: id,
101
- },
102
- },
103
- },
107
+ providerAccountId: id
108
+ }
109
+ }
110
+ }
104
111
  });
105
112
  const token = await prisma.token.create({
106
113
  data: {
107
114
  id: generateId('Token'),
108
115
  user: {
109
116
  connect: {
110
- id: user.id,
111
- },
117
+ id: user.id
118
+ }
112
119
  },
113
120
  expiresAt: addDays(new Date(), 365),
114
- scope: 'all',
115
- },
121
+ scope: 'all'
122
+ }
116
123
  });
117
124
  // We don't want this to be a task. It should be instant
118
125
  await sendVerificationEmail(user.id);
119
126
  if (!noMarketing) {
120
- await setContactInList(user.id, { generalMarketing: true });
127
+ await setContactInList(user.id, {
128
+ generalMarketing: true
129
+ });
121
130
  }
122
131
  await refreshEmailPreferences(user.id);
123
132
  return new Ok({
124
133
  token,
125
- user,
134
+ user
126
135
  });
127
136
  };
128
- //# sourceMappingURL=register.js.map