feat: add absence notification and analytics features
- Implemented AbsenceNotifyView for notifying absence with session details. - Created AnalyticsView for displaying group analytics and managing students at risk. - Added ClassAttendanceView for marking attendance in classes. - Defined new schemas for analytics and attendance in shared package.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
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<ClassesDb, 'classSession' | 'attendee'>;
|
||||
unitOfWork?: UnitOfWork;
|
||||
now?: Date;
|
||||
releaseHours?: number;
|
||||
};
|
||||
|
||||
export class NotifySessionAbsence {
|
||||
constructor(private readonly deps: NotifySessionAbsenceDeps = {}) {}
|
||||
|
||||
async execute(
|
||||
sessionId: string,
|
||||
payload: NotifyAbsence,
|
||||
userId: string,
|
||||
): Promise<Result<NotifyAbsenceResult, ProblemDetails>> {
|
||||
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,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user