shellphone.app/app/settings/api/queue/subscription-cancelled.ts

45 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Queue } from "quirrel/blitz";
import type { PaddleSdkSubscriptionCancelledEvent } from "@devoxa/paddle-sdk";
import db from "db";
import appLogger from "integrations/logger";
import type { Metadata } from "integrations/paddle";
import { translateSubscriptionStatus } from "integrations/paddle";
const logger = appLogger.child({ queue: "subscription-cancelled" });
type Payload = {
event: PaddleSdkSubscriptionCancelledEvent<Metadata>;
};
export const subscriptionCancelledQueue = Queue<Payload>("api/queue/subscription-cancelled", async ({ event }) => {
const paddleSubscriptionId = event.subscriptionId;
const subscription = await db.subscription.findFirst({ where: { paddleSubscriptionId } });
if (!subscription) {
2021-10-20 17:25:07 +00:00
// user deleted their account, no need to update their subscription
return;
}
const lastEventTime = event.eventTime;
const isEventOlderThanLastUpdate = subscription.lastEventTime > lastEventTime;
if (isEventOlderThanLastUpdate) {
return;
}
await db.subscription.update({
where: { paddleSubscriptionId },
data: {
paddleSubscriptionId,
paddlePlanId: event.productId,
paddleCheckoutId: event.checkoutId,
status: translateSubscriptionStatus(event.status),
lastEventTime,
currency: event.currency,
unitPrice: event.unitPrice,
cancellationEffectiveDate: event.cancelledFrom,
},
});
});
export default subscriptionCancelledQueue;