2021-07-31 15:57:43 +00:00
|
|
|
import { resolver } from "blitz";
|
|
|
|
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";
|
|
|
|
import getCurrentCustomer from "../../customers/queries/get-current-customer";
|
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
|
|
|
|
|
|
|
export default resolver.pipe(
|
|
|
|
resolver.zod(GetConversations),
|
|
|
|
resolver.authorize(),
|
|
|
|
async ({ recipient }, context) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const customer = await getCurrentCustomer(null, context);
|
2021-07-31 14:33:18 +00:00
|
|
|
const conversation = await db.message.findMany({
|
|
|
|
where: {
|
|
|
|
OR: [{ from: recipient }, { to: recipient }],
|
|
|
|
},
|
|
|
|
orderBy: { sentAt: Prisma.SortOrder.asc },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
return conversation.map((message) => {
|
|
|
|
return {
|
|
|
|
...message,
|
|
|
|
content: decrypt(message.content, customer!.encryptionKey),
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|