return early when data is not available as expected

This commit is contained in:
m5r
2021-08-02 18:02:49 +08:00
parent 09568ef684
commit 827ed9f1c0
11 changed files with 88 additions and 43 deletions

View File

@ -11,17 +11,19 @@ const GetConversations = z.object({
export default resolver.pipe(resolver.zod(GetConversations), resolver.authorize(), async ({ recipient }, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer) {
return;
}
const conversation = await db.message.findMany({
where: {
OR: [{ from: recipient }, { to: recipient }],
},
where: { OR: [{ from: recipient }, { to: recipient }] },
orderBy: { sentAt: Prisma.SortOrder.asc },
});
return conversation.map((message) => {
return {
...message,
content: decrypt(message.content, customer!.encryptionKey),
content: decrypt(message.content, customer.encryptionKey),
};
});
});

View File

@ -6,8 +6,12 @@ import { decrypt } from "../../../db/_encryption";
export default resolver.pipe(resolver.authorize(), async (_ = null, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer) {
return;
}
const messages = await db.message.findMany({
where: { customerId: customer!.id },
where: { customerId: customer.id },
orderBy: { sentAt: Prisma.SortOrder.asc },
});
@ -26,7 +30,7 @@ export default resolver.pipe(resolver.authorize(), async (_ = null, context) =>
conversations[recipient]!.push({
...message,
content: decrypt(message.content, customer!.encryptionKey),
content: decrypt(message.content, customer.encryptionKey),
});
conversations[recipient]!.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());