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),
|
||||
|
@ -2,7 +2,7 @@ import { Direction } from "../../../db";
|
||||
import usePhoneCalls from "../hooks/use-phone-calls";
|
||||
|
||||
export default function PhoneCallsList() {
|
||||
const phoneCalls = usePhoneCalls();
|
||||
const phoneCalls = usePhoneCalls()[0];
|
||||
|
||||
if (phoneCalls.length === 0) {
|
||||
return <div>empty state</div>;
|
||||
@ -13,7 +13,7 @@ export default function PhoneCallsList() {
|
||||
{phoneCalls.map((phoneCall) => {
|
||||
const recipient = Direction.Outbound ? phoneCall.to : phoneCall.from;
|
||||
return (
|
||||
<li key={phoneCall.twilioSid} className="flex flex-row justify-between py-2">
|
||||
<li key={phoneCall.id} className="flex flex-row justify-between py-2">
|
||||
<div>{recipient}</div>
|
||||
<div>{new Date(phoneCall.createdAt).toLocaleString("fr-FR")}</div>
|
||||
</li>
|
||||
|
@ -1,15 +1,13 @@
|
||||
import { NotFoundError, useQuery } from "blitz";
|
||||
|
||||
import useCurrentCustomer from "../../core/hooks/use-current-customer";
|
||||
import useCurrentPhoneNumber from "../..//core/hooks/use-current-phone-number";
|
||||
import getPhoneCalls from "../queries/get-phone-calls";
|
||||
|
||||
export default function usePhoneCalls() {
|
||||
const { customer } = useCurrentCustomer();
|
||||
if (!customer) {
|
||||
const phoneNumber = useCurrentPhoneNumber();
|
||||
if (!phoneNumber) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
const { phoneCalls } = useQuery(getPhoneCalls, { customerId: customer.id })[0];
|
||||
|
||||
return phoneCalls;
|
||||
return useQuery(getPhoneCalls, { phoneNumberId: phoneNumber.id });
|
||||
}
|
||||
|
@ -1,31 +1,16 @@
|
||||
import { paginate, resolver } from "blitz";
|
||||
import db, { Prisma, Customer } from "db";
|
||||
import { resolver } from "blitz";
|
||||
import { z } from "zod";
|
||||
import db, { Prisma } from "db";
|
||||
|
||||
interface GetPhoneCallsInput extends Pick<Prisma.PhoneCallFindManyArgs, "where" | "orderBy" | "skip" | "take"> {
|
||||
customerId: Customer["id"];
|
||||
}
|
||||
const Body = z.object({
|
||||
phoneNumberId: z.string(),
|
||||
});
|
||||
|
||||
export default resolver.pipe(
|
||||
resolver.authorize(),
|
||||
async ({ where, orderBy, skip = 0, take = 100 }: GetPhoneCallsInput) => {
|
||||
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
|
||||
const {
|
||||
items: phoneCalls,
|
||||
hasMore,
|
||||
nextPage,
|
||||
count,
|
||||
} = await paginate({
|
||||
skip,
|
||||
take,
|
||||
count: () => db.phoneCall.count({ where }),
|
||||
query: (paginateArgs) => db.phoneCall.findMany({ ...paginateArgs, where, orderBy }),
|
||||
});
|
||||
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ phoneNumberId }, context) => {
|
||||
const organizationId = context.session.orgId;
|
||||
|
||||
return {
|
||||
phoneCalls,
|
||||
nextPage,
|
||||
hasMore,
|
||||
count,
|
||||
};
|
||||
},
|
||||
);
|
||||
return db.phoneCall.findMany({
|
||||
where: { organizationId, phoneNumberId },
|
||||
orderBy: { createdAt: Prisma.SortOrder.asc },
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user