2021-10-19 21:49:28 +00:00
|
|
|
import { NotFoundError, resolver } from "blitz";
|
2021-07-31 15:57:43 +00:00
|
|
|
import { z } from "zod";
|
2021-08-08 04:34:29 +00:00
|
|
|
import twilio from "twilio";
|
2021-10-19 21:12:16 +00:00
|
|
|
import RestException from "twilio/lib/base/RestException";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-10-15 23:25:13 +00:00
|
|
|
import db from "db";
|
|
|
|
import getCurrentUser from "app/users/queries/get-current-user";
|
2021-08-01 10:46:10 +00:00
|
|
|
import setTwilioWebhooks from "../api/queue/set-twilio-webhooks";
|
2021-10-20 21:57:32 +00:00
|
|
|
import fetchMessagesQueue from "app/messages/api/queue/fetch-messages";
|
|
|
|
import fetchCallsQueue from "app/phone-calls/api/queue/fetch-calls";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
const Body = z.object({
|
|
|
|
phoneNumberSid: z.string(),
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 14:01:51 +00:00
|
|
|
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberSid }, context) => {
|
2021-08-05 17:07:15 +00:00
|
|
|
const user = await getCurrentUser(null, context);
|
|
|
|
const organization = user?.memberships[0]!.organization;
|
2021-08-08 04:34:29 +00:00
|
|
|
if (!user || !organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
|
2021-08-02 10:02:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-08 04:34:29 +00:00
|
|
|
const phoneNumbers = await twilio(
|
|
|
|
organization.twilioAccountSid,
|
|
|
|
organization.twilioAuthToken,
|
|
|
|
).incomingPhoneNumbers.list();
|
2021-10-19 21:49:28 +00:00
|
|
|
const twilioPhoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid);
|
|
|
|
if (!twilioPhoneNumber) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
const organizationId = organization.id;
|
2021-10-19 21:49:28 +00:00
|
|
|
const orgCurrentlyActivePhoneNumber = await db.phoneNumber.findFirst({ where: { organizationId } });
|
|
|
|
if (orgCurrentlyActivePhoneNumber) {
|
|
|
|
// TODO: delete this and allow switching phone numbers easily
|
|
|
|
await db.phoneNumber.delete({
|
|
|
|
where: {
|
|
|
|
organizationId_id: {
|
|
|
|
organizationId,
|
|
|
|
id: orgCurrentlyActivePhoneNumber.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const phoneNumber = await db.phoneNumber.findFirst({ where: { id: phoneNumberSid } });
|
|
|
|
if (!phoneNumber) {
|
|
|
|
await db.phoneNumber.create({
|
|
|
|
data: {
|
|
|
|
organizationId,
|
|
|
|
id: phoneNumberSid,
|
|
|
|
number: twilioPhoneNumber.phoneNumber,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-10-19 21:12:16 +00:00
|
|
|
let newApiKey;
|
2021-08-08 05:34:15 +00:00
|
|
|
const mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken);
|
2021-10-19 21:12:16 +00:00
|
|
|
if (!organization.twilioApiKey) {
|
|
|
|
newApiKey = await mainTwilioClient.newKeys.create({ friendlyName: "Shellphone API key" });
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await mainTwilioClient.keys.get(organization.twilioApiKey);
|
|
|
|
} 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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-08-08 05:34:15 +00:00
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
const phoneNumberId = phoneNumberSid;
|
2021-10-20 21:57:32 +00:00
|
|
|
await Promise.all([
|
2021-08-05 17:07:15 +00:00
|
|
|
setTwilioWebhooks.enqueue(
|
|
|
|
{ organizationId, phoneNumberId },
|
|
|
|
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
|
|
|
|
),
|
2021-10-20 21:57:32 +00:00
|
|
|
db.processingPhoneNumber.create({
|
|
|
|
data: {
|
|
|
|
organizationId,
|
|
|
|
phoneNumberId,
|
|
|
|
hasFetchedMessages: false,
|
|
|
|
hasFetchedCalls: false,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
fetchMessagesQueue.enqueue(
|
|
|
|
{ organizationId, phoneNumberId },
|
|
|
|
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` },
|
|
|
|
),
|
|
|
|
fetchCallsQueue.enqueue(
|
|
|
|
{ organizationId, phoneNumberId },
|
|
|
|
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` },
|
|
|
|
),
|
|
|
|
]);
|
2021-08-01 14:01:51 +00:00
|
|
|
});
|