1
0
Fork 0
polaris-task-force/src/components/admin/tech-tree/mappers.ts
Z8MB1E caf77e3a6b feat(tech-tree): optimistic concurrency for the tree editor
Guard the Technologies collection with an X-PTF-If-Unmodified-At
precondition hook (update-only, header-only) that rejects stale writes
with HTTP 409 and a stale_write payload, placed before the approval
reset hook. Bulk position writes gain per-row ifUnmodifiedAt tokens in
one all-or-nothing statement; conflicts return the offending ids and
successful writes return fresh tokens so clients advance their own
versions. The lite editor shows a Version Conflict dialog (load latest
or overwrite anyway); canvas position conflicts surface as a toolbar
banner with refresh / force-overwrite plus red node rings.

Tests: new tech-tree-concurrency int spec (guard, coexistence with the
approval reset, positions preconditions), two e2e scenarios for the
dialog and the banner; existing positions spec adapted to the new
return shape.
2026-10-15 11:48:30 -04:00

68 lines
2.6 KiB
TypeScript

import type { TreeCategory, TreeTech } from "./types";
/**
* Project a `technologies` REST/local API doc (depth 0: relationships are
* plain ids) into the lightweight TreeTech shape used by the tree.
*/
export function mapTechDoc(doc: Record<string, unknown>): TreeTech {
const requirements = doc.requirements as { prerequisiteTechnologies?: unknown[] } | null;
const rows = Array.isArray(requirements?.prerequisiteTechnologies)
? (requirements!.prerequisiteTechnologies as Record<string, unknown>[])
: [];
const prereqAnd: number[] = [];
const prereqOr: number[] = [];
for (const row of rows) {
const requiresAnd = row?.requiresAnd as { technology?: unknown[] } | undefined;
const requiresOr = row?.requiresOr as { technology?: unknown[] } | undefined;
for (const t of requiresAnd?.technology ?? []) {
if (typeof t === "number") prereqAnd.push(t);
}
for (const t of requiresOr?.technology ?? []) {
if (typeof t === "number") prereqOr.push(t);
}
}
const researchCosts = doc.researchCosts as { resources?: unknown[] } | null;
const costRows = Array.isArray(researchCosts?.resources)
? (researchCosts!.resources as Record<string, unknown>[])
: [];
const resourceCosts = costRows
.map((row) => ({
resource: row?.resource as number,
amount: typeof row?.amount === "number" ? row.amount : 0,
}))
.filter((row) => typeof row.resource === "number");
const manualPosition = doc.treePosition as { x?: unknown; y?: unknown } | null | undefined;
const treePosition =
manualPosition &&
typeof manualPosition.x === "number" &&
typeof manualPosition.y === "number" &&
Number.isFinite(manualPosition.x) &&
Number.isFinite(manualPosition.y)
? { x: manualPosition.x, y: manualPosition.y }
: null;
return {
id: doc.id as number,
name: String(doc.name ?? ""),
type: doc.type === "asset" ? "asset" : "upgrade",
approvalStatus: String(doc.approvalStatus ?? "in_progress"),
summary: String(doc.summary ?? ""),
categoryId: typeof doc.category === "number" ? doc.category : null,
prereqAnd: [...new Set(prereqAnd)],
prereqOr: [...new Set(prereqOr)].filter((id) => !prereqAnd.includes(id)),
resourceCosts,
treePosition,
updatedAt: String(doc.updatedAt ?? ""),
};
}
/** Project a `tech-categories` doc into the TreeCategory shape. */
export function mapCategoryDoc(doc: Record<string, unknown>): TreeCategory {
return {
id: doc.id as number,
name: String(doc.name ?? ""),
position: typeof doc.position === "number" ? doc.position : 1,
color: typeof doc.color === "string" && doc.color ? doc.color : null,
};
}