multi tenancy stuff
This commit is contained in:
@ -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,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
Reference in New Issue
Block a user