All files / src/features/appointments/api appointments.selectors.ts

100% Statements 6/6
85.71% Branches 6/7
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 27 28                          4x   5x       4x 2x     2x 1x      
import type { Appointment } from './appointments.schemas'
 
/**
 * The most recent visit that actually measured something.
 *
 * The pair is taken from **one** visit rather than the latest weight and the latest height read
 * independently. Two numbers from two different days, shown side by side with a single date, would
 * be a lie about at least one of them — and this card has room for one date, not two.
 *
 * Cancelled visits are excluded even though the API can hold a measurement on one (it only refuses
 * measurements on a visit still to come): a visit that was called off did not weigh anybody.
 */
export function latestMeasuredVisit(appointments: Appointment[]): Appointment | null {
  const measured = appointments.filter(
    (appointment) =>
      appointment.status === 'COMPLETED' &&
      (appointment.weightGrams !== null || appointment.heightMillimeters !== null),
  )
 
  if (measured.length === 0) {
    return null
  }
 
  return measured.reduce((latest, candidate) =>
    candidate.scheduledAt > latest.scheduledAt ? candidate : latest,
  )
}