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";
|
|
|
|
import 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-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;
|
|
|
|
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-08 04:26:15 +00:00
|
|
|
const mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken);
|
|
|
|
const [twimlApp, apiKey] = await Promise.all([
|
|
|
|
getTwimlApplication(mainTwilioClient, organization.twimlAppSid),
|
|
|
|
mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" }),
|
|
|
|
]);
|
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 04:26:15 +00:00
|
|
|
data: {
|
|
|
|
twimlAppSid,
|
|
|
|
twilioApiKey: apiKey.sid,
|
|
|
|
twilioApiSecret: apiKey.secret,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
mainTwilioClient.incomingPhoneNumbers.get(phoneNumber.id).update({
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
export default setTwilioWebhooks;
|