use Promise.all

pr/2623
CarinaWolli 2022-04-26 10:57:28 +02:00
parent 99ebf8508f
commit 22cd1a27a7
1 changed files with 32 additions and 24 deletions

View File

@ -750,38 +750,46 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
// Send Webhook call if hooked to BOOKING_CREATED & BOOKING_RESCHEDULED
const subscribers = await getSubscribers({
...subscriberOptions,
subscriptionType: SubscriptionType.WEBHOOK,
});
const subscriberPromises = [
getSubscribers({
...subscriberOptions,
subscriptionType: SubscriptionType.WEBHOOK,
}),
];
if (zapierAppInstalled) {
const zapierSubscribers = await getSubscribers({
...subscriberOptions,
subscriptionType: SubscriptionType.ZAPIER,
});
subscribers.push(...zapierSubscribers);
subscriberPromises.push(
getSubscribers({
...subscriberOptions,
subscriptionType: SubscriptionType.ZAPIER,
})
);
}
const allSubscribers = await Promise.all(subscriberPromises);
console.log("evt:", {
...evt,
metadata: reqBody.metadata,
});
const promises = subscribers.map((sub) =>
sendPayload(
eventTrigger,
new Date().toISOString(),
sub.subscriberUrl,
{
...evt,
rescheduleUid,
metadata: reqBody.metadata,
},
sub.subscriptionType,
sub.payloadTemplate
).catch((e) => {
console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
})
const promises = allSubscribers.map((subArray) =>
subArray.map((sub) =>
sendPayload(
eventTrigger,
new Date().toISOString(),
sub.subscriberUrl,
{
...evt,
rescheduleUid,
metadata: reqBody.metadata,
},
sub.subscriptionType,
sub.payloadTemplate
).catch((e) => {
console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
})
)
);
await Promise.all(promises);
// Avoid passing referencesToCreate with id unique constrain values