85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
import type { AttendeePaymentSummary } from '@gruperly/shared'
|
|
import { UpdateAttendeeSchema } from '@gruperly/shared'
|
|
import { validationMessage } from '../../i18n/validation'
|
|
import { i18n } from '../../i18n/instance'
|
|
|
|
export function contactEmailError(email: string): string | null {
|
|
const result = UpdateAttendeeSchema.safeParse({ email })
|
|
if (result.success) return null
|
|
return validationMessage(result.error.issues[0]?.message ?? 'email.invalid') ?? null
|
|
}
|
|
|
|
export type PaymentPresentation = {
|
|
periodLabel: string
|
|
statusLabel: string
|
|
chipLabel: string
|
|
isDue: boolean
|
|
}
|
|
|
|
export function paymentPresentation(
|
|
payment: AttendeePaymentSummary | null,
|
|
today = new Date(),
|
|
): PaymentPresentation {
|
|
const periodLabel = capitalize(formatMonthAndYear(today))
|
|
if (!payment) {
|
|
return {
|
|
periodLabel,
|
|
statusLabel: i18n.t('attendeeDetail:payments.noCharge'),
|
|
chipLabel: i18n.t('attendeeDetail:payments.upToDate'),
|
|
isDue: false,
|
|
}
|
|
}
|
|
|
|
const isDue = payment.status === 'PENDING' || payment.status === 'OVERDUE'
|
|
return {
|
|
periodLabel,
|
|
statusLabel: paymentStatusLabel(payment.status),
|
|
chipLabel: isDue
|
|
? i18n.t('attendeeDetail:payments.owes', { month: formatMonth(new Date(payment.dueDate)) })
|
|
: i18n.t('attendeeDetail:payments.upToDate'),
|
|
isDue,
|
|
}
|
|
}
|
|
|
|
export function formatNotesUpdatedAt(updatedAt: string, today = new Date()): string {
|
|
const date = new Date(updatedAt)
|
|
if (sameCalendarDate(date, today)) return i18n.t('attendeeDetail:notes.editedToday')
|
|
return i18n.t('attendeeDetail:notes.edited', {
|
|
date: date.toLocaleDateString(i18n.language, { day: 'numeric', month: 'short', year: 'numeric' }),
|
|
})
|
|
}
|
|
|
|
function paymentStatusLabel(status: AttendeePaymentSummary['status']): string {
|
|
switch (status) {
|
|
case 'PAID':
|
|
case 'OVERDUE':
|
|
case 'CANCELLED':
|
|
return i18n.t(`common:paymentStatus.${status}`)
|
|
default:
|
|
return i18n.t('common:paymentStatus.PENDING')
|
|
}
|
|
}
|
|
|
|
function formatMonth(date: Date): string {
|
|
return date.toLocaleDateString(i18n.language, { month: 'long' })
|
|
}
|
|
|
|
function formatMonthAndYear(date: Date): string {
|
|
const parts = new Intl.DateTimeFormat(i18n.language, { month: 'long', year: 'numeric' }).formatToParts(date)
|
|
const month = parts.find((part) => part.type === 'month')?.value ?? ''
|
|
const year = parts.find((part) => part.type === 'year')?.value ?? ''
|
|
return `${month} ${year}`.trim()
|
|
}
|
|
|
|
function capitalize(value: string): string {
|
|
return `${value.charAt(0).toUpperCase()}${value.slice(1)}`
|
|
}
|
|
|
|
function sameCalendarDate(left: Date, right: Date): boolean {
|
|
return (
|
|
left.getFullYear() === right.getFullYear() &&
|
|
left.getMonth() === right.getMonth() &&
|
|
left.getDate() === right.getDate()
|
|
)
|
|
}
|