2021-08-03 13:03:10 +00:00
|
|
|
import { getConfig } from "blitz";
|
2021-07-31 15:57:43 +00:00
|
|
|
import { Queue } from "quirrel/blitz";
|
2021-08-08 05:34:15 +00:00
|
|
|
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";
|
2021-08-30 11:24:05 +00:00
|
|
|
import getTwilioClient, { getTwiMLName, smsUrl, voiceUrl } 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 15:57:43 +00:00
|
|
|
};
|
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) {
|
2021-08-02 10:02:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
const organization = phoneNumber.organization;
|
2021-08-08 05:34:15 +00:00
|
|
|
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 },
|
2021-08-08 05:34:15 +00:00
|
|
|
data: { twimlAppSid },
|
2021-08-08 04:26:15 +00:00
|
|
|
}),
|
2021-08-08 05:34: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) {
|
2021-08-30 11:24:05 +00:00
|
|
|
return await updateTwimlApplication(twilioClient, organizationTwimlAppSid);
|
2021-08-08 04:26:15 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// twiml app with sid `organizationTwimlAppSid` probably doesn't exist anymore
|
|
|
|
}
|
|
|
|
|
|
|
|
const twimlApps = await twilioClient.applications.list();
|
2021-08-29 22:58:00 +00:00
|
|
|
const twimlApp = twimlApps.find((app) => app.friendlyName.startsWith("Shellphone"));
|
2021-08-08 04:26:15 +00:00
|
|
|
if (twimlApp) {
|
|
|
|
return updateTwimlApplication(twilioClient, twimlApp.sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return twilioClient.applications.create({
|
2021-08-29 22:58:00 +00:00
|
|
|
friendlyName: getTwiMLName(),
|
2021-08-30 11:24:05 +00:00
|
|
|
smsUrl,
|
2021-08-08 04:26:15 +00:00
|
|
|
smsMethod: "POST",
|
2021-08-30 11:24:05 +00:00
|
|
|
voiceUrl,
|
2021-08-08 04:26:15 +00:00
|
|
|
voiceMethod: "POST",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateTwimlApplication(twilioClient: twilio.Twilio, twimlAppSid: string) {
|
|
|
|
await twilioClient.applications.get(twimlAppSid).update({
|
2021-08-30 11:24:05 +00:00
|
|
|
friendlyName: getTwiMLName(),
|
|
|
|
smsUrl,
|
2021-08-08 04:26:15 +00:00
|
|
|
smsMethod: "POST",
|
2021-08-30 11:24:05 +00:00
|
|
|
voiceUrl,
|
2021-08-08 04:26:15 +00:00
|
|
|
voiceMethod: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
return twilioClient.applications.get(twimlAppSid).fetch();
|
|
|
|
}
|
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
export default setTwilioWebhooks;
|