- 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>
127 lines
4.2 KiB
TypeScript
127 lines
4.2 KiB
TypeScript
import type { AttendeeDetailDto, AttendeeDto, AttendeeListItem } from '@gruperly/shared';
|
|
|
|
/* Contact data of the person behind an attendee (shared by all their groups). */
|
|
export const personContactSelect = {
|
|
id: true,
|
|
fullName: true,
|
|
email: true,
|
|
phone: true,
|
|
guardianName: true,
|
|
guardianPhone: true,
|
|
notes: true,
|
|
} as const;
|
|
|
|
/* `include` for any attendee query that ends up in `toAttendeeDto`. */
|
|
export const attendeeWithPerson = { person: { select: personContactSelect } } as const;
|
|
|
|
export type PersonContactRecord = {
|
|
id: string;
|
|
fullName: string;
|
|
email: string | null;
|
|
phone: string | null;
|
|
guardianName: string | null;
|
|
guardianPhone: string | null;
|
|
notes: string | null;
|
|
};
|
|
|
|
export type AttendeeRecord = {
|
|
id: string;
|
|
groupId: string;
|
|
personId: string;
|
|
notifyToken?: string | null;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
person: PersonContactRecord;
|
|
};
|
|
|
|
/** Listing item with the attendance and payment summary already resolved. */
|
|
export type AttendeeListItemRecord = AttendeeRecord & {
|
|
attendances: { status: AttendeeListItem['recentAttendance'][number] }[];
|
|
payments: {
|
|
status: NonNullable<AttendeeListItem['payment']>['status'];
|
|
dueDate: Date;
|
|
}[];
|
|
};
|
|
|
|
export type AttendeeDetailRecord = AttendeeRecord & {
|
|
payments: AttendeeListItemRecord['payments'];
|
|
otherGroups: AttendeeDetailDto['otherGroups'];
|
|
};
|
|
|
|
export function toAttendeeDto(record: AttendeeRecord): AttendeeDto {
|
|
return {
|
|
id: record.id,
|
|
groupId: record.groupId,
|
|
personId: record.personId,
|
|
fullName: record.person.fullName,
|
|
email: record.person.email,
|
|
phone: record.person.phone,
|
|
guardianName: record.person.guardianName,
|
|
guardianPhone: record.person.guardianPhone,
|
|
notes: record.person.notes,
|
|
notifyToken: record.notifyToken ?? null,
|
|
createdAt: record.createdAt.toISOString(),
|
|
updatedAt: record.updatedAt.toISOString(),
|
|
};
|
|
}
|
|
|
|
/* The row that summarizes the current charge. If something is unpaid the
|
|
oldest due date wins: someone carrying an old charge has to see how many
|
|
days late they are, not the due date of a new charge. If nothing is left
|
|
unpaid, the last closed payment serves to mark the row as ok. */
|
|
function pickRelevantPayment(
|
|
payments: AttendeeListItemRecord['payments'],
|
|
): AttendeeListItem['payment'] {
|
|
// CANCELLED is not a charge. The query already filters them out, but dropping
|
|
// them here too keeps a voided charge from winning the row for being the oldest.
|
|
const byDueDate = payments
|
|
.filter((payment) => payment.status !== 'CANCELLED')
|
|
.sort((a, b) => a.dueDate.getTime() - b.dueDate.getTime());
|
|
const outstanding = byDueDate.find((payment) => payment.status !== 'PAID');
|
|
const relevant = outstanding ?? byDueDate[byDueDate.length - 1];
|
|
|
|
if (!relevant) {
|
|
return null;
|
|
}
|
|
return { status: relevant.status, dueDate: relevant.dueDate.toISOString() };
|
|
}
|
|
|
|
export function toAttendeeDetailDto(record: AttendeeDetailRecord): AttendeeDetailDto {
|
|
return {
|
|
...toAttendeeDto(record),
|
|
payment: pickRelevantPayment(record.payments),
|
|
otherGroups: record.otherGroups,
|
|
};
|
|
}
|
|
|
|
/* The query comes in descending order; the row shows them from oldest to most
|
|
recent so the dot on the right is the latest event. */
|
|
export function toAttendeeListItemDto(record: AttendeeListItemRecord): AttendeeListItem {
|
|
return {
|
|
...toAttendeeDto(record),
|
|
recentAttendance: [...record.attendances].reverse().map((attendance) => attendance.status),
|
|
payment: pickRelevantPayment(record.payments),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Normalizes phone numbers by stripping spaces, dashes, parentheses and dots.
|
|
* Keeps a leading '+' for international numbers.
|
|
*/
|
|
export function normalizePhone(rawPhone: string): string {
|
|
const trimmed = rawPhone.trim();
|
|
const hasPlus = trimmed.startsWith('+');
|
|
const digitsOnly = trimmed.replace(/\D/g, '');
|
|
return hasPlus ? `+${digitsOnly}` : digitsOnly;
|
|
}
|
|
|
|
/* For queries that select a few person fields next to the attendee's: turns
|
|
`{ id, person: { fullName } }` into `{ id, fullName }`, so the code reading
|
|
flat rows stays as it was. */
|
|
export function flattenPerson<TRow extends { person: object }>(
|
|
row: TRow,
|
|
): Omit<TRow, 'person'> & TRow['person'] {
|
|
const { person, ...rest } = row;
|
|
return { ...rest, ...person };
|
|
}
|