53 lines
2.3 KiB
TypeScript
53 lines
2.3 KiB
TypeScript
import { type StudentAttendeeQuery, StudentAttendeeQuerySchema } from '@gruperly/shared';
|
|
import { Hono } from 'hono';
|
|
import type { BackendEnv } from '@/http/env';
|
|
import { validationProblem } from '@/http/problem-builders';
|
|
import { problemJson, resultJson } from '@/http/problem-details';
|
|
import { validate } from '@/http/validate';
|
|
import { RECEIPT_MAX_BYTES } from '@/lib/file-type';
|
|
import { currentStudent, studentSession } from '../../middleware';
|
|
import { ReportStudentPayment } from '../payment-reports/use-case';
|
|
import { GetStudentFees } from './use-case';
|
|
|
|
const route = new Hono<BackendEnv>();
|
|
|
|
route.get('/fees', studentSession, validate.query(StudentAttendeeQuerySchema), async (c) => {
|
|
const { accountId } = currentStudent(c);
|
|
const { attendeeId } = c.req.valid('query') as StudentAttendeeQuery;
|
|
return resultJson(c, await new GetStudentFees().execute(accountId, attendeeId));
|
|
});
|
|
|
|
// 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).
|
|
const length = Number(c.req.header('Content-Length') ?? 0);
|
|
if (length > RECEIPT_MAX_BYTES + 64 * 1024) {
|
|
return problemJson(c, validationProblem({ detail: 'El comprobante tiene que pesar hasta 10 MB.', errors: { file: ['too_large'] } }));
|
|
}
|
|
|
|
let form: FormData;
|
|
try {
|
|
form = await c.req.formData();
|
|
} catch {
|
|
return problemJson(c, validationProblem({ detail: 'Invalid form data' }));
|
|
}
|
|
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.' }));
|
|
}
|
|
|
|
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 });
|
|
});
|
|
|
|
export default route;
|