- Nuevo modelo `Person` (`people`): contacto y notas por profe, con teléfono único (índice parcial `people_owner_phone_key`). `Attendee` pasa a ser la membresía (`group_id` + `person_id`, único por grupo). - La cuenta de la app de alumnos se vincula a la persona (`student_account_people`): una sola cuenta ve todos sus grupos. - Migración unifica attendees del mismo profe y teléfono, y fusiona sus cuentas de alumno (conserva la usada más recientemente). - `POST /groups/:gid/attendees/:aid/add-to-group` y `GET /people/lookup` para detectar personas existentes al cargar; el alta responde `409 person_exists` y acepta `personId`. Bulk, lista de espera y preinscripciones vinculan solas. - El límite del plan cuenta personas con al menos un grupo activo. - Web: "Agregar a otro grupo", sección "También está en" en la ficha y aviso "Ya tenés a esta persona" en el alta individual. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type { AttendeeDto, ProblemDetails, Result } from '@gruperly/shared';
|
|
import { err, ok } from '@gruperly/shared';
|
|
import { capacityReachedProblem } from '@/http/problem-builders';
|
|
import { attendeeWithPerson, toAttendeeDto } from '@/modules/attendees/lib/helpers';
|
|
import { alreadyInGroupProblem, findOrCreatePerson } from '@/modules/people/lib/people';
|
|
import { type GroupWaitlistEntryRecord } from './helpers';
|
|
|
|
export type PromoteDb = Pick<PrismaClient, 'attendee' | 'groupWaitlistEntry' | 'person'>;
|
|
|
|
type PromoteGroup = {
|
|
id: string;
|
|
capacity: number | null;
|
|
createdById: string;
|
|
};
|
|
|
|
export async function findFirstPendingWaitlistEntry(
|
|
db: PromoteDb,
|
|
groupId: string,
|
|
): Promise<GroupWaitlistEntryRecord | null> {
|
|
const record = await db.groupWaitlistEntry.findFirst({
|
|
where: { groupId, status: 'PENDING' },
|
|
orderBy: { createdAt: 'asc' },
|
|
});
|
|
return record as GroupWaitlistEntryRecord | null;
|
|
}
|
|
|
|
/* Whether someone can join `group`: free capacity (ACTIVE members) and the
|
|
person (when there already is one) not being a member there yet. Shared by
|
|
the waitlist promotion, the member move and "Agregar a otro grupo". */
|
|
export async function checkCanJoinGroup(
|
|
db: Pick<PrismaClient, 'attendee'>,
|
|
group: Pick<PromoteGroup, 'id' | 'capacity'>,
|
|
personId: string | null,
|
|
): Promise<Result<void, ProblemDetails>> {
|
|
if (group.capacity !== null) {
|
|
const currentCount = await db.attendee.count({
|
|
where: { groupId: group.id, status: 'ACTIVE' },
|
|
});
|
|
if (currentCount >= group.capacity) {
|
|
return err(capacityReachedProblem(group.capacity));
|
|
}
|
|
}
|
|
|
|
if (personId) {
|
|
const existing = await db.attendee.findFirst({
|
|
where: { groupId: group.id, personId },
|
|
select: { id: true },
|
|
});
|
|
if (existing) {
|
|
return err(alreadyInGroupProblem());
|
|
}
|
|
}
|
|
|
|
return ok(undefined);
|
|
}
|
|
|
|
/* The entry has its own contact data; if its phone is already one of the
|
|
teacher's people, the new membership goes to that person. */
|
|
export async function promoteWaitlistEntryRecord(
|
|
db: PromoteDb,
|
|
group: PromoteGroup,
|
|
entry: GroupWaitlistEntryRecord,
|
|
): Promise<Result<AttendeeDto, ProblemDetails>> {
|
|
const person = await findOrCreatePerson(db, group.createdById, {
|
|
fullName: entry.fullName,
|
|
phone: entry.phone,
|
|
email: entry.email,
|
|
notes: entry.notes,
|
|
});
|
|
|
|
const canJoin = await checkCanJoinGroup(db, group, person.created ? null : person.id);
|
|
if (!canJoin.ok) {
|
|
return canJoin;
|
|
}
|
|
|
|
const attendee = await db.attendee.create({
|
|
data: { groupId: group.id, personId: person.id },
|
|
include: attendeeWithPerson,
|
|
});
|
|
|
|
return ok(toAttendeeDto(attendee));
|
|
}
|
|
|
|
export type PromoteFirstResult = {
|
|
attendee: AttendeeDto;
|
|
entry: GroupWaitlistEntryRecord;
|
|
};
|
|
|
|
export async function promoteFirstPendingWaitlistEntry(
|
|
db: PromoteDb,
|
|
group: PromoteGroup,
|
|
): Promise<Result<PromoteFirstResult | null, ProblemDetails>> {
|
|
const entry = await findFirstPendingWaitlistEntry(db, group.id);
|
|
if (!entry) {
|
|
return ok(null);
|
|
}
|
|
|
|
const result = await promoteWaitlistEntryRecord(db, group, entry);
|
|
if (!result.ok) {
|
|
return result;
|
|
}
|
|
|
|
return ok({ attendee: result.value, entry });
|
|
} |