import type { NotifyAbsence, NotifyAbsenceResult, ProblemDetails, Result, } from '@gruperly/shared'; import { err } from '@gruperly/shared'; import { validationProblem } from '@/http/problem-builders'; import prisma, { UnitOfWork } from '@/lib/prisma'; import { absenceReleaseHours, type ClassesDb, ensureSessionAccess, registerAbsence, sessionNotFound, } from '../../lib'; type NotifySessionAbsenceDeps = { db?: Pick; unitOfWork?: UnitOfWork; now?: Date; releaseHours?: number; }; export class NotifySessionAbsence { constructor(private readonly deps: NotifySessionAbsenceDeps = {}) {} async execute( sessionId: string, payload: NotifyAbsence, userId: string, ): Promise> { const db = this.deps.db ?? prisma; const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma); const now = this.deps.now ?? new Date(); const releaseHours = this.deps.releaseHours ?? absenceReleaseHours(); if (payload.classSessionId !== sessionId) { return err( validationProblem({ detail: 'El identificador de la clase no coincide con la ruta.', }), ); } const session = await db.classSession.findUnique({ where: { id: sessionId }, include: { group: { include: { members: { where: { userId } } } }, }, }); if (!session) { return err(sessionNotFound(sessionId)); } const access = ensureSessionAccess(session, userId); if (!access.ok) { return access; } return registerAbsence( { db, unitOfWork }, { attendeeId: payload.attendeeId, classSessionId: sessionId, now, releaseHours, }, ); } }