2021-08-02 13:43:27 +00:00
|
|
|
import { NotFoundError, resolver } from "blitz";
|
2021-07-31 15:57:43 +00:00
|
|
|
import { z } from "zod";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import db, { Prisma } from "../../../db";
|
|
|
|
import { decrypt } from "../../../db/_encryption";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
const GetConversations = z.object({
|
|
|
|
recipient: z.string(),
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 14:03:49 +00:00
|
|
|
export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(), async ({ recipient }, context) => {
|
2021-08-05 17:07:15 +00:00
|
|
|
const organization = await db.organization.findFirst({ where: { id: context.session.orgId } });
|
|
|
|
if (!organization) {
|
2021-08-02 13:43:27 +00:00
|
|
|
throw new NotFoundError();
|
2021-08-02 10:02:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:03:49 +00:00
|
|
|
const conversation = await db.message.findMany({
|
2021-08-02 10:02:49 +00:00
|
|
|
where: { OR: [{ from: recipient }, { to: recipient }] },
|
2021-08-13 15:54:13 +00:00
|
|
|
orderBy: { sentAt: Prisma.SortOrder.desc },
|
2021-08-01 14:03:49 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 14:03:49 +00:00
|
|
|
return conversation.map((message) => {
|
|
|
|
return {
|
|
|
|
...message,
|
2021-08-05 17:07:15 +00:00
|
|
|
content: decrypt(message.content, organization.encryptionKey),
|
2021-08-01 14:03:49 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|