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['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( row: TRow, ): Omit & TRow['person'] { const { person, ...rest } = row; return { ...rest, ...person }; }