feat(booth): cancel wrongly-printed ticket (signed void) + refused-vs-anomaly display; fix CI uv
CI / check (push) Failing after 56s
CI / check (push) Failing after 56s
Cancel a misprinted/test/wrong-vehicle ticket via a SIGNED `void` event — the
vehicle_entry is never edited/deleted (append-only). VoidFlow appends void{
voidedEntryRef, voidReason, operator, reasonCode:"void.ticketCancelled" }; route
POST /api/tickets/void gated event:void + open shift; reason REQUIRED. Refuses a
subscription / already-exited / already-voided / paid ticket (refund out of scope).
The void folds the session CLOSED everywhere it's counted — occupancy (count +
reserved spots), pay-station (lookup/activeSessions), exit-flow (#sessionFor), and
reports (excluded from entries) — so a voided car stops occupying a spot, can't be
paid/exited, and doesn't inflate "cars entered". No barrier action. Booth UI: a
"Cancel ticket" action in the pay/exit lookup modal (transient + unpaid + open;
gated on event:void) with a preset-or-free reason prompt.
Reclassify the Live feed: refused-action events (exitRefused/entryRefused/
permitRefused — e.g. a double card-scan, at-capacity subscriber, exit on a closed
session) are benign warnings, not red anomalies. event-detail.tsx now shows them as
amber REFUZUAR/REFUSED, reserving red ANOMALI for genuine red-flags. Display-only —
no ledger change, so historical events reclassify too.
CI: install uv + sync vision deps before the Turbo run. @parking/vision's lint/
typecheck/test shell to `uv run …`, but CI set up only Node+pnpm, so `uv run ruff`
failed ("uv not found") and broke the whole Turbo run. The Python checks pass once
uv provisions the toolchain.
- new: void-flow.ts (+ tests, 8) ; occupancy void-fold test
- shared: reason code void.ticketCancelled ; both web catalogs (sq/en parity)
- wiki: parking-session (ticket-void folds + guards, refused/anomaly split), log
Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import { eq, ledgerEvents, sessions, type Db } from "@parking/db";
|
||||
import { reasonPayload } from "@parking/shared";
|
||||
import type { FastifyBaseLogger } from "fastify";
|
||||
import type { EventLog } from "./event-log.js";
|
||||
|
||||
// Cancel a wrongly-printed transient ticket by appending a SIGNED `void` event that
|
||||
// references the entry. The signed ledger is append-only and hash-chained — the
|
||||
// vehicle_entry is NEVER edited or deleted; the void is a new appended row that the
|
||||
// session projection folds to CLOSE the session (so a voided car stops counting inside
|
||||
// and can't be paid/exited). Fully traceable: the operator + a required reason are signed
|
||||
// into the void payload. A misprinted ticket's car never entered, so voiding opens NO
|
||||
// barrier. See wiki/concepts/append-only-event-chain.md, parking-session.md.
|
||||
|
||||
export interface VoidResult {
|
||||
readonly ok: boolean;
|
||||
/** English reason on refusal (localized client-side via the reasonCode it mirrors). */
|
||||
readonly reason?: string;
|
||||
/** The void event's identity on success (= the entry identity). */
|
||||
readonly identity?: string;
|
||||
}
|
||||
|
||||
export class VoidFlow {
|
||||
readonly #db: Db;
|
||||
readonly #log: EventLog;
|
||||
readonly #logger: FastifyBaseLogger;
|
||||
/** Serialize concurrent voids of the SAME ticket (double-click / double-scan). */
|
||||
readonly #inFlight = new Set<string>();
|
||||
|
||||
constructor(db: Db, log: EventLog, logger: FastifyBaseLogger) {
|
||||
this.#db = db;
|
||||
this.#log = log;
|
||||
this.#logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Void (cancel) a transient ticket. Guards, then appends a signed `void`. Refuses:
|
||||
* unknown ticket, a subscription occurrence (use the subscription flow), an already-
|
||||
* exited or already-voided session, or a session that has a payment (a paid ticket is a
|
||||
* refund situation — out of scope). `reason` is REQUIRED (the route enforces non-empty).
|
||||
*/
|
||||
async voidTicket(args: { identity: string; reason: string; operator: string }): Promise<VoidResult> {
|
||||
const identity = args.identity.trim();
|
||||
const reason = args.reason.trim();
|
||||
if (!identity) return { ok: false, reason: "missing ticket id" };
|
||||
if (!reason) return { ok: false, reason: "a cancellation reason is required" };
|
||||
|
||||
if (this.#inFlight.has(identity)) return { ok: false, reason: "cancel already in flight" };
|
||||
this.#inFlight.add(identity);
|
||||
try {
|
||||
return await this.#run(identity, reason, args.operator);
|
||||
} catch (err) {
|
||||
this.#logger.error(`void-flow failed (${identity}): ${(err as Error).message}`);
|
||||
return { ok: false, reason: (err as Error).message };
|
||||
} finally {
|
||||
this.#inFlight.delete(identity);
|
||||
}
|
||||
}
|
||||
|
||||
async #run(identity: string, reason: string, operator: string): Promise<VoidResult> {
|
||||
const rows = this.#db
|
||||
.select()
|
||||
.from(ledgerEvents)
|
||||
.where(eq(ledgerEvents.identity, identity))
|
||||
.orderBy(ledgerEvents.index)
|
||||
.all();
|
||||
|
||||
const entry = rows.find((r) => r.type === "vehicle_entry");
|
||||
if (!entry) return { ok: false, reason: "no such ticket (no entry for this id)" };
|
||||
|
||||
// Subscriptions are closed via their own flow — ticket-void would double-mean permitId.
|
||||
const entryPl = (entry.payload ?? {}) as { permit?: boolean; permitId?: string };
|
||||
if (entryPl.permit === true || entryPl.permitId != null) {
|
||||
return { ok: false, reason: "this is a subscription occurrence — cancel it via the subscription, not a ticket void" };
|
||||
}
|
||||
if (rows.some((r) => r.type === "vehicle_exit")) {
|
||||
return { ok: false, reason: "session already exited — nothing to cancel" };
|
||||
}
|
||||
if (rows.some((r) => r.type === "void")) {
|
||||
return { ok: false, reason: "ticket already cancelled" };
|
||||
}
|
||||
// A paid ticket is a refund, not a misprint cancel — out of scope.
|
||||
if (rows.some((r) => r.type === "payment")) {
|
||||
return { ok: false, reason: "ticket already paid — a refund is a separate action, not a cancellation" };
|
||||
}
|
||||
|
||||
await this.#log.append({
|
||||
type: "void",
|
||||
identity,
|
||||
// `sessionRef` + `voidedEntryRef` tie the void to the entry; `voidReason` + `operator`
|
||||
// make it traceable. The reasonCode localizes; the free-text reason is the operator's note.
|
||||
payload: {
|
||||
...reasonPayload("void.ticketCancelled", { reason }),
|
||||
sessionRef: identity,
|
||||
voidedEntryRef: entry.id,
|
||||
voidReason: reason,
|
||||
operator,
|
||||
},
|
||||
});
|
||||
|
||||
// Best-effort close the projection cache (the ledger fold is the truth either way).
|
||||
try {
|
||||
this.#db
|
||||
.update(sessions)
|
||||
.set({ exitedAt: new Date().toISOString(), state: "voided" })
|
||||
.where(eq(sessions.id, identity))
|
||||
.run();
|
||||
} catch (err) {
|
||||
this.#logger.error(`void session-cache close failed for ${identity}: ${(err as Error).message}`);
|
||||
}
|
||||
|
||||
this.#logger.info(`ticket ${identity} cancelled by ${operator}: ${reason}`);
|
||||
// NO barrier action — the misprinted ticket's car never entered.
|
||||
return { ok: true, identity };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user