diff --git a/app/settings/api/queue/subscription-created.ts b/app/settings/api/queue/subscription-created.ts index 781fbca..bb0dc43 100644 --- a/app/settings/api/queue/subscription-created.ts +++ b/app/settings/api/queue/subscription-created.ts @@ -70,10 +70,6 @@ export const subscriptionCreatedQueue = Queue("api/queue/subscription-c { organizationId, phoneNumberId }, { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, ), - setTwilioWebhooks.enqueue( - { organizationId, phoneNumberId }, - { id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` }, - ), ]); if (isReturningSubscriber) { diff --git a/app/settings/mutations/set-phone-number.ts b/app/settings/mutations/set-phone-number.ts index 4b04df9..e7c551e 100644 --- a/app/settings/mutations/set-phone-number.ts +++ b/app/settings/mutations/set-phone-number.ts @@ -1,10 +1,13 @@ import { resolver } from "blitz"; import { z } from "zod"; import twilio from "twilio"; +import RestException from "twilio/lib/base/RestException"; import db from "db"; import getCurrentUser from "app/users/queries/get-current-user"; import setTwilioWebhooks from "../api/queue/set-twilio-webhooks"; +import fetchMessagesQueue from "../../messages/api/queue/fetch-messages"; +import fetchCallsQueue from "../../phone-calls/api/queue/fetch-calls"; const Body = z.object({ phoneNumberSid: z.string(), @@ -31,21 +34,53 @@ export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ }, }); + let newApiKey; const mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken); - const apiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" }); - await db.organization.update({ - where: { id: organizationId }, - data: { - twilioApiKey: apiKey.sid, - twilioApiSecret: apiKey.secret, - }, - }); + if (!organization.twilioApiKey) { + newApiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" }); + } else { + try { + await mainTwilioClient.keys.get(organization.twilioApiKey); + } catch (error) { + if (!(error instanceof RestException) || error.code !== 20404) { + throw error; + } + + // API key was not found, create a new one + newApiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" }); + } + } + if (newApiKey) { + await db.organization.update({ + where: { id: organizationId }, + data: { + twilioApiKey: newApiKey.sid, + twilioApiSecret: newApiKey.secret, + }, + }); + } const phoneNumberId = phoneNumberSid; - await Promise.all([ + let promises = [ setTwilioWebhooks.enqueue( { organizationId, phoneNumberId }, { id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` }, ), - ]); + ]; + + const hasActiveSubscription = organization.subscriptions.length > 0; + if (hasActiveSubscription) { + promises.push( + fetchMessagesQueue.enqueue( + { organizationId, phoneNumberId }, + { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, + ), + fetchCallsQueue.enqueue( + { organizationId, phoneNumberId }, + { id: `fetch-messages-${organizationId}-${phoneNumberId}` }, + ), + ); + } + + await Promise.all(promises); });