multi tenancy stuff
This commit is contained in:
@ -5,23 +5,31 @@ import twilio from "twilio";
|
||||
import db from "../../../../db";
|
||||
|
||||
type Payload = {
|
||||
customerId: string;
|
||||
organizationId: string;
|
||||
phoneNumberId: string;
|
||||
};
|
||||
|
||||
const { serverRuntimeConfig } = getConfig();
|
||||
|
||||
const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async ({ customerId }) => {
|
||||
const [customer, phoneNumber] = await Promise.all([
|
||||
db.customer.findFirst({ where: { id: customerId } }),
|
||||
db.phoneNumber.findFirst({ where: { customerId } }),
|
||||
]);
|
||||
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
|
||||
const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async ({ organizationId, phoneNumberId }) => {
|
||||
const phoneNumber = await db.phoneNumber.findFirst({
|
||||
where: { id: phoneNumberId, organizationId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
const twimlApp = customer.twimlAppSid
|
||||
? await twilio(customer.accountSid, customer.authToken).applications.get(customer.twimlAppSid).fetch()
|
||||
: await twilio(customer.accountSid, customer.authToken).applications.create({
|
||||
const organization = phoneNumber.organization;
|
||||
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
const twimlApp = organization.twimlAppSid
|
||||
? await twilio(organization.twilioAccountSid, organization.twilioAuthToken)
|
||||
.applications.get(organization.twimlAppSid)
|
||||
.fetch()
|
||||
: await twilio(organization.twilioAccountSid, organization.twilioAuthToken).applications.create({
|
||||
friendlyName: "Shellphone",
|
||||
smsUrl: `https://${serverRuntimeConfig.app.baseUrl}/api/webhook/incoming-message`,
|
||||
smsMethod: "POST",
|
||||
@ -31,14 +39,16 @@ const setTwilioWebhooks = Queue<Payload>("api/queue/set-twilio-webhooks", async
|
||||
const twimlAppSid = twimlApp.sid;
|
||||
|
||||
await Promise.all([
|
||||
db.customer.update({
|
||||
where: { id: customerId },
|
||||
db.organization.update({
|
||||
where: { id: organizationId },
|
||||
data: { twimlAppSid },
|
||||
}),
|
||||
twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.get(phoneNumber.phoneNumberSid).update({
|
||||
smsApplicationSid: twimlAppSid,
|
||||
voiceApplicationSid: twimlAppSid,
|
||||
}),
|
||||
twilio(organization.twilioAccountSid, organization.twilioAuthToken)
|
||||
.incomingPhoneNumbers.get(phoneNumber.id)
|
||||
.update({
|
||||
smsApplicationSid: twimlAppSid,
|
||||
voiceApplicationSid: twimlAppSid,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
|
@ -3,7 +3,7 @@ import { z } from "zod";
|
||||
import twilio from "twilio";
|
||||
|
||||
import db from "../../../db";
|
||||
import getCurrentCustomer from "../../customers/queries/get-current-customer";
|
||||
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";
|
||||
@ -13,26 +13,40 @@ const Body = z.object({
|
||||
});
|
||||
|
||||
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberSid }, context) => {
|
||||
const customer = await getCurrentCustomer(null, context);
|
||||
if (!customer || !customer.accountSid || !customer.authToken) {
|
||||
const user = await getCurrentUser(null, context);
|
||||
const organization = user?.memberships[0]!.organization;
|
||||
if (!user || !organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
const customerId = customer.id;
|
||||
const phoneNumbers = await twilio(customer.accountSid, customer.authToken).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({
|
||||
data: {
|
||||
customerId,
|
||||
phoneNumberSid,
|
||||
phoneNumber: phoneNumber.phoneNumber,
|
||||
organizationId,
|
||||
id: phoneNumberSid,
|
||||
number: phoneNumber.phoneNumber,
|
||||
},
|
||||
});
|
||||
context.session.$setPrivateData({ hasCompletedOnboarding: true });
|
||||
|
||||
const phoneNumberId = phoneNumberSid;
|
||||
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}` }),
|
||||
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}` },
|
||||
),
|
||||
]);
|
||||
});
|
||||
|
@ -2,7 +2,7 @@ import { resolver } from "blitz";
|
||||
import { z } from "zod";
|
||||
|
||||
import db from "../../../db";
|
||||
import getCurrentCustomer from "../../customers/queries/get-current-customer";
|
||||
import getCurrentUser from "../../users/queries/get-current-user";
|
||||
|
||||
const Body = z.object({
|
||||
twilioAccountSid: z.string(),
|
||||
@ -13,16 +13,17 @@ export default resolver.pipe(
|
||||
resolver.zod(Body),
|
||||
resolver.authorize(),
|
||||
async ({ twilioAccountSid, twilioAuthToken }, context) => {
|
||||
const customer = await getCurrentCustomer(null, context);
|
||||
if (!customer) {
|
||||
const user = await getCurrentUser(null, context);
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
await db.customer.update({
|
||||
where: { id: customer.id },
|
||||
const organizationId = user.memberships[0]!.id;
|
||||
await db.organization.update({
|
||||
where: { id: organizationId },
|
||||
data: {
|
||||
accountSid: twilioAccountSid,
|
||||
authToken: twilioAuthToken,
|
||||
twilioAccountSid: twilioAccountSid,
|
||||
twilioAuthToken: twilioAuthToken,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
@ -2,11 +2,11 @@ import type { BlitzPage, GetServerSideProps } from "blitz";
|
||||
import { getSession, Routes } from "blitz";
|
||||
|
||||
import OnboardingLayout from "../../components/onboarding-layout";
|
||||
import useCurrentCustomer from "../../../core/hooks/use-current-customer";
|
||||
import useCurrentUser from "../../../core/hooks/use-current-user";
|
||||
import db from "../../../../db";
|
||||
|
||||
const StepOne: BlitzPage = () => {
|
||||
useCurrentCustomer(); // preload for step two
|
||||
useCurrentUser(); // preload for step two
|
||||
|
||||
return (
|
||||
<div className="flex flex-col space-y-4 items-center">
|
||||
@ -35,7 +35,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
||||
};
|
||||
}
|
||||
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } });
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId: session.orgId } });
|
||||
if (phoneNumber) {
|
||||
await session.$setPublicData({ hasCompletedOnboarding: true });
|
||||
return {
|
||||
|
@ -100,7 +100,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
|
||||
};
|
||||
}
|
||||
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } });
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId: session.orgId } });
|
||||
if (phoneNumber) {
|
||||
await session.$setPublicData({ hasCompletedOnboarding: true });
|
||||
return {
|
||||
@ -111,8 +111,8 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
|
||||
};
|
||||
}
|
||||
|
||||
const customer = await db.customer.findFirst({ where: { id: session.userId } });
|
||||
if (!customer) {
|
||||
const organization = await db.organization.findFirst({ where: { id: session.orgId } });
|
||||
if (!organization) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: Routes.StepOne().pathname,
|
||||
@ -121,7 +121,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
|
||||
};
|
||||
}
|
||||
|
||||
if (!customer.accountSid || !customer.authToken) {
|
||||
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: Routes.StepTwo().pathname,
|
||||
@ -130,7 +130,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
|
||||
};
|
||||
}
|
||||
|
||||
const incomingPhoneNumbers = await twilio(customer.accountSid, customer.authToken).incomingPhoneNumbers.list();
|
||||
const incomingPhoneNumbers = await twilio(
|
||||
organization.twilioAccountSid,
|
||||
organization.twilioAuthToken,
|
||||
).incomingPhoneNumbers.list();
|
||||
const phoneNumbers = incomingPhoneNumbers.map(({ phoneNumber, sid }) => ({ phoneNumber, sid }));
|
||||
|
||||
return {
|
||||
|
@ -11,7 +11,7 @@ import db from "db";
|
||||
import setTwilioApiFields from "../../mutations/set-twilio-api-fields";
|
||||
import OnboardingLayout from "../../components/onboarding-layout";
|
||||
import HelpModal from "../../components/help-modal";
|
||||
import useCurrentCustomer from "../../../core/hooks/use-current-customer";
|
||||
import useCurrentUser from "../../../core/hooks/use-current-user";
|
||||
|
||||
type Form = {
|
||||
twilioAccountSid: string;
|
||||
@ -26,14 +26,14 @@ const StepTwo: BlitzPage = () => {
|
||||
formState: { isSubmitting },
|
||||
} = useForm<Form>();
|
||||
const router = useRouter();
|
||||
const { customer } = useCurrentCustomer();
|
||||
const { organization } = useCurrentUser();
|
||||
const [setTwilioApiFieldsMutation] = useMutation(setTwilioApiFields);
|
||||
const [isHelpModalOpen, setIsHelpModalOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setValue("twilioAuthToken", customer?.authToken ?? "");
|
||||
setValue("twilioAccountSid", customer?.accountSid ?? "");
|
||||
}, [setValue, customer?.authToken, customer?.accountSid]);
|
||||
setValue("twilioAuthToken", organization?.twilioAuthToken ?? "");
|
||||
setValue("twilioAccountSid", organization?.twilioAccountSid ?? "");
|
||||
}, [setValue, organization?.twilioAuthToken, organization?.twilioAccountSid]);
|
||||
|
||||
const onSubmit = handleSubmit(async ({ twilioAccountSid, twilioAuthToken }) => {
|
||||
if (isSubmitting) {
|
||||
@ -105,9 +105,9 @@ StepTwo.getLayout = (page) => {
|
||||
};
|
||||
|
||||
const StepTwoLayout: FunctionComponent = ({ children }) => {
|
||||
const { customer } = useCurrentCustomer();
|
||||
const initialAuthToken = customer?.authToken ?? "";
|
||||
const initialAccountSid = customer?.accountSid ?? "";
|
||||
const { organization } = useCurrentUser();
|
||||
const initialAuthToken = organization?.twilioAuthToken ?? "";
|
||||
const initialAccountSid = organization?.twilioAccountSid ?? "";
|
||||
const hasTwilioCredentials = initialAccountSid.length > 0 && initialAuthToken.length > 0;
|
||||
|
||||
return (
|
||||
@ -135,7 +135,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
||||
};
|
||||
}
|
||||
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId: session.userId } });
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { organizationId: session.orgId } });
|
||||
if (phoneNumber) {
|
||||
await session.$setPublicData({ hasCompletedOnboarding: true });
|
||||
return {
|
||||
|
Reference in New Issue
Block a user