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

51 lines
1.7 KiB
TypeScript
Raw Normal View History

import { resolver } from "blitz";
import { z } from "zod";
2021-07-31 14:33:18 +00:00
import db from "../../../db";
2021-08-05 17:07:15 +00:00
import getCurrentUser from "../../users/queries/get-current-user";
2021-08-01 10:46:10 +00:00
import fetchMessagesQueue from "../../messages/api/queue/fetch-messages";
import fetchCallsQueue from "../../phone-calls/api/queue/fetch-calls";
import setTwilioWebhooks from "../api/queue/set-twilio-webhooks";
import getTwilioClient from "../../../integrations/twilio";
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;
if (!user || !organization) {
return;
}
const twilioClient = getTwilioClient(organization);
const phoneNumbers = await twilioClient.incomingPhoneNumbers.list();
2021-08-01 14:01:51 +00:00
const phoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid)!;
2021-08-05 17:07:15 +00:00
const organizationId = organization.id;
2021-08-01 14:01:51 +00:00
await db.phoneNumber.create({
data: {
2021-08-05 17:07:15 +00:00
organizationId,
id: phoneNumberSid,
number: phoneNumber.phoneNumber,
2021-08-01 14:01:51 +00:00
},
});
context.session.$setPrivateData({ hasCompletedOnboarding: true });
2021-07-31 14:33:18 +00:00
2021-08-05 17:07:15 +00:00
const phoneNumberId = phoneNumberSid;
2021-08-01 14:01:51 +00:00
await Promise.all([
2021-08-05 17:07:15 +00:00
fetchMessagesQueue.enqueue(
{ organizationId, phoneNumberId },
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` },
),
fetchCallsQueue.enqueue(
{ organizationId, phoneNumberId },
{ id: `fetch-messages-${organizationId}-${phoneNumberId}` },
),
setTwilioWebhooks.enqueue(
{ organizationId, phoneNumberId },
{ id: `set-twilio-webhooks-${organizationId}-${phoneNumberId}` },
),
2021-08-01 14:01:51 +00:00
]);
});