shellphone.app/app/onboarding/api/queue/set-twilio-webhooks.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-08-03 13:03:10 +00:00
import { getConfig } from "blitz";
import { Queue } from "quirrel/blitz";
import type twilio from "twilio";
2021-08-08 04:26:15 +00:00
import type { ApplicationInstance } from "twilio/lib/rest/api/v2010/account/application";
2021-07-31 14:33:18 +00:00
2021-08-01 10:46:10 +00:00
import db from "../../../../db";
import getTwilioClient from "../../../../integrations/twilio";
2021-07-31 14:33:18 +00:00
type Payload = {
2021-08-05 17:07:15 +00:00
organizationId: string;
phoneNumberId: string;
};
2021-07-31 14:33:18 +00:00
2021-08-03 13:03:10 +00:00
const { serverRuntimeConfig } = getConfig();
2021-08-05 17:07:15 +00:00
const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async ({ organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
2021-08-05 17:07:15 +00:00
const organization = phoneNumber.organization;
const twilioClient = getTwilioClient(organization);
const twimlApp = await getTwimlApplication(twilioClient, organization.twimlAppSid);
2021-08-01 14:03:49 +00:00
const twimlAppSid = twimlApp.sid;
2021-07-31 14:33:18 +00:00
2021-08-01 14:03:49 +00:00
await Promise.all([
2021-08-05 17:07:15 +00:00
db.organization.update({
where: { id: organizationId },
data: { twimlAppSid },
2021-08-08 04:26:15 +00:00
}),
twilioClient.incomingPhoneNumbers.get(phoneNumber.id).update({
2021-08-08 04:26:15 +00:00
smsApplicationSid: twimlAppSid,
voiceApplicationSid: twimlAppSid,
2021-08-01 14:03:49 +00:00
}),
]);
});
2021-07-31 14:33:18 +00:00
2021-08-08 04:26:15 +00:00
async function getTwimlApplication(
twilioClient: twilio.Twilio,
organizationTwimlAppSid: string | null,
): Promise<ApplicationInstance> {
try {
if (organizationTwimlAppSid) {
return updateTwimlApplication(twilioClient, organizationTwimlAppSid);
}
} catch {
// twiml app with sid `organizationTwimlAppSid` probably doesn't exist anymore
}
const twimlApps = await twilioClient.applications.list();
const twimlApp = twimlApps.find((app) => app.friendlyName === "Shellphone");
if (twimlApp) {
return updateTwimlApplication(twilioClient, twimlApp.sid);
}
return twilioClient.applications.create({
friendlyName: "Shellphone",
smsUrl: `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-message`,
smsMethod: "POST",
voiceUrl: `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-call`,
voiceMethod: "POST",
});
}
async function updateTwimlApplication(twilioClient: twilio.Twilio, twimlAppSid: string) {
await twilioClient.applications.get(twimlAppSid).update({
smsUrl: `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-message`,
smsMethod: "POST",
voiceUrl: `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-call`,
voiceMethod: "POST",
});
return twilioClient.applications.get(twimlAppSid).fetch();
}
export default setTwilioWebhooks;