All files / src/features/appointments/components appointment-view.ts

100% Statements 6/6
87.5% Branches 7/8
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26            39x             4x 4x 7x 7x 7x                
import type { Appointment } from '../api/appointments.schemas'
export type AppointmentView = 'ALL' | 'UPCOMING' | 'REVIEW' | 'COMPLETED' | 'CANCELLED'
export function appointmentGroup(
  appointment: Appointment,
  now: number,
): Exclude<AppointmentView, 'ALL'> {
  return appointment.status === 'SCHEDULED'
    ? new Date(appointment.scheduledAt).getTime() >= now
      ? 'UPCOMING'
      : 'REVIEW'
    : appointment.status
}
export function sortAppointments(items: Appointment[], now: number) {
  const rank = { UPCOMING: 0, REVIEW: 1, COMPLETED: 2, CANCELLED: 3 }
  return [...items].sort((a, b) => {
    const groupA = appointmentGroup(a, now),
      groupB = appointmentGroup(b, now)
    return (
      rank[groupA] - rank[groupB] ||
      (groupA === 'UPCOMING'
        ? a.scheduledAt.localeCompare(b.scheduledAt)
        : b.scheduledAt.localeCompare(a.scheduledAt))
    )
  })
}