2021-10-01 21:04:12 +00:00
|
|
|
import { NotFoundError } from "blitz";
|
|
|
|
import { Queue } from "quirrel/blitz";
|
|
|
|
import type { PaddleSdkSubscriptionCreatedEvent } from "@devoxa/paddle-sdk";
|
|
|
|
|
|
|
|
import db, { MembershipRole } from "db";
|
|
|
|
import appLogger from "integrations/logger";
|
2021-10-13 19:42:23 +00:00
|
|
|
import { sendEmail } from "integrations/aws-ses";
|
2021-10-03 18:56:31 +00:00
|
|
|
import type { Metadata } from "integrations/paddle";
|
2021-10-01 21:04:12 +00:00
|
|
|
import { translateSubscriptionStatus } from "integrations/paddle";
|
2021-10-20 22:39:09 +00:00
|
|
|
import fetchMessagesQueue from "app/messages/api/queue/fetch-messages";
|
|
|
|
import fetchCallsQueue from "app/phone-calls/api/queue/fetch-calls";
|
2021-10-01 21:04:12 +00:00
|
|
|
|
|
|
|
const logger = appLogger.child({ queue: "subscription-created" });
|
|
|
|
|
|
|
|
type Payload = {
|
2021-10-03 18:56:31 +00:00
|
|
|
event: PaddleSdkSubscriptionCreatedEvent<Metadata>;
|
2021-10-01 21:04:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const subscriptionCreatedQueue = Queue<Payload>("api/queue/subscription-created", async ({ event }) => {
|
|
|
|
const { organizationId } = event.metadata;
|
|
|
|
const organization = await db.organization.findFirst({
|
|
|
|
where: { id: organizationId },
|
|
|
|
include: {
|
2021-10-15 19:57:56 +00:00
|
|
|
phoneNumbers: true,
|
2021-10-03 16:19:45 +00:00
|
|
|
subscriptions: true,
|
2021-10-01 21:04:12 +00:00
|
|
|
memberships: {
|
|
|
|
include: { user: true },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!organization) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
|
|
|
|
2021-10-03 16:19:45 +00:00
|
|
|
const isReturningSubscriber = organization.subscriptions.length > 0;
|
2021-10-01 21:04:12 +00:00
|
|
|
const orgOwner = organization.memberships.find((membership) => membership.role === MembershipRole.OWNER);
|
|
|
|
const email = orgOwner!.user!.email;
|
2021-10-03 16:19:45 +00:00
|
|
|
await db.subscription.create({
|
2021-10-01 22:19:06 +00:00
|
|
|
data: {
|
2021-10-03 16:19:45 +00:00
|
|
|
organizationId,
|
|
|
|
paddleSubscriptionId: event.subscriptionId,
|
|
|
|
paddlePlanId: event.productId,
|
|
|
|
paddleCheckoutId: event.checkoutId,
|
|
|
|
nextBillDate: event.nextPaymentDate,
|
|
|
|
status: translateSubscriptionStatus(event.status),
|
|
|
|
lastEventTime: event.eventTime,
|
|
|
|
updateUrl: event.updateUrl,
|
|
|
|
cancelUrl: event.cancelUrl,
|
|
|
|
currency: event.currency,
|
|
|
|
unitPrice: event.unitPrice,
|
2021-10-01 22:19:06 +00:00
|
|
|
},
|
|
|
|
});
|
2021-10-01 21:04:12 +00:00
|
|
|
|
2021-10-20 22:39:09 +00:00
|
|
|
// fetch dismissed messages and phone calls that might have happened while on free plan
|
|
|
|
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}` },
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-10-03 16:19:45 +00:00
|
|
|
if (isReturningSubscriber) {
|
2021-10-01 21:04:12 +00:00
|
|
|
sendEmail({
|
|
|
|
subject: "Welcome back to Shellphone",
|
|
|
|
body: "Welcome back to Shellphone",
|
|
|
|
recipients: [email],
|
|
|
|
}).catch((error) => {
|
|
|
|
logger.error(error);
|
|
|
|
});
|
2021-10-03 16:19:45 +00:00
|
|
|
|
|
|
|
return;
|
2021-10-01 21:04:12 +00:00
|
|
|
}
|
2021-10-03 16:19:45 +00:00
|
|
|
|
|
|
|
sendEmail({
|
|
|
|
subject: "Welcome to Shellphone",
|
|
|
|
body: `Welcome to Shellphone`,
|
|
|
|
recipients: [email],
|
|
|
|
}).catch((error) => {
|
|
|
|
logger.error(error);
|
|
|
|
});
|
2021-10-01 21:04:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default subscriptionCreatedQueue;
|