@driveflux/api-functions 1.0.7 → 1.0.9
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 +29 -24
- 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 +34 -42
- package/dist/auth/tokens.js +55 -58
- package/dist/auth/verifications.js +52 -53
- package/dist/constants.js +1 -0
- 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 +7 -6
- package/dist/reservation/checks.js +4 -3
- package/dist/reservation/display-vehicle.js +71 -65
- package/dist/reservation/fetch-or-create.js +54 -48
- package/dist/reservation/invoice.js +74 -62
- package/dist/reservation/payer.js +6 -5
- 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.js +79 -77
- 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,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 &&
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
}
|
|
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
|
-
|
|
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 &&
|
|
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 &&
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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 &&
|
|
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
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
|
|
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
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { prisma } from '@driveflux/db';
|
|
2
|
-
import { makeProblem, PROBLEM_CONDITION_FAILED, PROBLEM_NOT_FOUND } from '@driveflux/problem';
|
|
2
|
+
import { makeProblem, PROBLEM_CONDITION_FAILED, PROBLEM_NOT_FOUND, } from '@driveflux/problem';
|
|
3
3
|
import { Err, Ok } from '@driveflux/result';
|
|
4
|
-
export const getPayer = async ({ subscribingUser, asBusiness })=>{
|
|
4
|
+
export const getPayer = async ({ subscribingUser, asBusiness, }) => {
|
|
5
5
|
let business = null;
|
|
6
6
|
let payer = subscribingUser;
|
|
7
7
|
if (asBusiness) {
|
|
@@ -11,8 +11,8 @@ export const getPayer = async ({ subscribingUser, asBusiness })=>{
|
|
|
11
11
|
// load business
|
|
12
12
|
business = await prisma.business.findUnique({
|
|
13
13
|
where: {
|
|
14
|
-
id: subscribingUser.businessId
|
|
15
|
-
}
|
|
14
|
+
id: subscribingUser.businessId,
|
|
15
|
+
},
|
|
16
16
|
});
|
|
17
17
|
if (!business) {
|
|
18
18
|
return new Err(makeProblem(PROBLEM_NOT_FOUND, `The business ${subscribingUser.businessId} was not found.`));
|
|
@@ -26,7 +26,7 @@ export const getPayer = async ({ subscribingUser, asBusiness })=>{
|
|
|
26
26
|
}
|
|
27
27
|
return new Ok(payer);
|
|
28
28
|
};
|
|
29
|
-
const checkPayerObjectSanity = (subscriber)=>{
|
|
29
|
+
const checkPayerObjectSanity = (subscriber) => {
|
|
30
30
|
if (!subscriber.email) {
|
|
31
31
|
return new Err(makeProblem(PROBLEM_CONDITION_FAILED, `Subscriber ${subscriber.id} has no email. Please add a verified email first.`));
|
|
32
32
|
}
|
|
@@ -35,3 +35,4 @@ const checkPayerObjectSanity = (subscriber)=>{
|
|
|
35
35
|
}
|
|
36
36
|
return new Ok(true);
|
|
37
37
|
};
|
|
38
|
+
//# sourceMappingURL=payer.js.map
|
|
@@ -4,14 +4,14 @@ import { checkIfUserCanReserve } from './checks.js';
|
|
|
4
4
|
import { fetchOrCreateReservation } from './fetch-or-create.js';
|
|
5
5
|
import { getPayer } from './payer.js';
|
|
6
6
|
import { getVehicle } from './vehicle.js';
|
|
7
|
-
const processBody = (body)=>{
|
|
7
|
+
const processBody = (body) => {
|
|
8
8
|
// Switch mileage package
|
|
9
9
|
if (body.plan === 'plan1' || body.plan === 'planWeekly') {
|
|
10
10
|
body.mileagePackage = 'unlimited';
|
|
11
11
|
}
|
|
12
12
|
return body;
|
|
13
13
|
};
|
|
14
|
-
export const reserveVehicle = async (vehicleId, unprocessed)=>{
|
|
14
|
+
export const reserveVehicle = async (vehicleId, unprocessed) => {
|
|
15
15
|
const body = processBody(unprocessed);
|
|
16
16
|
// Agree to terms if not done yet
|
|
17
17
|
await handleAgreeToTerms(body);
|
|
@@ -33,7 +33,7 @@ export const reserveVehicle = async (vehicleId, unprocessed)=>{
|
|
|
33
33
|
const reservationResult = await fetchOrCreateReservation({
|
|
34
34
|
vehicle,
|
|
35
35
|
payer,
|
|
36
|
-
body
|
|
36
|
+
body,
|
|
37
37
|
});
|
|
38
38
|
if (reservationResult.err) {
|
|
39
39
|
return reservationResult;
|
|
@@ -41,3 +41,4 @@ export const reserveVehicle = async (vehicleId, unprocessed)=>{
|
|
|
41
41
|
const reservation = reservationResult.val;
|
|
42
42
|
return new Ok(reservation);
|
|
43
43
|
};
|
|
44
|
+
//# sourceMappingURL=reserve.js.map
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -4,19 +4,21 @@ import { makeProblem, PROBLEM_NOT_FOUND } from '@driveflux/problem';
|
|
|
4
4
|
import { Err, Ok } from '@driveflux/result';
|
|
5
5
|
import { checkIfVehicleIsAvailableForReservation } from './checks.js';
|
|
6
6
|
import { createVehicleFromDisplayVehicle } from './display-vehicle.js';
|
|
7
|
-
export const getVehicle = async (id, { selectedColor, plan, requestUser }, requesterAbility)=>{
|
|
8
|
-
const vehicle = id.startsWith('DV_')
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
export const getVehicle = async (id, { selectedColor, plan, requestUser, }, requesterAbility) => {
|
|
8
|
+
const vehicle = id.startsWith('DV_')
|
|
9
|
+
? await createVehicleFromDisplayVehicle(id, selectedColor || undefined)
|
|
10
|
+
: await prisma.vehicle.findUnique({
|
|
11
|
+
where: {
|
|
12
|
+
id,
|
|
13
|
+
},
|
|
14
|
+
include: {
|
|
15
|
+
host: {
|
|
16
|
+
select: {
|
|
17
|
+
id: true,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
20
22
|
if (!vehicle) {
|
|
21
23
|
return new Err(makeProblem(PROBLEM_NOT_FOUND, 'Vehicle not found'));
|
|
22
24
|
}
|
|
@@ -27,3 +29,4 @@ export const getVehicle = async (id, { selectedColor, plan, requestUser }, reque
|
|
|
27
29
|
}
|
|
28
30
|
return new Ok(vehicle);
|
|
29
31
|
};
|
|
32
|
+
//# sourceMappingURL=vehicle.js.map
|
package/dist/slack.js
CHANGED
|
@@ -10,11 +10,12 @@ const slackBlocks = global.__slackBlocks;
|
|
|
10
10
|
/**
|
|
11
11
|
*
|
|
12
12
|
* @deprecated Use slackLater instead and commitSlack to commit
|
|
13
|
-
*/
|
|
13
|
+
*/
|
|
14
|
+
export const slack = async (message, channel = config.slack.defaultChannelId) => {
|
|
14
15
|
return await enhancedFetch('https://slack.com/api/chat.postMessage', {
|
|
15
16
|
method: 'POST',
|
|
16
17
|
headers: {
|
|
17
|
-
Authorization: `Bearer ${config.slack.token}
|
|
18
|
+
Authorization: `Bearer ${config.slack.token}`,
|
|
18
19
|
},
|
|
19
20
|
body: JSON.stringify({
|
|
20
21
|
channel,
|
|
@@ -23,20 +24,23 @@ const slackBlocks = global.__slackBlocks;
|
|
|
23
24
|
type: 'section',
|
|
24
25
|
text: {
|
|
25
26
|
type: 'mrkdwn',
|
|
26
|
-
text: message
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
]
|
|
30
|
-
})
|
|
27
|
+
text: message,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
}),
|
|
31
32
|
});
|
|
32
33
|
};
|
|
33
|
-
export const getSlackBlocks = (channel)=>channel ? slackBlocks.get(channel) : slackBlocks;
|
|
34
|
+
export const getSlackBlocks = (channel) => channel ? slackBlocks.get(channel) : slackBlocks;
|
|
34
35
|
/**
|
|
35
36
|
* Naive implementation, however, for now (uncrowded serverless env), this works
|
|
36
37
|
* @param blocks
|
|
37
38
|
* @param channel
|
|
38
|
-
*/
|
|
39
|
-
|
|
39
|
+
*/
|
|
40
|
+
export function slackLater(blocks, channel) {
|
|
41
|
+
const targetChannel = (config.appEnv === 'production'
|
|
42
|
+
? channel
|
|
43
|
+
: config.slack.defaultChannelId) || config.slack.defaultChannelId;
|
|
40
44
|
if (!targetChannel || !blocks) {
|
|
41
45
|
return;
|
|
42
46
|
}
|
|
@@ -46,13 +50,15 @@ export const getSlackBlocks = (channel)=>channel ? slackBlocks.get(channel) : sl
|
|
|
46
50
|
type: 'section',
|
|
47
51
|
text: {
|
|
48
52
|
type: 'mrkdwn',
|
|
49
|
-
text: blocks
|
|
50
|
-
}
|
|
53
|
+
text: blocks,
|
|
54
|
+
},
|
|
51
55
|
});
|
|
52
|
-
}
|
|
56
|
+
}
|
|
57
|
+
else if (!Array.isArray(blocks)) {
|
|
53
58
|
messages.add(blocks);
|
|
54
|
-
}
|
|
55
|
-
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
for (const b of blocks) {
|
|
56
62
|
messages.add(b);
|
|
57
63
|
}
|
|
58
64
|
}
|
|
@@ -63,22 +69,22 @@ export async function sendSlackMessages(channel, blocks) {
|
|
|
63
69
|
return await enhancedFetch('https://slack.com/api/chat.postMessage', {
|
|
64
70
|
method: 'POST',
|
|
65
71
|
headers: {
|
|
66
|
-
Authorization: `Bearer ${config.slack.token}
|
|
72
|
+
Authorization: `Bearer ${config.slack.token}`,
|
|
67
73
|
},
|
|
68
74
|
body: JSON.stringify({
|
|
69
75
|
channel: targetChannel,
|
|
70
|
-
blocks: Array.from(blocks)
|
|
71
|
-
})
|
|
76
|
+
blocks: Array.from(blocks),
|
|
77
|
+
}),
|
|
72
78
|
});
|
|
73
79
|
}
|
|
74
|
-
export const commitSlack = ()=>{
|
|
80
|
+
export const commitSlack = () => {
|
|
75
81
|
if (!slackBlocks.size) {
|
|
76
82
|
return;
|
|
77
83
|
}
|
|
78
84
|
const channels = slackBlocks.keys();
|
|
79
85
|
let i = 1;
|
|
80
86
|
const baseUrl = config.appEnv === 'development' ? config.tunnelUrl : config.appUrl;
|
|
81
|
-
for (const channel of channels){
|
|
87
|
+
for (const channel of channels) {
|
|
82
88
|
const blocks = slackBlocks.get(channel);
|
|
83
89
|
if (!blocks?.size) {
|
|
84
90
|
continue;
|
|
@@ -88,13 +94,12 @@ export const commitSlack = ()=>{
|
|
|
88
94
|
name: TASK_COMMIT_SLACK_CHANNEL,
|
|
89
95
|
metadata: {
|
|
90
96
|
channel,
|
|
91
|
-
blocks: [
|
|
92
|
-
...blocks
|
|
93
|
-
]
|
|
97
|
+
blocks: [...blocks],
|
|
94
98
|
},
|
|
95
99
|
taskHandlerUrl: `${baseUrl || config.appUrl}/api/hooks/slack-pipeline`,
|
|
96
|
-
scheduledAt: addSeconds(new Date(), i++)
|
|
100
|
+
scheduledAt: addSeconds(new Date(), i++),
|
|
97
101
|
});
|
|
98
102
|
}
|
|
99
103
|
slackBlocks.clear();
|
|
100
104
|
};
|
|
105
|
+
//# sourceMappingURL=slack.js.map
|