Files
gruperly/apps/backend/src/modules/payments/lib/review-notifications.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

57 lines
2.3 KiB
TypeScript

import type { PrismaClient } from '@generated/prisma/client';
import type { CountryCode } from '@gruperly/shared';
import { type EmailProvider, emailProvider } from '@/lib/email';
import prisma from '@/lib/prisma';
import { logger } from '@/logger';
import { formatAmount } from '@/modules/whatsapp/lib/format';
// Long enough to cover the "Deshacer" on the card: an undone review emails nothing.
const NOTIFY_DELAY_MS = 60 * 1000;
export type ReviewNotifier = (reportId: string, reviewedAt: Date) => void;
/* Tells the student by email (only if they confirmed a recovery email) once
the review settles. The in-app banner is the main channel; this is a
courtesy, so an in-process timer lost on a restart is acceptable. */
export const scheduleReviewNotification: ReviewNotifier = (reportId, reviewedAt) => {
const timer = setTimeout(() => {
void sendReviewNotification(reportId, reviewedAt).catch((error) =>
logger.error({ error, reportId }, 'Payment review notification failed'),
);
}, NOTIFY_DELAY_MS);
timer.unref?.();
};
export async function sendReviewNotification(
reportId: string,
reviewedAt: Date,
deps: { db?: Pick<PrismaClient, 'reportedPayment'>; email?: Pick<EmailProvider, 'sendStudentPaymentReviewed'> } = {},
): Promise<boolean> {
const db = deps.db ?? prisma;
const report = await db.reportedPayment.findUnique({
where: { id: reportId },
select: {
status: true,
reviewedAt: true,
rejectionReason: true,
totalAmount: true,
currency: true,
group: { select: { owner: { select: { country: true } } } },
account: { select: { name: true, recoveryEmail: true, recoveryEmailVerifiedAt: true } },
},
});
// Undone or reviewed again since: this notification is stale.
if (!report || report.status === 'REPORTED' || report.reviewedAt?.getTime() !== reviewedAt.getTime()) return false;
const account = report.account;
if (!account?.recoveryEmail || !account.recoveryEmailVerifiedAt) return false;
await (deps.email ?? emailProvider).sendStudentPaymentReviewed({
email: account.recoveryEmail,
name: account.name,
confirmed: report.status === 'CONFIRMED',
amount: formatAmount(report.totalAmount.toNumber(), report.currency, report.group.owner.country as CountryCode | null),
reason: report.rejectionReason,
});
return true;
}