1
0
Fork 0
polaris-task-force/src/collections/operations/OperationReservations.ts
Z8MB1E 67c993ca10 feat(operations): mission reservations and deterministic settlement
Reserve personnel, vehicles, cargo, and budget with idempotent keys and exactly-once settlement; extraction effects preflight before any write so validated HQ deposits and locker credits are final and survive mission failure. Failure sweeps touch only unextracted reservations.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-20 02:43:47 -04:00

146 lines
4.7 KiB
TypeScript

import type { CollectionConfig } from "payload";
/**
* Mission allocation reservations.
*
* One row per allocation request against an operation: personnel, vehicles,
* cargo/resources, and a treasury budget earmarked for a mission, plus the
* origin/destination structures and an optional expiry. Reservations are
* earmarks only — no locker, shipment, bank, or storage rows are mutated at
* reserve or settle time; the deterministic settlement record on the row
* captures what was consumed versus returned, exactly once.
*
* Settlement state machine: `reserved` -> `settled` | `cancelled` | `expired`
* | `no-show` (all terminal). `reservationKey` is the idempotency key: a
* retry with the same key returns the existing row instead of creating a
* second reservation.
*
* Writes are internal only (create/update/delete denied; the allocation
* service in `src/lib/operations/allocation.ts` writes via `overrideAccess`).
* Read is open to any logged-in user.
*/
export const OperationReservations: CollectionConfig = {
slug: "operation-reservations",
admin: {
group: "Operations",
useAsTitle: "reservationKey",
defaultColumns: ["reservationKey", "operationId", "status", "expiresAt", "settledAt"],
},
access: {
read: ({ req }) => !!req.user,
create: () => false,
update: () => false,
delete: () => false,
},
fields: [
{
name: "reservationKey",
type: "text",
required: true,
unique: true,
index: true,
admin: {
description: "Idempotency key for the allocation request. Retries with the same key are no-ops.",
},
},
{
name: "operationId",
type: "text",
required: true,
index: true,
admin: { description: "Operation identity this allocation belongs to." },
},
{
name: "mission",
type: "relationship",
relationTo: "missions",
admin: { description: "The mission these assets are allocated to, when known." },
},
{
name: "status",
type: "select",
required: true,
defaultValue: "reserved",
options: [
{ label: "Reserved", value: "reserved" },
{ label: "Settled", value: "settled" },
{ label: "Cancelled", value: "cancelled" },
{ label: "Expired", value: "expired" },
{ label: "No-Show", value: "no-show" },
],
admin: {
description: "reserved is the only non-terminal state; every terminal state is settled exactly once.",
},
},
{
name: "personnel",
type: "array",
admin: { description: "Reserved unit members." },
fields: [
{ name: "user", type: "relationship", relationTo: "users", required: true },
{ name: "slot", type: "text", admin: { description: "Optional role/slot label." } },
],
},
{
name: "vehicles",
type: "array",
admin: { description: "Reserved deployed vehicles." },
fields: [
{ name: "vehicle", type: "relationship", relationTo: "game-vehicles", required: true },
],
},
{
name: "cargo",
type: "array",
admin: { description: "Reserved resources (drawn from the origin structure). " },
fields: [
{ name: "resource", type: "relationship", relationTo: "resources", required: true },
{ name: "amount", type: "number", required: true, min: 1 },
],
},
{
name: "budget",
type: "group",
admin: { description: "Treasury budget earmarked for the operation." },
fields: [
{ name: "account", type: "relationship", relationTo: "bank-accounts" },
{ name: "amount", type: "number", min: 0 },
],
},
{
name: "origin",
type: "relationship",
relationTo: "game-structures",
admin: { description: "Structure the reserved cargo/vehicles are drawn from." },
},
{
name: "destination",
type: "relationship",
relationTo: "game-structures",
admin: { description: "Structure the cargo is bound for (storage rules checked at reserve time)." },
},
{
name: "expiresAt",
type: "date",
admin: { description: "When the reservation lapses; the expiry sweeper settles it as expired." },
},
{ name: "createdBy", type: "relationship", relationTo: "users" },
{ name: "settledAt", type: "date" },
{
name: "settledByEffect",
type: "relationship",
relationTo: "operation-effects",
admin: {
description: "Provenance: the operation effect that drove this settlement, when effect-driven.",
},
},
{
name: "settlement",
type: "json",
admin: {
description:
"Deterministic settlement record: outcome, reason, consumed and returned amounts, actor, effect, timestamp.",
},
},
],
};