diff --git a/apps/backend/prisma/migrations/20261028120000_add_reported_payment_note/migration.sql b/apps/backend/prisma/migrations/20261028120000_add_reported_payment_note/migration.sql new file mode 100644 index 0000000..28c4ff1 --- /dev/null +++ b/apps/backend/prisma/migrations/20261028120000_add_reported_payment_note/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "reported_payments" ADD COLUMN "note" TEXT; diff --git a/apps/backend/prisma/models/student-access.prisma b/apps/backend/prisma/models/student-access.prisma index 9f00c64..3c9d16b 100644 --- a/apps/backend/prisma/models/student-access.prisma +++ b/apps/backend/prisma/models/student-access.prisma @@ -203,6 +203,8 @@ model ReportedPayment { receiptMime String @map("receipt_mime") receiptName String @map("receipt_name") receiptSize Int @map("receipt_size") + // Optional message from the student to the teacher. + note String? status ReportedPaymentStatus @default(REPORTED) reviewedById String? @map("reviewed_by_id") reviewedAt DateTime? @map("reviewed_at") diff --git a/apps/backend/src/modules/payments/features/reported/use-case.ts b/apps/backend/src/modules/payments/features/reported/use-case.ts index fc583fe..b50fee0 100644 --- a/apps/backend/src/modules/payments/features/reported/use-case.ts +++ b/apps/backend/src/modules/payments/features/reported/use-case.ts @@ -26,6 +26,7 @@ const reportSelect = { createdAt: true, totalAmount: true, currency: true, + note: true, receiptKey: true, receiptMime: true, receiptName: true, @@ -206,6 +207,7 @@ export class ReportedPayments { createdAt: row.createdAt.toISOString(), totalAmount: row.totalAmount.toNumber(), currency: row.currency, + note: row.note, fees: row.items .map(({ payment }) => ({ id: payment.id, diff --git a/apps/backend/src/modules/student-portal/features/fees/route.ts b/apps/backend/src/modules/student-portal/features/fees/route.ts index ae63295..d30d3b4 100644 --- a/apps/backend/src/modules/student-portal/features/fees/route.ts +++ b/apps/backend/src/modules/student-portal/features/fees/route.ts @@ -17,7 +17,7 @@ route.get('/fees', studentSession, validate.query(StudentAttendeeQuerySchema), a return resultJson(c, await new GetStudentFees().execute(accountId, attendeeId)); }); -// multipart/form-data: attendeeId, paymentIds (repeated) and file. +// multipart/form-data: attendeeId, paymentIds (repeated), file and an optional note. route.post('/payment-reports', studentSession, async (c) => { const { accountId } = currentStudent(c); // Reject oversized bodies before buffering them (10 MB + room for the fields). @@ -35,6 +35,7 @@ route.post('/payment-reports', studentSession, async (c) => { const attendeeId = form.get('attendeeId'); const paymentIds = form.getAll('paymentIds').filter((value): value is string => typeof value === 'string' && value.length > 0); const file = form.get('file'); + const note = form.get('note'); if (typeof attendeeId !== 'string' || paymentIds.length === 0 || !(file instanceof File)) { return problemJson(c, validationProblem({ detail: 'Elegí al menos una cuota y adjuntá el comprobante.' })); } @@ -42,6 +43,7 @@ route.post('/payment-reports', studentSession, async (c) => { const result = await new ReportStudentPayment().execute(accountId, { attendeeId, paymentIds, + note: typeof note === 'string' ? note : null, file: { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()) }, }); return resultJson(c, result, { status: 201 }); diff --git a/apps/backend/src/modules/student-portal/features/fees/use-case.ts b/apps/backend/src/modules/student-portal/features/fees/use-case.ts index b8da8d4..677bc31 100644 --- a/apps/backend/src/modules/student-portal/features/fees/use-case.ts +++ b/apps/backend/src/modules/student-portal/features/fees/use-case.ts @@ -54,6 +54,7 @@ export class GetStudentFees { status: true, createdAt: true, reviewedAt: true, + note: true, rejectionReason: true, items: { select: { payment: { select: { id: true, description: true, dueDate: true } } } }, }, @@ -85,6 +86,7 @@ export class GetStudentFees { status: REPORT_STATUS[report.status], createdAt: report.createdAt.toISOString(), reviewedAt: report.reviewedAt?.toISOString() ?? null, + note: report.note, rejectionReason: report.rejectionReason, fees: report.items.map(({ payment }) => ({ id: payment.id, diff --git a/apps/backend/src/modules/student-portal/features/payment-reports/use-case.ts b/apps/backend/src/modules/student-portal/features/payment-reports/use-case.ts index 3422fa0..c936369 100644 --- a/apps/backend/src/modules/student-portal/features/payment-reports/use-case.ts +++ b/apps/backend/src/modules/student-portal/features/payment-reports/use-case.ts @@ -1,7 +1,7 @@ import { randomBytes } from 'node:crypto'; import { Prisma, type PrismaClient } from '@generated/prisma/client'; import type { ProblemDetails, Result } from '@gruperly/shared'; -import { err, ok } from '@gruperly/shared'; +import { err, ok, REPORTED_PAYMENT_NOTE_MAX } from '@gruperly/shared'; import { conflictProblem, validationProblem } from '@/http/problem-builders'; import { detectReceiptType, RECEIPT_EXTENSIONS, RECEIPT_MAX_BYTES } from '@/lib/file-type'; import prisma, { UnitOfWork } from '@/lib/prisma'; @@ -19,6 +19,7 @@ type ReportPaymentDeps = { export type ReportPaymentInput = { attendeeId: string; paymentIds: string[]; + note?: string | null; file: { name: string; bytes: Uint8Array }; }; @@ -37,6 +38,11 @@ export class ReportStudentPayment { const attendee = await findLinkedAttendee(db, accountId, input.attendeeId); if (!attendee) return err(studentAttendeeNotFoundProblem()); + const note = input.note?.trim() || null; + if (note && note.length > REPORTED_PAYMENT_NOTE_MAX) { + return err(validationProblem({ detail: `La nota puede tener hasta ${REPORTED_PAYMENT_NOTE_MAX} caracteres.`, errors: { note: ['too_long'] } })); + } + if (input.file.bytes.length === 0 || input.file.bytes.length > RECEIPT_MAX_BYTES) { return err(validationProblem({ detail: 'El comprobante tiene que pesar hasta 10 MB.', errors: { file: ['too_large'] } })); } @@ -83,6 +89,7 @@ export class ReportStudentPayment { receiptMime: mime, receiptName: input.file.name.slice(0, 200) || `comprobante.${RECEIPT_EXTENSIONS[mime]}`, receiptSize: input.file.bytes.length, + note, createdAt: now, items: { create: paymentIds.map((paymentId) => ({ paymentId })) }, }, diff --git a/apps/backend/test/student-portal-files.test.ts b/apps/backend/test/student-portal-files.test.ts index 39b34b8..7c9175f 100644 --- a/apps/backend/test/student-portal-files.test.ts +++ b/apps/backend/test/student-portal-files.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from 'bun:test'; +import { REPORTED_PAYMENT_NOTE_MAX } from '@gruperly/shared'; import { detectReceiptType, RECEIPT_MAX_BYTES } from '@/lib/file-type'; import type { FileStorage } from '@/lib/storage'; import { occurrencesBetween, startOfWeekInTimeZone } from '@/modules/home/lib/scheduler'; @@ -45,6 +46,15 @@ describe('tipo real del comprobante (magic bytes)', () => { const noStorage = new ReportStudentPayment({ db: db as never, storage: null }); const unavailable = await noStorage.execute('acc', { attendeeId: 'a1', paymentIds: ['p1'], file: { name: 'a.pdf', bytes: pad(ascii('%PDF-1.7')) } }); expect(!unavailable.ok && unavailable.error.status).toBe(503); + + const longNote = await useCase.execute('acc', { + attendeeId: 'a1', + paymentIds: ['p1'], + note: 'x'.repeat(REPORTED_PAYMENT_NOTE_MAX + 1), + file: { name: 'a.pdf', bytes: pad(ascii('%PDF-1.7')) }, + }); + expect(!longNote.ok && longNote.error.errors).toEqual({ note: ['too_long'] }); + expect(puts).toEqual([]); }); }); diff --git a/apps/web/src/features/payments/ReportedPaymentCard.tsx b/apps/web/src/features/payments/ReportedPaymentCard.tsx index 56c56d2..eacfdde 100644 --- a/apps/web/src/features/payments/ReportedPaymentCard.tsx +++ b/apps/web/src/features/payments/ReportedPaymentCard.tsx @@ -116,6 +116,13 @@ export function ReportedPaymentCard({ report, onOpenReceipt, onConfirm, onReject