From dad61ae77ef8ebe87624d4bc88bb71c38161cefe Mon Sep 17 00:00:00 2001 From: m5r Date: Fri, 15 Oct 2021 21:57:56 +0200 Subject: [PATCH] fetch messages when switching to free plan --- app/onboarding/mutations/set-phone-number.ts | 18 ------------ .../api/queue/subscription-created.ts | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/onboarding/mutations/set-phone-number.ts b/app/onboarding/mutations/set-phone-number.ts index d6ece29..72ce9b4 100644 --- a/app/onboarding/mutations/set-phone-number.ts +++ b/app/onboarding/mutations/set-phone-number.ts @@ -4,8 +4,6 @@ import twilio from "twilio"; import db from "../../../db"; 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"; const Body = z.object({ @@ -46,22 +44,6 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ const phoneNumberId = phoneNumberSid; 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}` }, diff --git a/app/settings/api/queue/subscription-created.ts b/app/settings/api/queue/subscription-created.ts index ac6447a..871dfde 100644 --- a/app/settings/api/queue/subscription-created.ts +++ b/app/settings/api/queue/subscription-created.ts @@ -7,6 +7,9 @@ import appLogger from "integrations/logger"; import { sendEmail } from "integrations/aws-ses"; 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 "../../../onboarding/api/queue/set-twilio-webhooks"; const logger = appLogger.child({ queue: "subscription-created" }); @@ -19,6 +22,7 @@ export const subscriptionCreatedQueue = Queue("api/queue/subscription-c const organization = await db.organization.findFirst({ where: { id: organizationId }, include: { + phoneNumbers: true, subscriptions: true, memberships: { include: { user: true }, @@ -48,6 +52,30 @@ 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}` }, + ), + setTwilioWebhooks.enqueue( + { organizationId, phoneNumberId }, + { id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` }, + ), + ]); + if (isReturningSubscriber) { sendEmail({ subject: "Welcome back to Shellphone",