fix(healthpost): resolve referrals by a stable cross-device id, not the local Dexie id

healthpost.referral.resolve joined on StoredEventLogEntry.id (a device-local Dexie autoincrement). Once referrals sync cross-device, the same referral has a different local id on each device, so a resolution never matched - a handled case stayed 'open' on every other device, breaking the cross-device clinician loop the referral sync was meant to enable. Each referral now carries an intrinsic UUID (ReferralPayload.referralId) in its signed payload, and the resolve + extractReferrals join on that stable id. Verified by a test asserting a resolve matches its referral despite differing entry ids.
This commit is contained in:
OmO Orchestrator committed 2026-11-20 05:42:33 -05:00
1 parent b9deb73592
commit 127f646412
4 files changed
+31 -19

No files matched your search

@@ -63,15 +63,16 @@ describe("ReferralQueue (close-the-loop - Mark handled resolves a referral and d
it("removes a referral from the open queue after 'Mark handled', via a real appended resolve event", async () => {
const user = userEvent.setup();
const report = await postReferral(URGENT_RESULT);
const refId = (report.payload as { referralId: string }).referralId;
render(<ReferralQueue />);
const resolveButton = await screen.findByTestId(`referral-resolve-${report.id}`);
const resolveButton = await screen.findByTestId(`referral-resolve-${refId}`);
await user.click(resolveButton);
await waitFor(() => {
expect(screen.getByTestId("referral-queue-empty")).toBeInTheDocument();
});
expect(screen.queryByTestId(`referral-row-${report.id}`)).not.toBeInTheDocument();
expect(screen.queryByTestId(`referral-row-${refId}`)).not.toBeInTheDocument();
});
});
+2 -2
View File
@@ -28,7 +28,7 @@ const URGENCY_CLASS: Record<Urgency, string> = {
export function ReferralQueue() {
const entries = useEventLogEntries();
const [resolving, setResolving] = useState<ReadonlySet<number>>(new Set());
const [resolving, setResolving] = useState<ReadonlySet<string>>(new Set());
const referrals = useMemo(() => extractReferrals(entries), [entries]);
const openReferrals = useMemo(
() =>
@@ -38,7 +38,7 @@ export function ReferralQueue() {
[referrals],
);
async function handleResolve(id: number) {
async function handleResolve(id: string) {
setResolving((prev) => new Set(prev).add(id));
try {
// Append-only: this writes a new healthpost.referral.resolve event; the
@@ -145,15 +145,16 @@ describe("postSymptomReport / postReferral backend sync wiring (plan todo 3)", (
describe("resolveReferral / extractReferrals (close-the-loop - a handled referral drops out, append-only)", () => {
it("folds a signed healthpost.referral.resolve event in so the referral reads back as resolved", async () => {
const report = await postReferral(URGENT_RESULT);
const referralId = (report.payload as { referralId: string }).referralId;
const [openReferral] = extractReferrals(await readAll());
expect(openReferral.status).toBe("open");
expect(openReferral.id).toBe(report.id);
expect(openReferral.id).toBe(referralId);
const resolution = await resolveReferral({ id: openReferral.id });
expect(resolution.payload).toMatchObject({
type: REFERRAL_RESOLVE_PAYLOAD_TYPE,
referralId: report.id,
referralId,
});
const [resolvedReferral] = extractReferrals(await readAll());
@@ -165,7 +166,7 @@ describe("resolveReferral / extractReferrals (close-the-loop - a handled referra
it("keeps the Event Log append-only - the resolve is a second entry; the original report entry is untouched", async () => {
const report = await postReferral(URGENT_RESULT);
await resolveReferral({ id: report.id });
await resolveReferral({ id: (report.payload as { referralId: string }).referralId });
const entries = await readAll();
expect(entries).toHaveLength(2);
@@ -177,21 +178,22 @@ describe("resolveReferral / extractReferrals (close-the-loop - a handled referra
it("only resolves the referral whose id is referenced, leaving other open referrals open", async () => {
const first = await postReferral(URGENT_RESULT);
const firstReferralId = (first.payload as { referralId: string }).referralId;
await postReferral({ ...URGENT_RESULT, overallUrgency: "needs_treatment", overallLabel: "PNEUMONIA" });
await resolveReferral({ id: first.id });
await resolveReferral({ id: firstReferralId });
const referrals = extractReferrals(await readAll());
expect(referrals).toHaveLength(2);
const resolved = referrals.filter((r) => r.status === "resolved");
const open = referrals.filter((r) => r.status === "open");
expect(resolved).toHaveLength(1);
expect(resolved[0].id).toBe(first.id);
expect(resolved[0].id).toBe(firstReferralId);
expect(open).toHaveLength(1);
});
it("ignores a resolve referencing an unknown referral id rather than inventing a referral", async () => {
await resolveReferral({ id: 9999 });
await resolveReferral({ id: "unknown-referral-id" });
expect(extractReferrals(await readAll())).toHaveLength(0);
});
});
+18 -9
View File
@@ -112,6 +112,11 @@ const URGENCY_VALUES: readonly Urgency[] = [
/** The Event Log payload shape one case relay is signed over. */
export interface ReferralPayload {
type: typeof REFERRAL_PAYLOAD_TYPE;
/** Stable, device-independent id minted when the referral is created, so the
* resolve event can target the SAME referral on every device after sync (the
* local Dexie `StoredEventLogEntry.id` differs per device and must not be the
* join key for cross-device resolution). */
referralId: string;
triageResult: TriageResult;
overallUrgency: Urgency;
status: "open";
@@ -127,15 +132,17 @@ export interface ReferralPayload {
*/
export interface ReferralResolvePayload {
type: typeof REFERRAL_RESOLVE_PAYLOAD_TYPE;
/** Event Log id (`StoredEventLogEntry.id`) of the referral being resolved. */
referralId: number;
/** Stable, device-independent `ReferralPayload.referralId` of the referral
* being resolved - NOT the local Dexie id, so a resolution matches the same
* referral on every device after sync. */
referralId: string;
resolvedAt: number;
}
/** A reconstructed referral, as `ReferralQueue` renders it. */
export interface Referral {
/** Event Log id of the original `healthpost.referral` entry - the resolve target. */
id: number;
/** Stable cross-device referral id (`ReferralPayload.referralId`), the resolve target. */
id: string;
triageResult: TriageResult;
overallUrgency: Urgency;
createdAtLamportTs: number;
@@ -161,6 +168,7 @@ function isReferralPayload(payload: unknown): payload is ReferralPayload {
const p = payload as Record<string, unknown>;
return (
p.type === REFERRAL_PAYLOAD_TYPE &&
typeof p.referralId === "string" &&
isTriageResult(p.triageResult) &&
typeof p.overallUrgency === "string" &&
(URGENCY_VALUES as readonly string[]).includes(p.overallUrgency) &&
@@ -173,7 +181,7 @@ function isReferralResolvePayload(payload: unknown): payload is ReferralResolveP
const p = payload as Record<string, unknown>;
return (
p.type === REFERRAL_RESOLVE_PAYLOAD_TYPE &&
typeof p.referralId === "number" &&
typeof p.referralId === "string" &&
typeof p.resolvedAt === "number"
);
}
@@ -188,6 +196,7 @@ function isReferralResolvePayload(payload: unknown): payload is ReferralResolveP
export async function postReferral(triageResult: TriageResult): Promise<StoredEventLogEntry> {
const payload: ReferralPayload = {
type: REFERRAL_PAYLOAD_TYPE,
referralId: crypto.randomUUID(),
triageResult,
overallUrgency: triageResult.overallUrgency,
status: "open",
@@ -234,13 +243,13 @@ export async function resolveReferral(
* open queue without the append-only log ever being mutated.
*/
export function extractReferrals(entries: StoredEventLogEntry[]): Referral[] {
const referrals = new Map<number, Referral>();
const resolutions = new Map<number, number>();
const referrals = new Map<string, Referral>();
const resolutions = new Map<string, number>();
for (const entry of entries) {
if (isReferralPayload(entry.payload)) {
referrals.set(entry.id, {
id: entry.id,
referrals.set(entry.payload.referralId, {
id: entry.payload.referralId,
triageResult: entry.payload.triageResult,
overallUrgency: entry.payload.overallUrgency,
createdAtLamportTs: entry.lamportTs,