fetch messages when switching to free plan

This commit is contained in:
m5r 2021-10-15 21:57:56 +02:00
parent 526ab4e451
commit dad61ae77e
2 changed files with 28 additions and 18 deletions

View File

@ -4,8 +4,6 @@ import twilio from "twilio";
import db from "../../../db"; import db from "../../../db";
import getCurrentUser from "../../users/queries/get-current-user"; import getCurrentUser from "../../users/queries/get-current-user";
import fetchMessagesQueue from "../../messages/api/queue/fetch-messages";
import fetchCallsQueue from "../../phone-calls/api/queue/fetch-calls";
import setTwilioWebhooks from "../api/queue/set-twilio-webhooks"; import setTwilioWebhooks from "../api/queue/set-twilio-webhooks";
const Body = z.object({ const Body = z.object({
@ -46,22 +44,6 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({
const phoneNumberId = phoneNumberSid; const phoneNumberId = phoneNumberSid;
await Promise.all([ 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}` },
),
setTwilioWebhooks.enqueue( setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId }, { organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` }, { id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },

View File

@ -7,6 +7,9 @@ import appLogger from "integrations/logger";
import { sendEmail } from "integrations/aws-ses"; import { sendEmail } from "integrations/aws-ses";
import type { Metadata } from "integrations/paddle"; 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 fetchCallsQueue from "../../../phone-calls/api/queue/fetch-calls";
import setTwilioWebhooks from "../../../onboarding/api/queue/set-twilio-webhooks";
const logger = appLogger.child({ queue: "subscription-created" }); const logger = appLogger.child({ queue: "subscription-created" });
@ -19,6 +22,7 @@ export const subscriptionCreatedQueue = Queue<Payload>("api/queue/subscription-c
const organization = await db.organization.findFirst({ const organization = await db.organization.findFirst({
where: { id: organizationId }, where: { id: organizationId },
include: { include: {
phoneNumbers: true,
subscriptions: true, subscriptions: true,
memberships: { memberships: {
include: { user: true }, include: { user: true },
@ -48,6 +52,30 @@ export const subscriptionCreatedQueue = Queue<Payload>("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}` },
),
setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
),
]);
if (isReturningSubscriber) { if (isReturningSubscriber) {
sendEmail({ sendEmail({
subject: "Welcome back to Shellphone", subject: "Welcome back to Shellphone",