All files / src/features/sorting directSubmit.ts

100% Statements 4/4
100% Branches 4/4
100% Functions 1/1
100% Lines 4/4

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                    13x   13x                         5x   1x      
import type { PendingEntry } from "./decisionQueue";
 
// The storage-unavailable fallback (spec ยง3.1, F18): when enqueue() cannot persist even after
// a flush, the island sends this ONE entry directly and awaits it. A decision goes to the
// single-decision route (the flusher batches decisions; this never does); a disposition uses the
// flusher's routes. A network throw reads as "not landed", so each island shows its own line.
export async function directSubmit(
  route: { sessionId: string; collectionId: string },
  entry: PendingEntry,
): Promise<boolean> {
  try {
    const res =
      entry.kind === "decision"
        ? await fetch(`/api/sessions/${route.sessionId}/decisions`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ type: entry.type, decision: entry.decision, clientKey: entry.clientKey }),
          })
        : entry.disposition === "clear"
          ? await fetch(`/api/collections/${route.collectionId}/dispositions/${entry.itemId}`, { method: "DELETE" })
          : await fetch(`/api/collections/${route.collectionId}/dispositions`, {
              method: "POST",
              headers: { "Content-Type": "application/json" },
              body: JSON.stringify({ itemId: entry.itemId, disposition: entry.disposition }),
            });
    return res.ok;
  } catch {
    return false;
  }
}