@driveflux/api-functions 0.0.7-next.33 → 0.0.7-next.34

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.
@@ -13,8 +13,8 @@ export declare const createToken: (userId: string | null, type: "email" | "phone
13
13
  metadata: PrismaJson.AnyMetadata | null;
14
14
  createdAt: Date;
15
15
  updatedAt: Date;
16
- value: string | null;
17
16
  userId: string | null;
17
+ value: string | null;
18
18
  client: string | null;
19
19
  identifier: string | null;
20
20
  expiresAt: Date | null;
@@ -29,8 +29,8 @@ export declare const createEmailToken: (userId: string, email: string) => Promis
29
29
  metadata: PrismaJson.AnyMetadata | null;
30
30
  createdAt: Date;
31
31
  updatedAt: Date;
32
- value: string | null;
33
32
  userId: string | null;
33
+ value: string | null;
34
34
  client: string | null;
35
35
  identifier: string | null;
36
36
  expiresAt: Date | null;
@@ -45,8 +45,8 @@ export declare const createSMSToken: (userId: string | null, phoneNumber: string
45
45
  metadata: PrismaJson.AnyMetadata | null;
46
46
  createdAt: Date;
47
47
  updatedAt: Date;
48
- value: string | null;
49
48
  userId: string | null;
49
+ value: string | null;
50
50
  client: string | null;
51
51
  identifier: string | null;
52
52
  expiresAt: Date | null;
@@ -190,8 +190,8 @@ export declare const notionDatabasePropertyRollupSchema: ZodObject<{
190
190
  type: z.ZodEnum<{
191
191
  number: "number";
192
192
  date: "date";
193
- array: "array";
194
193
  incomplete: "incomplete";
194
+ array: "array";
195
195
  unsupported: "unsupported";
196
196
  }>;
197
197
  }, z.core.$strip>;
@@ -231,8 +231,8 @@ export declare const notionPagePropertyRollupSchema: ZodObject<{
231
231
  type: z.ZodEnum<{
232
232
  number: "number";
233
233
  date: "date";
234
- array: "array";
235
234
  incomplete: "incomplete";
235
+ array: "array";
236
236
  unsupported: "unsupported";
237
237
  }>;
238
238
  }, z.core.$strip>;
@@ -1,7 +1,6 @@
1
1
  import { type DisplayVehicle, type MileagePackage, type PlanType, type UltraTier, type Vehicle, type VehicleType } from '@driveflux/db';
2
2
  export declare const fetchDisplayVehicle: (displayVehicle: DisplayVehicle | string) => Promise<{
3
3
  object: import("@driveflux/db").DisplayVehicleObject;
4
- type: VehicleType;
5
4
  id: string;
6
5
  createdAt: Date;
7
6
  updatedAt: Date;
@@ -9,6 +8,7 @@ export declare const fetchDisplayVehicle: (displayVehicle: DisplayVehicle | stri
9
8
  vehicleModel: string;
10
9
  variant: string;
11
10
  year: number;
11
+ type: VehicleType;
12
12
  brandSlug: string;
13
13
  brandId: string;
14
14
  defaultHostId: string;
@@ -191,20 +191,20 @@ export declare const fetchDisplayVehicle: (displayVehicle: DisplayVehicle | stri
191
191
  export declare const createVehicleFromDisplayVehicle: (displayVehicle: DisplayVehicle | string, selectedColor?: string) => Promise<({
192
192
  host: {
193
193
  object: import("@driveflux/db").HostObject;
194
- phoneNumber: string | null;
195
- type: import("@driveflux/db").HostType | null;
194
+ name: string;
196
195
  id: string;
197
196
  metadata: PrismaJson.AnyMetadata | null;
198
- name: string;
199
197
  createdAt: Date;
200
198
  updatedAt: Date;
199
+ registrationNumber: string | null;
200
+ type: import("@driveflux/db").HostType | null;
201
+ active: boolean;
201
202
  email: string | null;
203
+ phoneNumber: string | null;
202
204
  race: import("@driveflux/db").Race | null;
203
205
  emergencyContactName: string | null;
204
206
  emergencyPhoneNumber: string | null;
205
- active: boolean;
206
207
  commencementDate: Date | null;
207
- registrationNumber: string | null;
208
208
  stripeAccountId: string | null;
209
209
  isStripeConnected: boolean;
210
210
  serviceCenterIds: string[];
@@ -262,15 +262,11 @@ export declare const createVehicleFromDisplayVehicle: (displayVehicle: DisplayVe
262
262
  };
263
263
  } & {
264
264
  object: import("@driveflux/db").VehicleObject;
265
- type: VehicleType;
266
265
  id: string;
266
+ status: import("@driveflux/db").VehicleStatus;
267
267
  metadata: PrismaJson.AnyMetadata | null;
268
268
  createdAt: Date;
269
269
  updatedAt: Date;
270
- status: import("@driveflux/db").VehicleStatus;
271
- temporary: boolean;
272
- registrationNumber: string | null;
273
- hostId: string;
274
270
  make: string;
275
271
  vehicleModel: string;
276
272
  variant: string;
@@ -280,6 +276,7 @@ export declare const createVehicleFromDisplayVehicle: (displayVehicle: DisplayVe
280
276
  compositeSearchScore: number;
281
277
  registrationDate: Date | null;
282
278
  mileage: number | null;
279
+ registrationNumber: string | null;
283
280
  vin: string | null;
284
281
  engineNumber: string | null;
285
282
  availability: import("@driveflux/db").VehicleAvailablity | null;
@@ -294,6 +291,8 @@ export declare const createVehicleFromDisplayVehicle: (displayVehicle: DisplayVe
294
291
  delistedAt: Date | null;
295
292
  imagesNotActual: boolean;
296
293
  imagesNotActualMessage: string | null;
294
+ hostId: string;
295
+ type: VehicleType;
297
296
  allowedServiceCenterIds: string[];
298
297
  displayVehicleId: string | null;
299
298
  isHostConfirmed: boolean | null;
@@ -306,6 +305,7 @@ export declare const createVehicleFromDisplayVehicle: (displayVehicle: DisplayVe
306
305
  featureOnLandingPage: boolean;
307
306
  lastMileageRecordedAt: Date | null;
308
307
  locationAvailability: string[];
308
+ temporary: boolean;
309
309
  hotDeal: boolean | null;
310
310
  activeSubscriptionId: string | null;
311
311
  addresses: {
@@ -689,15 +689,11 @@ export declare const createVehicleFromDisplayVehicle: (displayVehicle: DisplayVe
689
689
  }) | undefined>;
690
690
  export declare const transfromVehicleDisplayToVehicle: (displayVehicle: DisplayVehicle | string, selectedColor: string) => Promise<({
691
691
  object: import("@driveflux/db").VehicleObject;
692
- type: VehicleType;
693
692
  id: string;
693
+ status: import("@driveflux/db").VehicleStatus;
694
694
  metadata: PrismaJson.AnyMetadata | null;
695
695
  createdAt: Date;
696
696
  updatedAt: Date;
697
- status: import("@driveflux/db").VehicleStatus;
698
- temporary: boolean;
699
- registrationNumber: string | null;
700
- hostId: string;
701
697
  make: string;
702
698
  vehicleModel: string;
703
699
  variant: string;
@@ -707,6 +703,7 @@ export declare const transfromVehicleDisplayToVehicle: (displayVehicle: DisplayV
707
703
  compositeSearchScore: number;
708
704
  registrationDate: Date | null;
709
705
  mileage: number | null;
706
+ registrationNumber: string | null;
710
707
  vin: string | null;
711
708
  engineNumber: string | null;
712
709
  availability: import("@driveflux/db").VehicleAvailablity | null;
@@ -721,6 +718,8 @@ export declare const transfromVehicleDisplayToVehicle: (displayVehicle: DisplayV
721
718
  delistedAt: Date | null;
722
719
  imagesNotActual: boolean;
723
720
  imagesNotActualMessage: string | null;
721
+ hostId: string;
722
+ type: VehicleType;
724
723
  allowedServiceCenterIds: string[];
725
724
  displayVehicleId: string | null;
726
725
  isHostConfirmed: boolean | null;
@@ -733,6 +732,7 @@ export declare const transfromVehicleDisplayToVehicle: (displayVehicle: DisplayV
733
732
  featureOnLandingPage: boolean;
734
733
  lastMileageRecordedAt: Date | null;
735
734
  locationAvailability: string[];
735
+ temporary: boolean;
736
736
  hotDeal: boolean | null;
737
737
  activeSubscriptionId: string | null;
738
738
  addresses: {
@@ -1,6 +1,6 @@
1
1
  import { config } from '@driveflux/config/backend';
2
2
  import { loadCoupon, PROBLEM_APPLICABLE_NOT_FOUND } from '@driveflux/coupon';
3
- import { prisma } from '@driveflux/db';
3
+ import { prisma, } from '@driveflux/db';
4
4
  import { generateId } from '@driveflux/db/id';
5
5
  import { EMPTY_BILLING_ADDRESS } from '@driveflux/db/models/other';
6
6
  import { PURPOSE_RESERVATION } from '@driveflux/db/models/subscription';
@@ -12,15 +12,16 @@ import { Ok } from '@driveflux/result';
12
12
  import { format } from '@driveflux/time';
13
13
  import { isAfter } from 'date-fns/isAfter';
14
14
  import { subMinutes } from 'date-fns/subMinutes';
15
- export const updateReservationInvoiceIfNeeded = async (oldReservationInvoice, newParams)=>{
15
+ export const updateReservationInvoiceIfNeeded = async (oldReservationInvoice, newParams) => {
16
16
  const couponIsDifferent = oldReservationInvoice.couponCode !== newParams.couponCode;
17
- if (!couponIsDifferent && oldReservationInvoice.stripePaymentIntentId === newParams.paymentIntentId) {
17
+ if (!couponIsDifferent &&
18
+ oldReservationInvoice.stripePaymentIntentId === newParams.paymentIntentId) {
18
19
  return new Ok(oldReservationInvoice);
19
20
  }
20
21
  // If the coupon or payment intent are differnt we should update the invoice
21
22
  let update = {
22
23
  stripePaymentIntentId: newParams.paymentIntentId,
23
- couponCode: newParams.couponCode
24
+ couponCode: newParams.couponCode,
24
25
  };
25
26
  // If the coupon is different, we need to apply the coupon to the invoice
26
27
  if (couponIsDifferent) {
@@ -28,56 +29,63 @@ export const updateReservationInvoiceIfNeeded = async (oldReservationInvoice, ne
28
29
  const result = await coupons.applyCouponToUnsavedInvoice(newParams.couponCode, oldReservationInvoice);
29
30
  if (result.ok) {
30
31
  const { discounts, ...rest } = result.val;
31
- const toDisconnect = discounts ? oldReservationInvoice.discountIds?.filter((id)=>!discounts.connect?.some((d)=>d.id === id)) : undefined;
32
- const disconnectClauses = toDisconnect?.length ? {
33
- disconnect: toDisconnect.map((id)=>({
34
- id
35
- }))
36
- } : undefined;
32
+ const toDisconnect = discounts
33
+ ? oldReservationInvoice.discountIds?.filter((id) => !discounts.connect?.some((d) => d.id === id))
34
+ : undefined;
35
+ const disconnectClauses = toDisconnect?.length
36
+ ? {
37
+ disconnect: toDisconnect.map((id) => ({ id })),
38
+ }
39
+ : undefined;
37
40
  update = {
38
41
  ...update,
39
42
  ...rest,
40
- ...discounts || disconnectClauses ? {
41
- discounts: {
42
- ...discounts || {},
43
- ...disconnectClauses || {}
43
+ ...(discounts || disconnectClauses
44
+ ? {
45
+ discounts: {
46
+ ...(discounts || {}),
47
+ ...(disconnectClauses || {}),
48
+ },
44
49
  }
45
- } : {},
50
+ : {}),
46
51
  metadata: {
47
52
  ...update.metadata,
48
- couponCode: newParams.couponCode
53
+ couponCode: newParams.couponCode,
49
54
  },
50
55
  providerMetadata: {
51
56
  ...update.providerMetadata,
52
- couponCode: newParams.couponCode
53
- }
57
+ couponCode: newParams.couponCode,
58
+ },
54
59
  };
55
60
  }
56
61
  if (result.err && result.val.code !== PROBLEM_APPLICABLE_NOT_FOUND) {
57
62
  return result;
58
63
  }
59
- } else if (oldReservationInvoice.discountIds.length) {
64
+ }
65
+ else if (oldReservationInvoice.discountIds.length) {
60
66
  // No discounts, so we disconnect all of them
61
67
  update = {
62
68
  discounts: {
63
- disconnect: oldReservationInvoice.discountIds.map((id)=>({
64
- id
65
- }))
66
- }
69
+ disconnect: oldReservationInvoice.discountIds.map((id) => ({
70
+ id,
71
+ })),
72
+ },
67
73
  };
68
74
  }
69
75
  }
70
76
  let updated = await wrapInResult(biller.updateInvoice(oldReservationInvoice.id, update));
71
77
  // If the invoice was locked due to pending status and we're more than 1 minute old, we unlock it
72
78
  if (updated.err && updated.val.code === 'invoice_pending') {
73
- if (oldReservationInvoice.lockedAt && isAfter(oldReservationInvoice.lockedAt, subMinutes(new Date(), 1)) || !oldReservationInvoice.lockedAt) {
79
+ if ((oldReservationInvoice.lockedAt &&
80
+ isAfter(oldReservationInvoice.lockedAt, subMinutes(new Date(), 1))) ||
81
+ !oldReservationInvoice.lockedAt) {
74
82
  updated = await wrapInResult(biller.updateInvoice(oldReservationInvoice.id, {
75
83
  ...update,
76
84
  locked: false,
77
85
  lockedAt: null,
78
86
  lockReason: null,
79
87
  unlockAt: null,
80
- status: 'draft'
88
+ status: 'draft',
81
89
  }));
82
90
  }
83
91
  }
@@ -86,7 +94,7 @@ export const updateReservationInvoiceIfNeeded = async (oldReservationInvoice, ne
86
94
  }
87
95
  return new Ok(updated.val);
88
96
  };
89
- export const createReservationInvoice = async ({ invoiceId, couponCode, plan, mileagePackage, referralCode, reservationId, paymentIntentId, freeReservation, freeReservationReason, analytics, vehicle, payer, subscribingUser })=>{
97
+ export const createReservationInvoice = async ({ invoiceId, couponCode, plan, mileagePackage, referralCode, reservationId, paymentIntentId, freeReservation, freeReservationReason, analytics, vehicle, payer, subscribingUser, }) => {
90
98
  if (freeReservation) {
91
99
  // It's a free reservation, so we don't need to add a coupon code
92
100
  return await wrapInResult(biller.createInvoice(getInvoiceCreateDetails({
@@ -99,17 +107,21 @@ export const createReservationInvoice = async ({ invoiceId, couponCode, plan, mi
99
107
  freeReservation,
100
108
  freeReservationReason,
101
109
  analytics,
102
- reservationId
110
+ reservationId,
103
111
  })));
104
112
  }
105
113
  let finalCouponCode = couponCode;
106
114
  // If we don't have a reservation coupon ID, we apply the referral discount if any
107
- const referral = referralCode && await prisma.referral.findUnique({
108
- where: {
109
- id: referralCode
110
- }
111
- });
112
- if (!finalCouponCode && referral && referral.discountToReceiverCouponId && referral.discountToReceiverCouponApplicationName === 'reservationFee') {
115
+ const referral = referralCode &&
116
+ (await prisma.referral.findUnique({
117
+ where: {
118
+ id: referralCode,
119
+ },
120
+ }));
121
+ if (!finalCouponCode &&
122
+ referral &&
123
+ referral.discountToReceiverCouponId &&
124
+ referral.discountToReceiverCouponApplicationName === 'reservationFee') {
113
125
  const referralCoupon = await loadCoupon(referral.discountToReceiverCouponId);
114
126
  if (referralCoupon) {
115
127
  finalCouponCode = referralCoupon.code;
@@ -128,15 +140,15 @@ export const createReservationInvoice = async ({ invoiceId, couponCode, plan, mi
128
140
  freeReservation,
129
141
  freeReservationReason,
130
142
  analytics,
131
- reservationId
143
+ reservationId,
132
144
  }),
133
145
  lines: [
134
146
  {
135
147
  amount: reservationAmount,
136
148
  description: `Reservation fee for ${vehicleName(vehicle)}`,
137
- chargingFor: 'reservationFee'
138
- }
139
- ]
149
+ chargingFor: 'reservationFee',
150
+ },
151
+ ],
140
152
  };
141
153
  if (finalCouponCode) {
142
154
  const withCoupon = await coupons.applyCouponToUnsavedInvoice(finalCouponCode, invoiceCreateDetails, 'reservationFee', {
@@ -146,9 +158,10 @@ export const createReservationInvoice = async ({ invoiceId, couponCode, plan, mi
146
158
  vehicleMake: vehicle.make,
147
159
  userId: subscribingUser.id,
148
160
  businessId: payer.object === 'business' ? payer.id : undefined,
149
- vehicleType: vehicle.type
161
+ vehicleType: vehicle.type,
150
162
  });
151
- if (withCoupon.err && withCoupon.val.code !== PROBLEM_APPLICABLE_NOT_FOUND) {
163
+ if (withCoupon.err &&
164
+ withCoupon.val.code !== PROBLEM_APPLICABLE_NOT_FOUND) {
152
165
  return withCoupon;
153
166
  }
154
167
  if (withCoupon.ok) {
@@ -156,18 +169,18 @@ export const createReservationInvoice = async ({ invoiceId, couponCode, plan, mi
156
169
  ...withCoupon.val,
157
170
  metadata: {
158
171
  ...withCoupon.val.metadata,
159
- couponCode: finalCouponCode
172
+ couponCode: finalCouponCode,
160
173
  },
161
174
  providerMetadata: {
162
175
  ...withCoupon.val.providerMetadata,
163
- couponCode: finalCouponCode
164
- }
176
+ couponCode: finalCouponCode,
177
+ },
165
178
  };
166
179
  }
167
180
  }
168
181
  return await wrapInResult(biller.createInvoice(invoiceCreateDetails));
169
182
  };
170
- const getInvoiceCreateDetails = ({ invoiceId, payer, date, reservationDiscountId, vehicle, plan, mileagePackage, couponCode, paymentIntentId, freeReservation, freeReservationReason, analytics, reservationId })=>{
183
+ const getInvoiceCreateDetails = ({ invoiceId, payer, date, reservationDiscountId, vehicle, plan, mileagePackage, couponCode, paymentIntentId, freeReservation, freeReservationReason, analytics, reservationId, }) => {
171
184
  return {
172
185
  id: invoiceId || generateId('Invoice'),
173
186
  taxPercentage: 0,
@@ -183,13 +196,15 @@ const getInvoiceCreateDetails = ({ invoiceId, payer, date, reservationDiscountId
183
196
  description: 'Reservation fee for a FLUX subscription',
184
197
  footer: `Invoice for a FLUX subscription reservation. Due by ${format(date ?? new Date())}`,
185
198
  invoiceUrl: `${config.appUrl}/invoice-preview/${invoiceId}`,
186
- ...reservationDiscountId ? {
187
- discounts: {
188
- connect: {
189
- id: reservationDiscountId
190
- }
199
+ ...(reservationDiscountId
200
+ ? {
201
+ discounts: {
202
+ connect: {
203
+ id: reservationDiscountId,
204
+ },
205
+ },
191
206
  }
192
- } : {},
207
+ : {}),
193
208
  lines: [],
194
209
  subscriptionDetails: {
195
210
  vehicleId: vehicle.id,
@@ -198,7 +213,7 @@ const getInvoiceCreateDetails = ({ invoiceId, payer, date, reservationDiscountId
198
213
  variant: vehicle.variant,
199
214
  year: vehicle.year,
200
215
  plan: plan,
201
- mileagePackage: mileagePackage
216
+ mileagePackage: mileagePackage,
202
217
  },
203
218
  stripePaymentIntentId: paymentIntentId,
204
219
  providerMetadata: {
@@ -206,18 +221,14 @@ const getInvoiceCreateDetails = ({ invoiceId, payer, date, reservationDiscountId
206
221
  mileagePackage,
207
222
  vehicleId: vehicle.id,
208
223
  vehicleName: vehicleName(vehicle),
209
- ...reservationId ? {
210
- reservationId
211
- } : {},
212
- ...couponCode ? {
213
- couponCode
214
- } : {}
224
+ ...(reservationId ? { reservationId } : {}),
225
+ ...(couponCode ? { couponCode } : {}),
215
226
  },
216
227
  autoRetry: {
217
228
  enabled: false,
218
229
  interval: 0,
219
230
  maxAttempts: 0,
220
- attempts: 0
231
+ attempts: 0,
221
232
  },
222
233
  metadata: {
223
234
  purpose: PURPOSE_RESERVATION,
@@ -226,10 +237,11 @@ const getInvoiceCreateDetails = ({ invoiceId, payer, date, reservationDiscountId
226
237
  plan,
227
238
  mileagePackage,
228
239
  couponCode,
229
- ...freeReservation && freeReservationReason ? {
230
- freeReservationReason
231
- } : {},
232
- analytics
233
- }
240
+ ...(freeReservation && freeReservationReason
241
+ ? { freeReservationReason }
242
+ : {}),
243
+ analytics,
244
+ },
234
245
  };
235
246
  };
247
+ //# sourceMappingURL=invoice.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@driveflux/api-functions",
3
- "version": "0.0.7-next.33",
3
+ "version": "0.0.7-next.34",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./notion": {
@@ -73,9 +73,9 @@
73
73
  ],
74
74
  "dependencies": {
75
75
  "@casl/ability": "^6.7.3",
76
- "@driveflux/auth": "3.0.0-next.18",
76
+ "@driveflux/auth": "3.0.0-next.19",
77
77
  "@driveflux/billing": "7.0.0-develop.42",
78
- "@driveflux/config": "2.1.0-next.4",
78
+ "@driveflux/config": "2.1.0-next.5",
79
79
  "@driveflux/coupon": "8.0.0-develop.61",
80
80
  "@driveflux/db": "3.0.0-develop.74",
81
81
  "@driveflux/email": "6.0.0-develop.55",