smol fixes to subscription webhooks

This commit is contained in:
m5r 2021-10-20 19:25:07 +02:00
parent 4096cfaa6c
commit 4ce3c2503a
2 changed files with 24 additions and 22 deletions

View File

@ -1,4 +1,3 @@
import { NotFoundError } from "blitz";
import { Queue } from "quirrel/blitz"; import { Queue } from "quirrel/blitz";
import type { PaddleSdkSubscriptionCancelledEvent } from "@devoxa/paddle-sdk"; import type { PaddleSdkSubscriptionCancelledEvent } from "@devoxa/paddle-sdk";
@ -17,7 +16,8 @@ export const subscriptionCancelledQueue = Queue<Payload>("api/queue/subscription
const paddleSubscriptionId = event.subscriptionId; const paddleSubscriptionId = event.subscriptionId;
const subscription = await db.subscription.findFirst({ where: { paddleSubscriptionId } }); const subscription = await db.subscription.findFirst({ where: { paddleSubscriptionId } });
if (!subscription) { if (!subscription) {
throw new NotFoundError(); // user deleted their account, no need to update their subscription
return;
} }
const lastEventTime = event.eventTime; const lastEventTime = event.eventTime;

View File

@ -9,7 +9,6 @@ import type { Metadata } from "integrations/paddle";
import { translateSubscriptionStatus } from "integrations/paddle"; import { translateSubscriptionStatus } from "integrations/paddle";
import fetchMessagesQueue from "../../../messages/api/queue/fetch-messages"; import fetchMessagesQueue from "../../../messages/api/queue/fetch-messages";
import fetchCallsQueue from "../../../phone-calls/api/queue/fetch-calls"; import fetchCallsQueue from "../../../phone-calls/api/queue/fetch-calls";
import setTwilioWebhooks from "./set-twilio-webhooks";
const logger = appLogger.child({ queue: "subscription-created" }); const logger = appLogger.child({ queue: "subscription-created" });
@ -52,25 +51,28 @@ export const subscriptionCreatedQueue = Queue<Payload>("api/queue/subscription-c
}, },
}); });
const phoneNumberId = organization.phoneNumbers[0]!.id; const phoneNumber = organization.phoneNumbers[0];
await Promise.all([ if (phoneNumber) {
db.processingPhoneNumber.create({ const phoneNumberId = phoneNumber.id;
data: { await Promise.all([
organizationId, db.processingPhoneNumber.create({
phoneNumberId, data: {
hasFetchedMessages: false, organizationId,
hasFetchedCalls: false, phoneNumberId,
}, hasFetchedMessages: false,
}), hasFetchedCalls: false,
fetchMessagesQueue.enqueue( },
{ organizationId, phoneNumberId }, }),
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` }, fetchMessagesQueue.enqueue(
), { organizationId, phoneNumberId },
fetchCallsQueue.enqueue( { id: `fetch-messages-${organizationId}-${phoneNumberId}` },
{ organizationId, phoneNumberId }, ),
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` }, fetchCallsQueue.enqueue(
), { organizationId, phoneNumberId },
]); { id: `fetch-messages-${organizationId}-${phoneNumberId}` },
),
]);
}
if (isReturningSubscriber) { if (isReturningSubscriber) {
sendEmail({ sendEmail({