Skip to article
Documentation

src/features/reports/report-input.ts

Linked repository file from this build.

import { downstreamGateStatus } from "@/features/reviews/downstream";
import type { SnapReview } from "@/programs/snap-negative/types";
import type { ReportRow } from "./report-data";

/** The index columns a report reads. `report` is the projection saved with each version. */
export type ReportInput = Pick<SnapReview, "sampleMonth" | "examiner" | "county" | "state"> & {
  report: Pick<ReportRow, "eligible" | "variances">;
};

export function reportRow({ report, ...input }: ReportInput): ReportRow {
  return { ...input, ...report };
}

/** Build the projection from a full review. Saves, the seed and the rebuild script use it. */
export function reportRowFromReview(review: SnapReview): ReportRow {
  const eligible =
    review.state === "released_to_reporting" &&
    (review.outcome === "clean" || review.outcome === "error") &&
    downstreamGateStatus(review).ok
      ? review.outcome
      : null;
  return {
    sampleMonth: review.sampleMonth,
    examiner: review.examiner,
    county: review.county,
    state: review.state,
    eligible,
    variances: eligible === "error" ? (review.facts.answers.variances ?? []).slice(0, 1) : [],
  };
}