shellphone.app/app/settings/mutations/set-phone-number.ts

115 lines
3.3 KiB
TypeScript
Raw Normal View History

import { NotFoundError, resolver } from "blitz";
import { z } from "zod";
2021-08-08 04:34:29 +00:00
import twilio from "twilio";
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";
import fetchMessagesQueue from "../../messages/api/queue/fetch-messages";
import fetchCallsQueue from "../../phone-calls/api/queue/fetch-calls";
2021-07-31 14:33:18 +00:00
const Body = z.object({
phoneNumberSid: z.string(),
});
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) {
return;
}
2021-08-08 04:34:29 +00:00
const phoneNumbers = await twilio(
organization.twilioAccountSid,
organization.twilioAuthToken,
).incomingPhoneNumbers.list();
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;
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
let newApiKey;
const mainTwilioClient = twilio(organization.twilioAccountSid, organization.twilioAuthToken);
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-05 17:07:15 +00:00
const phoneNumberId = phoneNumberSid;
let promises: Promise<any>[] = [
2021-08-05 17:07:15 +00:00
setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
),
];
const hasActiveSubscription = organization.subscriptions.length > 0;
if (hasActiveSubscription) {
promises.push(
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}` },
),
);
}
await Promise.all(promises);
2021-08-01 14:01:51 +00:00
});