- 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.
102 lines
2.8 KiB
TypeScript
102 lines
2.8 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type { AttendeeDto, JoinGroupViaInvite, ProblemDetails, Result } from '@gruperly/shared';
|
|
import { err, ok } from '@gruperly/shared';
|
|
import { conflictProblem, notFoundResourceProblem } from '@/http/problem-builders';
|
|
import prisma from '@/lib/prisma';
|
|
import { type AttendeeRecord, normalizePhone, toAttendeeDto } from '@/modules/attendees/lib/helpers';
|
|
|
|
type JoinViaInviteDeps = {
|
|
db?: Pick<PrismaClient, 'group' | 'attendee' | 'groupWaitlistEntry'>;
|
|
};
|
|
|
|
export type JoinViaInviteResult = {
|
|
status: 'registered' | 'waitlisted';
|
|
message: string;
|
|
attendee: AttendeeDto | null;
|
|
};
|
|
|
|
export class JoinViaInvite {
|
|
constructor(private readonly deps: JoinViaInviteDeps = {}) {}
|
|
|
|
async execute(
|
|
token: string,
|
|
payload: JoinGroupViaInvite,
|
|
): Promise<Result<JoinViaInviteResult, ProblemDetails>> {
|
|
const db = this.deps.db ?? prisma;
|
|
|
|
const group = await db.group.findUnique({
|
|
where: { inviteToken: token },
|
|
});
|
|
|
|
if (!group) {
|
|
return err(notFoundResourceProblem('Invitación', token));
|
|
}
|
|
|
|
const phone = normalizePhone(payload.phone);
|
|
const existing = await db.attendee.findFirst({
|
|
where: {
|
|
groupId: group.id,
|
|
OR: [
|
|
{ phone },
|
|
{ phone: payload.phone.trim() },
|
|
],
|
|
},
|
|
});
|
|
|
|
if (existing) {
|
|
return err(
|
|
conflictProblem({
|
|
detail: 'Ya existe un alumno registrado con este número de teléfono en este grupo.',
|
|
code: 'attendee_already_registered',
|
|
}),
|
|
);
|
|
}
|
|
|
|
const fullName = `${payload.firstName.trim()} ${payload.lastName.trim()}`.trim();
|
|
|
|
if (group.capacity !== null) {
|
|
const currentCount = await db.attendee.count({
|
|
where: { groupId: group.id, status: 'ACTIVE' },
|
|
});
|
|
|
|
if (currentCount >= group.capacity) {
|
|
const existingWaitlistEntry = await db.groupWaitlistEntry.findFirst({
|
|
where: { groupId: group.id, phone },
|
|
});
|
|
|
|
if (!existingWaitlistEntry) {
|
|
await db.groupWaitlistEntry.create({
|
|
data: {
|
|
groupId: group.id,
|
|
fullName,
|
|
phone,
|
|
email: payload.email?.trim() || null,
|
|
},
|
|
});
|
|
}
|
|
|
|
return ok({
|
|
status: 'waitlisted',
|
|
message:
|
|
'El grupo está completo. Fuiste agregado a la lista de espera. Te contactaremos cuando haya un lugar disponible.',
|
|
attendee: null,
|
|
});
|
|
}
|
|
}
|
|
|
|
const created = await db.attendee.create({
|
|
data: {
|
|
groupId: group.id,
|
|
fullName,
|
|
phone,
|
|
email: payload.email?.trim() || null,
|
|
},
|
|
});
|
|
|
|
return ok({
|
|
status: 'registered',
|
|
attendee: toAttendeeDto(created as unknown as AttendeeRecord),
|
|
message: 'Inscripción realizada con éxito',
|
|
});
|
|
}
|
|
} |