106 lines
3.7 KiB
TypeScript
106 lines
3.7 KiB
TypeScript
import type { AttendeeListItem, AttendanceStatus } from '@gruperly/shared'
|
|
import { RECENT_ATTENDANCE_LIMIT } from '@gruperly/shared'
|
|
import { i18n } from '../../../i18n/instance'
|
|
import { PAYMENT_DUE_SOON_DAYS } from './constants'
|
|
|
|
/* The listing's row does not ask the backend for presentation verdicts: it asks
|
|
for the raw data (recent classes, current charge) and the text comes from
|
|
here. The days remaining change depending on when you look, so they cannot
|
|
come stored from the server. */
|
|
|
|
/** Calendar days between two dates, without times: "due on the 10th" has no
|
|
* time of day. */
|
|
function calendarDaysBetween(from: Date, to: Date): number {
|
|
const startOf = (date: Date) =>
|
|
new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime()
|
|
return Math.round((startOf(to) - startOf(from)) / 86_400_000)
|
|
}
|
|
|
|
export type PaymentState =
|
|
| { kind: 'settled' }
|
|
| { kind: 'dueSoon'; days: number }
|
|
| { kind: 'overdue'; days: number }
|
|
|
|
/**
|
|
* What to mark on the charge in the row.
|
|
*
|
|
* Returns `null` when there is nothing to warn about: either the attendee has no
|
|
* recorded charge, or the one they have is due later than the warning window and
|
|
* has not expired yet. That is not the same as "ok", which is a closed payment.
|
|
*
|
|
* A `PENDING` whose due date has already passed counts as overdue, same as an
|
|
* `OVERDUE`: the stored state often stays at PENDING until someone updates it by
|
|
* hand, and the row cannot depend on that discipline.
|
|
*/
|
|
export function resolvePaymentState(
|
|
payment: AttendeeListItem['payment'],
|
|
today: Date = new Date(),
|
|
): PaymentState | null {
|
|
if (!payment) return null
|
|
if (payment.status === 'PAID') return { kind: 'settled' }
|
|
|
|
const days = calendarDaysBetween(today, new Date(payment.dueDate))
|
|
|
|
if (payment.status === 'OVERDUE' || days < 0) {
|
|
return { kind: 'overdue', days: Math.abs(days) }
|
|
}
|
|
if (days <= PAYMENT_DUE_SOON_DAYS) {
|
|
return { kind: 'dueSoon', days }
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function isOverdue(state: PaymentState | null): boolean {
|
|
return state?.kind === 'overdue'
|
|
}
|
|
|
|
export function paymentStateLabel(state: PaymentState): string {
|
|
switch (state.kind) {
|
|
case 'settled':
|
|
return i18n.t('groupDetail:row.settled')
|
|
case 'dueSoon':
|
|
// "Vence en 0 d" is not natural: today and tomorrow have words.
|
|
if (state.days === 0) return i18n.t('groupDetail:row.dueToday')
|
|
if (state.days === 1) return i18n.t('groupDetail:row.dueTomorrow')
|
|
return i18n.t('groupDetail:row.dueIn', { days: state.days })
|
|
case 'overdue':
|
|
return i18n.t('groupDetail:row.overdue', { days: state.days })
|
|
}
|
|
}
|
|
|
|
export type AttendanceSummary = {
|
|
/** States to draw, from the oldest to the most recent. */
|
|
dots: AttendanceStatus[]
|
|
attended: number
|
|
recorded: number
|
|
/** "Sin clases aún" when nothing is recorded. */
|
|
label: string
|
|
}
|
|
|
|
/** The last recorded class points, counted over that same window. */
|
|
export function summarizeAttendance(
|
|
recentAttendance: AttendanceStatus[],
|
|
): AttendanceSummary {
|
|
// The window is set by the backend; the final cut prevents a row from getting
|
|
// longer if the contract ever delivers more classes than expected.
|
|
const dots = recentAttendance.slice(-RECENT_ATTENDANCE_LIMIT)
|
|
const recorded = dots.length
|
|
|
|
if (recorded === 0) {
|
|
return { dots, attended: 0, recorded: 0, label: i18n.t('groupDetail:row.noClasses') }
|
|
}
|
|
|
|
const attended = dots.filter((status) => status === 'PRESENT').length
|
|
return {
|
|
dots,
|
|
attended,
|
|
recorded,
|
|
label: i18n.t('groupDetail:row.attended', { attended, count: recorded }),
|
|
}
|
|
}
|
|
|
|
/** The listing's fixed alphabetical order. */
|
|
export function byFullName(a: AttendeeListItem, b: AttendeeListItem): number {
|
|
return a.fullName.localeCompare(b.fullName, 'es', { sensitivity: 'base' })
|
|
}
|