src/features/reviews/section-state.ts
Linked repository file from this build.
import { normalizeReviewAnswers } from "@/programs/snap-negative/field-catalog";
import type { Scenario, SnapReview } from "@/programs/snap-negative/types";
import type { Actor } from "./types";
import { captureReceivedSnapshot, type ReviewCommand } from "./commands";
export function receivedSnapshot(review: SnapReview, actor: Actor) {
const role =
actor.role === "program_manager"
? "program_manager"
: actor.role === "supervisor"
? "supervisor"
: "examiner";
return review.receivedSnapshots?.[role];
}
function blankScenario(): Scenario {
return {
populated: false,
householdSize: 0,
income: [],
medicalDeductionCents: 0,
dependentCareCents: 0,
childSupportCents: 0,
rentCents: 0,
taxesInsuranceCents: 0,
installationCents: 0,
otherShelterCents: 0,
sua: "none",
elderlyDisabled: false,
categoricalEligibility: false,
homeless: false,
};
}
/** Clear changes facts only. Reset uses the received copy, never an empty substitute. */
export function sectionCommand(
review: SnapReview,
actor: Actor,
section: "all" | "household" | "computation" | "identity",
reset: boolean,
): ReviewCommand | undefined {
const current = captureReceivedSnapshot(review);
const source = reset ? receivedSnapshot(review, actor) : undefined;
if (reset && !source) return undefined;
if (section === "all")
return {
type: "restore_facts",
...(source ?? {
...current,
identity: {
clientName: "",
address: "",
county: "",
district: "",
localAgency: "",
caseNumber: "",
},
outcome: "" as const,
dropKind: undefined,
facts: {
...current.facts,
negativeAction: "",
noticeDate: "",
actionDate: "",
caoIssuedBenefitCents: undefined,
members: [],
answers: normalizeReviewAnswers({}),
scenarios: {
eligibility: blankScenario(),
compI: blankScenario(),
compII: blankScenario(),
},
final: undefined,
},
}),
};
if (section === "household")
return {
type: "restore_facts",
...current,
facts: {
...current.facts,
members: source?.facts.members ?? [],
scenarios: Object.fromEntries(
Object.entries(current.facts.scenarios).map(([id, value]) => [
id,
{
...value,
income: value.income.filter((row) =>
source?.facts.members.some((member) => member.id === row.memberId),
),
},
]),
) as SnapReview["facts"]["scenarios"],
final: undefined,
},
};
if (section === "computation")
return {
type: "restore_facts",
...current,
facts: {
...current.facts,
scenarios: source?.facts.scenarios ?? {
eligibility: blankScenario(),
compI: blankScenario(),
compII: blankScenario(),
},
final: source?.facts.final,
},
};
return {
type: "restore_facts",
...current,
identity: source?.identity ?? {
clientName: "",
address: "",
county: "",
district: "",
localAgency: "",
caseNumber: "",
},
facts: {
...current.facts,
negativeAction: source?.facts.negativeAction ?? "",
noticeDate: source?.facts.noticeDate ?? "",
actionDate: source?.facts.actionDate ?? "",
},
};
}