From 4ce3c2503a7116b9d4bbcaacc16f08f6761f7e13 Mon Sep 17 00:00:00 2001 From: m5r Date: Wed, 20 Oct 2021 19:25:07 +0200 Subject: [PATCH] smol fixes to subscription webhooks --- .../api/queue/subscription-cancelled.ts | 4 +- .../api/queue/subscription-created.ts | 42 ++++++++++--------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/settings/api/queue/subscription-cancelled.ts b/app/settings/api/queue/subscription-cancelled.ts index 64bd10a..2f6f044 100644 --- a/app/settings/api/queue/subscription-cancelled.ts +++ b/app/settings/api/queue/subscription-cancelled.ts @@ -1,4 +1,3 @@ -import { NotFoundError } from "blitz"; import { Queue } from "quirrel/blitz"; import type { PaddleSdkSubscriptionCancelledEvent } from "@devoxa/paddle-sdk"; @@ -17,7 +16,8 @@ export const subscriptionCancelledQueue = Queue("api/queue/subscription const paddleSubscriptionId = event.subscriptionId; const subscription = await db.subscription.findFirst({ where: { paddleSubscriptionId } }); if (!subscription) { - throw new NotFoundError(); + // user deleted their account, no need to update their subscription + return; } const lastEventTime = event.eventTime; diff --git a/app/settings/api/queue/subscription-created.ts b/app/settings/api/queue/subscription-created.ts index bb0dc43..6743872 100644 --- a/app/settings/api/queue/subscription-created.ts +++ b/app/settings/api/queue/subscription-created.ts @@ -9,7 +9,6 @@ import type { Metadata } from "integrations/paddle"; import { translateSubscriptionStatus } from "integrations/paddle"; import fetchMessagesQueue from "../../../messages/api/queue/fetch-messages"; import fetchCallsQueue from "../../../phone-calls/api/queue/fetch-calls"; -import setTwilioWebhooks from "./set-twilio-webhooks"; const logger = appLogger.child({ queue: "subscription-created" }); @@ -52,25 +51,28 @@ export const subscriptionCreatedQueue = Queue("api/queue/subscription-c }, }); - const phoneNumberId = organization.phoneNumbers[0]!.id; - await Promise.all([ - db.processingPhoneNumber.create({ - data: { - organizationId, - phoneNumberId, - hasFetchedMessages: false, - hasFetchedCalls: false, - }, - }), - fetchMessagesQueue.enqueue( - { organizationId, phoneNumberId }, - { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, - ), - fetchCallsQueue.enqueue( - { organizationId, phoneNumberId }, - { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, - ), - ]); + const phoneNumber = organization.phoneNumbers[0]; + if (phoneNumber) { + const phoneNumberId = phoneNumber.id; + await Promise.all([ + db.processingPhoneNumber.create({ + data: { + organizationId, + phoneNumberId, + hasFetchedMessages: false, + hasFetchedCalls: false, + }, + }), + fetchMessagesQueue.enqueue( + { organizationId, phoneNumberId }, + { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, + ), + fetchCallsQueue.enqueue( + { organizationId, phoneNumberId }, + { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, + ), + ]); + } if (isReturningSubscriber) { sendEmail({