fix onboarding

This commit is contained in:
m5r 2021-08-08 12:34:29 +08:00
parent 6f672bfeb2
commit 6ad516a99e
3 changed files with 12 additions and 7 deletions

View File

@ -1,12 +1,12 @@
import { resolver } from "blitz";
import { z } from "zod";
import twilio from "twilio";
import db from "../../../db";
import getCurrentUser from "../../users/queries/get-current-user";
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";
const Body = z.object({
phoneNumberSid: z.string(),
@ -15,12 +15,14 @@ const Body = z.object({
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberSid }, context) => {
const user = await getCurrentUser(null, context);
const organization = user?.memberships[0]!.organization;
if (!user || !organization) {
if (!user || !organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
return;
}
const twilioClient = getTwilioClient(organization);
const phoneNumbers = await twilioClient.incomingPhoneNumbers.list();
const phoneNumbers = await twilio(
organization.twilioAccountSid,
organization.twilioAuthToken,
).incomingPhoneNumbers.list();
const phoneNumber = phoneNumbers.find((phoneNumber) => phoneNumber.sid === phoneNumberSid)!;
const organizationId = organization.id;
await db.phoneNumber.create({

View File

@ -3,11 +3,11 @@ import { Routes, getSession, useRouter, useMutation } from "blitz";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import clsx from "clsx";
import twilio from "twilio";
import db from "../../../../db";
import OnboardingLayout from "../../components/onboarding-layout";
import setPhoneNumber from "../../mutations/set-phone-number";
import getTwilioClient from "../../../../integrations/twilio";
type PhoneNumber = {
phoneNumber: string;
@ -130,8 +130,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
};
}
const twilioClient = getTwilioClient(organization);
const incomingPhoneNumbers = await twilioClient.incomingPhoneNumbers.list();
const incomingPhoneNumbers = await twilio(
organization.twilioAccountSid,
organization.twilioAuthToken,
).incomingPhoneNumbers.list();
const phoneNumbers = incomingPhoneNumbers.map(({ phoneNumber, sid }) => ({ phoneNumber, sid }));
return {

View File

@ -21,6 +21,7 @@ export default async function getCurrentUser(_ = null, { session }: Ctx) {
paddleCustomerId: true,
paddleSubscriptionId: true,
twilioAccountSid: true,
twilioAuthToken: true,
twilioApiKey: true,
twilioApiSecret: true,
twimlAppSid: true,