Files
gruperly/apps/backend/src/modules/student-portal/features/fees/route.ts
Jose Selesan 060a4f09e4 feat: app de alumnos sin contraseña (invitación de un solo uso, avisos de falta y pagos informados)
- Acceso del alumno o tutor con link de un solo uso (hash SHA-256, consumo
  atómico, el GET no consume), sesión propia de 90 días deslizante, passkeys
  opcionales (SimpleWebAuthn) y mail de recuperación con rate limit.
- Lado alumno (/i/:token, /acceso, /alumno/*): inicio, clases, avisar falta,
  pagos, informar pago con comprobante (S3 privado, magic bytes) y perfil.
- Lado profe: acceso a la app en la ficha del alumno (invitar/reinvitar),
  "Pagos para verificar" en Cobros con badge, faltas avisadas en la ficha de
  grupo y datos para transferir en Ajustes.
- "Requiere atención" y el recordatorio de WhatsApp excluyen cuotas informadas.
- Zona horaria por defecto: Buenos Aires.
- Migración add_student_access, tests contra Postgres real (test:db) y docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 15:39:09 -03:00

51 lines
2.2 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) and file.
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');
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,
file: { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()) },
});
return resultJson(c, result, { status: 201 });
});
export default route;