@driveflux/api-functions 1.0.147 → 1.0.148
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 +24 -24
- package/dist/auth/consent.js +27 -22
- package/dist/auth/emails.js +13 -12
- package/dist/auth/formatter.js +5 -5
- package/dist/auth/otp.js +50 -66
- package/dist/auth/register.js +35 -48
- package/dist/auth/tokens.js +55 -58
- package/dist/auth/verifications.js +52 -53
- package/dist/constants.js +1 -0
- package/dist/create-logger.js +2 -1
- package/dist/mailjet/calls/manage-contacts-in-list.js +6 -5
- package/dist/mailjet/calls/manage-subscription-status.js +5 -4
- package/dist/mailjet/calls/request-service.js +6 -7
- package/dist/mailjet/refresh-email-preferences.js +12 -11
- package/dist/mailjet/set-contact.js +12 -11
- package/dist/mailjet/types.js +2 -1
- package/dist/mailjet/utils/convert-to-array.js +6 -8
- package/dist/mailjet/utils/extract-email-preferences.js +15 -14
- package/dist/mailjet/utils/lists.js +8 -7
- package/dist/mailjet/utils/update-email-references.js +15 -16
- package/dist/notion/client.js +19 -22
- package/dist/notion/helpful.js +9 -6
- package/dist/notion/schemas/block.js +48 -42
- package/dist/notion/schemas/common.js +14 -9
- package/dist/notion/schemas/database.js +60 -62
- package/dist/notion/schemas/emoji.js +2 -1
- package/dist/notion/schemas/file.js +9 -9
- package/dist/notion/schemas/kb.js +6 -5
- package/dist/notion/schemas/page.js +61 -72
- package/dist/notion/schemas/parent.js +5 -4
- package/dist/notion/schemas/user.js +19 -18
- package/dist/reservation/agree.js +3 -2
- package/dist/reservation/checks.js +4 -3
- package/dist/reservation/display-vehicle.js +83 -73
- package/dist/reservation/ensure-user-billing-address.js +11 -9
- package/dist/reservation/fetch-or-create.js +56 -49
- package/dist/reservation/invoice.js +88 -77
- package/dist/reservation/payer.js +6 -5
- package/dist/reservation/payment-intent-sync.js +6 -4
- package/dist/reservation/reserve.js +4 -3
- package/dist/reservation/types.js +2 -1
- package/dist/reservation/vehicle.js +16 -13
- package/dist/slack.js +29 -24
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +85 -81
- package/dist/validation.js.map +1 -1
- package/dist/vehicle/vehicle-pricing/constants.js +19 -22
- package/dist/vehicle/vehicle-pricing/index.js +42 -28
- package/dist/vehicle/vehicle-pricing/types.js +2 -1
- package/package.json +11 -11
|
@@ -1,75 +1,73 @@
|
|
|
1
1
|
import { config } from '@driveflux/config/backend';
|
|
2
|
-
import { prisma } from '@driveflux/db';
|
|
2
|
+
import { prisma, } from '@driveflux/db';
|
|
3
3
|
import { generateId } from '@driveflux/db/id';
|
|
4
4
|
import { createPricingController } from '@driveflux/db/models/vehicle';
|
|
5
|
-
import { getFluxExcessMileage, getFluxPiceMatrix, getHostExcessMileage, getHostsMatrix, getRecommendedMotorcycleDeposit, getStartFeeMatrix } from '../vehicle/vehicle-pricing/index.js';
|
|
6
|
-
export const fetchDisplayVehicle = async (displayVehicle)=>{
|
|
7
|
-
return typeof displayVehicle === 'string'
|
|
8
|
-
where: {
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
}) : displayVehicle;
|
|
5
|
+
import { getFluxExcessMileage, getFluxPiceMatrix, getHostExcessMileage, getHostsMatrix, getRecommendedMotorcycleDeposit, getStartFeeMatrix, } from '../vehicle/vehicle-pricing/index.js';
|
|
6
|
+
export const fetchDisplayVehicle = async (displayVehicle) => {
|
|
7
|
+
return typeof displayVehicle === 'string'
|
|
8
|
+
? await prisma.displayVehicle.findFirst({ where: { id: displayVehicle } })
|
|
9
|
+
: displayVehicle;
|
|
12
10
|
};
|
|
13
|
-
export const createVehicleFromDisplayVehicle = async (displayVehicle, selectedColor)=>{
|
|
11
|
+
export const createVehicleFromDisplayVehicle = async (displayVehicle, selectedColor) => {
|
|
14
12
|
const dv = await fetchDisplayVehicle(displayVehicle);
|
|
15
|
-
if (!dv || typeof dv !== 'object')
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
if (!dv || typeof dv !== 'object')
|
|
14
|
+
return;
|
|
15
|
+
if (!dv.details)
|
|
16
|
+
return;
|
|
17
|
+
const mainImage = dv.images.main.find((m) => m.variant === selectedColor)?.url;
|
|
18
18
|
const vehicleData = {
|
|
19
19
|
id: generateId('Vehicle'),
|
|
20
20
|
...transformBasicVehicleData(dv),
|
|
21
21
|
images: [
|
|
22
|
-
...mainImage ? [
|
|
23
|
-
|
|
24
|
-
] : [],
|
|
25
|
-
...transformImages(dv.images)
|
|
22
|
+
...(mainImage ? [transfromSingleImage(mainImage)] : []),
|
|
23
|
+
...transformImages(dv.images),
|
|
26
24
|
],
|
|
27
25
|
...transformBasePrices(dv.pricing),
|
|
28
26
|
pricing: transformPriceMatrix(dv.type, dv.pricing),
|
|
29
27
|
details: transformDetails(dv.details, selectedColor),
|
|
30
28
|
host: {
|
|
31
29
|
connect: {
|
|
32
|
-
id: dv.defaultHostId
|
|
33
|
-
}
|
|
30
|
+
id: dv.defaultHostId,
|
|
31
|
+
},
|
|
34
32
|
},
|
|
35
33
|
displayVehicle: {
|
|
36
34
|
connect: {
|
|
37
|
-
id: dv.id
|
|
38
|
-
}
|
|
39
|
-
}
|
|
35
|
+
id: dv.id,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
40
38
|
};
|
|
41
39
|
return await prisma.vehicle.create({
|
|
42
40
|
data: vehicleData,
|
|
43
41
|
include: {
|
|
44
|
-
host: true
|
|
45
|
-
}
|
|
42
|
+
host: true,
|
|
43
|
+
},
|
|
46
44
|
});
|
|
47
45
|
};
|
|
48
|
-
export const transfromVehicleDisplayToVehicle = async (displayVehicle, selectedColor)=>{
|
|
46
|
+
export const transfromVehicleDisplayToVehicle = async (displayVehicle, selectedColor) => {
|
|
49
47
|
const dv = await fetchDisplayVehicle(displayVehicle);
|
|
50
|
-
if (!dv || typeof dv !== 'object')
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
if (!dv || typeof dv !== 'object')
|
|
49
|
+
return;
|
|
50
|
+
if (!dv.details)
|
|
51
|
+
return;
|
|
52
|
+
const mainImage = dv.images.main.find((m) => m.variant === selectedColor)?.url;
|
|
53
53
|
return {
|
|
54
54
|
id: dv.id,
|
|
55
55
|
...transformBasicVehicleData(dv),
|
|
56
56
|
images: [
|
|
57
|
-
...mainImage ? [
|
|
58
|
-
|
|
59
|
-
] : [],
|
|
60
|
-
...transformImages(dv.images)
|
|
57
|
+
...(mainImage ? [transfromSingleImage(mainImage)] : []),
|
|
58
|
+
...transformImages(dv.images),
|
|
61
59
|
],
|
|
62
60
|
...transformBasePrices(dv.pricing),
|
|
63
61
|
pricing: transformPriceMatrix(dv.type, dv.pricing),
|
|
64
62
|
details: transformDetails(dv.details, selectedColor),
|
|
65
63
|
host: {
|
|
66
64
|
address: {
|
|
67
|
-
state: 'Kuala Lumpur'
|
|
68
|
-
}
|
|
69
|
-
}
|
|
65
|
+
state: 'Kuala Lumpur',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
70
68
|
};
|
|
71
69
|
};
|
|
72
|
-
export const transformBasicVehicleData = (dv)=>{
|
|
70
|
+
export const transformBasicVehicleData = (dv) => {
|
|
73
71
|
return {
|
|
74
72
|
registrationNumber: null,
|
|
75
73
|
featured: false,
|
|
@@ -87,20 +85,22 @@ export const transformBasicVehicleData = (dv)=>{
|
|
|
87
85
|
type: dv.type,
|
|
88
86
|
niceName: `${dv.make} ${dv.vehicleModel} ${dv.variant} ${dv.year}`,
|
|
89
87
|
niceNameShort: `${dv.make} ${dv.vehicleModel}`,
|
|
90
|
-
appliedCoupon: dv.appliedCoupon
|
|
88
|
+
appliedCoupon: dv.appliedCoupon,
|
|
91
89
|
};
|
|
92
90
|
};
|
|
93
|
-
export const transformImages = (images)=>{
|
|
94
|
-
const exterior = images.exterior[0]?.url
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
91
|
+
export const transformImages = (images) => {
|
|
92
|
+
const exterior = images.exterior[0]?.url
|
|
93
|
+
? transfromSingleImage(images.exterior[0].url, 'exterior')
|
|
94
|
+
: null;
|
|
95
|
+
const interior = images.interior[0]?.url
|
|
96
|
+
? transfromSingleImage(images.interior[0].url, 'interior')
|
|
97
|
+
: null;
|
|
98
|
+
const gallery = images.gallery
|
|
99
|
+
.map((i) => (i.url ? transfromSingleImage(i.url) : null))
|
|
100
|
+
.filter(Boolean);
|
|
101
|
+
return [exterior, interior, ...gallery].filter(Boolean);
|
|
102
102
|
};
|
|
103
|
-
const transfromSingleImage = (imageUrl, inspectionType)=>{
|
|
103
|
+
const transfromSingleImage = (imageUrl, inspectionType) => {
|
|
104
104
|
return {
|
|
105
105
|
default: imageUrl,
|
|
106
106
|
blurBase64: null,
|
|
@@ -111,20 +111,20 @@ const transfromSingleImage = (imageUrl, inspectionType)=>{
|
|
|
111
111
|
description: null,
|
|
112
112
|
inspection: !!inspectionType,
|
|
113
113
|
inspectionType: inspectionType || null,
|
|
114
|
-
notActualPhoto: true
|
|
114
|
+
notActualPhoto: true,
|
|
115
115
|
};
|
|
116
116
|
};
|
|
117
|
-
const transformBasePrices = (pricing)=>{
|
|
117
|
+
const transformBasePrices = (pricing) => {
|
|
118
118
|
return {
|
|
119
119
|
basePrice: pricing.matrix.plan36.lite,
|
|
120
120
|
basePricePlan1: pricing.matrix.plan1.lite,
|
|
121
121
|
basePricePlan12: pricing.matrix.plan12.lite,
|
|
122
122
|
basePricePlan24: pricing.matrix.plan24.lite,
|
|
123
123
|
basePricePlan36: pricing.matrix.plan36.lite,
|
|
124
|
-
basePricePlan60: pricing.matrix.plan60.lite
|
|
124
|
+
basePricePlan60: pricing.matrix.plan60.lite,
|
|
125
125
|
};
|
|
126
126
|
};
|
|
127
|
-
const transformPriceMatrix = (vehicleType, pricing)=>{
|
|
127
|
+
const transformPriceMatrix = (vehicleType, pricing) => {
|
|
128
128
|
const baseline = pricing?.matrix.plan36.lite;
|
|
129
129
|
const ultraTier = 'tier1';
|
|
130
130
|
const fluxPriceCoefficient = 25;
|
|
@@ -137,14 +137,18 @@ const transformPriceMatrix = (vehicleType, pricing)=>{
|
|
|
137
137
|
const { availablePlans, availableMileagePackages } = getNonZeroKeys(priceMatrix);
|
|
138
138
|
const pricingController = createPricingController({
|
|
139
139
|
coefficient: fluxPriceCoefficient,
|
|
140
|
-
vehicleType
|
|
140
|
+
vehicleType,
|
|
141
141
|
});
|
|
142
142
|
const startFeeMatrix = excludeServiceFeesFrom(getStartFeeMatrix(pricingController, baseline, flatPricing));
|
|
143
143
|
const hostMatrix = excludeServiceFeesFrom(getHostsMatrix(pricingController, baseline, ultraTier, flatPricing));
|
|
144
144
|
const hostExcessMileage = getHostExcessMileage(pricingController, baseline);
|
|
145
|
-
const fluxPriceMatrix = pricing.keepPricingIntact
|
|
145
|
+
const fluxPriceMatrix = pricing.keepPricingIntact
|
|
146
|
+
? priceMatrix
|
|
147
|
+
: excludeServiceFeesFrom(getFluxPiceMatrix(pricingController, vehicleType, baseline, hostMatrix, ultraTier, flatPricing));
|
|
146
148
|
const fluxExcessMileage = getFluxExcessMileage(pricingController, baseline);
|
|
147
|
-
const recommendedDeposit = pricing.refundableDeposit || vehicleType === 'motorcycle'
|
|
149
|
+
const recommendedDeposit = pricing.refundableDeposit || vehicleType === 'motorcycle'
|
|
150
|
+
? getRecommendedMotorcycleDeposit(baseline)
|
|
151
|
+
: null;
|
|
148
152
|
return {
|
|
149
153
|
tmv,
|
|
150
154
|
gfv,
|
|
@@ -163,31 +167,31 @@ const transformPriceMatrix = (vehicleType, pricing)=>{
|
|
|
163
167
|
excessMileage: fluxExcessMileage,
|
|
164
168
|
originalExcessMileage: fluxExcessMileage,
|
|
165
169
|
matrixComments: null,
|
|
166
|
-
add: null
|
|
170
|
+
add: null,
|
|
167
171
|
},
|
|
168
172
|
hostTake: {
|
|
169
173
|
matrix: hostMatrix,
|
|
170
174
|
originalMatrix: hostMatrix,
|
|
171
175
|
excessMileage: hostExcessMileage,
|
|
172
176
|
originalExcessMileage: hostExcessMileage,
|
|
173
|
-
matrixComments: null
|
|
177
|
+
matrixComments: null,
|
|
174
178
|
},
|
|
175
179
|
startFee: {
|
|
176
180
|
matrix: startFeeMatrix,
|
|
177
181
|
originalMatrix: startFeeMatrix,
|
|
178
|
-
matrixComments: null
|
|
179
|
-
}
|
|
182
|
+
matrixComments: null,
|
|
183
|
+
},
|
|
180
184
|
};
|
|
181
185
|
};
|
|
182
|
-
const getNonZeroKeys = (matrix)=>{
|
|
186
|
+
const getNonZeroKeys = (matrix) => {
|
|
183
187
|
const planKeys = new Set();
|
|
184
188
|
const mileageKeys = new Set();
|
|
185
|
-
for (const [plan, mileageMap] of Object.entries(matrix)){
|
|
189
|
+
for (const [plan, mileageMap] of Object.entries(matrix)) {
|
|
186
190
|
const entries = Object.entries(mileageMap ?? {});
|
|
187
|
-
const hasNonZero = entries.some(([_, value])=>(value ?? 0) > 0);
|
|
191
|
+
const hasNonZero = entries.some(([_, value]) => (value ?? 0) > 0);
|
|
188
192
|
if (hasNonZero) {
|
|
189
193
|
planKeys.add(plan);
|
|
190
|
-
for (const [mileage, value] of entries){
|
|
194
|
+
for (const [mileage, value] of entries) {
|
|
191
195
|
if ((value ?? 0) > 0) {
|
|
192
196
|
mileageKeys.add(mileage);
|
|
193
197
|
}
|
|
@@ -196,20 +200,20 @@ const getNonZeroKeys = (matrix)=>{
|
|
|
196
200
|
}
|
|
197
201
|
return {
|
|
198
202
|
availablePlans: Array.from(planKeys),
|
|
199
|
-
availableMileagePackages: Array.from(mileageKeys)
|
|
203
|
+
availableMileagePackages: Array.from(mileageKeys),
|
|
200
204
|
};
|
|
201
205
|
};
|
|
202
|
-
const _getInspection = ()=>{
|
|
206
|
+
const _getInspection = () => {
|
|
203
207
|
return {
|
|
204
208
|
exterior: 5,
|
|
205
209
|
interior: 5,
|
|
206
210
|
engine: 5,
|
|
207
211
|
frame: 5,
|
|
208
212
|
total: 5,
|
|
209
|
-
comment: 'COMMENT HERE'
|
|
213
|
+
comment: 'COMMENT HERE',
|
|
210
214
|
};
|
|
211
215
|
};
|
|
212
|
-
const transformDetails = (details, selectColor)=>{
|
|
216
|
+
const transformDetails = (details, selectColor) => {
|
|
213
217
|
const color = selectColor || details.colors[0].name || '';
|
|
214
218
|
return {
|
|
215
219
|
numberOfSeats: details.numberOfSeats,
|
|
@@ -217,25 +221,31 @@ const transformDetails = (details, selectColor)=>{
|
|
|
217
221
|
bodyType: details.bodyType,
|
|
218
222
|
transmission: details.transmission,
|
|
219
223
|
engineType: details.fuelType,
|
|
220
|
-
engineCapacity: details.fuelType === 'electric'
|
|
224
|
+
engineCapacity: details.fuelType === 'electric'
|
|
225
|
+
? details.batteryCapacity
|
|
226
|
+
: details.engineCapacity,
|
|
221
227
|
drivetrain: details.drivetrain,
|
|
222
228
|
accelerationTo100km: details.accelerationTo100km,
|
|
223
229
|
fuelConsumptionLitersPer100Km: details.fuelConsumptionLitersPer100Km,
|
|
224
230
|
color,
|
|
225
231
|
features: details.features,
|
|
226
|
-
condition: 'new'
|
|
232
|
+
condition: 'new',
|
|
227
233
|
};
|
|
228
234
|
};
|
|
229
|
-
const excludeServiceFeesFrom = (originalMatrix)=>{
|
|
235
|
+
const excludeServiceFeesFrom = (originalMatrix) => {
|
|
230
236
|
const matrix = {};
|
|
231
|
-
for (const [planKey, planValue] of Object.entries(originalMatrix)){
|
|
232
|
-
if (!planValue)
|
|
233
|
-
|
|
234
|
-
|
|
237
|
+
for (const [planKey, planValue] of Object.entries(originalMatrix)) {
|
|
238
|
+
if (!planValue)
|
|
239
|
+
continue;
|
|
240
|
+
if (!matrix[planKey])
|
|
241
|
+
matrix[planKey] = {};
|
|
242
|
+
for (const [mileageKey, price] of Object.entries(planValue)) {
|
|
235
243
|
if (typeof price === 'number') {
|
|
236
|
-
matrix[planKey][mileageKey] =
|
|
244
|
+
matrix[planKey][mileageKey] =
|
|
245
|
+
price / (1 + config.taxRate);
|
|
237
246
|
}
|
|
238
247
|
}
|
|
239
248
|
}
|
|
240
249
|
return matrix;
|
|
241
250
|
};
|
|
251
|
+
//# sourceMappingURL=display-vehicle.js.map
|
|
@@ -3,27 +3,29 @@ import { makeProblem, PROBLEM_INTERNAL } from '@driveflux/problem';
|
|
|
3
3
|
import { Err, Ok } from '@driveflux/result';
|
|
4
4
|
import { createScopedLogger } from '../create-logger.js';
|
|
5
5
|
const log = createScopedLogger('reservation:ensure-user-billing-address');
|
|
6
|
-
export const ensureUserBillingAddress = async (userOrBusiness, deliveryAddresses)=>{
|
|
6
|
+
export const ensureUserBillingAddress = async (userOrBusiness, deliveryAddresses) => {
|
|
7
7
|
if (userOrBusiness.addresses?.billing) {
|
|
8
8
|
return new Ok(userOrBusiness);
|
|
9
9
|
}
|
|
10
10
|
try {
|
|
11
11
|
const fullQuery = {
|
|
12
|
-
where: {
|
|
13
|
-
id: userOrBusiness.id
|
|
14
|
-
},
|
|
12
|
+
where: { id: userOrBusiness.id },
|
|
15
13
|
data: {
|
|
16
14
|
addresses: {
|
|
17
15
|
home: deliveryAddresses.home,
|
|
18
|
-
billing: deliveryAddresses.billing
|
|
19
|
-
}
|
|
20
|
-
}
|
|
16
|
+
billing: deliveryAddresses.billing,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
21
19
|
};
|
|
22
20
|
// Use the delivery addresses
|
|
23
|
-
const updatedUserOrBusiness = userOrBusiness.object === 'user'
|
|
21
|
+
const updatedUserOrBusiness = userOrBusiness.object === 'user'
|
|
22
|
+
? await prisma.user.update(fullQuery)
|
|
23
|
+
: await prisma.business.update(fullQuery);
|
|
24
24
|
return new Ok(updatedUserOrBusiness);
|
|
25
|
-
}
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
26
27
|
log.error(error, 'Failed to ensure user billing address');
|
|
27
28
|
return new Err(makeProblem(PROBLEM_INTERNAL, 'Failed to ensure user billing address'));
|
|
28
29
|
}
|
|
29
30
|
};
|
|
31
|
+
//# sourceMappingURL=ensure-user-billing-address.js.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { prisma } from '@driveflux/db';
|
|
1
|
+
import { prisma, } from '@driveflux/db';
|
|
2
2
|
import { generateId } from '@driveflux/db/id';
|
|
3
3
|
import { makeProblem, PROBLEM_UNKNOWN } from '@driveflux/problem';
|
|
4
4
|
import { reportError } from '@driveflux/reporter';
|
|
5
5
|
import { Err, Ok } from '@driveflux/result';
|
|
6
6
|
import { slackLater } from '../slack.js';
|
|
7
|
-
import { createReservationInvoice, updateReservationInvoiceIfNeeded } from './invoice.js';
|
|
8
|
-
export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
9
|
-
const { subscribingUser, plan, mileagePackage, couponCode, referralCode, asBusiness, source, deliveryAddresses, deliveryDate, analytics, paymentIntentId, freeReservation, freeReservationReason, fullBooking } = body;
|
|
7
|
+
import { createReservationInvoice, updateReservationInvoiceIfNeeded, } from './invoice.js';
|
|
8
|
+
export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
|
|
9
|
+
const { subscribingUser, plan, mileagePackage, couponCode, referralCode, asBusiness, source, deliveryAddresses, deliveryDate, analytics, paymentIntentId, freeReservation, freeReservationReason, fullBooking, } = body;
|
|
10
10
|
try {
|
|
11
11
|
const previousReservation = await prisma.subscriptionReservation.findFirst({
|
|
12
12
|
where: {
|
|
@@ -20,14 +20,14 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
20
20
|
source,
|
|
21
21
|
// If the subscription ID was populated, this means that this reservation was consumed into a subsription,
|
|
22
22
|
// so we shouldn't re-use it
|
|
23
|
-
subscription: null
|
|
23
|
+
subscription: null,
|
|
24
24
|
},
|
|
25
25
|
orderBy: {
|
|
26
|
-
updatedAt: 'desc'
|
|
26
|
+
updatedAt: 'desc',
|
|
27
27
|
},
|
|
28
28
|
include: {
|
|
29
|
-
invoice: true
|
|
30
|
-
}
|
|
29
|
+
invoice: true,
|
|
30
|
+
},
|
|
31
31
|
});
|
|
32
32
|
if (previousReservation) {
|
|
33
33
|
if (previousReservation.invoice.voided) {
|
|
@@ -40,7 +40,7 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
40
40
|
'partiallyPaid',
|
|
41
41
|
'partiallyRefunded',
|
|
42
42
|
'refunded',
|
|
43
|
-
'pendingRefund'
|
|
43
|
+
'pendingRefund',
|
|
44
44
|
];
|
|
45
45
|
if (wrongStatuses.includes(previousReservation.invoice.status)) {
|
|
46
46
|
const updatedReservation = await recreateReservationInvoiceAndUpdateReservation(previousReservation, body, vehicle, payer);
|
|
@@ -53,16 +53,16 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
53
53
|
text: {
|
|
54
54
|
type: 'plain_text',
|
|
55
55
|
text: '‼️ Reservation invoice in wrong status',
|
|
56
|
-
emoji: true
|
|
57
|
-
}
|
|
56
|
+
emoji: true,
|
|
57
|
+
},
|
|
58
58
|
},
|
|
59
59
|
{
|
|
60
60
|
type: 'section',
|
|
61
61
|
text: {
|
|
62
62
|
type: 'plain_text',
|
|
63
|
-
text: `The reservation invoice ${previousReservation.invoice.id} was in a wrong status (${previousReservation.invoice.status}), it was discarded and a new invoice (${updatedReservation.val.id}) was created, however, investigate the issue
|
|
64
|
-
}
|
|
65
|
-
}
|
|
63
|
+
text: `The reservation invoice ${previousReservation.invoice.id} was in a wrong status (${previousReservation.invoice.status}), it was discarded and a new invoice (${updatedReservation.val.id}) was created, however, investigate the issue.`,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
66
|
]);
|
|
67
67
|
return updatedReservation;
|
|
68
68
|
}
|
|
@@ -74,7 +74,8 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
74
74
|
// Otherwise, we need to create a new reservation
|
|
75
75
|
return new Ok(previousReservation);
|
|
76
76
|
}
|
|
77
|
-
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
78
79
|
// We couldn't fetch the reservation, so we create a new one and just log this
|
|
79
80
|
await reportError(error);
|
|
80
81
|
}
|
|
@@ -95,7 +96,7 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
95
96
|
vehicle,
|
|
96
97
|
payer,
|
|
97
98
|
subscribingUser,
|
|
98
|
-
fullBooking
|
|
99
|
+
fullBooking,
|
|
99
100
|
});
|
|
100
101
|
if (invoiceResult.err) {
|
|
101
102
|
return invoiceResult;
|
|
@@ -114,10 +115,11 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
114
115
|
deliveryAddresses,
|
|
115
116
|
deliveryDate,
|
|
116
117
|
asBusiness,
|
|
117
|
-
source
|
|
118
|
+
source,
|
|
118
119
|
});
|
|
119
120
|
return new Ok(reservation);
|
|
120
|
-
}
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
121
123
|
await reportError(e);
|
|
122
124
|
if (typeof e === 'object' && e && 'message' in e && e.message) {
|
|
123
125
|
return new Err(makeProblem(PROBLEM_UNKNOWN, e.message));
|
|
@@ -125,62 +127,66 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
|
|
|
125
127
|
return new Err(makeProblem(PROBLEM_UNKNOWN, 'Unknown error'));
|
|
126
128
|
}
|
|
127
129
|
};
|
|
128
|
-
const createReservation = async ({ reservationId, invoiceId, subscribingUserId, vehicleId, businessId, plan, mileagePackage, couponCode, referralCode, analytics, deliveryAddresses, deliveryDate, asBusiness, source })=>{
|
|
130
|
+
const createReservation = async ({ reservationId, invoiceId, subscribingUserId, vehicleId, businessId, plan, mileagePackage, couponCode, referralCode, analytics, deliveryAddresses, deliveryDate, asBusiness, source, }) => {
|
|
129
131
|
return await prisma.subscriptionReservation.create({
|
|
130
132
|
data: {
|
|
131
133
|
id: reservationId,
|
|
132
134
|
invoice: {
|
|
133
135
|
connect: {
|
|
134
|
-
id: invoiceId
|
|
135
|
-
}
|
|
136
|
+
id: invoiceId,
|
|
137
|
+
},
|
|
136
138
|
},
|
|
137
139
|
user: {
|
|
138
140
|
connect: {
|
|
139
|
-
id: subscribingUserId
|
|
140
|
-
}
|
|
141
|
+
id: subscribingUserId,
|
|
142
|
+
},
|
|
141
143
|
},
|
|
142
144
|
vehicle: {
|
|
143
145
|
connect: {
|
|
144
|
-
id: vehicleId
|
|
145
|
-
}
|
|
146
|
+
id: vehicleId,
|
|
147
|
+
},
|
|
146
148
|
},
|
|
147
|
-
...businessId
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
149
|
+
...(businessId
|
|
150
|
+
? {
|
|
151
|
+
business: {
|
|
152
|
+
connect: {
|
|
153
|
+
id: businessId,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
152
156
|
}
|
|
153
|
-
|
|
157
|
+
: undefined),
|
|
154
158
|
plan,
|
|
155
159
|
mileagePackage,
|
|
156
160
|
couponCode: couponCode,
|
|
157
161
|
deliveryAddresses: deliveryAddresses,
|
|
158
162
|
deliveryDate: deliveryDate,
|
|
159
|
-
...referralCode
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
163
|
+
...(referralCode
|
|
164
|
+
? {
|
|
165
|
+
referral: {
|
|
166
|
+
connect: {
|
|
167
|
+
id: referralCode,
|
|
168
|
+
},
|
|
169
|
+
},
|
|
164
170
|
}
|
|
165
|
-
|
|
171
|
+
: undefined),
|
|
166
172
|
asBusiness: !!asBusiness,
|
|
167
173
|
source,
|
|
168
174
|
metadata: {
|
|
169
|
-
analytics
|
|
170
|
-
}
|
|
175
|
+
analytics,
|
|
176
|
+
},
|
|
171
177
|
},
|
|
172
178
|
include: {
|
|
173
|
-
invoice: true
|
|
174
|
-
}
|
|
179
|
+
invoice: true,
|
|
180
|
+
},
|
|
175
181
|
});
|
|
176
182
|
};
|
|
177
|
-
const recreateReservationInvoiceAndUpdateReservation = async (previousReservation, body, vehicle, payer)=>{
|
|
183
|
+
const recreateReservationInvoiceAndUpdateReservation = async (previousReservation, body, vehicle, payer) => {
|
|
178
184
|
const invoiceResult = await createReservationInvoice({
|
|
179
185
|
...body,
|
|
180
186
|
invoiceId: generateId('Invoice'),
|
|
181
187
|
reservationId: previousReservation.id,
|
|
182
188
|
vehicle,
|
|
183
|
-
payer
|
|
189
|
+
payer,
|
|
184
190
|
});
|
|
185
191
|
if (!invoiceResult.ok) {
|
|
186
192
|
return invoiceResult;
|
|
@@ -188,18 +194,19 @@ const recreateReservationInvoiceAndUpdateReservation = async (previousReservatio
|
|
|
188
194
|
const invoice = invoiceResult.val;
|
|
189
195
|
const updatedReservation = await prisma.subscriptionReservation.update({
|
|
190
196
|
where: {
|
|
191
|
-
id: previousReservation.id
|
|
197
|
+
id: previousReservation.id,
|
|
192
198
|
},
|
|
193
199
|
data: {
|
|
194
200
|
invoice: {
|
|
195
201
|
connect: {
|
|
196
|
-
id: invoice.id
|
|
197
|
-
}
|
|
198
|
-
}
|
|
202
|
+
id: invoice.id,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
199
205
|
},
|
|
200
206
|
include: {
|
|
201
|
-
invoice: true
|
|
202
|
-
}
|
|
207
|
+
invoice: true,
|
|
208
|
+
},
|
|
203
209
|
});
|
|
204
210
|
return new Ok(updatedReservation);
|
|
205
211
|
};
|
|
212
|
+
//# sourceMappingURL=fetch-or-create.js.map
|