From cd96e22990a0a777edd6da097a79f3dc31faf1f6 Mon Sep 17 00:00:00 2001 From: CarinaWolli Date: Tue, 26 Apr 2022 00:28:14 +0200 Subject: [PATCH] clean code --- apps/web/pages/api/cancel.ts | 50 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/apps/web/pages/api/cancel.ts b/apps/web/pages/api/cancel.ts index 767116c3a5..ffcb416c07 100644 --- a/apps/web/pages/api/cancel.ts +++ b/apps/web/pages/api/cancel.ts @@ -149,35 +149,39 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }, }); - const subscribers = await getSubscribers({ - ...subscriberOptions, - subscriptionType: SubscriptionType.WEBHOOK, - }); - //const allSubscribersPromises = [getSubscribers(subscriberOptions)] + const subscriberPromises = [ + getSubscribers({ + ...subscriberOptions, + subscriptionType: SubscriptionType.WEBHOOK, + }), + ]; if (zapierAppInstalled) { - const zapierSubscribers = await getSubscribers({ - ...subscriberOptions, - subscriptionType: SubscriptionType.ZAPIER, - }); - subscribers.push(...zapierSubscribers); - //allSubscribersPromises.push(getZapierSubscribers(subscriberOptions)); + subscriberPromises.push( + getSubscribers({ + ...subscriberOptions, + subscriptionType: SubscriptionType.ZAPIER, + }) + ); } - //await Promise.all(allSubscribersPromises) + const allSubscribers = await Promise.all(subscriberPromises); - const promises = subscribers.map((sub) => - sendPayload( - eventTrigger, - new Date().toISOString(), - sub.subscriberUrl, - evt, - 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, + sub.subscriptionType, + sub.payloadTemplate + ).catch((e) => { + console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e); + }) + ) ); + await Promise.all(promises); // by cancelling first, and blocking whilst doing so; we can ensure a cancel