multi tenancy stuff
This commit is contained in:
@ -5,35 +5,42 @@ import db from "../../../../db";
|
||||
import insertCallsQueue from "./insert-calls";
|
||||
|
||||
type Payload = {
|
||||
customerId: string;
|
||||
organizationId: string;
|
||||
phoneNumberId: string;
|
||||
};
|
||||
|
||||
const fetchCallsQueue = Queue<Payload>("api/queue/fetch-calls", 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 fetchCallsQueue = Queue<Payload>("api/queue/fetch-calls", async ({ organizationId, phoneNumberId }) => {
|
||||
const phoneNumber = await db.phoneNumber.findFirst({
|
||||
where: { id: phoneNumberId, organizationId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
const organization = phoneNumber.organization;
|
||||
if (!organization.twilioAccountSid || !organization.twilioAuthToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [callsSent, callsReceived] = await Promise.all([
|
||||
twilio(customer.accountSid, customer.authToken).calls.list({
|
||||
from: phoneNumber.phoneNumber,
|
||||
twilio(organization.twilioAccountSid, organization.twilioAuthToken).calls.list({
|
||||
from: phoneNumber.number,
|
||||
}),
|
||||
twilio(customer.accountSid, customer.authToken).calls.list({
|
||||
to: phoneNumber.phoneNumber,
|
||||
twilio(organization.twilioAccountSid, organization.twilioAuthToken).calls.list({
|
||||
to: phoneNumber.number,
|
||||
}),
|
||||
]);
|
||||
const calls = [...callsSent, ...callsReceived].sort((a, b) => a.dateCreated.getTime() - b.dateCreated.getTime());
|
||||
|
||||
await insertCallsQueue.enqueue(
|
||||
{
|
||||
customerId,
|
||||
organizationId,
|
||||
phoneNumberId,
|
||||
calls,
|
||||
},
|
||||
{
|
||||
id: `insert-calls-${customerId}`,
|
||||
id: `insert-calls-${organizationId}-${phoneNumberId}`,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
@ -4,15 +4,25 @@ import type { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
|
||||
import db, { Direction, CallStatus } from "../../../../db";
|
||||
|
||||
type Payload = {
|
||||
customerId: string;
|
||||
organizationId: string;
|
||||
phoneNumberId: string;
|
||||
calls: CallInstance[];
|
||||
};
|
||||
|
||||
const insertCallsQueue = Queue<Payload>("api/queue/insert-calls", async ({ calls, customerId }) => {
|
||||
const insertCallsQueue = Queue<Payload>("api/queue/insert-calls", async ({ calls, organizationId, phoneNumberId }) => {
|
||||
const phoneNumber = await db.phoneNumber.findFirst({
|
||||
where: { id: phoneNumberId, organizationId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
const phoneCalls = calls
|
||||
.map((call) => ({
|
||||
customerId,
|
||||
twilioSid: call.sid,
|
||||
organizationId,
|
||||
phoneNumberId,
|
||||
id: call.sid,
|
||||
from: call.from,
|
||||
to: call.to,
|
||||
direction: translateDirection(call.direction),
|
||||
|
Reference in New Issue
Block a user