4e2e4feedb
A shift becomes a SITE-WIDE accountability period — at most one open at a
time — so every taking is unambiguously attributed to one operator. Login
stays decoupled from shifts (an operator can log in off-shift to review).
Backend:
- ShiftService.currentOpenShift()/requireOpenShift(); open() refuses when ANY
shift is open and throws ShiftAlreadyOpenError{heldBy} (self vs. other).
- requireShift preHandler gates /api/pay, /api/exit, /api/voucher,
/api/barrier/reopen → 409 {code:"no_shift"}; read-only lookups stay open.
- GET /api/shift/current returns site-wide {open:{startedAt,operator},isMine}.
- GET /api/events?since=<iso> for per-shift log scoping (db: re-export gte).
Frontend:
- Header shift button: open / close-mine / disabled-when-another-holds-it.
- Pay/exit modal gate banner (one-click open; "held by X" when another's);
pay/exit/voucher disabled until this operator's shift is open.
- Active-Sessions barrier re-open gated the same way.
- Live feed scoped to the open shift's window; shared useShift() Query
invalidated over the WS on shift_open/shift_z_report/cash_movement.
- sq/en strings for the control + gate.
Wiki: shift.md (site-wide single-open + gate; superseded per-operator note),
booth-console.md (header control + gate), log entry.
Verified: site-wide invariant + heldBy + handover + chain integrity on a
fresh migrated DB (11/11); db/server/web build clean.
80 lines
3.2 KiB
TypeScript
80 lines
3.2 KiB
TypeScript
import type { FastifyInstance } from "fastify";
|
||
import { requireRole } from "../auth.js";
|
||
import {
|
||
InvalidCashMovementError,
|
||
NoOpenShiftError,
|
||
ShiftAlreadyOpenError,
|
||
type ShiftService,
|
||
} from "../shift-service.js";
|
||
|
||
interface CashMovementBody {
|
||
/** Signed minor units: positive = load INTO drawer, negative = remove FROM drawer. */
|
||
amountMinor: number;
|
||
reason?: string;
|
||
currency?: string;
|
||
}
|
||
|
||
// Shift endpoints (manned mode). The operator is the logged-in user; a shift is
|
||
// opened/closed explicitly (not time-based — see wiki/concepts/shift.md and
|
||
// local-jwt-auth.md "until logout"). End Shift signs a shift_z_report + prints it.
|
||
|
||
export async function shiftRoutes(app: FastifyInstance, shift: ShiftService): Promise<void> {
|
||
// Cashier/operator/admin run shifts; readonly can't.
|
||
const guard = requireRole("admin", "operator", "cashier");
|
||
|
||
// The SITE-WIDE shift state (at most one shift open at a time). The UI uses this
|
||
// to render the header control: no shift → "Open"; my shift → "Close" (enabled);
|
||
// someone else's shift → disabled. Also returns the live drawer balance.
|
||
// - open: the open shift { startedAt, operator } or null (site-wide)
|
||
// - isMine: true iff the open shift belongs to the requesting operator
|
||
// - operator: the requesting user (for the UI's own identity)
|
||
app.get("/api/shift/current", { preHandler: guard }, async (req) => {
|
||
const me = req.user.username;
|
||
const open = shift.currentOpenShift();
|
||
const heldBy = open?.identity ?? null;
|
||
const drawer = shift.drawerBalance();
|
||
return {
|
||
operator: me,
|
||
open: open ? { startedAt: open.occurredAt, operator: heldBy } : null,
|
||
isMine: open != null && heldBy === me,
|
||
drawerMinor: drawer.balanceMinor,
|
||
currency: drawer.currency,
|
||
};
|
||
});
|
||
|
||
// Admin loads/removes physical drawer cash (the float). Signed cash_movement
|
||
// event. ADMIN ONLY — an operator takes payments but cannot move the float.
|
||
// amountMinor is signed: + load IN, − remove OUT. See wiki/concepts/shift.md.
|
||
app.post<{ Body: CashMovementBody }>(
|
||
"/api/cash-movement",
|
||
{ preHandler: requireRole("admin") },
|
||
async (req, reply) => {
|
||
const { amountMinor, reason, currency } = req.body ?? ({} as CashMovementBody);
|
||
try {
|
||
return await shift.recordCashMovement(req.user.username, amountMinor, reason ?? "", currency);
|
||
} catch (err) {
|
||
if (err instanceof InvalidCashMovementError) return reply.code(400).send({ error: err.message });
|
||
return reply.code(500).send({ error: (err as Error).message });
|
||
}
|
||
},
|
||
);
|
||
|
||
app.post("/api/shift/open", { preHandler: guard }, async (req, reply) => {
|
||
try {
|
||
return await shift.open(req.user.username);
|
||
} catch (err) {
|
||
if (err instanceof ShiftAlreadyOpenError) return reply.code(409).send({ error: err.message });
|
||
return reply.code(500).send({ error: (err as Error).message });
|
||
}
|
||
});
|
||
|
||
app.post("/api/shift/close", { preHandler: guard }, async (req, reply) => {
|
||
try {
|
||
return await shift.close(req.user.username);
|
||
} catch (err) {
|
||
if (err instanceof NoOpenShiftError) return reply.code(409).send({ error: err.message });
|
||
return reply.code(500).send({ error: (err as Error).message });
|
||
}
|
||
});
|
||
}
|