* avoid creating multiple api keys when not necessary

* fetch messages and calls if user switches phone number while on an active sub
This commit is contained in:
m5r 2021-10-19 23:12:16 +02:00
parent f11c7d3723
commit 29d24f9fb4
2 changed files with 45 additions and 14 deletions

View File

@ -70,10 +70,6 @@ export const subscriptionCreatedQueue = Queue<Payload>("api/queue/subscription-c
{ organizationId, phoneNumberId }, { organizationId, phoneNumberId },
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` }, { id: `fetch-messages-${organizationId}-${phoneNumberId}` },
), ),
setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
),
]); ]);
if (isReturningSubscriber) { if (isReturningSubscriber) {

View File

@ -1,10 +1,13 @@
import { resolver } from "blitz"; import { resolver } from "blitz";
import { z } from "zod"; import { z } from "zod";
import twilio from "twilio"; import twilio from "twilio";
import RestException from "twilio/lib/base/RestException";
import db from "db"; import db from "db";
import getCurrentUser from "app/users/queries/get-current-user"; import getCurrentUser from "app/users/queries/get-current-user";
import setTwilioWebhooks from "../api/queue/set-twilio-webhooks"; 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({ const Body = z.object({
phoneNumberSid: z.string(), 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 mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken);
const apiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" }); if (!organization.twilioApiKey) {
await db.organization.update({ newApiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" });
where: { id: organizationId }, } else {
data: { try {
twilioApiKey: apiKey.sid, await mainTwilioClient.keys.get(organization.twilioApiKey);
twilioApiSecret: apiKey.secret, } 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; const phoneNumberId = phoneNumberSid;
await Promise.all([ let promises = [
setTwilioWebhooks.enqueue( setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId }, { organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${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);
}); });