114a32e6f2
Rework drawer cash movements from synchronous admin-authorization-at-creation
(operator typed an admin's password inline for every receipt/disbursement) to
operator-records-freely -> admin-reviews-after.
- New `drawer` resource: drawer:create (operator records; admin-revocable per
role) + drawer:review (admin authorizes/denies). Migration 0018 grants the
default operator role drawer:create; admin gets all in code.
- New signed `cash_review` ledger event { refId, decision, reviewedBy, note? }.
A DENIAL is a FLAG, not a reversal: it never appends reversing cash and never
touches the drawer balance (the correction is settled outside the app). This
is what keeps a late review from leaking into the next operator's inherited
drawer — a denial that lands after the reviewed shift closed moves no cash.
Regression test: op1 disburses -> closes -> op2 inherits -> admin denies ->
op2 drawer unchanged.
- Move the feature OFF the polluted /shifts route to a top-level /drawer
(operator: record + own; admin: review queue + all). routes/drawer.ts lifted
from routes/shift.ts (retired the authorizer-password gate; kept shift:cash
for its other job = admin-sees-all-shifts). New DrawerManager.tsx.
Display fixes bundled:
- Render cash_review in the event-detail modal (decision / reviewed-by / note /
movement ref) — previously showed nothing.
- Relabel the shift drawer figures for clarity: Daily takings / Receipts /
Disbursements (was Cash payments / Cash added / Cash removed).
- Hide the Card figure everywhere when CARD_PAYMENTS_ENABLED is false (no POS
on-site), matching the card-tender gate.
shared/db/server/web all typecheck; 225 server tests pass (incl. the drawer
review + cross-shift-leak regression); web build + i18n parity green. Verified
end-to-end via Playwright. Recorded in wiki/concepts/shift.md.
Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
90 lines
4.3 KiB
TypeScript
90 lines
4.3 KiB
TypeScript
import type { FastifyInstance } from "fastify";
|
|
import { requirePermission, roleHasPermissions } from "../auth.js";
|
|
import { NoOpenShiftError, ShiftAlreadyOpenError, type ShiftService } from "../shift-service.js";
|
|
|
|
interface ShiftsQuery {
|
|
/** Filter to one operator (admin-only; non-admins are forced to themselves). */
|
|
operator?: string;
|
|
/** ISO window over shift START time. */
|
|
from?: string;
|
|
to?: 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> {
|
|
// Reading the shift state vs. opening/closing one's own shift.
|
|
const readGuard = requirePermission("shift:read");
|
|
const guard = requirePermission("shift:create");
|
|
|
|
// 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: readGuard }, 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,
|
|
};
|
|
});
|
|
|
|
// Mid-shift X-report: a READ-ONLY "so far" snapshot of the OPEN shift's takings +
|
|
// drawer (opening float, cash/card taken, pay-ins/outs, expected drawer), computed
|
|
// as of now. Appends nothing — it's not an accountability mark, just a projection
|
|
// (the Z-report at close is the signed record). 204 when no shift is open.
|
|
app.get("/api/shift/report", { preHandler: readGuard }, async (_req, reply) => {
|
|
const report = shift.currentReport();
|
|
if (!report) return reply.code(204).send();
|
|
return report;
|
|
});
|
|
|
|
// Completed shift history. SCOPED by permission:
|
|
// - `shift:read` (operators) → own shifts only; operator/from/to params ignored.
|
|
// - `shift:cash` (admin-grade) → all operators, optionally filtered by
|
|
// `operator` and a `from`/`to` time window over each shift's START.
|
|
// This keeps one operator from reading another's takings while letting admins
|
|
// reconcile across the site. The data is the signed shift_z_report chain.
|
|
app.get<{ Querystring: ShiftsQuery }>("/api/shifts", { preHandler: readGuard }, async (req) => {
|
|
const canSeeAll = roleHasPermissions(req.user.roleId, ["shift:cash"]);
|
|
const q = req.query ?? {};
|
|
// Non-admins are hard-scoped to themselves regardless of any operator param.
|
|
const operator = canSeeAll ? (q.operator?.trim() || undefined) : req.user.username;
|
|
const from = canSeeAll ? q.from?.trim() || undefined : undefined;
|
|
const to = canSeeAll ? q.to?.trim() || undefined : undefined;
|
|
const shifts = shift.listShifts({ operator, from, to });
|
|
return { shifts, scope: canSeeAll ? "all" : "self" };
|
|
});
|
|
|
|
// NB: drawer cash movements (record/review) moved to routes/drawer.ts (2026-07-01) — the
|
|
// feature is no longer part of the shift route. See wiki/concepts/shift.md.
|
|
|
|
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 });
|
|
}
|
|
});
|
|
}
|