@driveflux/api-functions 0.0.7-next.11 → 0.0.7-next.12

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.
@@ -1,11 +1,11 @@
1
- import { prisma, } from '@driveflux/db';
1
+ import { prisma } from '@driveflux/db';
2
2
  import { generateId } from '@driveflux/db/id';
3
3
  import { reportError } from '@driveflux/reporter';
4
4
  import { Ok } from '@driveflux/result';
5
5
  import { slackLater } from '../slack.js';
6
- import { createReservationInvoice, updateReservationInvoiceIfNeeded, } from './invoice.js';
7
- export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
8
- const { subscribingUser, plan, mileagePackage, couponCode, referralCode, asBusiness, source, deliveryAddresses, deliveryDate, analytics, paymentIntentId, freeReservation, freeReservationReason, invoiceId, } = body;
6
+ import { createReservationInvoice, updateReservationInvoiceIfNeeded } from './invoice.js';
7
+ export const fetchOrCreateReservation = async ({ vehicle, payer, body })=>{
8
+ const { subscribingUser, plan, mileagePackage, couponCode, referralCode, asBusiness, source, deliveryAddresses, deliveryDate, analytics, paymentIntentId, freeReservation, freeReservationReason, invoiceId } = body;
9
9
  try {
10
10
  const previousReservation = await prisma.subscriptionReservation.findFirst({
11
11
  where: {
@@ -19,10 +19,10 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
19
19
  source,
20
20
  // If the subscription ID was populated, this means that this reservation was consumed into a subsription,
21
21
  // so we shouldn't re-use it
22
- subscription: null,
22
+ subscription: null
23
23
  },
24
24
  orderBy: {
25
- updatedAt: 'desc',
25
+ updatedAt: 'desc'
26
26
  },
27
27
  include: {
28
28
  invoice: {
@@ -37,14 +37,13 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
37
37
  status: true,
38
38
  lockedAt: true,
39
39
  unlockAt: true,
40
- locked: true,
41
- },
42
- },
43
- },
40
+ locked: true
41
+ }
42
+ }
43
+ }
44
44
  });
45
45
  if (previousReservation) {
46
- if (previousReservation.invoice.voided ||
47
- previousReservation.invoice.id !== invoiceId) {
46
+ if (previousReservation.invoice.voided || previousReservation.invoice.id !== invoiceId) {
48
47
  return recreateReservationInvoiceAndUpdateReservation(previousReservation, body, vehicle, payer);
49
48
  }
50
49
  // If the invoice is paid or partially paid, this is a serious problem,
@@ -54,7 +53,7 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
54
53
  'partiallyPaid',
55
54
  'partiallyRefunded',
56
55
  'refunded',
57
- 'pendingRefund',
56
+ 'pendingRefund'
58
57
  ];
59
58
  if (wrongStatuses.includes(previousReservation.invoice.status)) {
60
59
  const updatedReservation = await recreateReservationInvoiceAndUpdateReservation(previousReservation, body, vehicle, payer);
@@ -67,16 +66,16 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
67
66
  text: {
68
67
  type: 'plain_text',
69
68
  text: '‼️ Reservation invoice in wrong status',
70
- emoji: true,
71
- },
69
+ emoji: true
70
+ }
72
71
  },
73
72
  {
74
73
  type: 'section',
75
74
  text: {
76
75
  type: 'plain_text',
77
- 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.`,
78
- },
79
- },
76
+ 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.`
77
+ }
78
+ }
80
79
  ]);
81
80
  return updatedReservation;
82
81
  }
@@ -88,8 +87,7 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
88
87
  // Otherwise, we need to create a new reservation
89
88
  return new Ok(previousReservation);
90
89
  }
91
- }
92
- catch (error) {
90
+ } catch (error) {
93
91
  // We couldn't fetch the reservation, so we create a new one and just log this
94
92
  await reportError(error);
95
93
  }
@@ -108,7 +106,7 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
108
106
  analytics,
109
107
  vehicle,
110
108
  payer,
111
- subscribingUser,
109
+ subscribingUser
112
110
  });
113
111
  if (invoiceResult.err) {
114
112
  return invoiceResult;
@@ -127,66 +125,62 @@ export const fetchOrCreateReservation = async ({ vehicle, payer, body, }) => {
127
125
  deliveryAddresses,
128
126
  deliveryDate,
129
127
  asBusiness,
130
- source,
128
+ source
131
129
  });
132
130
  return new Ok(reservation);
133
131
  };
134
- const createReservation = async ({ reservationId, invoiceId, subscribingUserId, vehicleId, businessId, plan, mileagePackage, couponCode, referralCode, analytics, deliveryAddresses, deliveryDate, asBusiness, source, }) => {
132
+ const createReservation = async ({ reservationId, invoiceId, subscribingUserId, vehicleId, businessId, plan, mileagePackage, couponCode, referralCode, analytics, deliveryAddresses, deliveryDate, asBusiness, source })=>{
135
133
  return await prisma.subscriptionReservation.create({
136
134
  data: {
137
135
  id: reservationId,
138
136
  invoice: {
139
137
  connect: {
140
- id: invoiceId,
141
- },
138
+ id: invoiceId
139
+ }
142
140
  },
143
141
  user: {
144
142
  connect: {
145
- id: subscribingUserId,
146
- },
143
+ id: subscribingUserId
144
+ }
147
145
  },
148
146
  vehicle: {
149
147
  connect: {
150
- id: vehicleId,
151
- },
148
+ id: vehicleId
149
+ }
152
150
  },
153
- ...(businessId
154
- ? {
155
- business: {
156
- connect: {
157
- id: businessId,
158
- },
159
- },
151
+ ...businessId ? {
152
+ business: {
153
+ connect: {
154
+ id: businessId
155
+ }
160
156
  }
161
- : undefined),
157
+ } : undefined,
162
158
  plan,
163
159
  mileagePackage,
164
160
  couponCode: couponCode,
165
161
  deliveryAddresses: deliveryAddresses,
166
162
  deliveryDate: deliveryDate,
167
- ...(referralCode
168
- ? {
169
- referral: {
170
- connect: {
171
- id: referralCode,
172
- },
173
- },
163
+ ...referralCode ? {
164
+ referral: {
165
+ connect: {
166
+ id: referralCode
167
+ }
174
168
  }
175
- : undefined),
169
+ } : undefined,
176
170
  asBusiness: !!asBusiness,
177
171
  source,
178
172
  metadata: {
179
- analytics,
180
- },
181
- },
173
+ analytics
174
+ }
175
+ }
182
176
  });
183
177
  };
184
- const recreateReservationInvoiceAndUpdateReservation = async (previousReservation, { invoiceId, ...body }, vehicle, payer) => {
178
+ const recreateReservationInvoiceAndUpdateReservation = async (previousReservation, { invoiceId, ...body }, vehicle, payer)=>{
185
179
  const invoiceResult = await createReservationInvoice({
186
180
  ...body,
187
181
  reservationId: previousReservation.id,
188
182
  vehicle,
189
- payer,
183
+ payer
190
184
  });
191
185
  if (!invoiceResult.ok) {
192
186
  return invoiceResult;
@@ -194,16 +188,15 @@ const recreateReservationInvoiceAndUpdateReservation = async (previousReservatio
194
188
  const invoice = invoiceResult.val;
195
189
  const updatedReservation = await prisma.subscriptionReservation.update({
196
190
  where: {
197
- id: previousReservation.id,
191
+ id: previousReservation.id
198
192
  },
199
193
  data: {
200
194
  invoice: {
201
195
  connect: {
202
- id: invoice.id,
203
- },
204
- },
205
- },
196
+ id: invoice.id
197
+ }
198
+ }
199
+ }
206
200
  });
207
201
  return new Ok(updatedReservation);
208
202
  };
209
- //# sourceMappingURL=fetch-or-create.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@driveflux/api-functions",
3
- "version": "0.0.7-next.11",
3
+ "version": "0.0.7-next.12",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./notion": {
@@ -73,22 +73,22 @@
73
73
  ],
74
74
  "dependencies": {
75
75
  "@casl/ability": "^6.7.3",
76
- "@driveflux/auth": "3.0.0-next.5",
77
- "@driveflux/billing": "7.0.0-develop.38",
76
+ "@driveflux/auth": "3.0.0-next.6",
77
+ "@driveflux/billing": "7.0.0-develop.39",
78
78
  "@driveflux/config": "2.1.0-next.1",
79
- "@driveflux/coupon": "8.0.0-develop.46",
80
- "@driveflux/db": "3.0.0-develop.63",
81
- "@driveflux/email": "6.0.0-develop.44",
82
- "@driveflux/email-templates": "0.0.2-develop.47",
83
- "@driveflux/engine": "0.1.2-develop.73",
79
+ "@driveflux/coupon": "8.0.0-develop.47",
80
+ "@driveflux/db": "3.0.0-develop.64",
81
+ "@driveflux/email": "6.0.0-develop.45",
82
+ "@driveflux/email-templates": "0.0.2-develop.49",
83
+ "@driveflux/engine": "0.1.2-develop.75",
84
84
  "@driveflux/fetch": "7.0.3",
85
85
  "@driveflux/format-money": "6.0.2",
86
86
  "@driveflux/problem": "5.0.3",
87
87
  "@driveflux/reporter": "6.0.4-next.2",
88
88
  "@driveflux/result": "5.0.3",
89
- "@driveflux/scheduler": "7.0.0-develop.45",
89
+ "@driveflux/scheduler": "7.0.0-develop.46",
90
90
  "@driveflux/singleton": "2.0.2",
91
- "@driveflux/subscription": "8.0.0-develop.30",
91
+ "@driveflux/subscription": "8.0.0-develop.31",
92
92
  "@driveflux/time": "5.0.2",
93
93
  "@driveflux/utils": "5.0.2",
94
94
  "@notionhq/client": "^4.0.2",