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

100% Statements 31/31
90% Branches 18/20
100% Functions 14/14
100% Lines 26/26

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                                                  47x     26x       17x 17x                 47x   1x 1x 1x                 47x                                           18x   24x 24x                 24x                                 19x                                                 13x   384x 11x   9x 9x 9x   9x 9x 9x     45x         9x         8x    
import type { Appointment } from '@/features/appointments/api/appointments.schemas'
import type { BabyMeasurement, SexAtBirth } from '@/features/babies/api/babies.schemas'
import { ageInMonthsAt, ageInMonthsExactAt } from '@/lib/date'
 
import { WHO_REFERENCE, WHO_REFERENCE_MAX_MONTHS, type WhoReferenceRow } from './who-reference'
 
/**
 * A dated profile measurement or completed visit, placed on an age axis.
 */
export interface GrowthPoint {
  appointmentId: string
  /** Visit timestamp or local noon for a date-only profile measurement. */
  scheduledAt: string
  /** Where the point sits on the axis: age in months, fractional. */
  ageMonths: number
  /** How that age is read out loud: completed months. */
  ageMonthsWhole: number
  weightGrams: number | null
  heightMillimeters: number | null
}
 
/**
 * The measured visits of one child, oldest first.
 *
 * Same admission rule as `latestMeasuredVisit`, and for the same reason: only
 * `COMPLETED`, because a cancelled visit did not weigh anybody even though the
 * API will hold a measurement on one. A visit with neither number is not a point
 * — a gap in the curve is honest, a point at zero is not.
 *
 * Oldest first because a curve is read left to right; the appointment list sorts
 * the other way, which is right for "what happened recently" and wrong here.
 */
export function growthSeries(appointments: Appointment[], birthDate: string, measurements: BabyMeasurement[] = []): GrowthPoint[] {
  const visits = appointments
    .filter(
      (appointment) =>
        appointment.status === 'COMPLETED' &&
        (appointment.weightGrams !== null || appointment.heightMillimeters !== null),
    )
    .map((appointment) => {
      const at = new Date(appointment.scheduledAt)
      return {
        appointmentId: appointment.id,
        scheduledAt: appointment.scheduledAt,
        ageMonths: ageInMonthsExactAt(birthDate, at),
        ageMonthsWhole: ageInMonthsAt(birthDate, at),
        weightGrams: appointment.weightGrams,
        heightMillimeters: appointment.heightMillimeters,
      }
    })
  const recorded = measurements.map((measurement): GrowthPoint => {
    // Calendar dates stay local. Noon avoids changing the recorded day in UTC offsets.
    const scheduledAt = `${measurement.measuredOn}T12:00:00`
    const at = new Date(scheduledAt)
    return {
      appointmentId: measurement.id,
      scheduledAt,
      ageMonths: ageInMonthsExactAt(birthDate, at),
      ageMonthsWhole: ageInMonthsAt(birthDate, at),
      weightGrams: measurement.weightGrams,
      heightMillimeters: measurement.heightMillimeters,
    }
  })
  return [...visits, ...recorded].sort((a, b) => new Date(a.scheduledAt).getTime() - new Date(b.scheduledAt).getTime())
}
 
export type GrowthIndicator = 'weight' | 'height'
 
/** A point as a chart reads it: an x, a y, and enough to label the tooltip. */
export interface GrowthPlotPoint {
  x: number
  y: number
  scheduledAt: string
  ageMonthsWhole: number
}
 
/**
 * The points of one indicator, dropping the visits that did not record it.
 *
 * Dropping rather than interpolating: a visit that weighed the child without
 * measuring her is not evidence about her height, and a line drawn straight
 * across the gap says it is. The two indicators therefore have different point
 * counts on the same page, which is the truth about how the visits went.
 */
export function indicatorPoints(points: GrowthPoint[], indicator: GrowthIndicator): GrowthPlotPoint[] {
  return points
    .map((point) => {
      const raw = indicator === 'weight' ? point.weightGrams : point.heightMillimeters
      return raw === null
        ? null
        : {
            x: point.ageMonths,
            y: raw,
            scheduledAt: point.scheduledAt,
            ageMonthsWhole: point.ageMonthsWhole,
          }
    })
    .filter((point): point is GrowthPlotPoint => point !== null)
}
 
/**
 * The WHO reference table for this indicator and sex, whole.
 *
 * `null` — never an empty array — when there is none to draw. The WHO curves are
 * one for boys and one for girls; there is no neutral table, and picking either
 * one for a child whose sex at birth was not recorded would be inventing the
 * comparison. `sexAtBirth` is optional since #82 and left blank on purpose by
 * people who have their reasons, so the page says why the band is missing rather
 * than guessing.
 */
export function referenceBand(
  indicator: GrowthIndicator,
  sexAtBirth: SexAtBirth | null,
): readonly WhoReferenceRow[] | null {
  return sexAtBirth ? WHO_REFERENCE[indicator][sexAtBirth] : null
}
 
/**
 * The rows a chart whose age axis ends at `maxAgeMonths` should draw.
 *
 * Clipping matters more than it sounds: the tables run to five years, and a
 * fourteen-month-old's curve on a five-year axis is a squiggle in the left tenth.
 * The axis belongs to the child; the reference covers the part of it that exists.
 *
 * **The chart clips, not the caller** — it is the only thing that knows where its
 * axis ends, and that end is not simply the oldest measurement (a newborn's axis
 * has a one-month floor). A band cut to the child's age instead of to the axis
 * stops mid-plot, with a visible step.
 *
 * The last row is **interpolated onto the edge**, not the first row past it. The
 * obvious version — keep one extra row and let the chart clamp it — clamps only
 * the x: the extra row's percentiles belong to an older child, so the band jumps
 * upward in a visible step right at the border. It looked like a rendering
 * glitch in the capture, which is how it was found.
 */
export function clipBand(
  rows: readonly WhoReferenceRow[] | null | undefined,
  maxAgeMonths: number,
): readonly WhoReferenceRow[] | null {
  if (!rows) return null
 
  const cut = rows.findIndex((row) => row[0] > maxAgeMonths)
  if (cut < 0) return rows.length > 1 ? rows : null
 
  const visible = rows.slice(0, cut)
  const before = visible.at(-1)
  const after = rows[cut]!
 
  Eif (before) {
    const ratio = (maxAgeMonths - before[0]) / (after[0] - before[0])
    visible.push([
      maxAgeMonths,
      ...([1, 2, 3, 4, 5] as const).map((column) =>
        Math.round(before[column] + (after[column] - before[column]) * ratio),
      ),
    ] as unknown as WhoReferenceRow)
  }
 
  return visible.length > 1 ? visible : null
}
 
/** Whether the child's series runs past the last age WHO publishes as a table. */
export function outgrewReference(points: GrowthPoint[]): boolean {
  return points.some((point) => point.ageMonths > WHO_REFERENCE_MAX_MONTHS)
}