2021-07-31 15:57:43 +00:00
|
|
|
import { resolver } from "blitz";
|
|
|
|
import { z } from "zod";
|
|
|
|
import twilio from "twilio";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import db from "../../../db";
|
|
|
|
import getCurrentCustomer from "../../customers/queries/get-current-customer";
|
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";
|
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) => {
|
|
|
|
const customer = await getCurrentCustomer(null, context);
|
2021-08-02 10:02:49 +00:00
|
|
|
if (!customer || !customer.accountSid || !customer.authToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const customerId = customer.id;
|
|
|
|
const phoneNumbers = await twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.list();
|
2021-08-01 14:01:51 +00:00
|
|
|
const phoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid)!;
|
|
|
|
await db.phoneNumber.create({
|
|
|
|
data: {
|
|
|
|
customerId,
|
|
|
|
phoneNumberSid,
|
|
|
|
phoneNumber: phoneNumber.phoneNumber,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
context.session.$setPrivateData({ hasCompletedOnboarding: true });
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 14:01:51 +00:00
|
|
|
await Promise.all([
|
|
|
|
fetchMessagesQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }),
|
|
|
|
fetchCallsQueue.enqueue({ customerId }, { id: `fetch-messages-${customerId}` }),
|
|
|
|
setTwilioWebhooks.enqueue({ customerId }, { id: `set-twilio-webhooks-${customerId}` }),
|
|
|
|
]);
|
|
|
|
});
|