shellphone.app/app/messages/queries/get-conversation.ts
m5r 2cf53533a3 replace early returns with NotFoundError
test /api/webhook/incoming-message
2021-08-02 21:44:04 +08:00

30 lines
844 B
TypeScript

import { NotFoundError, resolver } from "blitz";
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) {
throw new NotFoundError();
}
const conversation = await db.message.findMany({
where: { OR: [{ from: recipient }, { to: recipient }] },
orderBy: { sentAt: Prisma.SortOrder.asc },
});
return conversation.map((message) => {
return {
...message,
content: decrypt(message.content, customer.encryptionKey),
};
});
});