multi tenancy stuff
This commit is contained in:
@ -3,15 +3,14 @@ import { z } from "zod";
|
||||
|
||||
import db, { Prisma } from "../../../db";
|
||||
import { decrypt } from "../../../db/_encryption";
|
||||
import getCurrentCustomer from "../../customers/queries/get-current-customer";
|
||||
|
||||
const GetConversations = z.object({
|
||||
recipient: z.string(),
|
||||
});
|
||||
|
||||
export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(), async ({ recipient }, context) => {
|
||||
const customer = await getCurrentCustomer(null, context);
|
||||
if (!customer) {
|
||||
const organization = await db.organization.findFirst({ where: { id: context.session.orgId } });
|
||||
if (!organization) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
@ -23,7 +22,7 @@ export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(
|
||||
return conversation.map((message) => {
|
||||
return {
|
||||
...message,
|
||||
content: decrypt(message.content, customer.encryptionKey),
|
||||
content: decrypt(message.content, organization.encryptionKey),
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -1,45 +1,56 @@
|
||||
import { resolver, NotFoundError } from "blitz";
|
||||
import { z } from "zod";
|
||||
|
||||
import db, { Direction, Message, Prisma } from "../../../db";
|
||||
import getCurrentCustomer from "../../customers/queries/get-current-customer";
|
||||
import { decrypt } from "../../../db/_encryption";
|
||||
import { enforceSuperAdminIfNotCurrentOrganization, setDefaultOrganizationId } from "../../core/utils";
|
||||
|
||||
export default resolver.pipe(resolver.authorize(), async (_ = null, context) => {
|
||||
const customer = await getCurrentCustomer(null, context);
|
||||
if (!customer) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
const messages = await db.message.findMany({
|
||||
where: { customerId: customer.id },
|
||||
orderBy: { sentAt: Prisma.SortOrder.asc },
|
||||
});
|
||||
|
||||
let conversations: Record<string, Message[]> = {};
|
||||
for (const message of messages) {
|
||||
let recipient: string;
|
||||
if (message.direction === Direction.Outbound) {
|
||||
recipient = message.to;
|
||||
} else {
|
||||
recipient = message.from;
|
||||
export default resolver.pipe(
|
||||
resolver.zod(z.object({ organizationId: z.string().optional() })),
|
||||
resolver.authorize(),
|
||||
setDefaultOrganizationId,
|
||||
enforceSuperAdminIfNotCurrentOrganization,
|
||||
async ({ organizationId }) => {
|
||||
const organization = await db.organization.findFirst({
|
||||
where: { id: organizationId },
|
||||
include: { phoneNumbers: true },
|
||||
});
|
||||
if (!organization) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
if (!conversations[recipient]) {
|
||||
conversations[recipient] = [];
|
||||
}
|
||||
|
||||
conversations[recipient]!.push({
|
||||
...message,
|
||||
content: decrypt(message.content, customer.encryptionKey),
|
||||
const phoneNumberId = organization.phoneNumbers[0]!.id;
|
||||
const messages = await db.message.findMany({
|
||||
where: { organizationId, phoneNumberId },
|
||||
orderBy: { sentAt: Prisma.SortOrder.asc },
|
||||
});
|
||||
|
||||
conversations[recipient]!.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
|
||||
}
|
||||
conversations = Object.fromEntries(
|
||||
Object.entries(conversations).sort(
|
||||
([, a], [, b]) => b[b.length - 1]!.sentAt.getTime() - a[a.length - 1]!.sentAt.getTime(),
|
||||
),
|
||||
);
|
||||
let conversations: Record<string, Message[]> = {};
|
||||
for (const message of messages) {
|
||||
let recipient: string;
|
||||
if (message.direction === Direction.Outbound) {
|
||||
recipient = message.to;
|
||||
} else {
|
||||
recipient = message.from;
|
||||
}
|
||||
|
||||
return conversations;
|
||||
});
|
||||
if (!conversations[recipient]) {
|
||||
conversations[recipient] = [];
|
||||
}
|
||||
|
||||
conversations[recipient]!.push({
|
||||
...message,
|
||||
content: decrypt(message.content, organization.encryptionKey),
|
||||
});
|
||||
|
||||
conversations[recipient]!.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
|
||||
}
|
||||
conversations = Object.fromEntries(
|
||||
Object.entries(conversations).sort(
|
||||
([, a], [, b]) => b[b.length - 1]!.sentAt.getTime() - a[a.length - 1]!.sentAt.getTime(),
|
||||
),
|
||||
);
|
||||
|
||||
return conversations;
|
||||
},
|
||||
);
|
||||
|
Reference in New Issue
Block a user