import type { AttendeeDetailDto, AttendeeDto, AttendeeListItem } from '@gruperly/shared'; export type AttendeeRecord = { id: string; groupId: string; fullName: string; email: string | null; phone: string | null; guardianName: string | null; guardianPhone: string | null; notes: string | null; notifyToken?: string | null; createdAt: Date; updatedAt: Date; }; /** 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']; }; export function toAttendeeDto(record: AttendeeRecord): AttendeeDto { return { id: record.id, groupId: record.groupId, fullName: record.fullName, email: record.email, phone: record.phone, guardianName: record.guardianName, guardianPhone: record.guardianPhone, notes: record.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), }; } /* 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; }