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/app/api/s3-upload/route.ts | 8 ++ src/components/expense-documents-input.tsx | 145 +++++++++++++++++++++++++++++ src/components/expense-form.tsx | 29 ++++++ src/lib/api.ts | 28 +++++- src/lib/env.ts | 47 +++++++--- src/lib/schemas.ts | 10 ++ 6 files changed, 253 insertions(+), 14 deletions(-) create mode 100644 src/app/api/s3-upload/route.ts create mode 100644 src/components/expense-documents-input.tsx (limited to 'src') diff --git a/src/app/api/s3-upload/route.ts b/src/app/api/s3-upload/route.ts new file mode 100644 index 0000000..042d357 --- /dev/null +++ b/src/app/api/s3-upload/route.ts @@ -0,0 +1,8 @@ +import { sanitizeKey } from 'next-s3-upload' +import { POST as route } from 'next-s3-upload/route' + +export const POST = route.configure({ + key(req, filename) { + return sanitizeKey(filename).toLowerCase() + }, +}) diff --git a/src/components/expense-documents-input.tsx b/src/components/expense-documents-input.tsx new file mode 100644 index 0000000..11e5708 --- /dev/null +++ b/src/components/expense-documents-input.tsx @@ -0,0 +1,145 @@ +import { Button } from '@/components/ui/button' +import { + Dialog, + DialogClose, + DialogContent, + DialogTrigger, +} from '@/components/ui/dialog' +import { ToastAction } from '@/components/ui/toast' +import { useToast } from '@/components/ui/use-toast' +import { randomId } from '@/lib/api' +import { ExpenseFormValues } from '@/lib/schemas' +import { Loader2, Plus, Trash, X } from 'lucide-react' +import { getImageData, useS3Upload } from 'next-s3-upload' +import Image from 'next/image' +import { useState } from 'react' + +type Props = { + documents: ExpenseFormValues['documents'] + updateDocuments: (documents: ExpenseFormValues['documents']) => void +} + +export function ExpenseDocumentsInput({ documents, updateDocuments }: Props) { + const [pending, setPending] = useState(false) + const { FileInput, openFileDialog, uploadToS3 } = useS3Upload() + const { toast } = useToast() + + const handleFileChange = async (file: File) => { + const upload = async () => { + try { + setPending(true) + const { width, height } = await getImageData(file) + if (!width || !height) throw new Error('Cannot get image dimensions') + const { url } = await uploadToS3(file) + updateDocuments([...documents, { id: randomId(), url, width, height }]) + } catch (err) { + console.error(err) + toast({ + title: 'Error while uploading document', + description: + 'Something wrong happened when uploading the document. Please retry later or select a different file.', + variant: 'destructive', + action: ( + upload()}> + Retry + + ), + }) + } finally { + setPending(false) + } + } + upload() + } + + return ( +
+ + +
+ {documents.map((doc) => ( + { + updateDocuments(documents.filter((d) => d.id !== doc.id)) + }} + /> + ))} + +
+ +
+
+
+ ) +} + +export function DocumentThumbnail({ + document, + deleteDocument, +}: { + document: ExpenseFormValues['documents'][number] + deleteDocument: () => void +}) { + const [open, setOpen] = useState(false) + + return ( + + + + + +
+ + + + +
+ {/* eslint-disable-next-line @next/next/no-img-element */} + +
+
+ ) +} diff --git a/src/components/expense-form.tsx b/src/components/expense-form.tsx index 92e24d0..a847949 100644 --- a/src/components/expense-form.tsx +++ b/src/components/expense-form.tsx @@ -1,6 +1,7 @@ 'use client' import { AsyncButton } from '@/components/async-button' import { CategorySelector } from '@/components/category-selector' +import { ExpenseDocumentsInput } from '@/components/expense-documents-input' import { SubmitButton } from '@/components/submit-button' import { Button } from '@/components/ui/button' import { @@ -83,6 +84,7 @@ export function ExpenseForm({ })), splitMode: expense.splitMode, isReimbursement: expense.isReimbursement, + documents: expense.documents, } : searchParams.get('reimbursement') ? { @@ -100,6 +102,7 @@ export function ExpenseForm({ ], isReimbursement: true, splitMode: 'EVENLY', + documents: [], } : { title: '', @@ -114,6 +117,7 @@ export function ExpenseForm({ paidBy: getSelectedPayer(), isReimbursement: false, splitMode: 'EVENLY', + documents: [], }, }) @@ -506,6 +510,31 @@ export function ExpenseForm({ + {process.env.NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS && ( + + + + Attach documents + + + See and attach receipts to the expense. + + + + ( + + )} + /> + + + )} +
Creating… : <>Saving…} 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 }, }) } diff --git a/src/lib/env.ts b/src/lib/env.ts index 3db102e..9912945 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -1,16 +1,37 @@ -import { z } from 'zod' +import { ZodIssueCode, z } from 'zod' -const envSchema = z.object({ - POSTGRES_URL_NON_POOLING: z.string().url(), - POSTGRES_PRISMA_URL: z.string().url(), - NEXT_PUBLIC_BASE_URL: z - .string() - .optional() - .default( - process.env.VERCEL_URL - ? `https://${process.env.VERCEL_URL}` - : 'http://localhost:3000', - ), -}) +const envSchema = z + .object({ + POSTGRES_URL_NON_POOLING: z.string().url(), + POSTGRES_PRISMA_URL: z.string().url(), + NEXT_PUBLIC_BASE_URL: z + .string() + .optional() + .default( + process.env.VERCEL_URL + ? `https://${process.env.VERCEL_URL}` + : 'http://localhost:3000', + ), + NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS: z.coerce.boolean().default(false), + S3_UPLOAD_KEY: z.string().optional(), + S3_UPLOAD_SECRET: z.string().optional(), + S3_UPLOAD_BUCKET: z.string().optional(), + S3_UPLOAD_REGION: z.string().optional(), + }) + .superRefine((env, ctx) => { + if ( + env.NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS && + (!env.S3_UPLOAD_BUCKET || + !env.S3_UPLOAD_KEY || + !env.S3_UPLOAD_REGION || + !env.S3_UPLOAD_SECRET) + ) { + ctx.addIssue({ + code: ZodIssueCode.custom, + message: + 'If NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS is specified, then S3_* must be specified too', + }) + } + }) export const env = envSchema.parse(process.env) diff --git a/src/lib/schemas.ts b/src/lib/schemas.ts index be90481..981cea3 100644 --- a/src/lib/schemas.ts +++ b/src/lib/schemas.ts @@ -105,6 +105,16 @@ export const expenseFormSchema = z ) .default('EVENLY'), isReimbursement: z.boolean(), + documents: z + .array( + z.object({ + id: z.string(), + url: z.string().url(), + width: z.number().int().min(1), + height: z.number().int().min(1), + }), + ) + .default([]), }) .superRefine((expense, ctx) => { let sum = 0 -- cgit v1.3