2022-06-08 22:33:19 +00:00
|
|
|
import twilio from "twilio";
|
2022-05-21 23:18:32 +00:00
|
|
|
import type { ApplicationInstance } from "twilio/lib/rest/api/v2010/account/application";
|
|
|
|
|
|
|
|
import { Queue } from "~/utils/queue.server";
|
|
|
|
import db from "~/utils/db.server";
|
2022-06-08 22:33:19 +00:00
|
|
|
import { getTwiMLName, smsUrl, voiceUrl } from "~/utils/twilio.server";
|
|
|
|
import { decrypt } from "~/utils/encryption";
|
2022-05-21 23:18:32 +00:00
|
|
|
|
|
|
|
type Payload = {
|
|
|
|
phoneNumberId: string;
|
|
|
|
organizationId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Queue<Payload>("set twilio webhooks", async ({ data }) => {
|
|
|
|
const { phoneNumberId, organizationId } = data;
|
|
|
|
const phoneNumber = await db.phoneNumber.findFirst({
|
2022-06-11 00:09:37 +00:00
|
|
|
where: { id: phoneNumberId, twilioAccount: { organizationId } },
|
2022-05-21 23:18:32 +00:00
|
|
|
include: {
|
2022-06-11 00:09:37 +00:00
|
|
|
twilioAccount: {
|
|
|
|
select: { accountSid: true, twimlAppSid: true, authToken: true },
|
2022-05-21 23:18:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-06-11 00:09:37 +00:00
|
|
|
if (!phoneNumber) {
|
2022-05-21 23:18:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-11 00:09:37 +00:00
|
|
|
const twilioAccount = phoneNumber.twilioAccount;
|
2022-06-08 22:33:19 +00:00
|
|
|
const authToken = decrypt(twilioAccount.authToken);
|
|
|
|
const twilioClient = twilio(twilioAccount.accountSid, authToken);
|
2022-05-21 23:18:32 +00:00
|
|
|
const twimlApp = await getTwimlApplication(twilioClient, twilioAccount.twimlAppSid);
|
|
|
|
const twimlAppSid = twimlApp.sid;
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
db.twilioAccount.update({
|
|
|
|
where: { organizationId },
|
|
|
|
data: { twimlAppSid },
|
|
|
|
}),
|
|
|
|
twilioClient.incomingPhoneNumbers.get(phoneNumber.id).update({
|
|
|
|
smsApplicationSid: twimlAppSid,
|
|
|
|
voiceApplicationSid: twimlAppSid,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
async function getTwimlApplication(
|
|
|
|
twilioClient: twilio.Twilio,
|
|
|
|
organizationTwimlAppSid: string | null,
|
|
|
|
): Promise<ApplicationInstance> {
|
|
|
|
try {
|
|
|
|
if (organizationTwimlAppSid) {
|
|
|
|
return await 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.startsWith("Shellphone"));
|
|
|
|
if (twimlApp) {
|
|
|
|
return updateTwimlApplication(twilioClient, twimlApp.sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return twilioClient.applications.create({
|
|
|
|
friendlyName: getTwiMLName(),
|
|
|
|
smsUrl,
|
|
|
|
smsMethod: "POST",
|
|
|
|
voiceUrl,
|
|
|
|
voiceMethod: "POST",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateTwimlApplication(twilioClient: twilio.Twilio, twimlAppSid: string) {
|
|
|
|
await twilioClient.applications.get(twimlAppSid).update({
|
|
|
|
friendlyName: getTwiMLName(),
|
|
|
|
smsUrl,
|
|
|
|
smsMethod: "POST",
|
|
|
|
voiceUrl,
|
|
|
|
voiceMethod: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
return twilioClient.applications.get(twimlAppSid).fetch();
|
|
|
|
}
|