From d43e731fe191d7e35d96847fe095c94555dd8ca6 Mon Sep 17 00:00:00 2001 From: Sebastien Castiel Date: Sun, 28 Jan 2024 18:51:29 -0500 Subject: Attach documents to expenses (#64) * Upload documents to receipts * Improve documents * Make the feature opt-in * Fix file name issue --- src/lib/api.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/lib/api.ts') diff --git a/src/lib/api.ts b/src/lib/api.ts index c27e584..dfa9651 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -62,6 +62,16 @@ export async function createExpense( }, }, isReimbursement: expenseFormValues.isReimbursement, + documents: { + createMany: { + data: expenseFormValues.documents.map((doc) => ({ + id: randomId(), + url: doc.url, + width: doc.width, + height: doc.height, + })), + }, + }, }, }) } @@ -159,6 +169,22 @@ export async function updateExpense( ), }, isReimbursement: expenseFormValues.isReimbursement, + documents: { + connectOrCreate: expenseFormValues.documents.map((doc) => ({ + create: doc, + where: { id: doc.id }, + })), + deleteMany: existingExpense.documents + .filter( + (existingDoc) => + !expenseFormValues.documents.some( + (doc) => doc.id === existingDoc.id, + ), + ) + .map((doc) => ({ + id: doc.id, + })), + }, }, }) } @@ -231,6 +257,6 @@ export async function getExpense(groupId: string, expenseId: string) { const prisma = await getPrisma() return prisma.expense.findUnique({ where: { id: expenseId }, - include: { paidBy: true, paidFor: true, category: true }, + include: { paidBy: true, paidFor: true, category: true, documents: true }, }) } -- cgit v1.3