Compare commits
100 Commits
v0.1.5
...
52354d194a
| Author | SHA1 | Date | |
|---|---|---|---|
| 52354d194a | |||
| 4fd5bf852e | |||
| 2c3bdb7f34 | |||
| 301aefc186 | |||
| d893f4e991 | |||
| c6087b990c | |||
| 39fb5fb7c2 | |||
| b6f7ea65a4 | |||
| 6978f9e406 | |||
| 8b1f851add | |||
| 13ba593b69 | |||
| f4dd248e16 | |||
| 580412aa50 | |||
| f5f8865309 | |||
| 44e095cb95 | |||
| 73ada89a13 | |||
| 6eb65361a8 | |||
| c08e232339 | |||
| e842f036e2 | |||
| 005447f938 | |||
| 06d9297e9a | |||
| 965937352d | |||
| ec015911e9 | |||
| c94269379e | |||
| d93089a491 | |||
| 7b29331f52 | |||
| 9332802e5d | |||
| a86af4eb20 | |||
| 2ac26b5825 | |||
| 3f903cdbdd | |||
| ffcf0d3eda | |||
| e3b3a636e2 | |||
| 55c9c33432 | |||
| 82e5833e4e | |||
| 2e98167dba | |||
| a761398301 | |||
| 5d247a5729 | |||
| 30a1180108 | |||
| 354de04a3d | |||
| 32b4d9e92b | |||
| 1167e27b1d | |||
| f4e5c5037b | |||
| fc93db73c2 | |||
| 44f757ee37 | |||
| b00182d95d | |||
| b7efa081ec | |||
| 08351a09bf | |||
| 4593a397dd | |||
| 4d1d245680 | |||
| 5afdcc78ae | |||
| e3de7800e5 | |||
| 73bfc1d9b9 | |||
| f4b806a538 | |||
| fe3b12a60d | |||
| 88f9c53fda | |||
| e8cb057082 | |||
| 4a02e5fed3 | |||
| 552d87d75b | |||
| 7e21cf057e | |||
| 4fd175e0e4 | |||
| 535244209a | |||
| 0845e87ddd | |||
| 2d9bb15d4c | |||
| 29594f8bad | |||
| 4ff31557a8 | |||
| 3e77a4ad7c | |||
| 3f16925fe0 | |||
| f7a262ac9a | |||
| f9cb973fe9 | |||
| 1a0fe59488 | |||
| 8bfc29db2a | |||
| dbbb051ebd | |||
| 3e57af5abc | |||
| ef55d1c6a9 | |||
| 0411b71c2d | |||
| b485e9870b | |||
| ec44547122 | |||
| e67f0ccef0 | |||
| 78ca58d264 | |||
| 20a3cb3e80 | |||
| 5e1395db18 | |||
| 50c18405b6 | |||
| e14e31a840 | |||
| ea304bbfd1 | |||
| 2aa1045ddc | |||
| acde3bba5b | |||
| 9a13528611 | |||
| 6f88026d3e | |||
| c481c1e788 | |||
| 3a7c3fae11 | |||
| 55d6242c7d | |||
| a9ccf9e20c | |||
| 23d6379be8 | |||
| db9c3e0e31 | |||
| d86bffa500 | |||
| 9c05f86c86 | |||
| 54e691a4c9 | |||
| 52862db8ad | |||
| 8fa66c9911 | |||
| 70e1e9939f |
@@ -0,0 +1,10 @@
|
|||||||
|
* text=auto eol=lf
|
||||||
|
*.png binary
|
||||||
|
*.ico binary
|
||||||
|
*.pdf binary
|
||||||
|
*.woff2 binary
|
||||||
|
|
||||||
|
# raw sources are kept byte-for-byte as dropped in
|
||||||
|
wiki/raw/** -text
|
||||||
|
*.jpg binary
|
||||||
|
*.bin binary
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
name: Build & push images
|
name: Build & push images
|
||||||
|
|
||||||
# Build the SERVER (API + SPA) and VISION (ANPR) container images and push them to the
|
# Build the SERVER (API + SPA), COLLECTOR (wash review), VISION (ANPR) and TRAINER (phase-B job) container images and push them to the
|
||||||
# house Gitea registry, tagged by BRANCH + short SHA (branch-aware: dev→:dev, stage→:stage,
|
# house Gitea registry, tagged by BRANCH + short SHA (branch-aware: dev→:dev, stage→:stage,
|
||||||
# main→:main). Separate from ci.yml (checks-only) and release.yml (tag-only desktop bundle).
|
# main→:main). Separate from ci.yml (checks-only) and release.yml (tag-only desktop bundle).
|
||||||
# Mirrors the house pattern (cf. trm/processor build.yml). See
|
# Mirrors the house pattern (cf. trm/processor build.yml). See
|
||||||
@@ -13,6 +13,8 @@ on:
|
|||||||
- 'apps/server/**'
|
- 'apps/server/**'
|
||||||
- 'apps/web/**'
|
- 'apps/web/**'
|
||||||
- 'apps/vision/**'
|
- 'apps/vision/**'
|
||||||
|
- 'apps/collector/**'
|
||||||
|
- 'apps/trainer/**'
|
||||||
- 'packages/**'
|
- 'packages/**'
|
||||||
- 'package.json'
|
- 'package.json'
|
||||||
- 'pnpm-lock.yaml'
|
- 'pnpm-lock.yaml'
|
||||||
@@ -60,6 +62,11 @@ jobs:
|
|||||||
working-directory: apps/vision
|
working-directory: apps/vision
|
||||||
run: uv sync --frozen
|
run: uv sync --frozen
|
||||||
|
|
||||||
|
- name: Sync trainer deps
|
||||||
|
# Light core only — NOT the `train` extra (CPU torch, ~200 MB); the torch tests skip.
|
||||||
|
working-directory: apps/trainer
|
||||||
|
run: uv sync --frozen
|
||||||
|
|
||||||
# Don't publish a broken image — run the same checks as ci.yml first.
|
# Don't publish a broken image — run the same checks as ci.yml first.
|
||||||
- name: Build + lint + test (Turbo)
|
- name: Build + lint + test (Turbo)
|
||||||
run: pnpm turbo run build lint test
|
run: pnpm turbo run build lint test
|
||||||
@@ -100,18 +107,47 @@ jobs:
|
|||||||
cache-from: type=registry,ref=${{ env.REGISTRY }}/parking-server:buildcache
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/parking-server:buildcache
|
||||||
cache-to: type=registry,ref=${{ env.REGISTRY }}/parking-server:buildcache,mode=max
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/parking-server:buildcache,mode=max
|
||||||
|
|
||||||
|
- name: Build & push COLLECTOR (wash review)
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: apps/collector/Dockerfile
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
${{ env.REGISTRY }}/parking-collector:${{ steps.meta.outputs.branch }}
|
||||||
|
${{ env.REGISTRY }}/parking-collector:${{ steps.meta.outputs.branch }}-${{ steps.meta.outputs.sha }}
|
||||||
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/parking-collector:buildcache
|
||||||
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/parking-collector:buildcache,mode=max
|
||||||
|
|
||||||
- name: Build & push VISION (ANPR)
|
- name: Build & push VISION (ANPR)
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: apps/vision
|
context: apps/vision
|
||||||
file: apps/vision/Dockerfile
|
file: apps/vision/Dockerfile
|
||||||
push: true
|
push: true
|
||||||
|
# The phase-B body-type classifier is fetched from the Gitea generic package registry
|
||||||
|
# at build when apps/vision/models/bodytype.version pins a version (empty = none). The
|
||||||
|
# registry user's credentials double as the fetch auth (BuildKit secret, never a layer).
|
||||||
|
secrets: |
|
||||||
|
bodytype_auth=${{ secrets.REGISTRY_USERNAME }}:${{ secrets.REGISTRY_PASSWORD }}
|
||||||
tags: |
|
tags: |
|
||||||
${{ env.REGISTRY }}/parking-vision:${{ steps.meta.outputs.branch }}
|
${{ env.REGISTRY }}/parking-vision:${{ steps.meta.outputs.branch }}
|
||||||
${{ env.REGISTRY }}/parking-vision:${{ steps.meta.outputs.branch }}-${{ steps.meta.outputs.sha }}
|
${{ env.REGISTRY }}/parking-vision:${{ steps.meta.outputs.branch }}-${{ steps.meta.outputs.sha }}
|
||||||
cache-from: type=registry,ref=${{ env.REGISTRY }}/parking-vision:buildcache
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/parking-vision:buildcache
|
||||||
cache-to: type=registry,ref=${{ env.REGISTRY }}/parking-vision:buildcache,mode=max
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/parking-vision:buildcache,mode=max
|
||||||
|
|
||||||
|
- name: Build & push TRAINER (phase-B job)
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: apps/trainer
|
||||||
|
file: apps/trainer/Dockerfile
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
${{ env.REGISTRY }}/parking-trainer:${{ steps.meta.outputs.branch }}
|
||||||
|
${{ env.REGISTRY }}/parking-trainer:${{ steps.meta.outputs.branch }}-${{ steps.meta.outputs.sha }}
|
||||||
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/parking-trainer:buildcache
|
||||||
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/parking-trainer:buildcache,mode=max
|
||||||
|
|
||||||
# Optional: trigger a Komodo stack redeploy (cf. trm/processor). Enable by setting the
|
# Optional: trigger a Komodo stack redeploy (cf. trm/processor). Enable by setting the
|
||||||
# KOMODO_* secrets; left guarded so it no-ops until the parking stack is wired.
|
# KOMODO_* secrets; left guarded so it no-ops until the parking stack is wired.
|
||||||
- name: Trigger Komodo redeploy
|
- name: Trigger Komodo redeploy
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ jobs:
|
|||||||
working-directory: apps/vision
|
working-directory: apps/vision
|
||||||
run: uv sync --frozen
|
run: uv sync --frozen
|
||||||
|
|
||||||
|
- name: Sync trainer deps
|
||||||
|
# Same rule: light core only, not the `train` extra (CPU torch); torch tests skip.
|
||||||
|
working-directory: apps/trainer
|
||||||
|
run: uv sync --frozen
|
||||||
|
|
||||||
- name: Build + lint (Turbo)
|
- name: Build + lint (Turbo)
|
||||||
# Covers tsc typecheck, vite build, i18n catalog type-parity (a missing sq/en
|
# Covers tsc typecheck, vite build, i18n catalog type-parity (a missing sq/en
|
||||||
# key fails the build), AND the vision service's ruff lint via uv.
|
# key fails the build), AND the vision service's ruff lint via uv.
|
||||||
|
|||||||
@@ -129,8 +129,23 @@ jobs:
|
|||||||
# The Tauri updater fetches a manifest describing the newest version, its
|
# The Tauri updater fetches a manifest describing the newest version, its
|
||||||
# notes, and per-target {signature, url}. The URL points at the MIRROR
|
# notes, and per-target {signature, url}. The URL points at the MIRROR
|
||||||
# repo (mca/public_releases) — that's the unauthenticated endpoint field
|
# repo (mca/public_releases) — that's the unauthenticated endpoint field
|
||||||
# appliances actually reach; see the workflow header for why. Adjust the
|
# appliances actually reach; see the workflow header for why.
|
||||||
# platform keys you actually ship.
|
#
|
||||||
|
# ONE ENTRY PER INSTALLER TYPE — this is what made every in-app update
|
||||||
|
# v0.1.0→v0.1.6 fail. tauri-plugin-updater looks up
|
||||||
|
# `{os}-{arch}-{installer}` FIRST (linux-x86_64-deb / -rpm / -appimage,
|
||||||
|
# from the running app's detected bundle type) and only then the bare
|
||||||
|
# `linux-x86_64`. The booths run the .deb, and the manifest used to
|
||||||
|
# carry ONLY `linux-x86_64` → the AppImage. So a .deb install found the
|
||||||
|
# "update", downloaded the AppImage, verified its signature fine, then
|
||||||
|
# handed the bytes to install_deb(), which checks they're a .deb
|
||||||
|
# (infer::archive::is_deb) and bails with InvalidUpdaterFormat — after
|
||||||
|
# the download, before any relaunch, with the error swallowed client-
|
||||||
|
# side until v0.1.6. Now each installer gets its own signed asset; the
|
||||||
|
# bare key stays for an AppImage install. .deb/.rpm updates run
|
||||||
|
# `pkexec dpkg -i` / `rpm -U`, so the operator sees a polkit password
|
||||||
|
# prompt — intended: updating a root-installed package IS an admin
|
||||||
|
# action on this box (see wiki/decisions/desktop-shell-tauri.md).
|
||||||
env:
|
env:
|
||||||
SERVER_URL: ${{ github.server_url }}
|
SERVER_URL: ${{ github.server_url }}
|
||||||
MIRROR_REPO: mca/public_releases
|
MIRROR_REPO: mca/public_releases
|
||||||
@@ -138,22 +153,41 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
set -e
|
set -e
|
||||||
VERSION="${TAG#v}"
|
VERSION="${TAG#v}"
|
||||||
APPIMAGE=$(cd dist && ls *.AppImage | head -1)
|
ASSET_BASE="${SERVER_URL}/${MIRROR_REPO}/releases/download/desktop-latest"
|
||||||
SIG=$(cat "dist/${APPIMAGE}.sig")
|
cat > /tmp/latest.js <<'JS'
|
||||||
ASSET_URL="${SERVER_URL}/${MIRROR_REPO}/releases/download/desktop-latest/${APPIMAGE}"
|
const fs = require("fs");
|
||||||
cat > dist/latest.json <<JSON
|
const [version, tag, base] = process.argv.slice(2);
|
||||||
|
const files = fs.readdirSync("dist");
|
||||||
|
const pick = (ext) => files.find((f) => f.endsWith(ext));
|
||||||
|
const entry = (f) => ({
|
||||||
|
signature: fs.readFileSync(`dist/${f}.sig`, "utf8").trim(),
|
||||||
|
url: `${base}/${f}`,
|
||||||
|
});
|
||||||
|
const deb = pick(".deb"), rpm = pick(".rpm"), appimage = pick(".AppImage");
|
||||||
|
if (!deb || !appimage) {
|
||||||
|
console.error(`missing bundle in dist/: deb=${deb} appimage=${appimage}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const platforms = {
|
||||||
|
"linux-x86_64-deb": entry(deb),
|
||||||
|
...(rpm ? { "linux-x86_64-rpm": entry(rpm) } : {}),
|
||||||
|
"linux-x86_64": entry(appimage),
|
||||||
|
};
|
||||||
|
fs.writeFileSync(
|
||||||
|
"dist/latest.json",
|
||||||
|
JSON.stringify(
|
||||||
{
|
{
|
||||||
"version": "${VERSION}",
|
version,
|
||||||
"notes": "Parking System ${TAG}",
|
notes: `Parking System ${tag}`,
|
||||||
"pub_date": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
|
pub_date: new Date().toISOString().replace(/\.\d+Z$/, "Z"),
|
||||||
"platforms": {
|
platforms,
|
||||||
"linux-x86_64": {
|
},
|
||||||
"signature": "${SIG}",
|
null,
|
||||||
"url": "${ASSET_URL}"
|
2,
|
||||||
}
|
) + "\n",
|
||||||
}
|
);
|
||||||
}
|
JS
|
||||||
JSON
|
node /tmp/latest.js "${VERSION}" "${TAG}" "${ASSET_BASE}"
|
||||||
echo "latest.json:"; cat dist/latest.json
|
echo "latest.json:"; cat dist/latest.json
|
||||||
|
|
||||||
- name: Create release + upload assets (Gitea API)
|
- name: Create release + upload assets (Gitea API)
|
||||||
|
|||||||
+13
-2
@@ -24,7 +24,18 @@ dist/
|
|||||||
/dingtian/
|
/dingtian/
|
||||||
/QRCode_sdk*/
|
/QRCode_sdk*/
|
||||||
|
|
||||||
# Graphify knowledge-graph output (dev tool; generated, not committed)
|
|
||||||
graphify-out/
|
|
||||||
parking.sqlite*.bak-*
|
parking.sqlite*.bak-*
|
||||||
questions.txt
|
questions.txt
|
||||||
|
|
||||||
|
# session planning files (planning-with-files skill)
|
||||||
|
task_plan.md
|
||||||
|
findings.md
|
||||||
|
progress.md
|
||||||
|
|
||||||
|
# local scratch (site snapshots pulled for incident review, never committed)
|
||||||
|
tmp/
|
||||||
|
|
||||||
|
# Firmware PoC (firmware/relay-proxy/README.md): toolchain venv, build output, Wi-Fi secrets
|
||||||
|
firmware/.venv/
|
||||||
|
firmware/**/.pio/
|
||||||
|
firmware/**/include/secrets.h
|
||||||
|
|||||||
@@ -7,9 +7,15 @@
|
|||||||
# TLS later: replace `:80` with the real hostname (e.g. `parksystems.msai.al`), uncomment
|
# TLS later: replace `:80` with the real hostname (e.g. `parksystems.msai.al`), uncomment
|
||||||
# Caddy's :443 in docker-compose.prod.yml, and Caddy auto-provisions HTTPS. For a private
|
# Caddy's :443 in docker-compose.prod.yml, and Caddy auto-provisions HTTPS. For a private
|
||||||
# CA / internal cert, use `tls /path/cert.pem /path/key.pem`.
|
# CA / internal cert, use `tls /path/cert.pem /path/key.pem`.
|
||||||
:80 {
|
#
|
||||||
|
# PORTS ARE ENV-DRIVEN (2026-10-08): HTTP_PORT (listen, default 80) and SERVER_PORT (upstream,
|
||||||
|
# default 3000) come from docker-compose.prod.yml, which takes them from the stack env. A booth
|
||||||
|
# leaves both unset and gets :80 → :3000 exactly as before. A SHARED host (the lab stack moved
|
||||||
|
# onto art-docker-station, whose :80 belongs to another service) sets HTTP_PORT=8080 etc. in the
|
||||||
|
# Komodo stack env — nothing here or in the image changes. See wiki/decisions/container-deployment.md.
|
||||||
|
:{$HTTP_PORT:80} {
|
||||||
encode gzip
|
encode gzip
|
||||||
# Host network (prod): the server runs on the host's net namespace (to reach the booth LAN /
|
# Host network (prod): the server runs on the host's net namespace (to reach the booth LAN /
|
||||||
# device VLAN), so reach it over loopback, not the compose service name `server`.
|
# device VLAN), so reach it over loopback, not the compose service name `server`.
|
||||||
reverse_proxy 127.0.0.1:3000
|
reverse_proxy 127.0.0.1:{$SERVER_PORT:3000}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# Car Wash review collector (wiki/concepts/vision-review-outbox.md). Runs on the
|
||||||
|
# reviewer's host (art-docker-station), reachable by the booths ONLY over the Netbird
|
||||||
|
# overlay. Deployed by its own Komodo stack (komodo/resources.toml, "wash-collector").
|
||||||
|
|
||||||
|
# COLLECTOR_HOST=0.0.0.0 # in Docker the compose file binds the published port to the overlay IP
|
||||||
|
# COLLECTOR_PORT=8090
|
||||||
|
# COLLECTOR_DATA_DIR=/data # collector.sqlite + crops/<booth>/<item>.jpg
|
||||||
|
|
||||||
|
# One bearer token per booth: "<boothId>:<token>" pairs, comma- or newline-separated. The
|
||||||
|
# booth id is the pseudonymous CARWASH_REVIEW_BOOTH_ID that booth was deployed with — never
|
||||||
|
# a site name. Generate tokens with: openssl rand -hex 32
|
||||||
|
COLLECTOR_BOOTH_TOKENS=booth-7:REPLACE,booth-9:REPLACE
|
||||||
|
|
||||||
|
# The reviewer's login for the review screen and the export (HTTP Basic over the overlay).
|
||||||
|
COLLECTOR_REVIEWER_USER=reviewer
|
||||||
|
COLLECTOR_REVIEWER_PASS=REPLACE
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# parking-collector — the Car Wash review collector (wiki/concepts/vision-review-outbox.md).
|
||||||
|
# Built from the monorepo root (context: .) like the server image, so it shares the
|
||||||
|
# lockfile and @parking/shared. Runs on the REVIEWER's host (not a booth), delivered by
|
||||||
|
# its own Komodo stack (docker-compose.collector.yml). Data on /data: collector.sqlite +
|
||||||
|
# crops/<booth>/<item>.jpg — the trainer on the same host reads the crops off that volume.
|
||||||
|
|
||||||
|
FROM node:22-alpine AS deps
|
||||||
|
WORKDIR /app
|
||||||
|
RUN apk add --no-cache python3 make g++ # node-gyp for better-sqlite3
|
||||||
|
RUN corepack enable && corepack prepare pnpm@10.24.0 --activate
|
||||||
|
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml turbo.json ./
|
||||||
|
COPY apps/server/package.json apps/server/
|
||||||
|
COPY apps/web/package.json apps/web/
|
||||||
|
COPY apps/vision/package.json apps/vision/
|
||||||
|
COPY apps/collector/package.json apps/collector/
|
||||||
|
COPY packages/db/package.json packages/db/
|
||||||
|
COPY packages/devices/package.json packages/devices/
|
||||||
|
COPY packages/shared/package.json packages/shared/
|
||||||
|
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
|
||||||
|
pnpm fetch
|
||||||
|
|
||||||
|
FROM deps AS build
|
||||||
|
ENV CI=true
|
||||||
|
COPY . .
|
||||||
|
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
|
||||||
|
pnpm install --frozen-lockfile --offline
|
||||||
|
RUN pnpm turbo run build --filter=@parking/collector
|
||||||
|
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
|
||||||
|
pnpm --filter=@parking/collector --legacy deploy --prod /deploy
|
||||||
|
|
||||||
|
FROM node:22-alpine AS runtime
|
||||||
|
WORKDIR /app
|
||||||
|
ARG BUILD_VERSION=""
|
||||||
|
ENV BUILD_VERSION=$BUILD_VERSION
|
||||||
|
ENV NODE_ENV=production
|
||||||
|
RUN apk add --no-cache libstdc++ wget # better-sqlite3 native runtime; wget for the healthcheck
|
||||||
|
RUN addgroup -S app && adduser -S -G app app
|
||||||
|
COPY --from=build --chown=app:app /deploy ./
|
||||||
|
ENV COLLECTOR_DATA_DIR=/data
|
||||||
|
ENV COLLECTOR_HOST=0.0.0.0
|
||||||
|
ENV COLLECTOR_PORT=8090
|
||||||
|
RUN mkdir -p /data && chown app:app /data
|
||||||
|
VOLUME ["/data"]
|
||||||
|
USER app
|
||||||
|
EXPOSE 8090
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
||||||
|
CMD wget -qO- "http://localhost:${COLLECTOR_PORT:-8090}/health" >/dev/null 2>&1 || exit 1
|
||||||
|
CMD ["node", "dist/index.js"]
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"name": "@parking/collector",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"description": "Car Wash review collector: receives plate-blurred vehicle crops + the operator's category choice from booths over the private overlay, serves the reviewer's screen, exports labels for training. See wiki/concepts/vision-review-outbox.md.",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc -b",
|
||||||
|
"dev": "tsx watch --env-file-if-exists=.env src/index.ts",
|
||||||
|
"start": "node --env-file-if-exists=.env dist/index.js",
|
||||||
|
"typecheck": "tsc --noEmit",
|
||||||
|
"lint": "tsc --noEmit",
|
||||||
|
"test": "vitest run"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@fastify/multipart": "^9.2.1",
|
||||||
|
"@parking/shared": "workspace:*",
|
||||||
|
"better-sqlite3": "12.10.1",
|
||||||
|
"fastify": "5.8.5"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/better-sqlite3": "7.6.13",
|
||||||
|
"@types/node": "25.9.3",
|
||||||
|
"tsx": "4.22.4",
|
||||||
|
"typescript": "6.0.3",
|
||||||
|
"vitest": "^4.1.9"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,196 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { mkdtemp, rm } from "node:fs/promises";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import { buildCollector, type CollectorApp } from "./app.js";
|
||||||
|
import { parseBoothTokens } from "./config.js";
|
||||||
|
|
||||||
|
// The collector: one ingest surface (bearer per booth, idempotent), one review surface
|
||||||
|
// (Basic), one export. Exercised over app.inject with a hand-built multipart body.
|
||||||
|
|
||||||
|
let app: CollectorApp;
|
||||||
|
let dir: string;
|
||||||
|
const TOKENS = new Map([["booth-7", "0123456789abcdef0123456789abcdef"], ["booth-9", "fedcba9876543210fedcba9876543210"]]);
|
||||||
|
const REVIEWER = { user: "julian", pass: "review-pass-123" };
|
||||||
|
const basic = "Basic " + Buffer.from(`${REVIEWER.user}:${REVIEWER.pass}`).toString("base64");
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
dir = await mkdtemp(path.join(tmpdir(), "collector-"));
|
||||||
|
app = await buildCollector({ host: "127.0.0.1", port: 0, dataDir: dir, boothTokens: TOKENS, reviewer: REVIEWER, trainerUrl: null }, { dbFile: ":memory:" });
|
||||||
|
await app.ready();
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
await rm(dir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
/** A minimal JPEG-looking blob (SOI marker + padding) — the collector checks the magic only. */
|
||||||
|
const JPEG = Buffer.concat([Buffer.from([0xff, 0xd8, 0xff, 0xe0]), Buffer.alloc(200, 1)]);
|
||||||
|
|
||||||
|
function meta(over: Record<string, unknown> = {}) {
|
||||||
|
return {
|
||||||
|
v: 1, booth: "booth-7", item: "item-1", order: "o-1", at: "2026-09-06T10:00:00.000Z", operator: "ab12cd34ef56ab12",
|
||||||
|
operatorCategory: { id: "car", name: "Vetura", classes: ["car", "sedan", "hatchback"] }, service: "Standard",
|
||||||
|
vision: { class: "suv", confidence: 0.91, categoryId: "suv" }, downgraded: true,
|
||||||
|
image: { width: 320, height: 200, plateBlurred: true },
|
||||||
|
...over,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function multipart(fields: Record<string, string>, file: Buffer | null): { body: Buffer; type: string } {
|
||||||
|
const b = "----collector-test";
|
||||||
|
const parts: Buffer[] = [];
|
||||||
|
for (const [k, v] of Object.entries(fields)) parts.push(Buffer.from(`--${b}\r\nContent-Disposition: form-data; name="${k}"\r\n\r\n${v}\r\n`));
|
||||||
|
if (file) parts.push(Buffer.from(`--${b}\r\nContent-Disposition: form-data; name="image"; filename="x.jpg"\r\nContent-Type: image/jpeg\r\n\r\n`), file, Buffer.from("\r\n"));
|
||||||
|
parts.push(Buffer.from(`--${b}--\r\n`));
|
||||||
|
return { body: Buffer.concat(parts), type: `multipart/form-data; boundary=${b}` };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ingest(m: Record<string, unknown>, token = TOKENS.get("booth-7")!, file: Buffer | null = JPEG, extra: Record<string, string> = {}) {
|
||||||
|
const { body, type } = multipart({ meta: JSON.stringify(m) }, file);
|
||||||
|
return app.inject({ method: "POST", url: "/ingest", headers: { authorization: `Bearer ${token}`, "content-type": type, ...extra }, payload: body });
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("ingest", () => {
|
||||||
|
it("stores the crop and the decision under the token's booth; retries are idempotent", async () => {
|
||||||
|
const r = await ingest(meta());
|
||||||
|
expect(r.statusCode).toBe(201);
|
||||||
|
const row = app.collectorDb.get("item-1")!;
|
||||||
|
expect(row).toMatchObject({ booth: "booth-7", operatorCategoryName: "Vetura", visionClass: "suv", downgraded: 1, plateBlurred: 1, imagePath: "crops/booth-7/item-1.jpg" });
|
||||||
|
expect(JSON.parse(row.operatorClasses)).toEqual(["car", "sedan", "hatchback"]);
|
||||||
|
const again = await ingest(meta());
|
||||||
|
expect(again.statusCode).toBe(200);
|
||||||
|
expect(again.json()).toEqual({ ok: true, duplicate: true });
|
||||||
|
expect((await app.inject({ method: "GET", url: "/health" })).json()).toMatchObject({ ok: true, booths: 1, pending: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses a bad token, a booth mismatch, a non-JPEG, and malformed meta", async () => {
|
||||||
|
expect((await ingest(meta(), "nope-nope-nope-nope-nope")).statusCode).toBe(401);
|
||||||
|
expect((await ingest(meta({ booth: "booth-9" }))).statusCode).toBe(422); // token is booth-7's
|
||||||
|
expect((await ingest(meta(), TOKENS.get("booth-7")!, JPEG, { "x-booth-id": "booth-9" })).statusCode).toBe(403);
|
||||||
|
expect((await ingest(meta(), TOKENS.get("booth-7")!, Buffer.alloc(300, 7))).statusCode).toBe(415);
|
||||||
|
expect((await ingest(meta(), TOKENS.get("booth-7")!, null)).statusCode).toBe(400);
|
||||||
|
expect((await ingest(meta({ vision: { class: "spaceship", confidence: 0.5, categoryId: null } }))).statusCode).toBe(422);
|
||||||
|
expect((await ingest(meta({ item: "../../etc/passwd" }))).statusCode).toBe(422);
|
||||||
|
expect((await ingest(meta({ v: 2 }))).statusCode).toBe(422);
|
||||||
|
expect(app.collectorDb.stats().booths).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("review + export", () => {
|
||||||
|
it("the reviewer lists pending items, sees the crop, labels it; stats compare the label with the operator's category; the export lists usable labels only", async () => {
|
||||||
|
await ingest(meta());
|
||||||
|
await ingest(meta({ item: "item-2", operator: "ab12cd34ef56ab12", vision: { class: "car", confidence: 0.8, categoryId: "car" }, downgraded: false }));
|
||||||
|
await ingest(meta({ item: "item-3", booth: "booth-9", operator: "9999999999999999" }), TOKENS.get("booth-9")!);
|
||||||
|
|
||||||
|
// No login → 401 with a challenge; nothing without a configured reviewer is tested in config.
|
||||||
|
const anon = await app.inject({ method: "GET", url: "/api/items" });
|
||||||
|
expect(anon.statusCode).toBe(401);
|
||||||
|
expect(anon.headers["www-authenticate"]).toContain("Basic");
|
||||||
|
expect((await app.inject({ method: "GET", url: "/review", headers: { authorization: basic } })).headers["content-type"]).toContain("text/html");
|
||||||
|
|
||||||
|
const list = (await app.inject({ method: "GET", url: "/api/items?status=pending", headers: { authorization: basic } })).json();
|
||||||
|
expect(list.items.map((i: { id: string }) => i.id)).toEqual(["item-1", "item-2", "item-3"]);
|
||||||
|
expect(list.items[0].imagePath).toBeUndefined();
|
||||||
|
const img = await app.inject({ method: "GET", url: "/api/items/item-1/image", headers: { authorization: basic } });
|
||||||
|
expect(img.statusCode).toBe(200);
|
||||||
|
expect(img.headers["content-type"]).toBe("image/jpeg");
|
||||||
|
expect(img.rawPayload.subarray(0, 3)).toEqual(Buffer.from([0xff, 0xd8, 0xff]));
|
||||||
|
|
||||||
|
// item-1: operator said Vetura (car/sedan/hatchback), reviewer says suv → disagree.
|
||||||
|
// item-2: reviewer says sedan → inside Vetura → agree. item-3: unusable.
|
||||||
|
const post = (id: string, label: string) =>
|
||||||
|
app.inject({ method: "POST", url: `/api/items/${id}/review`, headers: { authorization: basic, "content-type": "application/json" }, payload: { label } });
|
||||||
|
expect((await post("item-1", "suv")).json()).toMatchObject({ reviewLabel: "suv", reviewer: "julian" });
|
||||||
|
expect((await post("item-2", "sedan")).statusCode).toBe(200);
|
||||||
|
expect((await post("item-3", "unusable")).statusCode).toBe(200);
|
||||||
|
expect((await post("item-3", "spaceship")).statusCode).toBe(400);
|
||||||
|
expect((await post("nope", "suv")).statusCode).toBe(404);
|
||||||
|
|
||||||
|
const stats = (await app.inject({ method: "GET", url: "/api/stats", headers: { authorization: basic } })).json();
|
||||||
|
expect(stats.booths).toEqual([
|
||||||
|
{ booth: "booth-7", received: 2, pending: 0, reviewed: 2, entries: 0 },
|
||||||
|
{ booth: "booth-9", received: 1, pending: 0, reviewed: 1, entries: 0 },
|
||||||
|
]);
|
||||||
|
expect(stats.operators).toEqual([
|
||||||
|
{ booth: "booth-7", operatorRef: "ab12cd34ef56ab12", reviewed: 2, agree: 1, disagree: 1, unusable: 0 },
|
||||||
|
{ booth: "booth-9", operatorRef: "9999999999999999", reviewed: 1, agree: 0, disagree: 0, unusable: 1 },
|
||||||
|
]);
|
||||||
|
|
||||||
|
const csv = await app.inject({ method: "GET", url: "/export/labels.csv", headers: { authorization: basic } });
|
||||||
|
expect(csv.statusCode).toBe(200);
|
||||||
|
const lines = csv.body.trim().split("\n");
|
||||||
|
expect(lines[0]).toBe("item,booth,kind,path,label,operator_category,operator_classes,vision_class,vision_confidence,downgraded,at,reviewed_at");
|
||||||
|
expect(lines).toHaveLength(3); // header + 2 usable labels; the unusable one is left out
|
||||||
|
expect(lines[1]).toContain('"item-1","booth-7","wash","crops/booth-7/item-1.jpg","suv","Vetura","car|sedan|hatchback","suv"');
|
||||||
|
|
||||||
|
// An ENTRY sample: no order, no operator — accepted, reviewable, in the export, and
|
||||||
|
// never counted in any operator's agreement.
|
||||||
|
const entry = await ingest({ v: 1, kind: "entry", booth: "booth-7", item: "entry-1", at: "2026-09-06T11:00:00.000Z", vision: { class: "car", confidence: 0.7 }, image: { width: 300, height: 180, plateBlurred: true } });
|
||||||
|
expect(entry.statusCode).toBe(201);
|
||||||
|
expect((await ingest({ v: 1, kind: "entry", booth: "booth-7", item: "entry-2", at: "x", vision: { class: "car", confidence: 0.7 }, image: { width: 1, height: 1, plateBlurred: true } })).statusCode).toBe(422);
|
||||||
|
expect((await post("entry-1", "suv")).statusCode).toBe(200);
|
||||||
|
const stats2 = (await app.inject({ method: "GET", url: "/api/stats", headers: { authorization: basic } })).json();
|
||||||
|
expect(stats2.booths[0]).toEqual({ booth: "booth-7", received: 3, pending: 0, reviewed: 3, entries: 1 });
|
||||||
|
expect(stats2.operators.find((o: { booth: string }) => o.booth === "booth-7")).toMatchObject({ reviewed: 2, agree: 1, disagree: 1 });
|
||||||
|
const csv3 = (await app.inject({ method: "GET", url: "/export/labels.csv", headers: { authorization: basic } })).body;
|
||||||
|
expect(csv3).toContain('"entry-1","booth-7","entry","crops/booth-7/entry-1.jpg","suv","","","car"');
|
||||||
|
|
||||||
|
// A booth-supplied name that looks like a spreadsheet formula is neutralised in the export.
|
||||||
|
await ingest(meta({ item: "item-4", operatorCategory: { id: "x", name: "=HYPERLINK(\"http://evil\")", classes: ["car"] } }));
|
||||||
|
await post("item-4", "car");
|
||||||
|
const csv2 = (await app.inject({ method: "GET", url: "/export/labels.csv", headers: { authorization: basic } })).body;
|
||||||
|
expect(csv2).toContain(`"'=HYPERLINK(""http://evil"")"`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("config", () => {
|
||||||
|
it("parses booth:token pairs and refuses short tokens", () => {
|
||||||
|
expect([...parseBoothTokens("a:0123456789abcdef, b:fedcba9876543210\nc:0000000000000000").keys()]).toEqual(["a", "b", "c"]);
|
||||||
|
expect(() => parseBoothTokens("a:short")).toThrow(/too short/);
|
||||||
|
expect(() => parseBoothTokens("nocolon")).toThrow(/bad pair/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("schema migration", () => {
|
||||||
|
it("opens a database created before the `kind` column and adds the missing columns, so ingest and stats work", async () => {
|
||||||
|
// art-docker-station, 2026-09-16: the volume's DB predated `kind`; CREATE TABLE IF NOT
|
||||||
|
// EXISTS left it alone, and /health, every ingest and the trainer's readiness failed
|
||||||
|
// with "no such column: kind". Replay: a file with the ORIGINAL column set.
|
||||||
|
const { default: Database } = await import("better-sqlite3");
|
||||||
|
const file = path.join(dir, "old.sqlite");
|
||||||
|
const old = new Database(file);
|
||||||
|
old.exec(`CREATE TABLE items (
|
||||||
|
id TEXT PRIMARY KEY, booth TEXT NOT NULL, order_ref TEXT NOT NULL, at TEXT NOT NULL,
|
||||||
|
service TEXT NOT NULL, vision_class TEXT NOT NULL, vision_confidence REAL NOT NULL,
|
||||||
|
image_width INTEGER NOT NULL, image_height INTEGER NOT NULL, plate_blurred INTEGER NOT NULL,
|
||||||
|
image_path TEXT NOT NULL, received_at TEXT NOT NULL, review_label TEXT, reviewed_at TEXT, reviewer TEXT)`);
|
||||||
|
old.prepare(
|
||||||
|
"INSERT INTO items (id, booth, order_ref, at, service, vision_class, vision_confidence, image_width, image_height, plate_blurred, image_path, received_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||||
|
).run("legacy-1", "booth-7", "o-0", "2026-09-01T00:00:00.000Z", "Standard", "suv", 0.8, 100, 100, 1, "crops/legacy-1.jpg", "2026-09-01T00:00:00.000Z");
|
||||||
|
old.close();
|
||||||
|
|
||||||
|
const legacy = await buildCollector({ host: "127.0.0.1", port: 0, dataDir: dir, boothTokens: TOKENS, reviewer: REVIEWER, trainerUrl: null }, { dbFile: file });
|
||||||
|
await legacy.ready();
|
||||||
|
try {
|
||||||
|
const health = await legacy.inject({ method: "GET", url: "/health" });
|
||||||
|
expect(health.statusCode).toBe(200);
|
||||||
|
expect(health.json()).toMatchObject({ ok: true, booths: 1, pending: 1 });
|
||||||
|
|
||||||
|
const { body, type } = multipart({ meta: JSON.stringify(meta({ item: "item-new" })) }, JPEG);
|
||||||
|
const r = await legacy.inject({ method: "POST", url: "/ingest", headers: { authorization: `Bearer ${TOKENS.get("booth-7")!}`, "content-type": type }, payload: body });
|
||||||
|
expect(r.statusCode).toBe(201);
|
||||||
|
|
||||||
|
const stats = await legacy.inject({ method: "GET", url: "/api/stats", headers: { authorization: basic } });
|
||||||
|
expect(stats.statusCode).toBe(200);
|
||||||
|
|
||||||
|
// The legacy row reads back with the defaults the new columns carry.
|
||||||
|
const cols = new Database(file, { readonly: true }).prepare("PRAGMA table_info(items)").all() as { name: string }[];
|
||||||
|
expect(cols.map((c) => c.name)).toEqual(expect.arrayContaining(["kind", "operator_ref", "operator_classes", "vision_category_id", "downgraded"]));
|
||||||
|
const legacyRow = new Database(file, { readonly: true }).prepare("SELECT kind, operator_classes, downgraded FROM items WHERE id = 'legacy-1'").get() as Record<string, unknown>;
|
||||||
|
expect(legacyRow).toEqual({ kind: "wash", operator_classes: "[]", downgraded: 0 });
|
||||||
|
} finally {
|
||||||
|
await legacy.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,302 @@
|
|||||||
|
import { timingSafeEqual } from "node:crypto";
|
||||||
|
import { createReadStream } from "node:fs";
|
||||||
|
import { mkdir, writeFile } from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
import Fastify, { type FastifyInstance, type FastifyReply, type FastifyRequest } from "fastify";
|
||||||
|
import multipart from "@fastify/multipart";
|
||||||
|
import { isVehicleClass } from "@parking/shared";
|
||||||
|
import type { CollectorConfig } from "./config.js";
|
||||||
|
import { CollectorDb, type ItemRow, type ReviewVerdict } from "./db.js";
|
||||||
|
import { reviewPage } from "./review-page.js";
|
||||||
|
|
||||||
|
// The collector — the far end of the booth's review outbox
|
||||||
|
// (wiki/concepts/vision-review-outbox.md). Three surfaces and nothing else:
|
||||||
|
// POST /ingest one package from one booth (bearer token per booth; idempotent)
|
||||||
|
// /review + /api/* the reviewer's screen (HTTP Basic, one login)
|
||||||
|
// GET /export/labels.csv the training set: reviewed, usable rows (crops sit beside it on
|
||||||
|
// the volume, so the trainer on this host reads them directly)
|
||||||
|
// /api/training/* the Training section: a thin proxy to the trainer's job API on
|
||||||
|
// the compose network (never published), behind the reviewer login
|
||||||
|
// It deliberately has no fleet features and no path back into a booth.
|
||||||
|
|
||||||
|
/** The package's `meta` part, as the booth sends it (review-outbox.ts). */
|
||||||
|
interface IngestMeta {
|
||||||
|
v: number;
|
||||||
|
/** "wash" (default when absent) = a desk decision; "entry" = a sampled entry read with
|
||||||
|
* no order and no operator — crop + the camera's class only. */
|
||||||
|
kind?: "wash" | "entry";
|
||||||
|
booth: string;
|
||||||
|
item: string;
|
||||||
|
order?: string;
|
||||||
|
at: string;
|
||||||
|
operator?: string;
|
||||||
|
operatorCategory?: { id: string; name: string; classes?: string[] };
|
||||||
|
service?: string;
|
||||||
|
vision: { class: string; confidence: number; categoryId?: string | null };
|
||||||
|
downgraded?: boolean;
|
||||||
|
image: { width: number; height: number; plateBlurred: boolean };
|
||||||
|
}
|
||||||
|
|
||||||
|
const ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
||||||
|
const MAX_IMAGE_BYTES = 2 * 1024 * 1024;
|
||||||
|
|
||||||
|
function str(v: unknown, max = 200): string | null {
|
||||||
|
return typeof v === "string" && v.length > 0 && v.length <= max ? v : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Validate the meta part; returns a message on the first problem. */
|
||||||
|
function checkMeta(m: unknown, booth: string): { ok: true; meta: IngestMeta } | { ok: false; why: string } {
|
||||||
|
if (!m || typeof m !== "object") return { ok: false, why: "meta must be an object" };
|
||||||
|
const x = m as Record<string, unknown>;
|
||||||
|
if (x.v !== 1) return { ok: false, why: "unsupported meta version" };
|
||||||
|
if (x.booth !== booth) return { ok: false, why: "meta.booth does not match the token's booth" };
|
||||||
|
if (!str(x.item, 64) || !ID_RE.test(x.item as string)) return { ok: false, why: "bad item id" };
|
||||||
|
if (!str(x.at, 40) || Number.isNaN(Date.parse(x.at as string))) return { ok: false, why: "bad timestamp" };
|
||||||
|
const kind = x.kind === undefined ? "wash" : x.kind;
|
||||||
|
if (kind !== "wash" && kind !== "entry") return { ok: false, why: "bad kind" };
|
||||||
|
const v = x.vision as Record<string, unknown> | undefined;
|
||||||
|
if (!v || !isVehicleClass(v.class) || typeof v.confidence !== "number" || v.confidence < 0 || v.confidence > 1) return { ok: false, why: "bad vision read" };
|
||||||
|
if (v.categoryId != null && !str(v.categoryId, 64)) return { ok: false, why: "bad vision.categoryId" };
|
||||||
|
if (kind === "wash") {
|
||||||
|
if (!str(x.order, 64)) return { ok: false, why: "bad order ref" };
|
||||||
|
if (!str(x.operator, 64)) return { ok: false, why: "bad operator ref" };
|
||||||
|
const oc = x.operatorCategory as Record<string, unknown> | undefined;
|
||||||
|
if (!oc || !str(oc.id, 64) || !str(oc.name, 120)) return { ok: false, why: "bad operatorCategory" };
|
||||||
|
if (oc.classes !== undefined && (!Array.isArray(oc.classes) || !oc.classes.every(isVehicleClass))) return { ok: false, why: "bad operatorCategory.classes" };
|
||||||
|
if (!str(x.service, 120)) return { ok: false, why: "bad service" };
|
||||||
|
if (typeof x.downgraded !== "boolean") return { ok: false, why: "bad downgraded" };
|
||||||
|
}
|
||||||
|
const im = x.image as Record<string, unknown> | undefined;
|
||||||
|
if (!im || typeof im.width !== "number" || typeof im.height !== "number" || typeof im.plateBlurred !== "boolean") return { ok: false, why: "bad image meta" };
|
||||||
|
return { ok: true, meta: x as unknown as IngestMeta };
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeEqual(a: string, b: string): boolean {
|
||||||
|
const ba = Buffer.from(a);
|
||||||
|
const bb = Buffer.from(b);
|
||||||
|
return ba.length === bb.length && timingSafeEqual(ba, bb);
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CollectorApp extends FastifyInstance {
|
||||||
|
collectorDb: CollectorDb;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function buildCollector(cfg: CollectorConfig, opts: { dbFile?: string } = {}): Promise<CollectorApp> {
|
||||||
|
await mkdir(path.join(cfg.dataDir, "crops"), { recursive: true });
|
||||||
|
const db = new CollectorDb(opts.dbFile ?? path.join(cfg.dataDir, "collector.sqlite"));
|
||||||
|
const app = Fastify({ logger: { level: process.env.LOG_LEVEL ?? "info" }, bodyLimit: 64 * 1024 }) as unknown as CollectorApp;
|
||||||
|
app.collectorDb = db;
|
||||||
|
await app.register(multipart, { limits: { fileSize: MAX_IMAGE_BYTES, files: 1, fields: 4, parts: 6 } });
|
||||||
|
app.addHook("onClose", async () => db.close());
|
||||||
|
|
||||||
|
/** Which booth this bearer token belongs to, or null. Constant-time per candidate. */
|
||||||
|
function boothForToken(req: FastifyRequest): string | null {
|
||||||
|
const h = req.headers.authorization ?? "";
|
||||||
|
if (!h.startsWith("Bearer ")) return null;
|
||||||
|
const token = h.slice(7).trim();
|
||||||
|
let found: string | null = null;
|
||||||
|
for (const [booth, t] of cfg.boothTokens) if (safeEqual(token, t)) found = booth;
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** HTTP Basic for the reviewer. */
|
||||||
|
async function requireReviewer(req: FastifyRequest, reply: FastifyReply): Promise<void> {
|
||||||
|
if (!cfg.reviewer) return reply.code(503).send({ error: "reviewer login not configured" });
|
||||||
|
const h = req.headers.authorization ?? "";
|
||||||
|
if (h.startsWith("Basic ")) {
|
||||||
|
const [user, ...rest] = Buffer.from(h.slice(6), "base64").toString("utf8").split(":");
|
||||||
|
const pass = rest.join(":");
|
||||||
|
if (user && safeEqual(user, cfg.reviewer.user) && safeEqual(pass, cfg.reviewer.pass)) return;
|
||||||
|
}
|
||||||
|
return reply.code(401).header("www-authenticate", 'Basic realm="wash review", charset="UTF-8"').send({ error: "unauthorized" });
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get("/health", async () => {
|
||||||
|
const s = db.stats();
|
||||||
|
return { ok: true, booths: s.booths.length, pending: s.booths.reduce((n, b) => n + b.pending, 0) };
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- Ingest (booths) -----------------------------------------------------------------
|
||||||
|
app.post("/ingest", async (req, reply) => {
|
||||||
|
const booth = boothForToken(req);
|
||||||
|
if (!booth) return reply.code(401).send({ error: "unauthorized" });
|
||||||
|
const claimed = req.headers["x-booth-id"];
|
||||||
|
if (typeof claimed === "string" && claimed !== booth) return reply.code(403).send({ error: "booth id does not match the token" });
|
||||||
|
if (!req.isMultipart()) return reply.code(415).send({ error: "multipart/form-data expected" });
|
||||||
|
|
||||||
|
let metaRaw: string | null = null;
|
||||||
|
let image: Buffer | null = null;
|
||||||
|
try {
|
||||||
|
for await (const part of req.parts()) {
|
||||||
|
if (part.type === "file" && part.fieldname === "image") {
|
||||||
|
image = await part.toBuffer();
|
||||||
|
} else if (part.type === "field" && part.fieldname === "meta") {
|
||||||
|
metaRaw = String(part.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
const code = (err as { code?: string }).code;
|
||||||
|
return reply.code(code === "FST_REQ_FILE_TOO_LARGE" ? 413 : 400).send({ error: (err as Error).message });
|
||||||
|
}
|
||||||
|
if (!metaRaw) return reply.code(400).send({ error: "meta part missing" });
|
||||||
|
if (!image || image.length < 100) return reply.code(400).send({ error: "image part missing" });
|
||||||
|
if (!(image[0] === 0xff && image[1] === 0xd8 && image[2] === 0xff)) return reply.code(415).send({ error: "image must be a JPEG" });
|
||||||
|
let parsed: unknown;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(metaRaw);
|
||||||
|
} catch {
|
||||||
|
return reply.code(400).send({ error: "meta is not JSON" });
|
||||||
|
}
|
||||||
|
const checked = checkMeta(parsed, booth);
|
||||||
|
if (!checked.ok) return reply.code(422).send({ error: checked.why });
|
||||||
|
const meta = checked.meta;
|
||||||
|
|
||||||
|
// Idempotent on the item id: a booth retrying after a lost 2xx must not duplicate.
|
||||||
|
if (db.get(meta.item)) return reply.code(200).send({ ok: true, duplicate: true });
|
||||||
|
|
||||||
|
const rel = path.posix.join("crops", booth, `${meta.item}.jpg`);
|
||||||
|
await mkdir(path.join(cfg.dataDir, "crops", booth), { recursive: true });
|
||||||
|
await writeFile(path.join(cfg.dataDir, rel), image);
|
||||||
|
const kind = meta.kind ?? "wash";
|
||||||
|
db.insert({
|
||||||
|
id: meta.item,
|
||||||
|
booth,
|
||||||
|
kind,
|
||||||
|
orderRef: meta.order ?? "",
|
||||||
|
at: meta.at,
|
||||||
|
operatorRef: meta.operator ?? "",
|
||||||
|
operatorCategoryId: meta.operatorCategory?.id ?? "",
|
||||||
|
operatorCategoryName: meta.operatorCategory?.name ?? "",
|
||||||
|
operatorClasses: JSON.stringify(meta.operatorCategory?.classes ?? []),
|
||||||
|
service: meta.service ?? "",
|
||||||
|
visionClass: meta.vision.class,
|
||||||
|
visionConfidence: meta.vision.confidence,
|
||||||
|
visionCategoryId: meta.vision.categoryId ?? null,
|
||||||
|
downgraded: meta.downgraded ? 1 : 0,
|
||||||
|
imageWidth: meta.image.width,
|
||||||
|
imageHeight: meta.image.height,
|
||||||
|
plateBlurred: meta.image.plateBlurred ? 1 : 0,
|
||||||
|
imagePath: rel,
|
||||||
|
receivedAt: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
req.log.info(`ingest: ${booth} ${kind} ${meta.item} (${meta.vision.class}${kind === "wash" ? ` → ${meta.operatorCategory!.name}` : ""})`);
|
||||||
|
return reply.code(201).send({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- Review (the trusted person) -----------------------------------------------------
|
||||||
|
const page = reviewPage();
|
||||||
|
app.get("/", { preHandler: requireReviewer }, async (_req, reply) => reply.redirect("/review"));
|
||||||
|
app.get("/review", { preHandler: requireReviewer }, async (_req, reply) => reply.type("text/html; charset=utf-8").send(page));
|
||||||
|
|
||||||
|
app.get<{ Querystring: { status?: string; limit?: string; booth?: string } }>(
|
||||||
|
"/api/items",
|
||||||
|
{ preHandler: requireReviewer },
|
||||||
|
async (req) => {
|
||||||
|
const status = req.query.status === "reviewed" ? "reviewed" : "pending";
|
||||||
|
const limit = Math.min(Math.max(Number(req.query.limit) || 25, 1), 200);
|
||||||
|
return { items: db.list(status, limit, req.query.booth || undefined).map(publicItem) };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get<{ Params: { id: string } }>("/api/items/:id/image", { preHandler: requireReviewer }, async (req, reply) => {
|
||||||
|
const row = db.get(req.params.id);
|
||||||
|
if (!row) return reply.code(404).send({ error: "not found" });
|
||||||
|
return reply.type("image/jpeg").header("cache-control", "private, max-age=3600").send(createReadStream(path.join(cfg.dataDir, row.imagePath)));
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post<{ Params: { id: string }; Body: { label?: unknown } }>("/api/items/:id/review", { preHandler: requireReviewer }, async (req, reply) => {
|
||||||
|
const label = req.body?.label;
|
||||||
|
if (label !== "unusable" && !isVehicleClass(label)) return reply.code(400).send({ error: "label must be a vehicle class or 'unusable'" });
|
||||||
|
if (!db.get(req.params.id)) return reply.code(404).send({ error: "not found" });
|
||||||
|
const row = db.review(req.params.id, label as ReviewVerdict, cfg.reviewer!.user);
|
||||||
|
return publicItem(row!);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/api/stats", { preHandler: requireReviewer }, async () => db.stats());
|
||||||
|
|
||||||
|
// --- Export (the training set) --------------------------------------------------------
|
||||||
|
app.get("/export/labels.csv", { preHandler: requireReviewer }, async (_req, reply) => {
|
||||||
|
const rows = db.labelled();
|
||||||
|
// Quote every cell; a cell starting like a spreadsheet formula (=, +, -, @, tab, CR)
|
||||||
|
// gets a leading apostrophe — the category/service names are booth-supplied text and
|
||||||
|
// the reviewer will open this in a spreadsheet (CSV formula injection).
|
||||||
|
const q = (s: string | number | null) => {
|
||||||
|
let v = String(s ?? "");
|
||||||
|
if (/^[=+\-@\t\r]/.test(v)) v = `'${v}`;
|
||||||
|
return `"${v.replace(/"/g, '""')}"`;
|
||||||
|
};
|
||||||
|
const head = "item,booth,kind,path,label,operator_category,operator_classes,vision_class,vision_confidence,downgraded,at,reviewed_at";
|
||||||
|
const lines = rows.map((r) =>
|
||||||
|
[r.id, r.booth, r.kind, r.imagePath, r.reviewLabel, r.operatorCategoryName, JSON.parse(r.operatorClasses).join("|"), r.visionClass, r.visionConfidence, r.downgraded, r.at, r.reviewedAt].map(q).join(","),
|
||||||
|
);
|
||||||
|
return reply.type("text/csv; charset=utf-8").header("content-disposition", 'attachment; filename="labels.csv"').send([head, ...lines].join("\n") + "\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- Training (proxy to the trainer's job API) ----------------------------------------
|
||||||
|
// The trainer is a sibling container reading the same volume; it is reachable only on the
|
||||||
|
// compose network, so the reviewer's login here is the only gate. The proxy forwards a
|
||||||
|
// fixed set of paths and passes the trainer's status codes through (409 = a job runs).
|
||||||
|
const trainer = cfg.trainerUrl;
|
||||||
|
async function viaTrainer(reply: FastifyReply, tpath: string, init?: RequestInit): Promise<unknown> {
|
||||||
|
if (!trainer) return reply.code(503).send({ error: "trainer not configured" });
|
||||||
|
let r: Response;
|
||||||
|
try {
|
||||||
|
r = await fetch(trainer + tpath, { ...init, signal: AbortSignal.timeout(15_000) });
|
||||||
|
} catch (err) {
|
||||||
|
return reply.code(502).send({ error: `trainer unreachable: ${(err as Error).message}` });
|
||||||
|
}
|
||||||
|
const ctype = r.headers.get("content-type") ?? "application/json";
|
||||||
|
return reply.code(r.status).type(ctype).send(Buffer.from(await r.arrayBuffer()));
|
||||||
|
}
|
||||||
|
app.get("/api/training/status", { preHandler: requireReviewer }, async (_req, reply) => {
|
||||||
|
if (!trainer) return { configured: false };
|
||||||
|
try {
|
||||||
|
const get = async (p: string) => {
|
||||||
|
const r = await fetch(trainer + p, { signal: AbortSignal.timeout(15_000) });
|
||||||
|
if (!r.ok) {
|
||||||
|
// Surface the trainer's own error text (its handlers answer 500 JSON), so the
|
||||||
|
// reviewer reads "no such column: kind", not just a status code.
|
||||||
|
const detail = await r.text().then((t) => {
|
||||||
|
try {
|
||||||
|
return String((JSON.parse(t) as { error?: unknown }).error ?? t);
|
||||||
|
} catch {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}, () => "");
|
||||||
|
throw new Error(`${p} → HTTP ${r.status}${detail ? `: ${detail.slice(0, 300)}` : ""}`);
|
||||||
|
}
|
||||||
|
return r.json() as Promise<Record<string, unknown>>;
|
||||||
|
};
|
||||||
|
const [health, readiness, versions, jobs] = await Promise.all([get("/health"), get("/readiness"), get("/versions"), get("/jobs")]);
|
||||||
|
return { configured: true, reachable: true, health, readiness, versions: versions.versions, jobs: jobs.jobs, current: jobs.current };
|
||||||
|
} catch (err) {
|
||||||
|
return reply.code(200).send({ configured: true, reachable: false, error: (err as Error).message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
app.post<{ Body: Record<string, unknown> }>("/api/training/jobs", { preHandler: requireReviewer }, async (req, reply) => {
|
||||||
|
const b = req.body && typeof req.body === "object" ? req.body : {};
|
||||||
|
const kind = b.kind;
|
||||||
|
if (kind !== "train" && kind !== "evaluate" && kind !== "publish") return reply.code(400).send({ error: "kind must be train, evaluate or publish" });
|
||||||
|
// Only the knobs the UI offers cross over; the trainer validates their values.
|
||||||
|
const allowed = ["kind", "mode", "backbone", "minAccuracy", "minPerClass", "epochs", "version"];
|
||||||
|
const body: Record<string, unknown> = {};
|
||||||
|
for (const k of allowed) if (b[k] !== undefined) body[k] = b[k];
|
||||||
|
return viaTrainer(reply, "/jobs", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
||||||
|
});
|
||||||
|
app.get<{ Params: { id: string } }>("/api/training/jobs/:id", { preHandler: requireReviewer }, async (req, reply) => {
|
||||||
|
if (!ID_RE.test(req.params.id)) return reply.code(400).send({ error: "bad job id" });
|
||||||
|
return viaTrainer(reply, `/jobs/${encodeURIComponent(req.params.id)}`);
|
||||||
|
});
|
||||||
|
app.get<{ Params: { v: string } }>("/api/training/versions/:v/report", { preHandler: requireReviewer }, async (req, reply) => {
|
||||||
|
if (!ID_RE.test(req.params.v)) return reply.code(400).send({ error: "bad version" });
|
||||||
|
return viaTrainer(reply, `/versions/${encodeURIComponent(req.params.v)}/report`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The row as the review screen sees it (no server paths). */
|
||||||
|
function publicItem(r: ItemRow): Omit<ItemRow, "imagePath"> {
|
||||||
|
const { imagePath: _p, ...rest } = r;
|
||||||
|
return rest;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
export interface CollectorConfig {
|
||||||
|
readonly host: string;
|
||||||
|
readonly port: number;
|
||||||
|
readonly dataDir: string;
|
||||||
|
/** boothId → bearer token. */
|
||||||
|
readonly boothTokens: ReadonlyMap<string, string>;
|
||||||
|
/** The single reviewer login; null = review screen and export refuse (503). */
|
||||||
|
readonly reviewer: { readonly user: string; readonly pass: string } | null;
|
||||||
|
/** The trainer's job API on the compose network (http://trainer:8091); null = the
|
||||||
|
* Training section is hidden and /api/training/* answers 503. */
|
||||||
|
readonly trainerUrl: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** "booth-7:abc,booth-9:def" (commas, whitespace or newlines between pairs). */
|
||||||
|
export function parseBoothTokens(raw: string): Map<string, string> {
|
||||||
|
const out = new Map<string, string>();
|
||||||
|
for (const pair of raw.split(/[,\s]+/)) {
|
||||||
|
if (!pair) continue;
|
||||||
|
const i = pair.indexOf(":");
|
||||||
|
if (i <= 0) throw new Error(`COLLECTOR_BOOTH_TOKENS: bad pair "${pair}" (want boothId:token)`);
|
||||||
|
const booth = pair.slice(0, i).trim();
|
||||||
|
const token = pair.slice(i + 1).trim();
|
||||||
|
if (!booth || token.length < 16) throw new Error(`COLLECTOR_BOOTH_TOKENS: token for "${booth}" too short (>=16 chars)`);
|
||||||
|
out.set(booth, token);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function configFromEnv(env: NodeJS.ProcessEnv = process.env): CollectorConfig {
|
||||||
|
const user = (env.COLLECTOR_REVIEWER_USER ?? "").trim();
|
||||||
|
const pass = env.COLLECTOR_REVIEWER_PASS ?? "";
|
||||||
|
return {
|
||||||
|
host: env.COLLECTOR_HOST ?? "0.0.0.0",
|
||||||
|
port: Number(env.COLLECTOR_PORT ?? 8090),
|
||||||
|
dataDir: env.COLLECTOR_DATA_DIR ?? "/data",
|
||||||
|
boothTokens: parseBoothTokens(env.COLLECTOR_BOOTH_TOKENS ?? ""),
|
||||||
|
reviewer: user && pass.length >= 8 ? { user, pass } : null,
|
||||||
|
trainerUrl: (env.COLLECTOR_TRAINER_URL ?? "").trim().replace(/\/+$/, "") || null,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
import Database from "better-sqlite3";
|
||||||
|
import type { VehicleClass } from "@parking/shared";
|
||||||
|
|
||||||
|
// One table. Each row is one booth decision: what the camera saw, what the operator
|
||||||
|
// chose, and (once reviewed) what a trusted person says the vehicle is. The crop itself
|
||||||
|
// lives on disk beside the DB (crops/<booth>/<item>.jpg) so the trainer on the same host
|
||||||
|
// reads it straight off the volume.
|
||||||
|
|
||||||
|
export interface ItemRow {
|
||||||
|
id: string;
|
||||||
|
booth: string;
|
||||||
|
/** "wash" = a desk decision (operator fields set); "entry" = a sampled entry read (pure
|
||||||
|
* training material: crop + the camera's class, operator fields empty). */
|
||||||
|
kind: "wash" | "entry";
|
||||||
|
orderRef: string;
|
||||||
|
at: string;
|
||||||
|
operatorRef: string;
|
||||||
|
operatorCategoryId: string;
|
||||||
|
operatorCategoryName: string;
|
||||||
|
/** The vision classes the operator's category covers at that site (its mapping) — what
|
||||||
|
* lets a reviewer's CLASS be compared with an operator's CATEGORY. JSON array. */
|
||||||
|
operatorClasses: string;
|
||||||
|
service: string;
|
||||||
|
visionClass: string;
|
||||||
|
visionConfidence: number;
|
||||||
|
visionCategoryId: string | null;
|
||||||
|
downgraded: number;
|
||||||
|
imageWidth: number;
|
||||||
|
imageHeight: number;
|
||||||
|
plateBlurred: number;
|
||||||
|
imagePath: string;
|
||||||
|
receivedAt: string;
|
||||||
|
reviewLabel: string | null; // a VehicleClass, or "unusable"
|
||||||
|
reviewedAt: string | null;
|
||||||
|
reviewer: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ReviewVerdict = VehicleClass | "unusable";
|
||||||
|
|
||||||
|
export class CollectorDb {
|
||||||
|
readonly #db: Database.Database;
|
||||||
|
|
||||||
|
constructor(file: string) {
|
||||||
|
this.#db = new Database(file);
|
||||||
|
this.#db.pragma("journal_mode = WAL");
|
||||||
|
this.#db.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS items (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
booth TEXT NOT NULL,
|
||||||
|
kind TEXT NOT NULL DEFAULT 'wash',
|
||||||
|
order_ref TEXT NOT NULL,
|
||||||
|
at TEXT NOT NULL,
|
||||||
|
operator_ref TEXT NOT NULL DEFAULT '',
|
||||||
|
operator_category_id TEXT NOT NULL DEFAULT '',
|
||||||
|
operator_category_name TEXT NOT NULL DEFAULT '',
|
||||||
|
operator_classes TEXT NOT NULL DEFAULT '[]',
|
||||||
|
service TEXT NOT NULL,
|
||||||
|
vision_class TEXT NOT NULL,
|
||||||
|
vision_confidence REAL NOT NULL,
|
||||||
|
vision_category_id TEXT,
|
||||||
|
downgraded INTEGER NOT NULL DEFAULT 0,
|
||||||
|
image_width INTEGER NOT NULL,
|
||||||
|
image_height INTEGER NOT NULL,
|
||||||
|
plate_blurred INTEGER NOT NULL,
|
||||||
|
image_path TEXT NOT NULL,
|
||||||
|
received_at TEXT NOT NULL,
|
||||||
|
review_label TEXT,
|
||||||
|
reviewed_at TEXT,
|
||||||
|
reviewer TEXT
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS items_pending ON items (reviewed_at, received_at);
|
||||||
|
CREATE INDEX IF NOT EXISTS items_booth ON items (booth, received_at);
|
||||||
|
`);
|
||||||
|
this.#migrate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Columns added after the first deploy, with the DDL that adds them to an EXISTING
|
||||||
|
* table. `CREATE TABLE IF NOT EXISTS` above only shapes a NEW database; a volume that
|
||||||
|
* was created by an earlier build keeps its old columns, and every query naming a new
|
||||||
|
* one then fails ("no such column: kind" — art-docker-station, 2026-09-16: the
|
||||||
|
* collector's /health, every ingest, and the trainer's readiness all broke on a DB
|
||||||
|
* from before `kind`). Each entry must be addable to a populated table, i.e. nullable
|
||||||
|
* or carrying a DEFAULT. Append here whenever a column joins the CREATE above. */
|
||||||
|
static readonly #ADDED_COLUMNS: ReadonlyArray<readonly [name: string, ddl: string]> = [
|
||||||
|
["kind", "TEXT NOT NULL DEFAULT 'wash'"],
|
||||||
|
["operator_ref", "TEXT NOT NULL DEFAULT ''"],
|
||||||
|
["operator_category_id", "TEXT NOT NULL DEFAULT ''"],
|
||||||
|
["operator_category_name", "TEXT NOT NULL DEFAULT ''"],
|
||||||
|
["operator_classes", "TEXT NOT NULL DEFAULT '[]'"],
|
||||||
|
["vision_category_id", "TEXT"],
|
||||||
|
["downgraded", "INTEGER NOT NULL DEFAULT 0"],
|
||||||
|
];
|
||||||
|
|
||||||
|
/** Bring an existing `items` table up to the current column set (idempotent). */
|
||||||
|
#migrate(): void {
|
||||||
|
const present = new Set(
|
||||||
|
(this.#db.prepare("PRAGMA table_info(items)").all() as { name: string }[]).map((c) => c.name),
|
||||||
|
);
|
||||||
|
for (const [name, ddl] of CollectorDb.#ADDED_COLUMNS) {
|
||||||
|
if (!present.has(name)) this.#db.exec(`ALTER TABLE items ADD COLUMN ${name} ${ddl}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The current column names of `items` (for tests and diagnostics). */
|
||||||
|
columns(): string[] {
|
||||||
|
return (this.#db.prepare("PRAGMA table_info(items)").all() as { name: string }[]).map((c) => c.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(): void {
|
||||||
|
this.#db.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
static #map(r: Record<string, unknown>): ItemRow {
|
||||||
|
return {
|
||||||
|
id: r.id as string,
|
||||||
|
booth: r.booth as string,
|
||||||
|
kind: r.kind === "entry" ? "entry" : "wash",
|
||||||
|
orderRef: r.order_ref as string,
|
||||||
|
at: r.at as string,
|
||||||
|
operatorRef: r.operator_ref as string,
|
||||||
|
operatorCategoryId: r.operator_category_id as string,
|
||||||
|
operatorCategoryName: r.operator_category_name as string,
|
||||||
|
operatorClasses: r.operator_classes as string,
|
||||||
|
service: r.service as string,
|
||||||
|
visionClass: r.vision_class as string,
|
||||||
|
visionConfidence: r.vision_confidence as number,
|
||||||
|
visionCategoryId: (r.vision_category_id as string | null) ?? null,
|
||||||
|
downgraded: r.downgraded as number,
|
||||||
|
imageWidth: r.image_width as number,
|
||||||
|
imageHeight: r.image_height as number,
|
||||||
|
plateBlurred: r.plate_blurred as number,
|
||||||
|
imagePath: r.image_path as string,
|
||||||
|
receivedAt: r.received_at as string,
|
||||||
|
reviewLabel: (r.review_label as string | null) ?? null,
|
||||||
|
reviewedAt: (r.reviewed_at as string | null) ?? null,
|
||||||
|
reviewer: (r.reviewer as string | null) ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
get(id: string): ItemRow | null {
|
||||||
|
const r = this.#db.prepare("SELECT * FROM items WHERE id = ?").get(id) as Record<string, unknown> | undefined;
|
||||||
|
return r ? CollectorDb.#map(r) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
insert(row: Omit<ItemRow, "reviewLabel" | "reviewedAt" | "reviewer">): void {
|
||||||
|
this.#db
|
||||||
|
.prepare(
|
||||||
|
`INSERT INTO items (id, booth, kind, order_ref, at, operator_ref, operator_category_id, operator_category_name,
|
||||||
|
operator_classes, service, vision_class, vision_confidence, vision_category_id, downgraded,
|
||||||
|
image_width, image_height, plate_blurred, image_path, received_at)
|
||||||
|
VALUES (@id, @booth, @kind, @orderRef, @at, @operatorRef, @operatorCategoryId, @operatorCategoryName,
|
||||||
|
@operatorClasses, @service, @visionClass, @visionConfidence, @visionCategoryId, @downgraded,
|
||||||
|
@imageWidth, @imageHeight, @plateBlurred, @imagePath, @receivedAt)`,
|
||||||
|
)
|
||||||
|
.run(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
list(status: "pending" | "reviewed", limit: number, booth?: string): ItemRow[] {
|
||||||
|
const where = [status === "pending" ? "reviewed_at IS NULL" : "reviewed_at IS NOT NULL"];
|
||||||
|
const params: unknown[] = [];
|
||||||
|
if (booth) {
|
||||||
|
where.push("booth = ?");
|
||||||
|
params.push(booth);
|
||||||
|
}
|
||||||
|
const order = status === "pending" ? "received_at ASC" : "reviewed_at DESC";
|
||||||
|
const rows = this.#db
|
||||||
|
.prepare(`SELECT * FROM items WHERE ${where.join(" AND ")} ORDER BY ${order} LIMIT ?`)
|
||||||
|
.all(...params, limit) as Record<string, unknown>[];
|
||||||
|
return rows.map((r) => CollectorDb.#map(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
review(id: string, label: ReviewVerdict, reviewer: string): ItemRow | null {
|
||||||
|
this.#db
|
||||||
|
.prepare("UPDATE items SET review_label = ?, reviewed_at = ?, reviewer = ? WHERE id = ?")
|
||||||
|
.run(label, new Date().toISOString(), reviewer, id);
|
||||||
|
return this.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Per booth: received / pending / reviewed. Per operator (booth + hash): how often the
|
||||||
|
* reviewer's class fell inside the operator's chosen category (agree) or outside
|
||||||
|
* (disagree) — the honest-mistake / fraud rate the outbox exists for. */
|
||||||
|
stats(): {
|
||||||
|
booths: { booth: string; received: number; pending: number; reviewed: number; entries: number }[];
|
||||||
|
operators: { booth: string; operatorRef: string; reviewed: number; agree: number; disagree: number; unusable: number }[];
|
||||||
|
} {
|
||||||
|
const booths = this.#db
|
||||||
|
.prepare(
|
||||||
|
`SELECT booth, COUNT(*) AS received,
|
||||||
|
SUM(CASE WHEN reviewed_at IS NULL THEN 1 ELSE 0 END) AS pending,
|
||||||
|
SUM(CASE WHEN reviewed_at IS NOT NULL THEN 1 ELSE 0 END) AS reviewed,
|
||||||
|
SUM(CASE WHEN kind = 'entry' THEN 1 ELSE 0 END) AS entries
|
||||||
|
FROM items GROUP BY booth ORDER BY booth`,
|
||||||
|
)
|
||||||
|
.all() as { booth: string; received: number; pending: number; reviewed: number; entries: number }[];
|
||||||
|
// Operator agreement is a WASH thing — an entry sample has no operator decision.
|
||||||
|
const reviewed = this.#db
|
||||||
|
.prepare("SELECT booth, operator_ref, operator_classes, review_label FROM items WHERE reviewed_at IS NOT NULL AND kind = 'wash'")
|
||||||
|
.all() as { booth: string; operator_ref: string; operator_classes: string; review_label: string }[];
|
||||||
|
const ops = new Map<string, { booth: string; operatorRef: string; reviewed: number; agree: number; disagree: number; unusable: number }>();
|
||||||
|
for (const r of reviewed) {
|
||||||
|
const key = `${r.booth} ${r.operator_ref}`;
|
||||||
|
let o = ops.get(key);
|
||||||
|
if (!o) ops.set(key, (o = { booth: r.booth, operatorRef: r.operator_ref, reviewed: 0, agree: 0, disagree: 0, unusable: 0 }));
|
||||||
|
o.reviewed += 1;
|
||||||
|
if (r.review_label === "unusable") o.unusable += 1;
|
||||||
|
else if ((JSON.parse(r.operator_classes) as string[]).includes(r.review_label)) o.agree += 1;
|
||||||
|
else o.disagree += 1;
|
||||||
|
}
|
||||||
|
return { booths, operators: [...ops.values()].sort((a, b) => b.disagree - a.disagree) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Reviewed, usable rows — the training set. */
|
||||||
|
labelled(): ItemRow[] {
|
||||||
|
const rows = this.#db
|
||||||
|
.prepare("SELECT * FROM items WHERE reviewed_at IS NOT NULL AND review_label != 'unusable' ORDER BY reviewed_at")
|
||||||
|
.all() as Record<string, unknown>[];
|
||||||
|
return rows.map((r) => CollectorDb.#map(r));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { buildCollector } from "./app.js";
|
||||||
|
import { configFromEnv } from "./config.js";
|
||||||
|
|
||||||
|
const cfg = configFromEnv();
|
||||||
|
const app = await buildCollector(cfg);
|
||||||
|
if (cfg.boothTokens.size === 0) app.log.warn("COLLECTOR_BOOTH_TOKENS is empty — no booth can ingest");
|
||||||
|
if (!cfg.reviewer) app.log.warn("COLLECTOR_REVIEWER_USER/PASS not set — the review screen and export refuse");
|
||||||
|
app.log.info(`collector: ${cfg.boothTokens.size} booth token(s), data in ${cfg.dataDir}, trainer ${cfg.trainerUrl ?? "not configured"}`);
|
||||||
|
await app.listen({ host: cfg.host, port: cfg.port });
|
||||||
|
|
||||||
|
const stop = async () => {
|
||||||
|
await app.close();
|
||||||
|
process.exit(0);
|
||||||
|
};
|
||||||
|
process.on("SIGTERM", () => void stop());
|
||||||
|
process.on("SIGINT", () => void stop());
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
import { VEHICLE_CLASSES } from "@parking/shared";
|
||||||
|
|
||||||
|
// The reviewer's screen: one pending crop at a time, the operator's pick and the camera's
|
||||||
|
// pick beside it, one button per vocabulary class + "unusable". Served by the collector
|
||||||
|
// itself (no build step, no framework) — this is deliberately the whole UI.
|
||||||
|
|
||||||
|
export function reviewPage(): string {
|
||||||
|
const classes = JSON.stringify(VEHICLE_CLASSES);
|
||||||
|
return `<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Wash review</title>
|
||||||
|
<style>
|
||||||
|
:root { --bg:#111; --panel:#1b1b1b; --text:#e8e8e8; --muted:#9a9a9a; --amber:#e0a030; --green:#4caf50; --red:#e05050; }
|
||||||
|
body { margin:0; background:var(--bg); color:var(--text); font:14px/1.4 system-ui, sans-serif; }
|
||||||
|
header { display:flex; justify-content:space-between; align-items:center; padding:.6rem 1rem; border-bottom:1px solid #333; }
|
||||||
|
header b { letter-spacing:.08em; text-transform:uppercase; color:var(--amber); font-size:.75rem; }
|
||||||
|
main { max-width:960px; margin:0 auto; padding:1rem; display:grid; gap:1rem; }
|
||||||
|
.card { background:var(--panel); border:1px solid #333; border-radius:6px; padding:1rem; }
|
||||||
|
img { max-width:100%; max-height:60vh; display:block; margin:0 auto; background:#000; border-radius:4px; }
|
||||||
|
dl { display:grid; grid-template-columns:max-content 1fr; gap:.2rem .8rem; margin:0; font-variant-numeric:tabular-nums; }
|
||||||
|
dt { color:var(--muted); }
|
||||||
|
.buttons { display:flex; flex-wrap:wrap; gap:.4rem; }
|
||||||
|
button { background:#2a2a2a; color:var(--text); border:1px solid #444; border-radius:4px; padding:.5rem .8rem; font:inherit; cursor:pointer; }
|
||||||
|
button:hover { border-color:var(--amber); }
|
||||||
|
button.mono { font-family:ui-monospace, monospace; }
|
||||||
|
button.hint { border-color:var(--amber); }
|
||||||
|
button.unusable { color:var(--red); }
|
||||||
|
button.skip { color:var(--muted); }
|
||||||
|
.muted { color:var(--muted); }
|
||||||
|
.warn { color:var(--amber); }
|
||||||
|
table { border-collapse:collapse; width:100%; font-variant-numeric:tabular-nums; }
|
||||||
|
td, th { text-align:left; padding:.2rem .5rem; border-bottom:1px solid #2a2a2a; }
|
||||||
|
th { color:var(--muted); font-weight:normal; font-size:.75rem; text-transform:uppercase; letter-spacing:.06em; }
|
||||||
|
kbd { background:#2a2a2a; border:1px solid #444; border-radius:3px; padding:0 .3rem; font-size:.75rem; }
|
||||||
|
h2 { font-size:.8rem; letter-spacing:.08em; text-transform:uppercase; color:var(--amber); margin:0 0 .6rem; }
|
||||||
|
.row { display:flex; flex-wrap:wrap; gap:.6rem; align-items:center; }
|
||||||
|
select, input { background:#2a2a2a; color:var(--text); border:1px solid #444; border-radius:4px; padding:.4rem .5rem; font:inherit; }
|
||||||
|
input[type=number] { width:5rem; }
|
||||||
|
label { color:var(--muted); font-size:.8rem; }
|
||||||
|
pre { background:#0d0d0d; border:1px solid #2a2a2a; border-radius:4px; padding:.6rem; max-height:22rem; overflow:auto; font-size:.75rem; white-space:pre-wrap; margin:.6rem 0 0; }
|
||||||
|
.ok { color:var(--green); }
|
||||||
|
.bad { color:var(--red); }
|
||||||
|
button:disabled { opacity:.45; cursor:not-allowed; }
|
||||||
|
button.small { padding:.25rem .5rem; font-size:.75rem; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header><b>Wash review</b><span id="counts" class="muted"></span></header>
|
||||||
|
<main>
|
||||||
|
<section class="card" id="item">
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
</section>
|
||||||
|
<section class="card">
|
||||||
|
<table id="stats"><thead><tr><th>booth</th><th>operator</th><th>reviewed</th><th>agree</th><th>disagree</th><th>unusable</th></tr></thead><tbody></tbody></table>
|
||||||
|
</section>
|
||||||
|
<section class="card" id="training" hidden>
|
||||||
|
<h2>Training</h2>
|
||||||
|
<div id="tr-body"></div>
|
||||||
|
</section>
|
||||||
|
<p class="muted">Keys: <kbd>1</kbd>–<kbd>9</kbd>, <kbd>0</kbd> pick a class in order · <kbd>u</kbd> unusable · <kbd>s</kbd> skip. Skipped items come back after a reload. Your verdict is the training label; the operator's pick is only compared against it.</p>
|
||||||
|
</main>
|
||||||
|
<script>
|
||||||
|
const CLASSES = ${classes};
|
||||||
|
const skipped = new Set();
|
||||||
|
let current = null;
|
||||||
|
|
||||||
|
async function api(path, init) {
|
||||||
|
const r = await fetch(path, init);
|
||||||
|
if (!r.ok) throw new Error(path + ' → HTTP ' + r.status);
|
||||||
|
return r.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
function esc(s) { return String(s).replace(/[&<>"]/g, c => ({'&':'&','<':'<','>':'>','"':'"'}[c])); }
|
||||||
|
|
||||||
|
async function loadStats() {
|
||||||
|
const s = await api('/api/stats');
|
||||||
|
const pending = s.booths.reduce((n, b) => n + b.pending, 0);
|
||||||
|
const reviewed = s.booths.reduce((n, b) => n + b.reviewed, 0);
|
||||||
|
document.getElementById('counts').textContent = pending + ' waiting · ' + reviewed + ' reviewed';
|
||||||
|
const tb = document.querySelector('#stats tbody');
|
||||||
|
tb.innerHTML = s.operators.map(o => '<tr><td>' + esc(o.booth) + '</td><td class="mono">' + esc(o.operatorRef) + '</td><td>' + o.reviewed + '</td><td>' + o.agree + '</td><td' + (o.disagree ? ' class="warn"' : '') + '>' + o.disagree + '</td><td>' + o.unusable + '</td></tr>').join('') || '<tr><td colspan="6" class="muted">nothing reviewed yet</td></tr>';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function next() {
|
||||||
|
const { items } = await api('/api/items?status=pending&limit=25');
|
||||||
|
current = items.find(i => !skipped.has(i.id)) || null;
|
||||||
|
const el = document.getElementById('item');
|
||||||
|
if (!current) { el.innerHTML = '<p class="muted">Nothing waiting for review.</p>'; return; }
|
||||||
|
const it = current;
|
||||||
|
const opClasses = JSON.parse(it.operatorClasses || '[]');
|
||||||
|
el.innerHTML =
|
||||||
|
'<img src="/api/items/' + encodeURIComponent(it.id) + '/image" alt="">' +
|
||||||
|
'<dl style="margin-top:.8rem">' +
|
||||||
|
(it.kind === 'entry'
|
||||||
|
? '<dt>sample</dt><dd><span class="muted">entry stream — no wash, no operator decision; label the vehicle</span></dd>'
|
||||||
|
: '<dt>operator chose</dt><dd><b>' + esc(it.operatorCategoryName) + '</b> <span class="muted">(' + esc(opClasses.join(', ') || 'no classes mapped') + ')</span></dd>') +
|
||||||
|
'<dt>camera saw</dt><dd class="mono">' + esc(it.visionClass) + ' <span class="muted">' + Math.round(it.visionConfidence * 100) + '%</span>' + (it.downgraded ? ' <span class="warn">flagged downgrade at the booth</span>' : '') + '</dd>' +
|
||||||
|
(it.kind === 'entry' ? '<dt>booth</dt><dd class="mono">' + esc(it.booth) + '</dd>' :
|
||||||
|
'<dt>service</dt><dd>' + esc(it.service) + '</dd>' +
|
||||||
|
'<dt>booth · operator</dt><dd class="mono">' + esc(it.booth) + ' · ' + esc(it.operatorRef) + '</dd>') +
|
||||||
|
'<dt>at</dt><dd>' + esc(it.at) + '</dd>' +
|
||||||
|
'</dl>' +
|
||||||
|
'<div class="buttons" style="margin-top:.8rem">' +
|
||||||
|
CLASSES.map((c, i) => '<button class="mono' + (c === it.visionClass ? ' hint' : '') + '" data-label="' + c + '" title="key ' + ((i + 1) % 10) + '">' + c + '</button>').join('') +
|
||||||
|
'<button class="unusable" data-label="unusable">unusable</button>' +
|
||||||
|
'<button class="skip" data-skip="1">skip</button>' +
|
||||||
|
'</div>';
|
||||||
|
el.querySelectorAll('button[data-label]').forEach(b => b.addEventListener('click', () => verdict(b.dataset.label)));
|
||||||
|
el.querySelector('button[data-skip]').addEventListener('click', skip);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function verdict(label) {
|
||||||
|
if (!current) return;
|
||||||
|
await api('/api/items/' + encodeURIComponent(current.id) + '/review', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ label }) });
|
||||||
|
await Promise.all([next(), loadStats()]);
|
||||||
|
}
|
||||||
|
function skip() { if (current) { skipped.add(current.id); next(); } }
|
||||||
|
|
||||||
|
document.addEventListener('keydown', e => {
|
||||||
|
if (e.target.tagName === 'INPUT') return;
|
||||||
|
if (e.key === 'u') verdict('unusable');
|
||||||
|
else if (e.key === 's') skip();
|
||||||
|
else if (/^[0-9]$/.test(e.key)) { const i = e.key === '0' ? 9 : Number(e.key) - 1; if (CLASSES[i]) verdict(CLASSES[i]); }
|
||||||
|
});
|
||||||
|
|
||||||
|
next().catch(e => { document.getElementById('item').innerHTML = '<p class="warn">' + esc(e.message) + '</p>'; });
|
||||||
|
loadStats().catch(() => {});
|
||||||
|
|
||||||
|
// ---- Training: the trainer's job API, proxied by the collector -------------------------
|
||||||
|
// Readiness (labels per class vs the minimum), one job at a time with a live log, the
|
||||||
|
// versions a run produced (written or refused) with Report / Evaluate / Publish. Pinning a
|
||||||
|
// published version into the vision image stays a git commit — that is the deploy control.
|
||||||
|
let trPoll = null;
|
||||||
|
let trShownReport = null;
|
||||||
|
const trDefaults = { mode: 'features', backbone: 'resnet18', minAccuracy: 0.85 };
|
||||||
|
|
||||||
|
function pct(x) { return x == null ? '—' : Math.round(x * 100) + ' %'; }
|
||||||
|
|
||||||
|
async function training() {
|
||||||
|
const box = document.getElementById('training');
|
||||||
|
const el = document.getElementById('tr-body');
|
||||||
|
let s;
|
||||||
|
try { s = await api('/api/training/status'); } catch (e) { box.hidden = false; el.innerHTML = '<p class="warn">' + esc(e.message) + '</p>'; return; }
|
||||||
|
if (!s.configured) { box.hidden = true; return; }
|
||||||
|
box.hidden = false;
|
||||||
|
if (!s.reachable) { el.innerHTML = '<p class="warn">trainer not reachable: ' + esc(s.error || '') + '</p>'; schedule(true); return; }
|
||||||
|
const r = s.readiness, run = r.run || {}, minPer = run.minPerClass || 20;
|
||||||
|
const byClass = (r.labelled && r.labelled.byClass) || {};
|
||||||
|
const classes = Object.keys(byClass);
|
||||||
|
const cur = s.current;
|
||||||
|
const readyLine = r.ready
|
||||||
|
? '<span class="ok">enough labels to train</span> — classes this run: ' + esc((run.classes || []).join(', '))
|
||||||
|
: '<span class="warn">not enough labels yet</span> — a class needs ' + minPer + ' reviewed crops; two classes must clear it';
|
||||||
|
let html = '<p>' + readyLine + ' <span class="muted">(' + (r.labelled ? r.labelled.total : 0) + ' labelled, ' + (r.missingCrops || 0) + ' missing crop files)</span></p>';
|
||||||
|
html += '<table><thead><tr><th>class</th><th>reviewed</th><th>train</th><th>val</th><th></th></tr></thead><tbody>' +
|
||||||
|
(classes.map(c => '<tr><td class="mono">' + esc(c) + '</td><td>' + byClass[c] + '</td><td>' + ((run.train || {})[c] ?? '—') + '</td><td>' + ((run.val || {})[c] ?? '—') + '</td><td class="muted">' + (byClass[c] < minPer ? 'below ' + minPer + ' — dropped' : '') + '</td></tr>').join('') || '<tr><td colspan="5" class="muted">no labels yet — review crops above</td></tr>') +
|
||||||
|
'</tbody></table>';
|
||||||
|
const d = Object.assign({}, trDefaults, r.defaults || {});
|
||||||
|
html += '<div class="row" style="margin-top:.8rem">' +
|
||||||
|
'<label>mode <select id="tr-mode">' + (r.modes || ['features', 'finetune']).map(m => '<option' + (m === d.mode ? ' selected' : '') + '>' + m + '</option>').join('') + '</select></label>' +
|
||||||
|
'<label>backbone <select id="tr-backbone">' + (r.backbones || ['resnet18']).map(b => '<option' + (b === d.backbone ? ' selected' : '') + '>' + b + '</option>').join('') + '</select></label>' +
|
||||||
|
'<label>floor <input id="tr-floor" type="number" min="0" max="1" step="0.01" value="' + d.minAccuracy + '"></label>' +
|
||||||
|
'<button id="tr-train"' + (r.ready && !cur ? '' : ' disabled') + '>Train</button>' +
|
||||||
|
(cur ? '<span class="warn">running: ' + esc(cur.kind) + ' ' + esc(cur.id) + '</span>' : '') +
|
||||||
|
'</div>';
|
||||||
|
const last = cur || (s.jobs && s.jobs[0]);
|
||||||
|
if (last) {
|
||||||
|
const cls = last.status === 'done' ? 'ok' : last.status === 'running' ? 'warn' : 'bad';
|
||||||
|
html += '<p style="margin:.8rem 0 0"><span class="' + cls + '">' + esc(last.status) + '</span> <span class="mono">' + esc(last.kind) + ' ' + esc(last.id) + '</span> <span class="muted">' + esc(last.startedAt || '') + (last.exitCode != null ? ' · exit ' + last.exitCode : '') + '</span> <button class="small" data-job="' + esc(last.id) + '">log</button></p>' +
|
||||||
|
'<pre id="tr-log" hidden></pre>';
|
||||||
|
}
|
||||||
|
const vs = s.versions || [];
|
||||||
|
html += '<h2 style="margin-top:1rem">Versions</h2>';
|
||||||
|
html += vs.length
|
||||||
|
? '<table><thead><tr><th>version</th><th>model</th><th>accuracy</th><th>classes</th><th>mode</th><th></th></tr></thead><tbody>' +
|
||||||
|
vs.map(v => '<tr><td class="mono">' + esc(v.version) + '</td><td>' + (v.written ? '<span class="ok">written</span>' : '<span class="bad">refused</span>') + '</td><td>' + pct(v.accuracy) + (v.floor != null ? ' <span class="muted">/ floor ' + pct(v.floor) + '</span>' : '') + '</td><td class="muted">' + esc((v.classes || []).join(', ')) + '</td><td class="muted">' + esc(v.mode || '') + '</td><td>' +
|
||||||
|
'<button class="small" data-report="' + esc(v.version) + '">report</button> ' +
|
||||||
|
(v.written ? '<button class="small" data-eval="' + esc(v.version) + '"' + (cur ? ' disabled' : '') + '>evaluate</button> <button class="small" data-publish="' + esc(v.version) + '"' + (cur ? ' disabled' : '') + '>publish</button>' : '') +
|
||||||
|
'</td></tr>').join('') + '</tbody></table>'
|
||||||
|
: '<p class="muted">no runs yet</p>';
|
||||||
|
html += '<pre id="tr-report" hidden></pre>';
|
||||||
|
html += '<p class="muted" style="margin:.8rem 0 0">A written model is only a file here. To put it on a booth: publish, then pin the version in <span class="mono">apps/vision/models/bodytype.version</span>, commit, and bump the TAG of the booth.</p>';
|
||||||
|
el.innerHTML = html;
|
||||||
|
|
||||||
|
const trainBtn = document.getElementById('tr-train');
|
||||||
|
if (trainBtn) trainBtn.addEventListener('click', () => startJob({ kind: 'train', mode: document.getElementById('tr-mode').value, backbone: document.getElementById('tr-backbone').value, minAccuracy: Number(document.getElementById('tr-floor').value) }));
|
||||||
|
el.querySelectorAll('button[data-eval]').forEach(b => b.addEventListener('click', () => startJob({ kind: 'evaluate', version: b.dataset.eval })));
|
||||||
|
el.querySelectorAll('button[data-publish]').forEach(b => b.addEventListener('click', () => { if (confirm('Publish ' + b.dataset.publish + ' to the package registry?')) startJob({ kind: 'publish', version: b.dataset.publish }); }));
|
||||||
|
el.querySelectorAll('button[data-job]').forEach(b => b.addEventListener('click', () => showLog(b.dataset.job)));
|
||||||
|
el.querySelectorAll('button[data-report]').forEach(b => b.addEventListener('click', () => showReport(b.dataset.report)));
|
||||||
|
if (cur) showLog(cur.id).catch(() => {});
|
||||||
|
if (trShownReport) showReport(trShownReport).catch(() => {});
|
||||||
|
schedule(!!cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
function schedule(soon) {
|
||||||
|
if (trPoll) clearTimeout(trPoll);
|
||||||
|
trPoll = setTimeout(() => training().catch(() => {}), soon ? 4000 : 60000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startJob(body) {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/training/jobs', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
|
||||||
|
if (!r.ok) { const e = await r.json().catch(() => ({})); alert('trainer: ' + (e.error || ('HTTP ' + r.status))); }
|
||||||
|
} catch (e) { alert(e.message); }
|
||||||
|
training().catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function showLog(id) {
|
||||||
|
const j = await api('/api/training/jobs/' + encodeURIComponent(id));
|
||||||
|
const pre = document.getElementById('tr-log');
|
||||||
|
if (!pre) return;
|
||||||
|
pre.hidden = false;
|
||||||
|
pre.textContent = j.log || '(no output yet)';
|
||||||
|
pre.scrollTop = pre.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function showReport(v) {
|
||||||
|
const r = await fetch('/api/training/versions/' + encodeURIComponent(v) + '/report');
|
||||||
|
const pre = document.getElementById('tr-report');
|
||||||
|
if (!pre) return;
|
||||||
|
trShownReport = v;
|
||||||
|
pre.hidden = false;
|
||||||
|
pre.textContent = r.ok ? await r.text() : 'no report for ' + v + ' (HTTP ' + r.status + ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
training().catch(() => {});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
||||||
|
import { mkdtemp, rm } from "node:fs/promises";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import { buildCollector, type CollectorApp } from "./app.js";
|
||||||
|
|
||||||
|
// The Training section's proxy: reviewer-gated, forwards a fixed set of paths to the
|
||||||
|
// trainer's job API, passes its status codes through, and degrades cleanly when the trainer
|
||||||
|
// is not configured or not reachable. The trainer is faked with a bare node http server.
|
||||||
|
|
||||||
|
const REVIEWER = { user: "julian", pass: "review-pass-123" };
|
||||||
|
const basic = "Basic " + Buffer.from(`${REVIEWER.user}:${REVIEWER.pass}`).toString("base64");
|
||||||
|
|
||||||
|
let dir: string;
|
||||||
|
let fake: Server;
|
||||||
|
let fakeUrl: string;
|
||||||
|
let seen: { method: string; url: string; body: string }[];
|
||||||
|
let app: CollectorApp;
|
||||||
|
|
||||||
|
async function start(trainerUrl: string | null): Promise<void> {
|
||||||
|
app = await buildCollector({ host: "127.0.0.1", port: 0, dataDir: dir, boothTokens: new Map(), reviewer: REVIEWER, trainerUrl }, { dbFile: ":memory:" });
|
||||||
|
await app.ready();
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
dir = await mkdtemp(path.join(tmpdir(), "collector-"));
|
||||||
|
seen = [];
|
||||||
|
fake = createServer((req: IncomingMessage, res: ServerResponse) => {
|
||||||
|
let body = "";
|
||||||
|
req.on("data", (c) => (body += c));
|
||||||
|
req.on("end", () => {
|
||||||
|
seen.push({ method: req.method ?? "", url: req.url ?? "", body });
|
||||||
|
const json = (code: number, obj: unknown) => {
|
||||||
|
res.writeHead(code, { "content-type": "application/json" });
|
||||||
|
res.end(JSON.stringify(obj));
|
||||||
|
};
|
||||||
|
if (req.url === "/health") return json(200, { ok: true, busy: false });
|
||||||
|
if (req.url === "/readiness") return json(200, { ready: false, labelled: { total: 3 } });
|
||||||
|
if (req.url === "/versions") return json(200, { versions: [{ version: "v1", written: true }] });
|
||||||
|
if (req.url === "/jobs" && req.method === "GET") return json(200, { jobs: [{ id: "j1" }], current: null });
|
||||||
|
if (req.url === "/jobs" && req.method === "POST") return body.includes('"busy"') ? json(409, { error: "a job is already running" }) : json(202, { id: "j2", status: "running" });
|
||||||
|
if (req.url === "/jobs/j1") return json(200, { id: "j1", status: "done", log: "ok" });
|
||||||
|
if (req.url === "/versions/v1/report") {
|
||||||
|
res.writeHead(200, { "content-type": "text/markdown; charset=utf-8" });
|
||||||
|
return res.end("# Body-type classifier v1\n");
|
||||||
|
}
|
||||||
|
return json(404, { error: "not found" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await new Promise<void>((r) => fake.listen(0, "127.0.0.1", r));
|
||||||
|
const a = fake.address() as { port: number };
|
||||||
|
fakeUrl = `http://127.0.0.1:${a.port}`;
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app?.close();
|
||||||
|
await new Promise<void>((r) => fake.close(() => r()));
|
||||||
|
await rm(dir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("review page script", () => {
|
||||||
|
it("parses as JavaScript (an apostrophe in a template literal once broke the whole page)", async () => {
|
||||||
|
const { reviewPage } = await import("./review-page.js");
|
||||||
|
const html = reviewPage();
|
||||||
|
const script = html.slice(html.indexOf("<script>") + 8, html.lastIndexOf("</script>"));
|
||||||
|
expect(() => new Function(script)).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("training proxy", () => {
|
||||||
|
it("is hidden when no trainer is configured", async () => {
|
||||||
|
await start(null);
|
||||||
|
const s = await app.inject({ method: "GET", url: "/api/training/status", headers: { authorization: basic } });
|
||||||
|
expect(s.json()).toEqual({ configured: false });
|
||||||
|
const j = await app.inject({ method: "POST", url: "/api/training/jobs", headers: { authorization: basic }, payload: { kind: "train" } });
|
||||||
|
expect(j.statusCode).toBe(503);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("aggregates status and forwards jobs and reports behind the reviewer login", async () => {
|
||||||
|
await start(fakeUrl);
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/training/status" })).statusCode).toBe(401);
|
||||||
|
const s = await app.inject({ method: "GET", url: "/api/training/status", headers: { authorization: basic } });
|
||||||
|
expect(s.statusCode).toBe(200);
|
||||||
|
const body = s.json();
|
||||||
|
expect(body.configured).toBe(true);
|
||||||
|
expect(body.reachable).toBe(true);
|
||||||
|
expect(body.readiness.labelled.total).toBe(3);
|
||||||
|
expect(body.versions[0].version).toBe("v1");
|
||||||
|
expect(body.jobs[0].id).toBe("j1");
|
||||||
|
|
||||||
|
const j = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/training/jobs",
|
||||||
|
headers: { authorization: basic },
|
||||||
|
payload: { kind: "train", mode: "features", minAccuracy: 0.9, secret: "nope", version: "v2" },
|
||||||
|
});
|
||||||
|
expect(j.statusCode).toBe(202);
|
||||||
|
expect(j.json().id).toBe("j2");
|
||||||
|
const posted = seen.find((r) => r.method === "POST")!;
|
||||||
|
expect(JSON.parse(posted.body)).toEqual({ kind: "train", mode: "features", minAccuracy: 0.9, version: "v2" }); // unknown keys dropped
|
||||||
|
|
||||||
|
const busy = await app.inject({ method: "POST", url: "/api/training/jobs", headers: { authorization: basic }, payload: { kind: "evaluate", version: "busy" } });
|
||||||
|
expect(busy.statusCode).toBe(409); // the trainer's answer passes through
|
||||||
|
|
||||||
|
const bad = await app.inject({ method: "POST", url: "/api/training/jobs", headers: { authorization: basic }, payload: { kind: "rm-rf" } });
|
||||||
|
expect(bad.statusCode).toBe(400);
|
||||||
|
|
||||||
|
const one = await app.inject({ method: "GET", url: "/api/training/jobs/j1", headers: { authorization: basic } });
|
||||||
|
expect(one.json().status).toBe("done");
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/training/jobs/..%2Fx", headers: { authorization: basic } })).statusCode).toBe(400);
|
||||||
|
|
||||||
|
const rep = await app.inject({ method: "GET", url: "/api/training/versions/v1/report", headers: { authorization: basic } });
|
||||||
|
expect(rep.statusCode).toBe(200);
|
||||||
|
expect(rep.headers["content-type"]).toContain("text/markdown");
|
||||||
|
expect(rep.body).toContain("# Body-type classifier v1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports an unreachable trainer without failing the page", async () => {
|
||||||
|
await start("http://127.0.0.1:9"); // nothing listens on the discard port
|
||||||
|
const s = await app.inject({ method: "GET", url: "/api/training/status", headers: { authorization: basic } });
|
||||||
|
expect(s.statusCode).toBe(200);
|
||||||
|
expect(s.json().reachable).toBe(false);
|
||||||
|
const j = await app.inject({ method: "POST", url: "/api/training/jobs", headers: { authorization: basic }, payload: { kind: "train" } });
|
||||||
|
expect(j.statusCode).toBe(502);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"rootDir": "./src",
|
||||||
|
"outDir": "./dist"
|
||||||
|
},
|
||||||
|
"references": [{ "path": "../../packages/shared" }],
|
||||||
|
"include": ["src/**/*"],
|
||||||
|
"exclude": ["src/**/*.test.ts"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { defineConfig } from "vitest/config";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: { include: ["src/**/*.test.ts"], env: { LOG_LEVEL: "silent" } },
|
||||||
|
});
|
||||||
@@ -44,6 +44,35 @@ see that workflow's header and `wiki/decisions/desktop-shell-tauri.md`). The upd
|
|||||||
signing pubkey live in `tauri.conf.json`; the private signing key is held outside the repo, never
|
signing pubkey live in `tauri.conf.json`; the private signing key is held outside the repo, never
|
||||||
committed.
|
committed.
|
||||||
|
|
||||||
|
**The manifest carries one entry per installer type** (`linux-x86_64-deb`, `linux-x86_64-rpm`,
|
||||||
|
and bare `linux-x86_64` for AppImage). The updater picks the entry matching how the running app
|
||||||
|
was installed — a `.deb` install will only ever accept a signed `.deb`. Booths run the `.deb`,
|
||||||
|
so an in-app update ends in a **polkit password prompt** (`pkexec dpkg -i`): that is expected,
|
||||||
|
and it is the right gate — the package lives in `/usr/bin`, root-owned, and the operator is not
|
||||||
|
supposed to be able to replace it silently. Cancel the prompt and the app keeps running the old
|
||||||
|
version; the failure is logged to the server's Logs viewer.
|
||||||
|
|
||||||
|
## Release gate — run the REAL bundle locally before tagging
|
||||||
|
|
||||||
|
`tauri dev` loads the SPA from `http://localhost:5173`, a plain http origin. The shipped bundle
|
||||||
|
loads it from `tauri://localhost`, a *secure* custom-scheme origin — and every desktop-only bug
|
||||||
|
found in the field on 2026-09-03/04 (relative-URL DOMException, mixed content, missing WS
|
||||||
|
`Origin`, the reqwest-vs-webview cookie split, the WS handshake that can't carry the cookie)
|
||||||
|
depends on that difference. **Dev mode cannot reproduce any of them**, so "works in `tauri dev`"
|
||||||
|
carries no information about a release. Before pushing a `vX.Y.Z` tag:
|
||||||
|
|
||||||
|
1. `pnpm --filter @parking/server dev` (local backend; `.env` must have `COOKIE_SECURE=0` and
|
||||||
|
`tauri://localhost` in `WS_ALLOWED_ORIGINS`).
|
||||||
|
2. `pnpm --filter @parking/desktop bundle` and run the produced AppImage from
|
||||||
|
`src-tauri/target/release/bundle/appimage/` (WSLg is enough).
|
||||||
|
3. On the ConnectScreen enter `127.0.0.1:3000`, **Test** must say reachable, then **Save**.
|
||||||
|
4. Log in. The booth header must show **LIVE** (not "JASHTË LINJË") within a few seconds.
|
||||||
|
5. Perform one mutation (e.g. change your UI language) — it must succeed (proves CSRF).
|
||||||
|
6. Open Setup → Logs and confirm a `frontend`-sourced row from this desktop session exists
|
||||||
|
(proves the desktop log channel; historically it was silently 403'd).
|
||||||
|
|
||||||
|
Only then tag. If a release still fails in the field, the gap is in this list — fix the list.
|
||||||
|
|
||||||
## Not here (deliberately)
|
## Not here (deliberately)
|
||||||
|
|
||||||
Kiosk lockdown (fullscreen/no-decorations) and launching Fastify from the shell are out of scope for
|
Kiosk lockdown (fullscreen/no-decorations) and launching Fastify from the shell are out of scope for
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://schema.tauri.app/config/2",
|
"$schema": "https://schema.tauri.app/config/2",
|
||||||
"productName": "Parking System",
|
"productName": "Parking System",
|
||||||
"version": "0.1.0",
|
"version": "0.2.0",
|
||||||
"identifier": "com.parking.desktop",
|
"identifier": "com.parking.desktop",
|
||||||
"build": {
|
"build": {
|
||||||
"devUrl": "http://localhost:5173",
|
"devUrl": "http://localhost:5173",
|
||||||
|
|||||||
@@ -76,4 +76,34 @@ WS_ALLOWED_ORIGINS=http://localhost:5173,tauri://localhost,http://tauri.localhos
|
|||||||
# camera's vehicle detection, admits them through the gated SubscriptionFlow. Opt-in per
|
# camera's vehicle detection, admits them through the gated SubscriptionFlow. Opt-in per
|
||||||
# camera (the camera's config.anpr checkbox in Setup); the camera must be BOUND to a relay.
|
# camera (the camera's config.anpr checkbox in Setup); the camera must be BOUND to a relay.
|
||||||
# VISION_ENTRY_MIN_CONFIDENCE=0.85 # stricter floor for a BARRIER-driving read (near-miss → falls back to card/QR)
|
# VISION_ENTRY_MIN_CONFIDENCE=0.85 # stricter floor for a BARRIER-driving read (near-miss → falls back to card/QR)
|
||||||
# ANPR_DEBOUNCE_MS=12000 # same plate/camera within this window = ONE presentation (camera re-fires ~1Hz)
|
# ANPR_DEBOUNCE_MS=12000 # same PLATE on the same camera within this window = ONE presentation (never per camera)
|
||||||
|
# ANPR_POLL_MS=0 # MIN spacing between frame pulls while a car is in the zone; 0 = back-to-back (~3 fps/camera)
|
||||||
|
# ANPR_POLL_WINDOW_MS=8000 # keep reading this long after the camera's LAST vehicle push (0 = one frame per push)
|
||||||
|
# ANPR_POLL_MAX_MS=30000 # hard cap on one loop from its start, however busy the lane
|
||||||
|
|
||||||
|
# Venue modules --------------------------------------------------------------
|
||||||
|
# Comma-separated ids of the modules this site is ENTITLED to (a vendor/deployment
|
||||||
|
# decision — set in the Komodo stack env, never by a site role). The site admin then
|
||||||
|
# ACTIVATES within this set in Setup → Site; effective = entitled ∩ activated. Unset or
|
||||||
|
# blank = every registered module (parking,validation,carwash) — a DEV convenience. In
|
||||||
|
# Docker, docker-compose.yml forwards it with a default of parking,validation, so a booth
|
||||||
|
# is never entitled to a module its Komodo stack env does not name. Required modules
|
||||||
|
# (parking) are always on. See wiki/decisions/venue-modules.md.
|
||||||
|
#MODULES_ENTITLED=parking,validation
|
||||||
|
|
||||||
|
# Car Wash review outbox (wiki/concepts/vision-review-outbox.md) -------------------------
|
||||||
|
# The operator's category choice is a hypothesis: each wash order with a vehicle read queues
|
||||||
|
# the vehicle CROP (plate blurred) + the choice for a trusted remote reviewer, drained one-way
|
||||||
|
# over the private overlay (Netbird). All three or off. URL = the collector's ingest endpoint
|
||||||
|
# (reachable only over the overlay); TOKEN = this booth's own bearer token; BOOTH_ID = a
|
||||||
|
# pseudonymous label the reviewer maps to a site (NEVER the site name — it travels with every
|
||||||
|
# item). Set in the Komodo stack env, per booth. Nothing is queued while off.
|
||||||
|
# CARWASH_REVIEW_URL=
|
||||||
|
# CARWASH_REVIEW_TOKEN=
|
||||||
|
# CARWASH_REVIEW_BOOTH_ID=
|
||||||
|
# CARWASH_REVIEW_INTERVAL_SEC=60
|
||||||
|
# Entry-stream sampling: also queue one in N ENTRY vehicle reads (no wash, no operator) as
|
||||||
|
# pure training material in the gate view — many times the wash stream, zero domain shift.
|
||||||
|
# 1 = every entry (the reviewer labels what they have time for; the rest waits and stays
|
||||||
|
# useful), N = one in N, 0/unset = off. Needs the three settings above.
|
||||||
|
# CARWASH_REVIEW_ENTRY_SAMPLE=1
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml turbo.json ./
|
|||||||
COPY apps/server/package.json apps/server/
|
COPY apps/server/package.json apps/server/
|
||||||
COPY apps/web/package.json apps/web/
|
COPY apps/web/package.json apps/web/
|
||||||
COPY apps/vision/package.json apps/vision/
|
COPY apps/vision/package.json apps/vision/
|
||||||
|
COPY apps/collector/package.json apps/collector/
|
||||||
COPY packages/db/package.json packages/db/
|
COPY packages/db/package.json packages/db/
|
||||||
COPY packages/devices/package.json packages/devices/
|
COPY packages/devices/package.json packages/devices/
|
||||||
COPY packages/shared/package.json packages/shared/
|
COPY packages/shared/package.json packages/shared/
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { devices, deviceEvents as deviceEventsTable, eq, siteConfig, type Db } from "@parking/db";
|
import { devices, deviceEvents as deviceEventsTable, eq, siteConfig, snapshots, type Db } from "@parking/db";
|
||||||
import { createTestDb } from "@parking/db/testing";
|
import { createTestDb } from "@parking/db/testing";
|
||||||
import { deviceEvents, type DeviceReadEvent } from "./device-events.js";
|
import { deviceEvents, type DeviceReadEvent, type VehicleReadEvent } from "./device-events.js";
|
||||||
import { silentLogger } from "./test-helpers.js";
|
import { silentLogger } from "./test-helpers.js";
|
||||||
import type { VisionClient, VisionResult } from "./vision-client.js";
|
import type { VisionClient, VisionResult, VisionVehicle } from "./vision-client.js";
|
||||||
import type { SubscriptionFlow, SubscriptionMatch } from "./subscription-flow.js";
|
import type { SubscriptionFlow, SubscriptionMatch } from "./subscription-flow.js";
|
||||||
|
|
||||||
// The ANPR bridge: a camera vehicle detection → (opt-in) snapshot → plate → MATCH a
|
// The ANPR bridge: a camera vehicle detection → (opt-in) snapshot → plate → MATCH a
|
||||||
@@ -14,7 +14,12 @@ import type { SubscriptionFlow, SubscriptionMatch } from "./subscription-flow.js
|
|||||||
|
|
||||||
// Mock buildCamera so the bridge gets a fake camera whose captureSnapshot is a stub
|
// Mock buildCamera so the bridge gets a fake camera whose captureSnapshot is a stub
|
||||||
// (no registry, no network). The factory returns a fresh shot each call.
|
// (no registry, no network). The factory returns a fresh shot each call.
|
||||||
const captureSnapshot = vi.fn(async () => ({ bytes: Buffer.from("jpg"), contentType: "image/jpeg" }));
|
const captureSnapshot = vi.fn(async () => ({ bytes: Buffer.from("jpg"), contentType: "image/jpeg", capturedAt: new Date().toISOString() }));
|
||||||
|
// The pass sample's box normalisation (needs a real JPEG header in prod) — stubbed so a
|
||||||
|
// test can hand the bridge a vehicle box, or none.
|
||||||
|
const { normalizedBoxes } = vi.hoisted(() => ({
|
||||||
|
normalizedBoxes: vi.fn(async (): Promise<{ vehicleBox: unknown; plateBox: unknown }> => ({ vehicleBox: null, plateBox: null })),
|
||||||
|
}));
|
||||||
// The bridge now goes through captureSnapshotShared (the dedup wrapper, exercised in
|
// The bridge now goes through captureSnapshotShared (the dedup wrapper, exercised in
|
||||||
// snapshot.test.ts); here it just delegates to the fake camera's captureSnapshot so this
|
// snapshot.test.ts); here it just delegates to the fake camera's captureSnapshot so this
|
||||||
// suite stays focused on the bridge's own match/debounce/emit logic.
|
// suite stays focused on the bridge's own match/debounce/emit logic.
|
||||||
@@ -22,6 +27,9 @@ vi.mock("./snapshot.js", () => ({
|
|||||||
buildCamera: () => ({ captureSnapshot }),
|
buildCamera: () => ({ captureSnapshot }),
|
||||||
captureSnapshotShared: (_id: string, camera: { captureSnapshot: typeof captureSnapshot }, ctx: unknown) =>
|
captureSnapshotShared: (_id: string, camera: { captureSnapshot: typeof captureSnapshot }, ctx: unknown) =>
|
||||||
camera.captureSnapshot(ctx as never),
|
camera.captureSnapshot(ctx as never),
|
||||||
|
// Storage re-encode is sharp's business (snapshot.test.ts); here the bytes pass through.
|
||||||
|
encodeForStorage: async (shot: { bytes: Buffer; contentType: string }) => ({ bytes: shot.bytes, contentType: shot.contentType }),
|
||||||
|
normalizedBoxes,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Import AFTER the mock is registered.
|
// Import AFTER the mock is registered.
|
||||||
@@ -31,11 +39,14 @@ let db: Db;
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
({ db } = createTestDb());
|
({ db } = createTestDb());
|
||||||
captureSnapshot.mockClear();
|
captureSnapshot.mockClear();
|
||||||
|
normalizedBoxes.mockReset();
|
||||||
|
normalizedBoxes.mockResolvedValue({ vehicleBox: null, plateBox: null });
|
||||||
delete process.env.VISION_ENTRY_MIN_CONFIDENCE;
|
delete process.env.VISION_ENTRY_MIN_CONFIDENCE;
|
||||||
delete process.env.ANPR_DEBOUNCE_MS;
|
delete process.env.ANPR_DEBOUNCE_MS;
|
||||||
// Poll-until-confident loop: keep the window + interval tiny so a below-floor / no-plate
|
// Read-every-frame loop: keep the window tiny so a loop ends in a few ticks instead of the
|
||||||
// case gives up in ~one tick instead of the 8s production window (tests stay fast). Each
|
// 8s production window (tests stay fast); the 1ms floor keeps a mocked loop from spinning
|
||||||
// bridge reads these in its constructor, so set them before `new AnprBridge`.
|
// thousands of frames. Each bridge reads these in its constructor, so set them before
|
||||||
|
// `new AnprBridge`.
|
||||||
process.env.ANPR_POLL_MS = "1";
|
process.env.ANPR_POLL_MS = "1";
|
||||||
process.env.ANPR_POLL_WINDOW_MS = "5";
|
process.env.ANPR_POLL_WINDOW_MS = "5";
|
||||||
});
|
});
|
||||||
@@ -74,16 +85,18 @@ function seedCamera(opts: { anpr?: boolean; anprAutoTrigger?: boolean } = {}): s
|
|||||||
return camId;
|
return camId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A fake VisionClient: enabled, returning a chosen plate/confidence (or null). */
|
/** A fake VisionClient: enabled, returning a chosen plate/confidence (or null), and
|
||||||
function fakeVision(opts: { enabled?: boolean; plate?: string; confidence?: number } = {}): VisionClient {
|
* optionally a vehicle (body type + box) the way the vehicle stage reports it. */
|
||||||
|
function fakeVision(opts: { enabled?: boolean; plate?: string; confidence?: number; vehicle?: VisionVehicle } = {}): VisionClient {
|
||||||
const enabled = opts.enabled ?? true;
|
const enabled = opts.enabled ?? true;
|
||||||
const result: VisionResult | null =
|
const result: VisionResult | null =
|
||||||
opts.plate == null
|
opts.plate == null && opts.vehicle == null
|
||||||
? null
|
? null
|
||||||
: {
|
: {
|
||||||
plate: { text: opts.plate, confidence: opts.confidence ?? 0.99 },
|
plate: opts.plate == null ? null : { text: opts.plate, confidence: opts.confidence ?? 0.99 },
|
||||||
plates: [],
|
plates: [],
|
||||||
lowConfidence: false,
|
lowConfidence: false,
|
||||||
|
vehicle: opts.vehicle ?? null,
|
||||||
modelVersion: "test",
|
modelVersion: "test",
|
||||||
tookMs: 1,
|
tookMs: 1,
|
||||||
};
|
};
|
||||||
@@ -95,19 +108,35 @@ function fakeVision(opts: { enabled?: boolean; plate?: string; confidence?: numb
|
|||||||
|
|
||||||
/** A fake SubscriptionFlow: only `match()` is called by the bridge. */
|
/** A fake SubscriptionFlow: only `match()` is called by the bridge. */
|
||||||
function fakeSubFlow(
|
function fakeSubFlow(
|
||||||
match: SubscriptionMatch | null,
|
// A fixed answer, or a matcher by plate (several subscribers in one loop).
|
||||||
|
match: SubscriptionMatch | null | ((plate: string) => SubscriptionMatch | null),
|
||||||
// openOccurrenceCount: a constant, or a sequence consumed per call (to simulate a
|
// openOccurrenceCount: a constant, or a sequence consumed per call (to simulate a
|
||||||
// credential closing an occurrence mid-poll → count changes).
|
// credential closing an occurrence mid-poll → count changes).
|
||||||
openCounts: number | number[] = 1,
|
openCounts: number | number[] = 1,
|
||||||
): SubscriptionFlow {
|
): SubscriptionFlow {
|
||||||
const seq = Array.isArray(openCounts) ? [...openCounts] : null;
|
const seq = Array.isArray(openCounts) ? [...openCounts] : null;
|
||||||
return {
|
return {
|
||||||
match: vi.fn(() => match),
|
match: vi.fn((e: DeviceReadEvent) => (typeof match === "function" ? match(e.value) : match)),
|
||||||
openOccurrenceCount: vi.fn(() => (seq ? (seq.length > 1 ? seq.shift()! : seq[0]) : (openCounts as number))),
|
openOccurrenceCount: vi.fn(() => (seq ? (seq.length > 1 ? seq.shift()! : seq[0]) : (openCounts as number))),
|
||||||
} as unknown as SubscriptionFlow;
|
} as unknown as SubscriptionFlow;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SUB_MATCH: SubscriptionMatch = { subscriptionId: "sub-1", carKey: "AA111BB", via: "plate" };
|
const SUB_MATCH: SubscriptionMatch = { subscriptionId: "sub-1", carKey: "AA111BB", via: "plate" };
|
||||||
|
const SUB_MATCH_2: SubscriptionMatch = { subscriptionId: "sub-2", carKey: "CC333DD", via: "plate" };
|
||||||
|
|
||||||
|
/** A VisionClient whose analyze() walks a sequence of (plate, confidence) frames, holding the
|
||||||
|
* last one — the way a car's read evolves as it approaches and settles, or as one car gives
|
||||||
|
* way to the next. `vehicle` (optional) rides every frame. */
|
||||||
|
function sequenceVision(frames: { plate: string; confidence: number }[], vehicle: VisionVehicle | null = null): VisionClient {
|
||||||
|
let i = 0;
|
||||||
|
return {
|
||||||
|
enabled: true,
|
||||||
|
analyze: vi.fn(async () => {
|
||||||
|
const f = frames[Math.min(i++, frames.length - 1)]!;
|
||||||
|
return { plate: { text: f.plate, confidence: f.confidence }, plates: [], lowConfidence: false, vehicle, modelVersion: "test", tookMs: 1 };
|
||||||
|
}),
|
||||||
|
} as unknown as VisionClient;
|
||||||
|
}
|
||||||
|
|
||||||
/** Capture read events emitted during `fn` (async). */
|
/** Capture read events emitted during `fn` (async). */
|
||||||
async function captureReads(fn: () => Promise<void>): Promise<DeviceReadEvent[]> {
|
async function captureReads(fn: () => Promise<void>): Promise<DeviceReadEvent[]> {
|
||||||
@@ -121,6 +150,79 @@ async function captureReads(fn: () => Promise<void>): Promise<DeviceReadEvent[]>
|
|||||||
return got;
|
return got;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Capture vehicle-read (pass sample) events during `fn`, waiting for the bridge's
|
||||||
|
* fire-and-forget recording to land. */
|
||||||
|
async function captureVehicleReads(fn: () => Promise<void>, expectCount: number): Promise<VehicleReadEvent[]> {
|
||||||
|
const got: VehicleReadEvent[] = [];
|
||||||
|
const off = deviceEvents.onVehicleRead((e) => got.push(e));
|
||||||
|
try {
|
||||||
|
await fn();
|
||||||
|
if (expectCount > 0) await vi.waitFor(() => expect(got).toHaveLength(expectCount));
|
||||||
|
else await new Promise((r) => setTimeout(r, 20));
|
||||||
|
} finally {
|
||||||
|
off();
|
||||||
|
}
|
||||||
|
return got;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("AnprBridge pass sample — every detected car, ticket or not", () => {
|
||||||
|
const VEHICLE: VisionVehicle = { bodyType: "car", confidence: 0.91, bbox: { x1: 10, y1: 10, x2: 200, y2: 120 } };
|
||||||
|
const BOX = { x1: 0.1, y1: 0.1, x2: 0.5, y2: 0.6 };
|
||||||
|
|
||||||
|
it("stores the frame + emits a vehicle read with NO plate at all (barriers up: no ticket, no session)", async () => {
|
||||||
|
normalizedBoxes.mockResolvedValue({ vehicleBox: BOX, plateBox: null });
|
||||||
|
const cam = seedCamera({ anpr: true });
|
||||||
|
const bridge = new AnprBridge(db, fakeVision({ vehicle: VEHICLE }), fakeSubFlow(null), silentLogger());
|
||||||
|
|
||||||
|
const got = await captureVehicleReads(() => bridge.onVehicleDetected(cam), 1);
|
||||||
|
const read = got[0]!;
|
||||||
|
expect(read).toMatchObject({ direction: "entry", deviceId: cam, read: { bodyType: "car", confidence: 0.91, box: BOX, plateBox: null } });
|
||||||
|
expect(read.identity).toBe(`pass:${read.read.snapshotId}`);
|
||||||
|
const row = db.select().from(snapshots).where(eq(snapshots.id, read.read.snapshotId!)).get();
|
||||||
|
expect(row).toMatchObject({ direction: "entry", deviceId: cam, identity: read.identity, contentType: "image/jpeg" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("is ONE sample per car even when the loop polls several frames, and rides along a subscriber emit", async () => {
|
||||||
|
normalizedBoxes.mockResolvedValue({ vehicleBox: BOX, plateBox: { x1: 0.3, y1: 0.4, x2: 0.4, y2: 0.45 } });
|
||||||
|
const cam = seedCamera({ anpr: true });
|
||||||
|
const bridge = new AnprBridge(db, fakeVision({ plate: "AA111BB", confidence: 0.97, vehicle: VEHICLE }), fakeSubFlow(SUB_MATCH), silentLogger());
|
||||||
|
|
||||||
|
let reads: DeviceReadEvent[] = [];
|
||||||
|
const got = await captureVehicleReads(async () => {
|
||||||
|
reads = await captureReads(() => bridge.onVehicleDetected(cam));
|
||||||
|
}, 1);
|
||||||
|
expect(reads).toHaveLength(1); // the subscriber's plate read still goes out
|
||||||
|
expect(got[0]!.read.plateBox).toEqual({ x1: 0.3, y1: 0.4, x2: 0.4, y2: 0.45 }); // for the crop's blur
|
||||||
|
expect(db.select().from(snapshots).all()).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("samples EACH plated car of a loop once — two cars back to back → two samples", async () => {
|
||||||
|
normalizedBoxes.mockResolvedValue({ vehicleBox: BOX, plateBox: null });
|
||||||
|
const cam = seedCamera({ anpr: true });
|
||||||
|
// Car A (3 frames) then car B, both framed; a non-subscriber lot (match null) — the
|
||||||
|
// sample is about the reviewer's stream, not about admission.
|
||||||
|
const vision = sequenceVision(
|
||||||
|
[{ plate: "AA111BB", confidence: 0.97 }, { plate: "AA111BB", confidence: 0.98 }, { plate: "AA111BB", confidence: 0.97 }, { plate: "CC333DD", confidence: 0.96 }],
|
||||||
|
VEHICLE,
|
||||||
|
);
|
||||||
|
process.env.ANPR_POLL_WINDOW_MS = "200";
|
||||||
|
const bridge = new AnprBridge(db, vision, fakeSubFlow(null), silentLogger());
|
||||||
|
|
||||||
|
const got = await captureVehicleReads(() => bridge.onVehicleDetected(cam), 2);
|
||||||
|
expect(got).toHaveLength(2);
|
||||||
|
expect(db.select().from(snapshots).all()).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("records nothing without a vehicle box (nothing to crop) — a bare class is not a sample", async () => {
|
||||||
|
const cam = seedCamera({ anpr: true });
|
||||||
|
const bridge = new AnprBridge(db, fakeVision({ vehicle: { bodyType: "car", confidence: 0.9 } }), fakeSubFlow(null), silentLogger());
|
||||||
|
|
||||||
|
const got = await captureVehicleReads(() => bridge.onVehicleDetected(cam), 0);
|
||||||
|
expect(got).toEqual([]);
|
||||||
|
expect(db.select().from(snapshots).all()).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("AnprBridge", () => {
|
describe("AnprBridge", () => {
|
||||||
it("does nothing for an opt-OUT camera (no anpr flag) — no analyze, no read", async () => {
|
it("does nothing for an opt-OUT camera (no anpr flag) — no analyze, no read", async () => {
|
||||||
const cam = seedCamera({ anpr: false });
|
const cam = seedCamera({ anpr: false });
|
||||||
@@ -177,14 +279,16 @@ describe("AnprBridge", () => {
|
|||||||
plate: { text: "AA111BB", confidence: confs[Math.min(i++, confs.length - 1)] },
|
plate: { text: "AA111BB", confidence: confs[Math.min(i++, confs.length - 1)] },
|
||||||
plates: [],
|
plates: [],
|
||||||
lowConfidence: false,
|
lowConfidence: false,
|
||||||
|
vehicle: null,
|
||||||
modelVersion: "test",
|
modelVersion: "test",
|
||||||
tookMs: 1,
|
tookMs: 1,
|
||||||
})),
|
})),
|
||||||
} as unknown as VisionClient;
|
} as unknown as VisionClient;
|
||||||
// Generous window so all 3 escalation attempts run deterministically under suite load
|
// Generous window so all 3 escalation attempts run deterministically under suite load
|
||||||
// (the global beforeEach sets a tiny 5ms window for the give-up cases).
|
// (the global beforeEach sets a tiny 5ms window for the give-up cases). The loop keeps
|
||||||
|
// reading until the window ends — the same plate must still be emitted exactly once.
|
||||||
process.env.ANPR_POLL_MS = "1";
|
process.env.ANPR_POLL_MS = "1";
|
||||||
process.env.ANPR_POLL_WINDOW_MS = "2000";
|
process.env.ANPR_POLL_WINDOW_MS = "150";
|
||||||
const bridge = new AnprBridge(db, vision, fakeSubFlow(SUB_MATCH), silentLogger());
|
const bridge = new AnprBridge(db, vision, fakeSubFlow(SUB_MATCH), silentLogger());
|
||||||
|
|
||||||
const reads = await captureReads(() => bridge.onVehicleDetected(cam));
|
const reads = await captureReads(() => bridge.onVehicleDetected(cam));
|
||||||
@@ -207,6 +311,7 @@ describe("AnprBridge", () => {
|
|||||||
plate: { text: "AA111BB", confidence: confs[Math.min(i++, confs.length - 1)] },
|
plate: { text: "AA111BB", confidence: confs[Math.min(i++, confs.length - 1)] },
|
||||||
plates: [],
|
plates: [],
|
||||||
lowConfidence: false,
|
lowConfidence: false,
|
||||||
|
vehicle: null,
|
||||||
modelVersion: "test",
|
modelVersion: "test",
|
||||||
tookMs: 1,
|
tookMs: 1,
|
||||||
})),
|
})),
|
||||||
@@ -229,18 +334,40 @@ describe("AnprBridge", () => {
|
|||||||
expect(reads[0]).toMatchObject({ value: "AA111BB" });
|
expect(reads[0]).toMatchObject({ value: "AA111BB" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("ABORTS if the subscriber transacts by another credential mid-poll (no double-act)", async () => {
|
it("does NOT emit if the subscriber transacts by another credential mid-poll (no double-act)", async () => {
|
||||||
// The car's plate is read (identity known) but stays below the floor; meanwhile the
|
// The car's plate is read (identity known) while still below the floor; meanwhile the
|
||||||
// subscriber scans their card → openOccurrenceCount drops. The bridge must abort and NOT
|
// subscriber scans their card → openOccurrenceCount drops. When the plate finally reads
|
||||||
// emit (which would exit the NEXT open occurrence — a phantom double-exit, esp. fleet).
|
// confidently the bridge must NOT emit (it would exit the NEXT open occurrence — a
|
||||||
|
// phantom double-exit, esp. fleet).
|
||||||
const cam = seedCamera({ anpr: true });
|
const cam = seedCamera({ anpr: true });
|
||||||
const vision = fakeVision({ plate: "AA111BB", confidence: 0.5 }); // never clears the floor
|
const vision = sequenceVision([{ plate: "AA111BB", confidence: 0.5 }, { plate: "AA111BB", confidence: 0.97 }]);
|
||||||
// openOccurrenceCount: 1 at baseline, then 0 (the card exit closed it) on the next check.
|
// openOccurrenceCount: 1 at first sighting, then 0 (the card exit closed it) at settle time.
|
||||||
const sub = fakeSubFlow(SUB_MATCH, [1, 0]);
|
const sub = fakeSubFlow(SUB_MATCH, [1, 0]);
|
||||||
|
process.env.ANPR_POLL_WINDOW_MS = "200";
|
||||||
const bridge = new AnprBridge(db, vision, sub, silentLogger());
|
const bridge = new AnprBridge(db, vision, sub, silentLogger());
|
||||||
|
|
||||||
const reads = await captureReads(() => bridge.onVehicleDetected(cam));
|
const reads = await captureReads(() => bridge.onVehicleDetected(cam));
|
||||||
expect(reads).toEqual([]); // aborted — the credential already handled it
|
expect(reads).toEqual([]); // declined — the credential already handled it
|
||||||
|
expect(vision.analyze).toHaveBeenCalledTimes; // (the loop itself kept going)
|
||||||
|
});
|
||||||
|
|
||||||
|
it("KEEPS READING after a confident plate: a second car in the same loop is emitted too", async () => {
|
||||||
|
// Car A reads confidently on frames 1-3, then car B pulls into the zone (frames 4+).
|
||||||
|
// Before 2026-10-11 the loop broke on A's first read and the camera-level debounce then
|
||||||
|
// dropped every push for 12s — B was invisible. Now A is settled once and the loop goes
|
||||||
|
// on; B is read by the same loop. Each plate exactly once, however many frames repeat.
|
||||||
|
const cam = seedCamera({ anpr: true });
|
||||||
|
const vision = sequenceVision([
|
||||||
|
{ plate: "AA111BB", confidence: 0.97 }, { plate: "AA111BB", confidence: 0.98 }, { plate: "AA111BB", confidence: 0.97 },
|
||||||
|
{ plate: "CC333DD", confidence: 0.96 },
|
||||||
|
]);
|
||||||
|
const sub = fakeSubFlow((plate) => (plate === "AA111BB" ? SUB_MATCH : plate === "CC333DD" ? SUB_MATCH_2 : null));
|
||||||
|
process.env.ANPR_POLL_WINDOW_MS = "200";
|
||||||
|
const bridge = new AnprBridge(db, vision, sub, silentLogger());
|
||||||
|
|
||||||
|
const reads = await captureReads(() => bridge.onVehicleDetected(cam));
|
||||||
|
expect(reads.map((r) => r.value)).toEqual(["AA111BB", "CC333DD"]);
|
||||||
|
expect(captureSnapshot.mock.calls.length).toBeGreaterThan(4); // it did not stop at A
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does NOT emit for a plate matching no subscription — records an advisory anpr-skip", async () => {
|
it("does NOT emit for a plate matching no subscription — records an advisory anpr-skip", async () => {
|
||||||
@@ -272,17 +399,34 @@ describe("AnprBridge", () => {
|
|||||||
expect(skips).toHaveLength(1);
|
expect(skips).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("debounces: two vehicle events within the window analyze/emit at most once", async () => {
|
it("debounces PER PLATE: the same car re-detected within the window is emitted once", async () => {
|
||||||
const cam = seedCamera({ anpr: true });
|
const cam = seedCamera({ anpr: true });
|
||||||
const vision = fakeVision({ plate: "AA111BB", confidence: 0.97 });
|
const vision = fakeVision({ plate: "AA111BB", confidence: 0.97 });
|
||||||
const bridge = new AnprBridge(db, vision, fakeSubFlow(SUB_MATCH), silentLogger());
|
const bridge = new AnprBridge(db, vision, fakeSubFlow(SUB_MATCH), silentLogger());
|
||||||
|
|
||||||
const reads = await captureReads(async () => {
|
const reads = await captureReads(async () => {
|
||||||
await bridge.onVehicleDetected(cam);
|
await bridge.onVehicleDetected(cam);
|
||||||
await bridge.onVehicleDetected(cam); // within the 12s window → suppressed
|
await bridge.onVehicleDetected(cam); // a second loop — the same plate within 12s → suppressed
|
||||||
});
|
});
|
||||||
expect(reads).toHaveLength(1);
|
expect(reads).toHaveLength(1);
|
||||||
expect(captureSnapshot).toHaveBeenCalledTimes(1); // 2nd was gated before the snapshot
|
expect(captureSnapshot.mock.calls.length).toBeGreaterThanOrEqual(2); // the 2nd loop DID look
|
||||||
|
});
|
||||||
|
|
||||||
|
it("is NOT blind after a read: a different car detected right after is emitted", async () => {
|
||||||
|
// The old camera-level debounce dropped every push for 12s after a success. A car behind
|
||||||
|
// the first, arriving after the first loop ended, must get its own loop and its own read.
|
||||||
|
const cam = seedCamera({ anpr: true });
|
||||||
|
const frames = [{ plate: "AA111BB", confidence: 0.97 }];
|
||||||
|
const vision = sequenceVision(frames);
|
||||||
|
const sub = fakeSubFlow((plate) => (plate === "AA111BB" ? SUB_MATCH : plate === "CC333DD" ? SUB_MATCH_2 : null));
|
||||||
|
const bridge = new AnprBridge(db, vision, sub, silentLogger());
|
||||||
|
|
||||||
|
const reads = await captureReads(async () => {
|
||||||
|
await bridge.onVehicleDetected(cam); // car A
|
||||||
|
frames[0] = { plate: "CC333DD", confidence: 0.96 }; // car B now in the zone
|
||||||
|
await bridge.onVehicleDetected(cam); // its push starts a new loop
|
||||||
|
});
|
||||||
|
expect(reads.map((r) => r.value)).toEqual(["AA111BB", "CC333DD"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("is a no-op (no throw) when vision is disabled or reads nothing", async () => {
|
it("is a no-op (no throw) when vision is disabled or reads nothing", async () => {
|
||||||
|
|||||||
+189
-116
@@ -1,21 +1,31 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { devices, deviceEvents as deviceEventsTable, eq, siteConfig, type Db, type DeviceRow } from "@parking/db";
|
import { devices, deviceEvents as deviceEventsTable, eq, siteConfig, snapshots, type Db, type DeviceRow } from "@parking/db";
|
||||||
|
import type { Snapshot } from "@parking/devices";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
import { deviceEvents, type DeviceReadEvent } from "./device-events.js";
|
import { deviceEvents, type DeviceReadEvent } from "./device-events.js";
|
||||||
import { directionOf, type FlowDirection } from "./device-resolve.js";
|
import { directionOf, type FlowDirection } from "./device-resolve.js";
|
||||||
import { buildCamera } from "./snapshot.js";
|
import { buildCamera, encodeForStorage, normalizedBoxes } from "./snapshot.js";
|
||||||
import type { SubscriptionFlow } from "./subscription-flow.js";
|
import type { SubscriptionFlow } from "./subscription-flow.js";
|
||||||
import type { VisionClient } from "./vision-client.js";
|
import type { VisionClient, VisionResult } from "./vision-client.js";
|
||||||
|
|
||||||
// The ANPR "bridge": a subscriber's plate, read from the lane camera, admits them through
|
// The ANPR "bridge": a subscriber's plate, read from the lane camera, admits them through
|
||||||
// the SAME gated SubscriptionFlow a QR/card scan uses. It is the one missing wire between
|
// the SAME gated SubscriptionFlow a QR/card scan uses. It is the one missing wire between
|
||||||
// the camera's vehicle PUSH (hikvision-alarm.ts) and the read bus — NOT a new service.
|
// the camera's vehicle PUSH (hikvision-alarm.ts) and the read bus — NOT a new service.
|
||||||
//
|
//
|
||||||
// On a `vehicle`/`active` event from an OPT-IN camera (config.anpr === true), the bridge:
|
// On a `vehicle`/`active` event from an OPT-IN camera (config.anpr === true), the bridge runs
|
||||||
// pull a fresh snapshot → vision.analyze → entry confidence floor → debounce → MATCH the
|
// ONE poll loop per camera: while the camera keeps pushing (a car is moving in the zone) it
|
||||||
// plate to a subscription → emit a DeviceReadEvent{kind:"plate"} ONLY if it matched.
|
// pulls fresh frames back-to-back → vision.analyze → entry confidence floor → MATCH the plate
|
||||||
// The existing onRead → ReadDispatcher then re-matches and runs the gated SubscriptionFlow
|
// to a subscription → emit a DeviceReadEvent{kind:"plate"} ONLY if it matched — once per
|
||||||
// (active / window / blocklist / car-count), which signs the entry/exit and opens the relay.
|
// plate per loop, and once per plate per ANPR_DEBOUNCE_MS across loops. The existing onRead →
|
||||||
|
// ReadDispatcher then re-matches and runs the gated SubscriptionFlow (active / window /
|
||||||
|
// blocklist / car-count), which signs the entry/exit and opens the relay.
|
||||||
|
//
|
||||||
|
// The loop does NOT stop at the first confident plate (2026-10-11). The camera fires an
|
||||||
|
// independent push on every motion (~1.2 s apart while a car moves — park-2, 2780 pushes, all
|
||||||
|
// activePostCount=1, never `inactive`), so a second car tailgating the first keeps the loop
|
||||||
|
// alive and is read by the same loop. Before, the loop broke on the first read and a 12 s
|
||||||
|
// CAMERA-level debounce then dropped every push — 40 of 57 plate-driven opens at park-2 had
|
||||||
|
// a push land in that blind window. The debounce is per PLATE now.
|
||||||
//
|
//
|
||||||
// INVARIANTS (see wiki/concepts/lane-presence-and-anpr-entry.md §2, append-only-event-chain.md):
|
// INVARIANTS (see wiki/concepts/lane-presence-and-anpr-entry.md §2, append-only-event-chain.md):
|
||||||
// - Advisory, never sole authority: the bridge only emitRead()s — the signed decision +
|
// - Advisory, never sole authority: the bridge only emitRead()s — the signed decision +
|
||||||
@@ -25,7 +35,8 @@ import type { VisionClient } from "./vision-client.js";
|
|||||||
// transient plate-as-ticket exit flow.
|
// transient plate-as-ticket exit flow.
|
||||||
// - Fail-soft + fire-and-forget: any snapshot/vision error degrades to the card/QR path;
|
// - Fail-soft + fire-and-forget: any snapshot/vision error degrades to the card/QR path;
|
||||||
// never throws into the push handler, never awaited on the camera's 200 response.
|
// never throws into the push handler, never awaited on the camera's 200 response.
|
||||||
// - Opt-in per camera, and debounced (the camera re-fires ~1Hz while a car sits).
|
// - Opt-in per camera, and debounced PER PLATE (the same car reads confidently on every
|
||||||
|
// frame once it is framed; one presentation = one read).
|
||||||
|
|
||||||
/** Camera config flag opting it into the ANPR bridge (same flag advisory ANPR uses). */
|
/** Camera config flag opting it into the ANPR bridge (same flag advisory ANPR uses). */
|
||||||
interface CameraConfig {
|
interface CameraConfig {
|
||||||
@@ -45,22 +56,26 @@ function entryMinConfidence(): number {
|
|||||||
return Number.isFinite(raw) && raw > 0 ? raw : 0.85;
|
return Number.isFinite(raw) && raw > 0 ? raw : 0.85;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Same plate/camera within this window = ONE credential presentation. The camera re-fires
|
/** Same plate on the same camera within this window = ONE credential presentation. A framed
|
||||||
* ~1Hz while a car is present; emitting every second would drive repeat entries (a fleet
|
* car reads confidently on every frame, and a car that stops and re-starts in the zone
|
||||||
* sub opens a 2nd occurrence) or exit spam. Required for correctness, not CPU. */
|
* starts a new loop; emitting each time would drive repeat entries (a fleet sub opens a 2nd
|
||||||
|
* occurrence) or exit spam. Keyed by PLATE, never by camera — a camera-level stamp made the
|
||||||
|
* lane blind to the next car for the whole window. Required for correctness, not CPU. */
|
||||||
function debounceMs(): number {
|
function debounceMs(): number {
|
||||||
const raw = Number(process.env.ANPR_DEBOUNCE_MS ?? 12_000);
|
const raw = Number(process.env.ANPR_DEBOUNCE_MS ?? 12_000);
|
||||||
return Number.isFinite(raw) && raw > 0 ? raw : 12_000;
|
return Number.isFinite(raw) && raw > 0 ? raw : 12_000;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A single alarm fires the INSTANT motion starts — the car is still approaching, so the
|
/** A single alarm fires the INSTANT motion starts — the car is still approaching, so the
|
||||||
* first frame often has a small/blurry/absent plate (a low-confidence misread). But the car
|
* first frame often has a small/blurry/absent plate (a low-confidence misread). The readable
|
||||||
* then STOPS at the barrier (waiting for it to open) — the same stationary, well-framed
|
* moments (stopped at the barrier; a gap in headlight glare) are short, so the loop pulls
|
||||||
* moment the manual test reads at ~100%. So instead of one shot, we POLL fresh frames and
|
* frames BACK-TO-BACK: a pull is ~0.1 s and an analyze ~0.25 s on the booth (park-2,
|
||||||
* re-run ANPR until one clears the confidence floor, or the window elapses. Poll interval: */
|
* 2026-10-11), i.e. ~3 frames/s per camera, serial, which the camera's single-threaded
|
||||||
|
* picture endpoint takes without a 503. This is the MINIMUM spacing between two pulls
|
||||||
|
* (ms); 0 = as fast as the camera + recognizer go. Raise it to throttle a weak host. */
|
||||||
function pollMs(): number {
|
function pollMs(): number {
|
||||||
const raw = Number(process.env.ANPR_POLL_MS ?? 1000);
|
const raw = Number(process.env.ANPR_POLL_MS ?? 0);
|
||||||
return Number.isFinite(raw) && raw > 0 ? raw : 1000;
|
return Number.isFinite(raw) && raw >= 0 ? raw : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** How long to keep polling AFTER THE LAST vehicle push before giving up. SLIDING: each new
|
/** How long to keep polling AFTER THE LAST vehicle push before giving up. SLIDING: each new
|
||||||
@@ -69,7 +84,7 @@ function pollMs(): number {
|
|||||||
* barrier (the loop tracks "whoever is here now", not the car that started it). */
|
* barrier (the loop tracks "whoever is here now", not the car that started it). */
|
||||||
function pollWindowMs(): number {
|
function pollWindowMs(): number {
|
||||||
const raw = Number(process.env.ANPR_POLL_WINDOW_MS ?? 8000);
|
const raw = Number(process.env.ANPR_POLL_WINDOW_MS ?? 8000);
|
||||||
return Number.isFinite(raw) && raw > 0 ? raw : 8000;
|
return Number.isFinite(raw) && raw >= 0 ? raw : 8000; // 0 = one frame per push
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Hard ceiling on a single loop from its START, so a continuously-busy lane (pushes never
|
/** Hard ceiling on a single loop from its START, so a continuously-busy lane (pushes never
|
||||||
@@ -97,11 +112,12 @@ export class AnprBridge {
|
|||||||
readonly #pollMs: number;
|
readonly #pollMs: number;
|
||||||
readonly #pollWindowMs: number;
|
readonly #pollWindowMs: number;
|
||||||
readonly #pollMaxMs: number;
|
readonly #pollMaxMs: number;
|
||||||
/** Last-fire timestamps, keyed by deviceId (camera-level, pre-snapshot) AND by
|
/** Last emit timestamps keyed by `deviceId:plate`, gated against #debounceMs. Nothing is
|
||||||
* `deviceId:plate` (post-match) — both gated against #debounceMs. */
|
* keyed by camera: the lane must never go blind to the NEXT car. Pruned on stamp. */
|
||||||
readonly #lastFire = new Map<string, number>();
|
readonly #lastFire = new Map<string, number>();
|
||||||
/** Cameras with a poll loop already in flight — a re-fired alarm (the camera pushes ~1Hz
|
/** Cameras with a poll loop already in flight — a re-fired alarm (the camera pushes on
|
||||||
* while the car sits) must NOT start a second concurrent loop on the same camera. */
|
* every motion while a car moves) must NOT start a second concurrent loop on the same
|
||||||
|
* camera: its picture endpoint is single-threaded (a parallel GET gets HTTP 503). */
|
||||||
readonly #polling = new Set<string>();
|
readonly #polling = new Set<string>();
|
||||||
/** Per-camera SLIDING deadline for the running poll loop. A push that joins a running loop
|
/** Per-camera SLIDING deadline for the running poll loop. A push that joins a running loop
|
||||||
* bumps this forward (lastPush + window, capped at start + max), so the loop keeps pulling
|
* bumps this forward (lastPush + window, capped at start + max), so the loop keeps pulling
|
||||||
@@ -143,13 +159,9 @@ export class AnprBridge {
|
|||||||
// car that just entered. Absent ⇒ true (back-compat: existing anpr cameras still trigger).
|
// car that just entered. Absent ⇒ true (back-compat: existing anpr cameras still trigger).
|
||||||
if (cfg.anprAutoTrigger === false) return;
|
if (cfg.anprAutoTrigger === false) return;
|
||||||
|
|
||||||
// Post-success debounce: once we've emitted a read for this camera, ignore the
|
|
||||||
// ~1Hz re-fires for #debounceMs (set on success below). A fresh alarm AFTER the
|
|
||||||
// window is a new presentation and may start a new poll loop.
|
|
||||||
if (this.#debounced(deviceId)) return;
|
|
||||||
// One poll loop per camera. A push that arrives while a loop runs JOINs it — and
|
// One poll loop per camera. A push that arrives while a loop runs JOINs it — and
|
||||||
// SLIDES the deadline forward (a different car arriving mid-loop keeps the loop alive
|
// SLIDES the deadline forward (a different car arriving mid-loop keeps the loop alive
|
||||||
// so it tracks whoever's at the barrier now, instead of giving up on the early car).
|
// so it reads whoever is at the barrier now; the loop never stops on the first plate).
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (this.#polling.has(deviceId)) {
|
if (this.#polling.has(deviceId)) {
|
||||||
const cur = this.#pollDeadline.get(deviceId) ?? now;
|
const cur = this.#pollDeadline.get(deviceId) ?? now;
|
||||||
@@ -172,134 +184,195 @@ export class AnprBridge {
|
|||||||
// the gated flow infers the verb from the camera's bound relay direction).
|
// the gated flow infers the verb from the camera's bound relay direction).
|
||||||
const direction: FlowDirection = directionOf(this.#db, row) === "exit" ? "exit" : "entry";
|
const direction: FlowDirection = directionOf(this.#db, row) === "exit" ? "exit" : "entry";
|
||||||
|
|
||||||
// POLL-UNTIL-CONFIDENT. The alarm fires as the car APPROACHES (small/blurry/absent
|
// READ-EVERY-FRAME. The alarm fires as the car APPROACHES (small/blurry/absent plate →
|
||||||
// plate → low-confidence misread, e.g. '111'@0.20). But the car then STOPS at the
|
// low-confidence misread, e.g. '111'@0.20); the readable moments come later and may be
|
||||||
// barrier — the stationary, well-framed moment the manual test reads at ~100%. So we
|
// short (stopped at the barrier; a gap in glare). So we pull FRESH frames back-to-back
|
||||||
// pull a FRESH frame every #pollMs and re-run ANPR until one clears the floor, or the
|
// (#pollMs is only a floor) and re-run ANPR for as long as the camera keeps pushing —
|
||||||
// #pollWindowMs window elapses (car drove off / non-subscriber). NB: a fresh pull each
|
// the sliding deadline — or until the hard cap. Every confident plate is SETTLED once
|
||||||
// tick — NOT captureSnapshotShared, whose TTL would re-serve the same bad frame.
|
// per loop (emitted, skipped, or declined); the loop goes on afterwards so a second car
|
||||||
// While polling, watch whether THIS subscriber transacts by another credential
|
// behind the first is read by the same loop. NB: a fresh pull each tick — NOT
|
||||||
// (card/QR at the reader). If their open-occurrence count drops mid-poll, the
|
// captureSnapshotShared, whose TTL would re-serve the same bad frame.
|
||||||
// subscriber already exited/entered — the bridge must NOT also emit (it would act on
|
//
|
||||||
// the NEXT open occurrence: a phantom double-exit, worst for a fleet sub). We learn the
|
// Credential race: a subscriber may scan their card/QR at the reader while we are still
|
||||||
// subscription as soon as a frame reads the bound plate (identity needs no confidence),
|
// reading them. We learn a subscription as soon as a frame reads its plate (identity
|
||||||
// snapshot the count, then keep polling for a CONFIDENT read; abort if the count moved.
|
// needs no confidence) and remember their open-occurrence count; if it has moved by the
|
||||||
let result: Awaited<ReturnType<VisionClient["analyze"]>> = null;
|
// time the plate reads confidently, they already transacted and we do NOT emit (it would
|
||||||
let watchedSubId: string | null = null;
|
// act on the NEXT open occurrence: a phantom double-exit, worst for a fleet sub).
|
||||||
let baselineOpen = 0;
|
|
||||||
// Hard ceiling for THIS loop (start + max); the sliding deadline (bumped by joining
|
|
||||||
// pushes) is read from #pollDeadline each tick but never allowed past this cap.
|
|
||||||
const hardCap = Date.now() + this.#pollMaxMs;
|
const hardCap = Date.now() + this.#pollMaxMs;
|
||||||
let attempts = 0;
|
let attempts = 0;
|
||||||
|
/** Plates settled in THIS loop — a framed car reads confidently on every frame. */
|
||||||
|
const handled = new Set<string>();
|
||||||
|
/** Below-floor plate texts already logged at info (repeats go to debug). */
|
||||||
|
const weakLogged = new Set<string>();
|
||||||
|
/** Subscriptions sighted this loop → open-occurrence count at first sighting. */
|
||||||
|
const sighted = new Map<string, number>();
|
||||||
|
let emitted = 0;
|
||||||
|
// The best-framed vehicle PER CONFIDENT PLATE (highest body-type confidence with a
|
||||||
|
// box), plus one "" bucket for frames with no confident plate: PASS samples, one per
|
||||||
|
// car the loop could tell apart, whatever happens to the plate.
|
||||||
|
const best = new Map<string, { shot: Snapshot; r: VisionResult }>();
|
||||||
try {
|
try {
|
||||||
// DO-while: a detection always analyzes AT LEAST ONE frame, however loaded the
|
// DO-while: a detection always analyzes AT LEAST ONE frame, however loaded the
|
||||||
// host — a plain while could zero-iterate if the window elapsed between setting
|
// host — a plain while could zero-iterate if the window elapsed between setting
|
||||||
// the deadline and reaching the loop (seen as a CI flake with the tests' 5ms
|
// the deadline and reaching the loop (seen as a CI flake with the tests' 5ms
|
||||||
// window; on a busy booth it would silently drop a real car's detection). Exit
|
// window; on a busy booth it would silently drop a real car's detection).
|
||||||
// is via the breaks below (confident read, or next tick would pass the deadline).
|
|
||||||
do {
|
do {
|
||||||
attempts++;
|
attempts++;
|
||||||
|
const tick = Date.now();
|
||||||
const shot = await camera.captureSnapshot({ direction });
|
const shot = await camera.captureSnapshot({ direction });
|
||||||
const r = await this.#vision.analyze(shot.bytes, shot.contentType);
|
const r = await this.#vision.analyze(shot.bytes, shot.contentType);
|
||||||
|
const plate = r?.plate?.text.trim().toUpperCase() ?? "";
|
||||||
|
const confident = plate !== "" && r!.plate!.confidence >= this.#entryMinConfidence;
|
||||||
|
|
||||||
// Identify the subscriber from ANY readable plate (even below the barrier floor),
|
if (r?.vehicle?.bbox) {
|
||||||
|
const key = confident ? plate : "";
|
||||||
|
const cur = best.get(key);
|
||||||
|
if (!cur || r.vehicle.confidence > cur.r.vehicle!.confidence) best.set(key, { shot, r });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Identify the subscriber from ANY readable plate (even below the barrier floor)
|
||||||
// and baseline their open count once — so we can detect a credential beating us.
|
// and baseline their open count once — so we can detect a credential beating us.
|
||||||
if (r?.plate?.text) {
|
if (plate) {
|
||||||
const m0 = this.#subscription.match({ ...baseRead(row, deviceId), value: r.plate.text.trim().toUpperCase() });
|
const m0 = this.#subscription.match({ ...baseRead(row, deviceId), value: plate });
|
||||||
if (m0 && watchedSubId == null) {
|
if (m0 && !sighted.has(m0.subscriptionId)) {
|
||||||
watchedSubId = m0.subscriptionId;
|
sighted.set(m0.subscriptionId, this.#subscription.openOccurrenceCount(m0.subscriptionId));
|
||||||
baselineOpen = this.#subscription.openOccurrenceCount(watchedSubId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A credential (card/QR) closed/opened an occurrence for this subscriber mid-poll →
|
|
||||||
// they already transacted; stop polling and do NOT emit.
|
|
||||||
if (watchedSubId && this.#subscription.openOccurrenceCount(watchedSubId) !== baselineOpen) {
|
|
||||||
this.#logger.info(
|
|
||||||
`anpr-bridge: subscriber ${watchedSubId} transacted by another credential mid-poll — aborting ANPR`,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r?.plate && r.plate.confidence >= this.#entryMinConfidence) {
|
if (confident && !handled.has(plate)) {
|
||||||
result = r;
|
handled.add(plate);
|
||||||
break;
|
if (this.#settle(row, deviceId, plate, r!.plate!.confidence, sighted)) emitted++;
|
||||||
|
} else if (plate && !confident && !handled.has(plate)) {
|
||||||
|
const line =
|
||||||
|
`anpr-bridge: '${plate}' (${r!.plate!.confidence.toFixed(3)}) below floor ` +
|
||||||
|
`${this.#entryMinConfidence} — re-pulling (attempt ${attempts})`;
|
||||||
|
if (weakLogged.has(plate)) this.#logger.debug(line);
|
||||||
|
else {
|
||||||
|
weakLogged.add(plate);
|
||||||
|
this.#logger.info(line);
|
||||||
}
|
}
|
||||||
if (r?.plate) {
|
|
||||||
this.#logger.info(
|
|
||||||
`anpr-bridge: '${r.plate.text}' (${r.plate.confidence.toFixed(3)}) below floor ` +
|
|
||||||
`${this.#entryMinConfidence} — re-pulling (attempt ${attempts})`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// Stop if the next tick would land past the (possibly slid) deadline or the cap.
|
|
||||||
|
// Stop once the (possibly slid) deadline or the cap has passed; else pace to #pollMs.
|
||||||
const effDeadline = Math.min(this.#pollDeadline.get(deviceId) ?? 0, hardCap);
|
const effDeadline = Math.min(this.#pollDeadline.get(deviceId) ?? 0, hardCap);
|
||||||
if (Date.now() + this.#pollMs >= effDeadline) break;
|
const left = effDeadline - Date.now();
|
||||||
await sleep(this.#pollMs);
|
if (left <= 0) break;
|
||||||
|
const wait = this.#pollMs - (Date.now() - tick);
|
||||||
|
if (wait > 0) await sleep(Math.min(wait, left));
|
||||||
} while (true);
|
} while (true);
|
||||||
} finally {
|
} finally {
|
||||||
this.#polling.delete(deviceId);
|
this.#polling.delete(deviceId);
|
||||||
this.#pollDeadline.delete(deviceId);
|
this.#pollDeadline.delete(deviceId);
|
||||||
|
// One pass sample per plated car. The plateless bucket only when NO car got a plate
|
||||||
|
// (otherwise it is mostly the plated cars' approach frames — not another car).
|
||||||
|
for (const [key, b] of best) {
|
||||||
|
if (key !== "" || best.size === 1) void this.#recordPass(deviceId, direction, b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result || !result.plate) {
|
if (handled.size === 0) {
|
||||||
this.#logger.info(
|
this.#logger.info(
|
||||||
`anpr-bridge: no confident plate from ${deviceId} after ${attempts} attempt(s) ` +
|
`anpr-bridge: no confident plate from ${deviceId} after ${attempts} frame(s) ` +
|
||||||
`in ${this.#pollWindowMs}ms — gave up`,
|
`in ${Date.now() - now}ms — gave up`,
|
||||||
);
|
);
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
const plate = result.plate.text.trim().toUpperCase();
|
|
||||||
if (!plate) return;
|
|
||||||
|
|
||||||
const e: DeviceReadEvent = {
|
|
||||||
driverId: row.driverId,
|
|
||||||
deviceId,
|
|
||||||
value: plate,
|
|
||||||
kind: "plate",
|
|
||||||
at: new Date().toISOString(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// MATCH BEFORE EMIT — subscriber-only. A non-subscriber plate records advisory
|
|
||||||
// telemetry and stops; it must NEVER reach the transient plate-as-ticket exit flow.
|
|
||||||
const match = this.#subscription.match(e);
|
|
||||||
if (!match) {
|
|
||||||
this.#recordSkip(deviceId, plate, result.plate.confidence);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final guard against the credential-mid-poll race: if the subscriber transacted between
|
|
||||||
// our baseline and now (e.g. a card scan in the last tick), don't double-act.
|
|
||||||
if (watchedSubId === match.subscriptionId && this.#subscription.openOccurrenceCount(match.subscriptionId) !== baselineOpen) {
|
|
||||||
this.#logger.info(`anpr-bridge: ${match.subscriptionId} already transacted — skipping ANPR emit`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Plate-level debounce — belt-and-suspenders against a gap that slips the
|
|
||||||
// camera-level gate re-emitting the SAME plate.
|
|
||||||
const plateKey = `${deviceId}:${plate}`;
|
|
||||||
if (this.#debounced(plateKey)) return;
|
|
||||||
this.#stamp(plateKey);
|
|
||||||
// Camera-level debounce stamp — now that we've emitted, suppress the camera's ~1Hz
|
|
||||||
// re-fires (and any new poll loop) for #debounceMs.
|
|
||||||
this.#stamp(deviceId);
|
|
||||||
|
|
||||||
this.#logger.info(
|
this.#logger.info(
|
||||||
`anpr-bridge: subscriber plate '${plate}' (${result.plate.confidence.toFixed(3)}) → read bus`,
|
`anpr-bridge: ${deviceId} loop done — ${attempts} frame(s) in ${Date.now() - now}ms, ` +
|
||||||
|
`${handled.size} plate(s), ${emitted} emitted`,
|
||||||
);
|
);
|
||||||
deviceEvents.emitRead(e); // → onRead → ReadDispatcher → gated SubscriptionFlow
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Fail-soft: an ANPR failure degrades to the subscriber's card/QR, never strands the lane.
|
// Fail-soft: an ANPR failure degrades to the subscriber's card/QR, never strands the lane.
|
||||||
this.#logger.warn(`anpr-bridge failed (${deviceId}): ${(err as Error).message}`);
|
this.#logger.warn(`anpr-bridge failed (${deviceId}): ${(err as Error).message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Settle ONE confident plate: match it to a subscriber (MATCH BEFORE EMIT — a non-subscriber
|
||||||
|
* plate records advisory telemetry and stops; it must NEVER reach the transient
|
||||||
|
* plate-as-ticket exit flow), decline if that subscriber already transacted by another
|
||||||
|
* credential during this loop, debounce per plate, then emit. Returns true if emitted.
|
||||||
|
*/
|
||||||
|
#settle(
|
||||||
|
row: { driverId: string },
|
||||||
|
deviceId: string,
|
||||||
|
plate: string,
|
||||||
|
confidence: number,
|
||||||
|
sighted: ReadonlyMap<string, number>,
|
||||||
|
): boolean {
|
||||||
|
const e: DeviceReadEvent = { ...baseRead(row, deviceId), value: plate };
|
||||||
|
const match = this.#subscription.match(e);
|
||||||
|
if (!match) {
|
||||||
|
this.#recordSkip(deviceId, plate, confidence);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const base = sighted.get(match.subscriptionId);
|
||||||
|
if (base != null && this.#subscription.openOccurrenceCount(match.subscriptionId) !== base) {
|
||||||
|
this.#logger.info(
|
||||||
|
`anpr-bridge: subscriber ${match.subscriptionId} transacted by another credential mid-poll — not emitting '${plate}'`,
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Per-PLATE debounce across loops: the same car re-starting in the zone within the window
|
||||||
|
// is still the same presentation. Never per camera — the next car must not be blind.
|
||||||
|
const plateKey = `${deviceId}:${plate}`;
|
||||||
|
if (this.#debounced(plateKey)) {
|
||||||
|
this.#logger.debug(`anpr-bridge: '${plate}' read again within ${this.#debounceMs}ms — debounced`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.#stamp(plateKey);
|
||||||
|
this.#logger.info(`anpr-bridge: subscriber plate '${plate}' (${confidence.toFixed(3)}) → read bus`);
|
||||||
|
deviceEvents.emitRead(e); // → onRead → ReadDispatcher → gated SubscriptionFlow
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
#debounced(key: string): boolean {
|
#debounced(key: string): boolean {
|
||||||
const last = this.#lastFire.get(key);
|
const last = this.#lastFire.get(key);
|
||||||
return last != null && Date.now() - last < this.#debounceMs;
|
return last != null && Date.now() - last < this.#debounceMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
#stamp(key: string): void {
|
#stamp(key: string): void {
|
||||||
this.#lastFire.set(key, Date.now());
|
const now = Date.now();
|
||||||
|
this.#lastFire.set(key, now);
|
||||||
|
// One key per plate ever seen would grow forever on a busy lane; drop expired stamps.
|
||||||
|
if (this.#lastFire.size > 256) {
|
||||||
|
for (const [k, t] of this.#lastFire) if (now - t >= this.#debounceMs) this.#lastFire.delete(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** EVERY car the camera flagged becomes one stored frame + one vehicle read on the bus —
|
||||||
|
* plate or no plate, ticket or no ticket. The read is what the Car Wash module samples
|
||||||
|
* for the reviewer (wiki/concepts/vision-review-outbox.md §entry stream); until this,
|
||||||
|
* only session-tied reads were sampled, so a lot with its barriers up (nobody takes a
|
||||||
|
* ticket) sent nothing however many cars the camera saw. One sample per car the loop
|
||||||
|
* could tell apart (per confident plate; one for a plateless loop), from its best-framed
|
||||||
|
* frame. Fire-and-forget: never on the ANPR path, a failure is a log line. Identity
|
||||||
|
* `pass:<snapshotId>` — no session to tie it to. */
|
||||||
|
async #recordPass(deviceId: string, direction: FlowDirection, best: { shot: Snapshot; r: VisionResult }): Promise<void> {
|
||||||
|
try {
|
||||||
|
const vehicle = best.r.vehicle;
|
||||||
|
if (!vehicle) return;
|
||||||
|
const { vehicleBox, plateBox } = await normalizedBoxes(best.shot.bytes, best.r);
|
||||||
|
if (!vehicleBox) return;
|
||||||
|
const stored = await encodeForStorage(best.shot, this.#logger);
|
||||||
|
const id = randomUUID();
|
||||||
|
const identity = `pass:${id}`;
|
||||||
|
this.#db
|
||||||
|
.insert(snapshots)
|
||||||
|
.values({ id, direction, deviceId, identity, contentType: stored.contentType, bytes: stored.bytes, capturedAt: best.shot.capturedAt })
|
||||||
|
.run();
|
||||||
|
// plateBox whenever the detector saw a plate, confident or not: it is what the
|
||||||
|
// review crop blurs — privacy, not identity.
|
||||||
|
deviceEvents.emitVehicleRead({
|
||||||
|
identity,
|
||||||
|
direction,
|
||||||
|
deviceId,
|
||||||
|
read: { bodyType: vehicle.bodyType, confidence: vehicle.confidence, snapshotId: id, box: vehicleBox, plateBox },
|
||||||
|
});
|
||||||
|
this.#logger.debug(`anpr-bridge: pass sample ${id} '${vehicle.bodyType}' (${vehicle.confidence.toFixed(3)}) from ${deviceId}`);
|
||||||
|
} catch (err) {
|
||||||
|
this.#logger.warn(`anpr-bridge: pass sample failed for ${deviceId}: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Advisory telemetry: a plate was read at the lane but matched no subscription. Not a
|
/** Advisory telemetry: a plate was read at the lane but matched no subscription. Not a
|
||||||
|
|||||||
+52
-3
@@ -1,6 +1,6 @@
|
|||||||
import { randomBytes } from "node:crypto";
|
import { randomBytes } from "node:crypto";
|
||||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||||
import { eq, rolePermissions, type Db } from "@parking/db";
|
import { eq, rolePermissions, users, type Db } from "@parking/db";
|
||||||
import { ADMIN_ROLE_ID, PERMISSIONS, type Permission } from "@parking/shared";
|
import { ADMIN_ROLE_ID, PERMISSIONS, type Permission } from "@parking/shared";
|
||||||
|
|
||||||
// Local JWT auth helpers — fully local, no external identity provider
|
// Local JWT auth helpers — fully local, no external identity provider
|
||||||
@@ -143,10 +143,41 @@ export function initAuth(db: Db): void {
|
|||||||
permsCache.clear();
|
permsCache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Clear the permission cache. Call after ANY write to roles / role_permissions
|
/** Clear the permission + role caches. Call after ANY write to roles / role_permissions
|
||||||
* (or a user's roleId) so the change takes effect on the next request. */
|
* or to a user's roleId / deletion, so the change takes effect on the next request. */
|
||||||
export function bumpPermsCache(): void {
|
export function bumpPermsCache(): void {
|
||||||
permsCache.clear();
|
permsCache.clear();
|
||||||
|
roleCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** userId → CURRENT roleId, cached until bumpPermsCache(). */
|
||||||
|
const roleCache = new Map<string, string | null>();
|
||||||
|
|
||||||
|
/** The user's CURRENT role. The token pins the roleId that was current at LOGIN; an
|
||||||
|
* admin reassigning a user's role (or deleting the user) must take effect on the next
|
||||||
|
* request exactly like editing a role does — otherwise the reassigned user keeps the
|
||||||
|
* old role's rights until they log out (found 2026-09-05: a user moved to a new
|
||||||
|
* wash role kept 403ing on the new role's permissions). null = the user is gone. */
|
||||||
|
export function currentRoleId(sub: string): string | null {
|
||||||
|
if (!authDb) throw new Error("auth not initialised (call initAuth)");
|
||||||
|
const hit = roleCache.get(sub);
|
||||||
|
if (hit !== undefined) return hit;
|
||||||
|
const row = authDb
|
||||||
|
.select({ roleId: users.roleId, deletedAt: users.deletedAt })
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.id, sub))
|
||||||
|
.get();
|
||||||
|
const roleId = row && row.deletedAt == null ? row.roleId : null;
|
||||||
|
roleCache.set(sub, roleId);
|
||||||
|
return roleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** After jwtVerify: replace the token's pinned roleId with the user's current one, or
|
||||||
|
* end the session if the user no longer exists. */
|
||||||
|
function refreshRole(req: FastifyRequest): void {
|
||||||
|
const roleId = currentRoleId(req.user.sub);
|
||||||
|
if (roleId === null) throw Object.assign(new Error("session no longer valid"), { statusCode: 401 });
|
||||||
|
if (roleId !== req.user.roleId) req.user.roleId = roleId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The permission set for a role id, cached. `admin` is always the full set. */
|
/** The permission set for a role id, cached. `admin` is always the full set. */
|
||||||
@@ -184,12 +215,29 @@ export function requirePermission(...required: Permission[]) {
|
|||||||
return async (req: FastifyRequest, _reply: FastifyReply) => {
|
return async (req: FastifyRequest, _reply: FastifyReply) => {
|
||||||
await req.jwtVerify(); // reads the token cookie (configured in server.ts)
|
await req.jwtVerify(); // reads the token cookie (configured in server.ts)
|
||||||
assertCsrf(req);
|
assertCsrf(req);
|
||||||
|
refreshRole(req);
|
||||||
if (!req.user || !roleHasPermissions(req.user.roleId, required)) {
|
if (!req.user || !roleHasPermissions(req.user.roleId, required)) {
|
||||||
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* preHandler guard satisfied by ANY ONE of the listed permissions — for a read that
|
||||||
|
* two jobs legitimately share (a module's master data: the desk that works with it
|
||||||
|
* reads it under the module's own permission, Setup reads it under site:read).
|
||||||
|
*/
|
||||||
|
export function requireAnyPermission(...anyOf: Permission[]) {
|
||||||
|
return async (req: FastifyRequest, _reply: FastifyReply) => {
|
||||||
|
await req.jwtVerify();
|
||||||
|
assertCsrf(req);
|
||||||
|
refreshRole(req);
|
||||||
|
if (!req.user || !anyOf.some((p) => roleHasPermissions(req.user!.roleId, [p]))) {
|
||||||
|
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* preHandler that requires a valid signed-in session but NO specific permission —
|
* preHandler that requires a valid signed-in session but NO specific permission —
|
||||||
* for "about me" routes (/me, change own language) every authenticated user may
|
* for "about me" routes (/me, change own language) every authenticated user may
|
||||||
@@ -201,4 +249,5 @@ export async function requireAuth(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await req.jwtVerify();
|
await req.jwtVerify();
|
||||||
assertCsrf(req);
|
assertCsrf(req);
|
||||||
|
refreshRole(req);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
type PrinterInstance,
|
type PrinterInstance,
|
||||||
type ReceiptData,
|
type ReceiptData,
|
||||||
type TicketHeader,
|
type TicketHeader,
|
||||||
|
printerRoleOf,
|
||||||
} from "@parking/devices";
|
} from "@parking/devices";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
import { devicesByDirection } from "./device-resolve.js";
|
import { devicesByDirection } from "./device-resolve.js";
|
||||||
@@ -41,7 +42,7 @@ function loadPrinters(db: Db): PrinterInstance[] {
|
|||||||
const driver = registry.get(row.driverId);
|
const driver = registry.get(row.driverId);
|
||||||
if (!driver) continue;
|
if (!driver) continue;
|
||||||
const cfg = row.config as Record<string, unknown>;
|
const cfg = row.config as Record<string, unknown>;
|
||||||
const role = cfg.role === "booth-receipt" ? "booth-receipt" : "entry-dispenser";
|
const role = printerRoleOf(cfg);
|
||||||
try {
|
try {
|
||||||
out.push({
|
out.push({
|
||||||
id: row.id,
|
id: row.id,
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import { CameraActivity } from "./camera-activity.js";
|
||||||
|
import { deviceEvents, type CameraActivityEvent } from "./device-events.js";
|
||||||
|
|
||||||
|
// Per-camera activity drives the booth tiles' snapshot→live switch: one "active" edge
|
||||||
|
// per run of pushes, re-armed by each push, one "idle" edge TTL after the last push.
|
||||||
|
|
||||||
|
const logger = { debug: vi.fn(), info: vi.fn(), warn: vi.fn() } as unknown as FastifyBaseLogger;
|
||||||
|
let seen: CameraActivityEvent[];
|
||||||
|
let off: () => void;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
seen = [];
|
||||||
|
off = deviceEvents.onCameraActivity((e) => seen.push(e));
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
off();
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("CameraActivity", () => {
|
||||||
|
it("emits active once, re-arms on each push, idles after the TTL", () => {
|
||||||
|
const a = new CameraActivity(logger, 30_000);
|
||||||
|
a.vehicleDetected("cam1");
|
||||||
|
vi.advanceTimersByTime(20_000);
|
||||||
|
a.vehicleDetected("cam1"); // re-arm
|
||||||
|
expect(seen).toEqual([{ deviceId: "cam1", active: true }]);
|
||||||
|
expect(a.snapshot()).toEqual(["cam1"]);
|
||||||
|
vi.advanceTimersByTime(29_000);
|
||||||
|
expect(seen).toHaveLength(1);
|
||||||
|
vi.advanceTimersByTime(1_000);
|
||||||
|
expect(seen).toEqual([
|
||||||
|
{ deviceId: "cam1", active: true },
|
||||||
|
{ deviceId: "cam1", active: false },
|
||||||
|
]);
|
||||||
|
expect(a.snapshot()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("tracks cameras independently", () => {
|
||||||
|
const a = new CameraActivity(logger, 1_000);
|
||||||
|
a.vehicleDetected("entry");
|
||||||
|
vi.advanceTimersByTime(600);
|
||||||
|
a.vehicleDetected("exit");
|
||||||
|
vi.advanceTimersByTime(400);
|
||||||
|
expect(a.snapshot()).toEqual(["exit"]);
|
||||||
|
a.stop();
|
||||||
|
vi.advanceTimersByTime(5_000);
|
||||||
|
expect(seen.filter((e) => !e.active)).toEqual([{ deviceId: "entry", active: false }]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import { deviceEvents, type CameraActivityEvent } from "./device-events.js";
|
||||||
|
import { busyTtlMs } from "./lane-status.js";
|
||||||
|
|
||||||
|
// Per-CAMERA "a vehicle is in view" state, for the booth camera tiles: a tile shows the
|
||||||
|
// camera's latest snapshot and switches to live video while its camera is active. Same
|
||||||
|
// signal and TTL as LaneStatus (vehicle `active` pushes, no leave event, 30 s after the
|
||||||
|
// last push) but keyed by camera, not lane: on a shared lane (park-lab: an entry and an
|
||||||
|
// exit camera on one physical lane) only the camera that saw the car goes live.
|
||||||
|
// ADVISORY ONLY — it gates nothing. See wiki/decisions/camera-live-view.md.
|
||||||
|
|
||||||
|
export type { CameraActivityEvent };
|
||||||
|
|
||||||
|
export class CameraActivity {
|
||||||
|
readonly #logger: FastifyBaseLogger;
|
||||||
|
readonly #ttlMs: number;
|
||||||
|
readonly #timers = new Map<string, ReturnType<typeof setTimeout>>();
|
||||||
|
|
||||||
|
constructor(logger: FastifyBaseLogger, ttlMs = busyTtlMs()) {
|
||||||
|
this.#logger = logger;
|
||||||
|
this.#ttlMs = ttlMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Camera ids currently active (for the WS hello). */
|
||||||
|
snapshot(): string[] {
|
||||||
|
return [...this.#timers.keys()];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A vehicle push from `deviceId`: mark it active and (re)arm its clear. */
|
||||||
|
vehicleDetected(deviceId: string): void {
|
||||||
|
const existing = this.#timers.get(deviceId);
|
||||||
|
if (existing) clearTimeout(existing);
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
this.#timers.delete(deviceId);
|
||||||
|
this.#logger.debug(`camera-activity: ${deviceId} -> idle`);
|
||||||
|
deviceEvents.emitCameraActivity({ deviceId, active: false });
|
||||||
|
}, this.#ttlMs);
|
||||||
|
timer.unref?.();
|
||||||
|
this.#timers.set(deviceId, timer);
|
||||||
|
if (!existing) {
|
||||||
|
this.#logger.debug(`camera-activity: ${deviceId} -> active`);
|
||||||
|
deviceEvents.emitCameraActivity({ deviceId, active: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
for (const t of this.#timers.values()) clearTimeout(t);
|
||||||
|
this.#timers.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import type { LiveStreamDevice, LiveVideoSource } from "@parking/devices";
|
||||||
|
import { CameraLiveHub, type LiveStatus, type LiveViewer } from "./camera-live.js";
|
||||||
|
|
||||||
|
// The hub's contract: one upstream per camera however many viewers, latest-frame-only
|
||||||
|
// delivery (a busy viewer misses frames, never queues them), teardown after the last
|
||||||
|
// viewer leaves (with a linger), reconnect with backoff while viewers remain, and a
|
||||||
|
// non-retryable failure (codec) reported as "unavailable".
|
||||||
|
|
||||||
|
const logger = { warn: vi.fn(), info: vi.fn(), debug: vi.fn() } as unknown as FastifyBaseLogger;
|
||||||
|
|
||||||
|
class FakeSource implements LiveVideoSource {
|
||||||
|
frameCbs: Array<(f: Buffer) => void> = [];
|
||||||
|
endCbs: Array<(e?: Error) => void> = [];
|
||||||
|
stopped = false;
|
||||||
|
constructor(private readonly fail?: Error) {}
|
||||||
|
async start() {
|
||||||
|
if (this.fail) {
|
||||||
|
queueMicrotask(() => this.end(this.fail));
|
||||||
|
throw this.fail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stop() {
|
||||||
|
this.stopped = true;
|
||||||
|
this.end();
|
||||||
|
}
|
||||||
|
onFrame(cb: (f: Buffer) => void) {
|
||||||
|
this.frameCbs.push(cb);
|
||||||
|
}
|
||||||
|
onEnd(cb: (e?: Error) => void) {
|
||||||
|
this.endCbs.push(cb);
|
||||||
|
}
|
||||||
|
frame(b = Buffer.from([0xff, 0xd8])) {
|
||||||
|
for (const cb of this.frameCbs) cb(b);
|
||||||
|
}
|
||||||
|
end(err?: Error) {
|
||||||
|
for (const cb of this.endCbs.splice(0)) cb(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function viewer(busy = false) {
|
||||||
|
const frames: Buffer[] = [];
|
||||||
|
const statuses: LiveStatus[] = [];
|
||||||
|
const v: LiveViewer & { frames: Buffer[]; statuses: LiveStatus[]; isBusy: boolean } = {
|
||||||
|
frames,
|
||||||
|
statuses,
|
||||||
|
isBusy: busy,
|
||||||
|
sendFrame: (f) => frames.push(f),
|
||||||
|
sendStatus: (s) => statuses.push(s),
|
||||||
|
busy: () => v.isBusy,
|
||||||
|
};
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sources: FakeSource[];
|
||||||
|
let nextFail: Error | undefined;
|
||||||
|
const device: LiveStreamDevice = {
|
||||||
|
openLiveStream: () => {
|
||||||
|
const s = new FakeSource(nextFail);
|
||||||
|
sources.push(s);
|
||||||
|
return s;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
sources = [];
|
||||||
|
nextFail = undefined;
|
||||||
|
});
|
||||||
|
afterEach(() => vi.useRealTimers());
|
||||||
|
|
||||||
|
describe("CameraLiveHub", () => {
|
||||||
|
it("shares one upstream between viewers and tears it down after the last leaves", async () => {
|
||||||
|
const hub = new CameraLiveHub(() => device, logger, { lingerMs: 1000, maxFps: 1000 });
|
||||||
|
const a = viewer();
|
||||||
|
const b = viewer();
|
||||||
|
const offA = hub.subscribe("cam1", a);
|
||||||
|
const offB = hub.subscribe("cam1", b);
|
||||||
|
expect(sources).toHaveLength(1);
|
||||||
|
sources[0]!.frame();
|
||||||
|
expect(a.frames).toHaveLength(1);
|
||||||
|
expect(b.frames).toHaveLength(1);
|
||||||
|
expect(a.statuses.at(-1)).toEqual({ state: "live" });
|
||||||
|
|
||||||
|
offA();
|
||||||
|
offB();
|
||||||
|
expect(sources[0]!.stopped).toBe(false); // lingering
|
||||||
|
vi.advanceTimersByTime(1000);
|
||||||
|
expect(sources[0]!.stopped).toBe(true);
|
||||||
|
expect(hub.upstreamCount).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a viewer returning within the linger reuses the session", () => {
|
||||||
|
const hub = new CameraLiveHub(() => device, logger, { lingerMs: 1000 });
|
||||||
|
const off = hub.subscribe("cam1", viewer());
|
||||||
|
off();
|
||||||
|
vi.advanceTimersByTime(500);
|
||||||
|
hub.subscribe("cam1", viewer());
|
||||||
|
vi.advanceTimersByTime(5000);
|
||||||
|
expect(sources).toHaveLength(1);
|
||||||
|
expect(sources[0]!.stopped).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips frames for a busy viewer and caps the frame rate", () => {
|
||||||
|
const hub = new CameraLiveHub(() => device, logger, { maxFps: 10 });
|
||||||
|
const slow = viewer(true);
|
||||||
|
const fast = viewer();
|
||||||
|
hub.subscribe("cam1", slow);
|
||||||
|
hub.subscribe("cam1", fast);
|
||||||
|
for (let i = 0; i < 5; i++) sources[0]!.frame(); // same instant: only the first passes the cap
|
||||||
|
expect(fast.frames).toHaveLength(1);
|
||||||
|
expect(slow.frames).toHaveLength(0);
|
||||||
|
vi.advanceTimersByTime(100);
|
||||||
|
slow.isBusy = false;
|
||||||
|
sources[0]!.frame();
|
||||||
|
expect(fast.frames).toHaveLength(2);
|
||||||
|
expect(slow.frames).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reconnects with backoff while viewers remain", async () => {
|
||||||
|
const hub = new CameraLiveHub(() => device, logger);
|
||||||
|
const v = viewer();
|
||||||
|
hub.subscribe("cam1", v);
|
||||||
|
sources[0]!.end(new Error("RTSP connection closed"));
|
||||||
|
expect(v.statuses.at(-1)).toEqual({ state: "retrying", reason: "RTSP connection closed" });
|
||||||
|
vi.advanceTimersByTime(1000);
|
||||||
|
expect(sources).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports a non-retryable failure as unavailable", async () => {
|
||||||
|
const hub = new CameraLiveHub(() => device, logger, { unavailableRetryMs: 60_000 });
|
||||||
|
nextFail = Object.assign(new Error("camera stream is H264, not MJPEG"), { retryable: false });
|
||||||
|
const v = viewer();
|
||||||
|
hub.subscribe("cam1", v);
|
||||||
|
await vi.advanceTimersByTimeAsync(0);
|
||||||
|
expect(v.statuses.at(-1)).toEqual({ state: "unavailable", reason: "camera stream is H264, not MJPEG" });
|
||||||
|
vi.advanceTimersByTime(59_000);
|
||||||
|
expect(sources).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a camera without live support is unavailable and opens nothing", () => {
|
||||||
|
const hub = new CameraLiveHub(() => "live view not supported by this camera driver", logger);
|
||||||
|
const v = viewer();
|
||||||
|
hub.subscribe("cam1", v);
|
||||||
|
expect(v.statuses.at(-1)).toEqual({ state: "unavailable", reason: "live view not supported by this camera driver" });
|
||||||
|
expect(hub.upstreamCount).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,199 @@
|
|||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import type { LiveStreamDevice, LiveVideoSource } from "@parking/devices";
|
||||||
|
|
||||||
|
// Live camera video for the booth's camera tiles. ONE upstream session per camera,
|
||||||
|
// shared by every viewer (booth tab, desktop shell) and torn down a few seconds after
|
||||||
|
// the last viewer leaves — never one camera session per tab (a camera has few stream
|
||||||
|
// slots, and the ANPR loop pulls stills from the same unit).
|
||||||
|
//
|
||||||
|
// Frames are complete JPEGs (the driver reassembles RTP/JPEG; nothing is decoded here).
|
||||||
|
// Delivery is LATEST-FRAME-ONLY: a viewer whose socket still holds unsent bytes simply
|
||||||
|
// misses frames, so a slow client can never grow server memory, and a per-viewer frame
|
||||||
|
// cap keeps the desktop shell's IPC light. Advisory viewing only: nothing here touches
|
||||||
|
// the ledger, a gate or a barrier. See wiki/decisions/camera-live-view.md.
|
||||||
|
|
||||||
|
export type LiveStatus =
|
||||||
|
| { state: "connecting" }
|
||||||
|
| { state: "live" }
|
||||||
|
| { state: "retrying"; reason: string }
|
||||||
|
| { state: "unavailable"; reason: string };
|
||||||
|
|
||||||
|
export interface LiveViewer {
|
||||||
|
sendFrame(jpeg: Buffer): void;
|
||||||
|
sendStatus(status: LiveStatus): void;
|
||||||
|
/** True while this viewer's transport still has unsent bytes: skip the frame. */
|
||||||
|
busy(): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Resolve a camera id to its live-stream capability, or a reason it has none. */
|
||||||
|
export type LiveResolver = (deviceId: string) => LiveStreamDevice | string;
|
||||||
|
|
||||||
|
export interface CameraLiveOptions {
|
||||||
|
/** Keep the upstream this long after the last viewer leaves (re-render churn). */
|
||||||
|
lingerMs?: number;
|
||||||
|
/** Per-viewer frame cap. The cameras send 12.5–20 fps; 10 is plenty to watch a car. */
|
||||||
|
maxFps?: number;
|
||||||
|
/** Backoff ceiling for reconnects while viewers are present. */
|
||||||
|
maxRetryMs?: number;
|
||||||
|
/** Retry delay after a non-retryable failure (codec/driver), in case it is fixed. */
|
||||||
|
unavailableRetryMs?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Upstream {
|
||||||
|
readonly deviceId: string;
|
||||||
|
readonly viewers: Map<LiveViewer, { lastSent: number }>;
|
||||||
|
source: LiveVideoSource | null;
|
||||||
|
status: LiveStatus;
|
||||||
|
attempt: number;
|
||||||
|
linger: ReturnType<typeof setTimeout> | null;
|
||||||
|
retry: ReturnType<typeof setTimeout> | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function liveMaxFps(): number {
|
||||||
|
const raw = Number(process.env.CAMERA_LIVE_MAX_FPS ?? 10);
|
||||||
|
return Number.isFinite(raw) && raw > 0 ? raw : 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CameraLiveHub {
|
||||||
|
readonly #resolve: LiveResolver;
|
||||||
|
readonly #logger: FastifyBaseLogger;
|
||||||
|
readonly #lingerMs: number;
|
||||||
|
readonly #minFrameGapMs: number;
|
||||||
|
readonly #maxRetryMs: number;
|
||||||
|
readonly #unavailableRetryMs: number;
|
||||||
|
readonly #upstreams = new Map<string, Upstream>();
|
||||||
|
|
||||||
|
constructor(resolve: LiveResolver, logger: FastifyBaseLogger, opts: CameraLiveOptions = {}) {
|
||||||
|
this.#resolve = resolve;
|
||||||
|
this.#logger = logger;
|
||||||
|
this.#lingerMs = opts.lingerMs ?? 5000;
|
||||||
|
this.#minFrameGapMs = 1000 / (opts.maxFps ?? liveMaxFps());
|
||||||
|
this.#maxRetryMs = opts.maxRetryMs ?? 30_000;
|
||||||
|
this.#unavailableRetryMs = opts.unavailableRetryMs ?? 60_000;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Number of open upstream sessions (diagnostic / tests). */
|
||||||
|
get upstreamCount(): number {
|
||||||
|
return [...this.#upstreams.values()].filter((u) => u.source !== null).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe(deviceId: string, viewer: LiveViewer): () => void {
|
||||||
|
let up = this.#upstreams.get(deviceId);
|
||||||
|
if (!up) {
|
||||||
|
up = { deviceId, viewers: new Map(), source: null, status: { state: "connecting" }, attempt: 0, linger: null, retry: null };
|
||||||
|
this.#upstreams.set(deviceId, up);
|
||||||
|
}
|
||||||
|
if (up.linger) {
|
||||||
|
clearTimeout(up.linger);
|
||||||
|
up.linger = null;
|
||||||
|
}
|
||||||
|
if (!up.source && !up.retry) {
|
||||||
|
up.status = { state: "connecting" };
|
||||||
|
this.#start(up); // may settle the status at once (e.g. "unavailable")
|
||||||
|
}
|
||||||
|
// Added after the start so the newcomer gets the CURRENT status exactly once.
|
||||||
|
up.viewers.set(viewer, { lastSent: 0 });
|
||||||
|
viewer.sendStatus(up.status);
|
||||||
|
|
||||||
|
const u = up;
|
||||||
|
let done = false;
|
||||||
|
return () => {
|
||||||
|
if (done) return;
|
||||||
|
done = true;
|
||||||
|
u.viewers.delete(viewer);
|
||||||
|
if (u.viewers.size === 0 && !u.linger) {
|
||||||
|
u.linger = setTimeout(() => this.#teardown(u), this.#lingerMs);
|
||||||
|
u.linger.unref?.();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
stopAll(): void {
|
||||||
|
for (const up of [...this.#upstreams.values()]) this.#teardown(up);
|
||||||
|
}
|
||||||
|
|
||||||
|
#start(up: Upstream): void {
|
||||||
|
const dev = this.#resolve(up.deviceId);
|
||||||
|
if (typeof dev === "string") {
|
||||||
|
this.#setStatus(up, { state: "unavailable", reason: dev });
|
||||||
|
return; // not a live-capable camera: nothing to retry
|
||||||
|
}
|
||||||
|
let source: LiveVideoSource;
|
||||||
|
try {
|
||||||
|
source = dev.openLiveStream();
|
||||||
|
} catch (err) {
|
||||||
|
// A driver that cannot even build a session (bad config): report it; the next
|
||||||
|
// subscribe tries again.
|
||||||
|
this.#setStatus(up, { state: "unavailable", reason: (err as Error).message });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
up.source = source;
|
||||||
|
let gotFrame = false;
|
||||||
|
source.onFrame((jpeg) => {
|
||||||
|
if (up.source !== source) return;
|
||||||
|
if (!gotFrame) {
|
||||||
|
gotFrame = true;
|
||||||
|
up.attempt = 0;
|
||||||
|
this.#setStatus(up, { state: "live" });
|
||||||
|
}
|
||||||
|
const now = Date.now();
|
||||||
|
for (const [viewer, st] of up.viewers) {
|
||||||
|
if (now - st.lastSent < this.#minFrameGapMs || viewer.busy()) continue;
|
||||||
|
st.lastSent = now;
|
||||||
|
try {
|
||||||
|
viewer.sendFrame(jpeg);
|
||||||
|
} catch {
|
||||||
|
/* a broken viewer is removed by its own close handler */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
source.onEnd((err) => this.#onEnded(up, source, err));
|
||||||
|
source.start().catch(() => undefined); // failures arrive through onEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
#onEnded(up: Upstream, source: LiveVideoSource | null, err?: Error): void {
|
||||||
|
if (source && up.source !== source) return; // a stale session
|
||||||
|
up.source = null;
|
||||||
|
if (up.viewers.size === 0 || !this.#upstreams.has(up.deviceId)) return;
|
||||||
|
const reason = err?.message ?? "stream ended";
|
||||||
|
const retryable = (err as { retryable?: boolean } | undefined)?.retryable !== false;
|
||||||
|
let delay: number;
|
||||||
|
if (retryable) {
|
||||||
|
delay = Math.min(this.#maxRetryMs, 1000 * 2 ** up.attempt);
|
||||||
|
up.attempt++;
|
||||||
|
this.#setStatus(up, { state: "retrying", reason });
|
||||||
|
} else {
|
||||||
|
delay = this.#unavailableRetryMs;
|
||||||
|
this.#setStatus(up, { state: "unavailable", reason });
|
||||||
|
}
|
||||||
|
// Once, not per retry: a camera that is down for hours must not flood the log.
|
||||||
|
if (up.attempt <= 1) this.#logger.warn(`camera-live ${up.deviceId}: ${reason}`);
|
||||||
|
up.retry = setTimeout(() => {
|
||||||
|
up.retry = null;
|
||||||
|
if (up.viewers.size > 0 && !up.source) this.#start(up);
|
||||||
|
}, delay);
|
||||||
|
up.retry.unref?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
#setStatus(up: Upstream, status: LiveStatus): void {
|
||||||
|
up.status = status;
|
||||||
|
for (const viewer of up.viewers.keys()) {
|
||||||
|
try {
|
||||||
|
viewer.sendStatus(status);
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#teardown(up: Upstream): void {
|
||||||
|
if (up.linger) clearTimeout(up.linger);
|
||||||
|
if (up.retry) clearTimeout(up.retry);
|
||||||
|
up.linger = null;
|
||||||
|
up.retry = null;
|
||||||
|
const source = up.source;
|
||||||
|
up.source = null;
|
||||||
|
this.#upstreams.delete(up.deviceId);
|
||||||
|
source?.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { EventEmitter } from "node:events";
|
import { EventEmitter } from "node:events";
|
||||||
import type { PrinterStatus } from "@parking/devices";
|
import type { PrinterStatus } from "@parking/devices";
|
||||||
import type { LedgerEventRow } from "@parking/db";
|
import type { LedgerEventRow } from "@parking/db";
|
||||||
|
import type { VehicleRead } from "@parking/shared";
|
||||||
|
|
||||||
// Internal event bus for device-originated events (button presses, etc.).
|
// Internal event bus for device-originated events (button presses, etc.).
|
||||||
// Hardware drivers / inbound device pushes emit here; business logic (entry
|
// Hardware drivers / inbound device pushes emit here; business logic (entry
|
||||||
@@ -52,7 +53,7 @@ export interface ReadOutcome {
|
|||||||
export interface PrinterStatusEvent {
|
export interface PrinterStatusEvent {
|
||||||
readonly deviceId: string; // devices id
|
readonly deviceId: string; // devices id
|
||||||
readonly driverId: string;
|
readonly driverId: string;
|
||||||
readonly role?: string; // entry-dispenser | booth-receipt
|
readonly role?: string; // entry-dispenser | booth-receipt | wash-desk
|
||||||
readonly status: PrinterStatus;
|
readonly status: PrinterStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,10 +75,10 @@ export interface DeviceStatusEvent {
|
|||||||
* chip reads e.g. "Lexuesi hyrje" / "Kamera dalje" / "Printer kabina":
|
* chip reads e.g. "Lexuesi hyrje" / "Kamera dalje" / "Printer kabina":
|
||||||
* - reader/camera: "entry" | "exit" | "both" (inherited from its bound relay)
|
* - reader/camera: "entry" | "exit" | "both" (inherited from its bound relay)
|
||||||
* - access: "entry" | "exit" | "both" | "mixed" (from its relays[])
|
* - access: "entry" | "exit" | "both" | "mixed" (from its relays[])
|
||||||
* - printer: "lane" (entry-dispenser) | "booth" (booth-receipt)
|
* - printer: "lane" (entry-dispenser) | "booth" (booth-receipt) | "wash" (wash-desk)
|
||||||
* - undetermined: null (chip shows the category alone)
|
* - undetermined: null (chip shows the category alone)
|
||||||
*/
|
*/
|
||||||
readonly roleKind: "entry" | "exit" | "both" | "mixed" | "lane" | "booth" | null;
|
readonly roleKind: "entry" | "exit" | "both" | "mixed" | "lane" | "booth" | "wash" | null;
|
||||||
readonly state: "ready" | "degraded" | "offline";
|
readonly state: "ready" | "degraded" | "offline";
|
||||||
readonly detail?: string;
|
readonly detail?: string;
|
||||||
readonly checkedAt: string; // ISO-8601
|
readonly checkedAt: string; // ISO-8601
|
||||||
@@ -88,6 +89,13 @@ export interface DeviceStatusEvent {
|
|||||||
* gate (it never blocks a ticket or opens a barrier). "busy" is set by a vehicle
|
* gate (it never blocks a ticket or opens a barrier). "busy" is set by a vehicle
|
||||||
* `active` event; it auto-clears to "free" after a timeout (this camera class sends
|
* `active` event; it auto-clears to "free" after a timeout (this camera class sends
|
||||||
* no leave/`inactive` signal — see wiki/entities/lpr-camera.md). */
|
* no leave/`inactive` signal — see wiki/entities/lpr-camera.md). */
|
||||||
|
/** A camera started/stopped seeing a vehicle (per camera, TTL-cleared). Drives the
|
||||||
|
* booth camera tiles' snapshot→live switch. Advisory only. See camera-activity.ts. */
|
||||||
|
export interface CameraActivityEvent {
|
||||||
|
readonly deviceId: string;
|
||||||
|
readonly active: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface LaneStatusEvent {
|
export interface LaneStatusEvent {
|
||||||
readonly entry: boolean; // true = busy (a vehicle is at the entry vicinity)
|
readonly entry: boolean; // true = busy (a vehicle is at the entry vicinity)
|
||||||
readonly exit: boolean; // true = busy (a vehicle is at the exit vicinity)
|
readonly exit: boolean; // true = busy (a vehicle is at the exit vicinity)
|
||||||
@@ -105,6 +113,20 @@ export interface PlateRecognizedEvent {
|
|||||||
readonly direction: "entry" | "exit";
|
readonly direction: "entry" | "exit";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Emitted when vision classified the vehicle in an entry/exit frame (advisory; stored on
|
||||||
|
* the read row like the plate). A module may sample these — the Car Wash review outbox
|
||||||
|
* queues one in N ENTRY reads for the remote reviewer, in the gate view the classifier
|
||||||
|
* will be trained on (wiki/concepts/vision-review-outbox.md). The core emits; it never
|
||||||
|
* knows who listens. */
|
||||||
|
export interface VehicleReadEvent {
|
||||||
|
readonly identity: string;
|
||||||
|
readonly direction: "entry" | "exit";
|
||||||
|
/** The camera that took the frame, so a listener can tell "same car, two reads seconds
|
||||||
|
* apart" (the bridge's pass sample + the ticket entry's read) from two cars. */
|
||||||
|
readonly deviceId?: string;
|
||||||
|
readonly read: VehicleRead;
|
||||||
|
}
|
||||||
|
|
||||||
/** Per-lane RADAR presence — a vehicle-presence INPUT (loop/radar) is shorted at the
|
/** Per-lane RADAR presence — a vehicle-presence INPUT (loop/radar) is shorted at the
|
||||||
* entry/exit barrier, i.e. "something is in the lane vicinity" BEFORE the camera has
|
* entry/exit barrier, i.e. "something is in the lane vicinity" BEFORE the camera has
|
||||||
* confirmed a vehicle. Same signal that makes the physical button lamp (relay 3) blink:
|
* confirmed a vehicle. Same signal that makes the physical button lamp (relay 3) blink:
|
||||||
@@ -178,6 +200,15 @@ class DeviceEventBus extends EventEmitter {
|
|||||||
return () => this.off("lane-status", cb);
|
return () => this.off("lane-status", cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Emitted when a camera's vehicle activity flips (camera-activity.ts). Advisory. */
|
||||||
|
emitCameraActivity(event: CameraActivityEvent): void {
|
||||||
|
this.emit("camera-activity", event);
|
||||||
|
}
|
||||||
|
onCameraActivity(cb: (event: CameraActivityEvent) => void): () => void {
|
||||||
|
this.on("camera-activity", cb);
|
||||||
|
return () => this.off("camera-activity", cb);
|
||||||
|
}
|
||||||
|
|
||||||
/** Emitted whenever a lane's RADAR presence CHANGES (a presence input shorted/cleared
|
/** Emitted whenever a lane's RADAR presence CHANGES (a presence input shorted/cleared
|
||||||
* at an entry/exit barrier). Drives the booth barrier light's blink. Advisory only. */
|
* at an entry/exit barrier). Drives the booth barrier light's blink. Advisory only. */
|
||||||
emitLanePresence(event: LanePresenceEvent): void {
|
emitLanePresence(event: LanePresenceEvent): void {
|
||||||
@@ -197,6 +228,13 @@ class DeviceEventBus extends EventEmitter {
|
|||||||
this.on("plate-recognized", cb);
|
this.on("plate-recognized", cb);
|
||||||
return () => this.off("plate-recognized", cb);
|
return () => this.off("plate-recognized", cb);
|
||||||
}
|
}
|
||||||
|
emitVehicleRead(event: VehicleReadEvent): void {
|
||||||
|
this.emit("vehicle-read", event);
|
||||||
|
}
|
||||||
|
onVehicleRead(cb: (event: VehicleReadEvent) => void): () => void {
|
||||||
|
this.on("vehicle-read", cb);
|
||||||
|
return () => this.off("vehicle-read", cb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Process-wide device event bus. */
|
/** Process-wide device event bus. */
|
||||||
|
|||||||
@@ -27,8 +27,10 @@ const POLL_MS = Number(process.env.DEVICE_POLL_MS ?? 8000);
|
|||||||
// Camera clock sync (Hikvision loses its clock on power cuts — reboots at the 1970
|
// Camera clock sync (Hikvision loses its clock on power cuts — reboots at the 1970
|
||||||
// epoch until a human logs into its web UI). The monitor re-syncs from the HOST
|
// epoch until a human logs into its web UI). The monitor re-syncs from the HOST
|
||||||
// clock (the site's offline time authority) at the offline→ready edge — exactly the
|
// clock (the site's offline time authority) at the offline→ready edge — exactly the
|
||||||
// power-restored moment — plus a daily backstop; drift under the threshold is left
|
// power-restored moment — plus a daily backstop, plus the moment the site's UTC offset
|
||||||
// alone. See wiki/entities/lpr-camera.md (clock sync).
|
// changes (a DST switch: the camera's zone is a FIXED offset the host rewrites, see
|
||||||
|
// HikvisionCamera.syncClock); drift under the threshold is left alone. See
|
||||||
|
// wiki/entities/lpr-camera.md (clock sync).
|
||||||
const CLOCK_SYNC_BACKSTOP_MS = 24 * 60 * 60 * 1000;
|
const CLOCK_SYNC_BACKSTOP_MS = 24 * 60 * 60 * 1000;
|
||||||
const CLOCK_MAX_DRIFT_SEC = 60;
|
const CLOCK_MAX_DRIFT_SEC = 60;
|
||||||
|
|
||||||
@@ -64,7 +66,7 @@ export function localIsoWithOffset(tz: string, at = new Date()): string {
|
|||||||
* - reader/camera → the direction inherited from its bound relay (entry/exit/both)
|
* - reader/camera → the direction inherited from its bound relay (entry/exit/both)
|
||||||
* - access → entry/exit/both from its relays[]; "mixed" if it spans more
|
* - access → entry/exit/both from its relays[]; "mixed" if it spans more
|
||||||
* than one direction; null if it declares none yet
|
* than one direction; null if it declares none yet
|
||||||
* - printer → "lane" (entry-dispenser) | "booth" (booth-receipt)
|
* - printer → "lane" (entry-dispenser) | "booth" (booth-receipt) | "wash" (wash-desk)
|
||||||
*/
|
*/
|
||||||
function roleKindOf(db: Db, row: DeviceRow): DeviceStatusEvent["roleKind"] {
|
function roleKindOf(db: Db, row: DeviceRow): DeviceStatusEvent["roleKind"] {
|
||||||
switch (row.category) {
|
switch (row.category) {
|
||||||
@@ -89,6 +91,7 @@ function roleKindOf(db: Db, row: DeviceRow): DeviceStatusEvent["roleKind"] {
|
|||||||
const role = (row.config as { role?: string }).role;
|
const role = (row.config as { role?: string }).role;
|
||||||
if (role === "booth-receipt") return "booth";
|
if (role === "booth-receipt") return "booth";
|
||||||
if (role === "entry-dispenser") return "lane";
|
if (role === "entry-dispenser") return "lane";
|
||||||
|
if (role === "wash-desk") return "wash";
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -200,18 +203,26 @@ export class DeviceMonitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Camera clock re-sync at the power-restored edge (prev offline/unknown →
|
// Camera clock re-sync at the power-restored edge (prev offline/unknown →
|
||||||
// ready) + a daily backstop. Stamped BEFORE the async attempt so a failing
|
// ready) + a daily backstop + the site's UTC-offset edge (DST switch). Stamped
|
||||||
// camera is retried at backstop cadence, never every poll.
|
// BEFORE the async attempt so a failing camera is retried at backstop cadence,
|
||||||
|
// never every poll.
|
||||||
if (row.category === "camera" && next.state === "ready" && device && isClockSyncable(device)) {
|
if (row.category === "camera" && next.state === "ready" && device && isClockSyncable(device)) {
|
||||||
const prev = this.#latest.get(row.id);
|
const prev = this.#latest.get(row.id);
|
||||||
const cameBack = !prev || prev.state === "offline";
|
const cameBack = !prev || prev.state === "offline";
|
||||||
const last = this.#clockSyncedAt.get(row.id) ?? 0;
|
const last = this.#clockSyncedAt.get(row.id) ?? 0;
|
||||||
if (cameBack || Date.now() - last > CLOCK_SYNC_BACKSTOP_MS) {
|
const nowIso = localIsoWithOffset(siteTz(this.#db));
|
||||||
|
const offset = nowIso.slice(-6);
|
||||||
|
const offsetChanged = this.#clockSyncedOffset.get(row.id) !== offset;
|
||||||
|
if (cameBack || offsetChanged || Date.now() - last > CLOCK_SYNC_BACKSTOP_MS) {
|
||||||
this.#clockSyncedAt.set(row.id, Date.now());
|
this.#clockSyncedAt.set(row.id, Date.now());
|
||||||
|
this.#clockSyncedOffset.set(row.id, offset);
|
||||||
const cam = device;
|
const cam = device;
|
||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const r = await cam.syncClock(localIsoWithOffset(siteTz(this.#db)), CLOCK_MAX_DRIFT_SEC);
|
const r = await cam.syncClock(nowIso, CLOCK_MAX_DRIFT_SEC);
|
||||||
|
if (r.zoneChanged) {
|
||||||
|
this.#log.warn(`device-monitor: camera ${row.id} time zone set to ${r.timeZone ?? "?"} (site offset ${offset})`);
|
||||||
|
}
|
||||||
if (r.synced) {
|
if (r.synced) {
|
||||||
// A large jump is the 1970 power-cut signature — warn (persisted) so
|
// A large jump is the 1970 power-cut signature — warn (persisted) so
|
||||||
// the reboot stays visible; a small correction is routine info.
|
// the reboot stays visible; a small correction is routine info.
|
||||||
@@ -248,6 +259,8 @@ export class DeviceMonitor {
|
|||||||
|
|
||||||
/** Per-camera timestamp of the last clock-sync ATTEMPT (backstop pacing). */
|
/** Per-camera timestamp of the last clock-sync ATTEMPT (backstop pacing). */
|
||||||
readonly #clockSyncedAt = new Map<string, number>();
|
readonly #clockSyncedAt = new Map<string, number>();
|
||||||
|
/** Per-camera site UTC offset ("+02:00") at the last attempt — a change (DST) re-syncs. */
|
||||||
|
readonly #clockSyncedOffset = new Map<string, string>();
|
||||||
|
|
||||||
/** Cache + emit a status, but only when it CHANGED (state or detail). */
|
/** Cache + emit a status, but only when it CHANGED (state or detail). */
|
||||||
#publish(id: string, next: DeviceStatusEvent): void {
|
#publish(id: string, next: DeviceStatusEvent): void {
|
||||||
|
|||||||
@@ -9,9 +9,11 @@ import {
|
|||||||
type PrinterInstance,
|
type PrinterInstance,
|
||||||
type TicketData,
|
type TicketData,
|
||||||
type TicketHeader,
|
type TicketHeader,
|
||||||
|
printerRoleOf,
|
||||||
} from "@parking/devices";
|
} from "@parking/devices";
|
||||||
import { DEFAULT_VEHICLE_CATEGORY, reasonPayload } from "@parking/shared";
|
import { DEFAULT_VEHICLE_CATEGORY, reasonPayload } from "@parking/shared";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import { pulseBarrier } from "./relay-pulse.js";
|
||||||
import type { DeviceInputEvent, LaneStatusEvent } from "./device-events.js";
|
import type { DeviceInputEvent, LaneStatusEvent } from "./device-events.js";
|
||||||
import { getOccupancy } from "./occupancy.js";
|
import { getOccupancy } from "./occupancy.js";
|
||||||
import type { EventLog } from "./event-log.js";
|
import type { EventLog } from "./event-log.js";
|
||||||
@@ -385,7 +387,12 @@ export class EntryFlow {
|
|||||||
const access = this.#buildAccess(resolved.controller);
|
const access = this.#buildAccess(resolved.controller);
|
||||||
let opened = false;
|
let opened = false;
|
||||||
if (access) {
|
if (access) {
|
||||||
await access.pulseOpen(resolved.relay);
|
await pulseBarrier(this.#db, this.#logger, access, resolved, {
|
||||||
|
direction: "entry",
|
||||||
|
identity: ticketId,
|
||||||
|
source: opts.source,
|
||||||
|
...(opts.operator ? { operator: opts.operator } : {}),
|
||||||
|
});
|
||||||
opened = true;
|
opened = true;
|
||||||
} else this.#logger.warn(`entry signed for ${ticketId} but the entry relay won't build`);
|
} else this.#logger.warn(`entry signed for ${ticketId} but the entry relay won't build`);
|
||||||
|
|
||||||
@@ -523,7 +530,7 @@ export class EntryFlow {
|
|||||||
const driver = registry.get(row.driverId);
|
const driver = registry.get(row.driverId);
|
||||||
if (!driver) continue;
|
if (!driver) continue;
|
||||||
const cfg = row.config as Record<string, unknown>;
|
const cfg = row.config as Record<string, unknown>;
|
||||||
const role = cfg.role === "booth-receipt" ? "booth-receipt" : "entry-dispenser";
|
const role = printerRoleOf(cfg);
|
||||||
try {
|
try {
|
||||||
out.push({
|
out.push({
|
||||||
id: row.id,
|
id: row.id,
|
||||||
|
|||||||
@@ -111,6 +111,44 @@ describe("exitForBooth — valid exit signs the vehicle_exit", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("forgiven exit (wiki/decisions/forgiven-exit.md)", () => {
|
||||||
|
function payments(identity: string) {
|
||||||
|
return db.select().from(ledgerEvents).where(eq(ledgerEvents.identity, identity)).all().filter((r) => r.type === "payment");
|
||||||
|
}
|
||||||
|
|
||||||
|
it("ticketed car: $0 payment carrying the forgone fee, then the exit is signed with the forgiven reason", async () => {
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60, gracePeriodExitMin: 0 });
|
||||||
|
await enter("T1", minutesAgo(90)); // 2 increments → 20000 owed
|
||||||
|
const f = await pay.forgive("T1", { operator: "owner", note: "regular customer, printer jammed" });
|
||||||
|
expect(f).toMatchObject({ ok: true, forgoneMinor: 20000 });
|
||||||
|
const p = payments("T1");
|
||||||
|
expect(p).toHaveLength(1);
|
||||||
|
expect(p[0]!.payload).toMatchObject({ amountMinor: 0, forgoneMinor: 20000, operator: "owner", reasonCode: "exit.forgiven" });
|
||||||
|
expect((p[0]!.payload as { reason: string }).reason).toContain("regular customer, printer jammed");
|
||||||
|
|
||||||
|
// Even with a 0-minute walk-back grace, the forgiven re-open goes through.
|
||||||
|
const r = await exit.reopenBarrier("T1", "owner", { forgiven: { note: "regular customer, printer jammed", forgoneMinor: 20000, currency: "ALL" } });
|
||||||
|
expect(r.ok).toBe(true);
|
||||||
|
expect(exitsSigned("T1")).toHaveLength(1);
|
||||||
|
expect(exitsSigned("T1")[0]!.payload).toMatchObject({ reasonCode: "exit.forgiven" });
|
||||||
|
expect(anomalies("manual exit by owner")).toHaveLength(1);
|
||||||
|
expect(anomalies("manual exit by owner")[0]!.payload).toMatchObject({ forgiven: true, forgoneMinor: 20000, operator: "owner" });
|
||||||
|
// The plain unpaid re-open path is still refused for the NEXT unpaid car.
|
||||||
|
await enter("T2", minutesAgo(90));
|
||||||
|
expect((await exit.reopenBarrier("T2", "owner")).ok).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("no session: a signed anomaly under a fresh forgiven:<id> ref, no exit, no payment", async () => {
|
||||||
|
const r = await exit.forgiveExitNoSession("owner", "came in while the barriers were up");
|
||||||
|
expect(r.ok).toBe(true);
|
||||||
|
const a = anomalies("no ticket / no session");
|
||||||
|
expect(a).toHaveLength(1);
|
||||||
|
expect(a[0]!.identity).toMatch(/^forgiven:/);
|
||||||
|
expect(a[0]!.payload).toMatchObject({ reasonCode: "exit.forgiven.noSession", forgiven: true, operator: "owner" });
|
||||||
|
expect(db.select().from(ledgerEvents).all().filter((e) => e.type === "payment" || e.type === "vehicle_exit")).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("reopenBarrier — no unpaid re-open", () => {
|
describe("reopenBarrier — no unpaid re-open", () => {
|
||||||
it("refuses to re-open an unpaid transient session", async () => {
|
it("refuses to re-open an unpaid transient session", async () => {
|
||||||
seedTariff(db, { pricePerIncrementMinor: 10000 });
|
seedTariff(db, { pricePerIncrementMinor: 10000 });
|
||||||
|
|||||||
+103
-16
@@ -1,5 +1,7 @@
|
|||||||
|
import { randomUUID } from "node:crypto";
|
||||||
import { desc, eq, ledgerEvents, sessions, tariffVersions, tariffs, type Db, type DeviceRow } from "@parking/db";
|
import { desc, eq, ledgerEvents, sessions, tariffVersions, tariffs, type Db, type DeviceRow } from "@parking/db";
|
||||||
import { registry, type AccessControlDevice } from "@parking/devices";
|
import { registry, type AccessControlDevice } from "@parking/devices";
|
||||||
|
import { pulseBarrier } from "./relay-pulse.js";
|
||||||
import { firstRelayByDirection, type ResolvedRelay } from "./device-resolve.js";
|
import { firstRelayByDirection, type ResolvedRelay } from "./device-resolve.js";
|
||||||
import { plateForIdentity, platesForIdentities } from "./plate-lookup.js";
|
import { plateForIdentity, platesForIdentities } from "./plate-lookup.js";
|
||||||
import { snapshotAsync } from "./snapshot.js";
|
import { snapshotAsync } from "./snapshot.js";
|
||||||
@@ -63,6 +65,14 @@ export type BoothReopenResult =
|
|||||||
| { ok: false; reason: string }
|
| { ok: false; reason: string }
|
||||||
| { ok: true; opened: boolean; reason?: string };
|
| { ok: true; opened: boolean; reason?: string };
|
||||||
|
|
||||||
|
/** A forgiven exit's audit facts (wiki/decisions/forgiven-exit.md): the operator's
|
||||||
|
* mandatory free-text note and the fee the $0 payment just waived. */
|
||||||
|
export interface ForgivenExit {
|
||||||
|
readonly note: string;
|
||||||
|
readonly forgoneMinor: number;
|
||||||
|
readonly currency: string;
|
||||||
|
}
|
||||||
|
|
||||||
/** Minimum ANPR confidence for a plate to participate in swap reconciliation, both for the
|
/** Minimum ANPR confidence for a plate to participate in swap reconciliation, both for the
|
||||||
* exiting read and the matched open session's entry read. Below this, the read is advisory-
|
* exiting read and the matched open session's entry read. Below this, the read is advisory-
|
||||||
* only and never triggers a swap warning (a fuzzy read must not block a legit car). */
|
* only and never triggers a swap warning (a fuzzy read must not block a legit car). */
|
||||||
@@ -201,7 +211,12 @@ export class ExitFlow {
|
|||||||
return { ok: true, opened: false, reason: renderReasonEn("exit.open.unavailable") };
|
return { ok: true, opened: false, reason: renderReasonEn("exit.open.unavailable") };
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await access.pulseOpen(resolved.relay);
|
await pulseBarrier(this.#db, this.#logger, access, resolved, {
|
||||||
|
direction: "exit",
|
||||||
|
identity: id,
|
||||||
|
source: "booth",
|
||||||
|
...(opts?.operator ? { operator: opts.operator } : {}),
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.#openFailedAnomaly(id, `pulseOpen failed: ${(err as Error).message}`);
|
await this.#openFailedAnomaly(id, `pulseOpen failed: ${(err as Error).message}`);
|
||||||
return { ok: true, opened: false, reason: renderReasonEn("exit.open.failed") };
|
return { ok: true, opened: false, reason: renderReasonEn("exit.open.failed") };
|
||||||
@@ -234,9 +249,10 @@ export class ExitFlow {
|
|||||||
* second exit (that would double-count occupancy): anomaly only, as before.
|
* second exit (that would double-count occupancy): anomaly only, as before.
|
||||||
* See wiki/concepts/booth-exit-flow.md.
|
* See wiki/concepts/booth-exit-flow.md.
|
||||||
*/
|
*/
|
||||||
async reopenBarrier(identity: string, operator?: string): Promise<BoothReopenResult> {
|
async reopenBarrier(identity: string, operator?: string, opts?: { forgiven?: ForgivenExit }): Promise<BoothReopenResult> {
|
||||||
const id = identity.trim();
|
const id = identity.trim();
|
||||||
if (!id) return { ok: false, reason: "ticket id required" };
|
if (!id) return { ok: false, reason: "ticket id required" };
|
||||||
|
const forgiven = opts?.forgiven ?? null;
|
||||||
|
|
||||||
const view = this.#sessionFor(id);
|
const view = this.#sessionFor(id);
|
||||||
if (!view) return { ok: false, reason: "no session for ticket" };
|
if (!view) return { ok: false, reason: "no session for ticket" };
|
||||||
@@ -251,7 +267,10 @@ export class ExitFlow {
|
|||||||
const paid = view.paidAt != null;
|
const paid = view.paidAt != null;
|
||||||
const withinGrace =
|
const withinGrace =
|
||||||
paid && view.graceExitMin != null && Date.now() - Date.parse(view.paidAt!) <= view.graceExitMin * 60_000;
|
paid && view.graceExitMin != null && Date.now() - Date.parse(view.paidAt!) <= view.graceExitMin * 60_000;
|
||||||
if (!view.subscription && (!paid || !withinGrace)) {
|
// FORGIVEN exit (barrier:create): the $0 payment carrying the forgone fee was just
|
||||||
|
// signed by PayStation.forgive(); the walk-back check is moot (and a 0-minute grace
|
||||||
|
// tariff must not refuse it). The audit lives on the anomaly + exit below.
|
||||||
|
if (!forgiven && !view.subscription && (!paid || !withinGrace)) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
reason: paid ? "walk-back grace expired — take a top-up payment first" : "session not paid — no barrier open without payment",
|
reason: paid ? "walk-back grace expired — take a top-up payment first" : "session not paid — no barrier open without payment",
|
||||||
@@ -269,9 +288,10 @@ export class ExitFlow {
|
|||||||
type: "anomaly",
|
type: "anomaly",
|
||||||
identity: id,
|
identity: id,
|
||||||
payload: {
|
payload: {
|
||||||
...reasonPayload("exit.manualOpen"),
|
...(forgiven ? reasonPayload("exit.forgiven", this.#forgivenParams(operator, forgiven)) : reasonPayload("exit.manualOpen")),
|
||||||
source: "booth",
|
source: "booth",
|
||||||
barrierReopen: true,
|
barrierReopen: true,
|
||||||
|
...(forgiven ? { forgiven: true, forgoneMinor: forgiven.forgoneMinor, currency: forgiven.currency } : {}),
|
||||||
...(operator ? { operator } : {}),
|
...(operator ? { operator } : {}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -280,32 +300,90 @@ export class ExitFlow {
|
|||||||
// session leaves the active list + occupancy settles. Skip when already exited
|
// session leaves the active list + occupancy settles. Skip when already exited
|
||||||
// (no double-count). Recorded as a human-intervention exit for the audit trail.
|
// (no double-count). Recorded as a human-intervention exit for the audit trail.
|
||||||
if (view.open) {
|
if (view.open) {
|
||||||
await this.#signExit(id, "manual");
|
await this.#signExit(id, "manual", forgiven ? reasonPayload("exit.forgiven", this.#forgivenParams(operator, forgiven)) : undefined);
|
||||||
this.#closeSessionCache(id);
|
this.#closeSessionCache(id);
|
||||||
this.#fireExitSnapshot(id);
|
this.#fireExitSnapshot(id);
|
||||||
this.#logger.info(`barrier re-open also closed open session ${id} (human-intervention exit)`);
|
this.#logger.info(`barrier re-open also closed open session ${id} (human-intervention exit)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const r = await this.#pulseExit(resolved, id, operator, `barrier re-open for ${id}`);
|
||||||
|
if (r.opened) {
|
||||||
|
if (forgiven) this.#logger.warn(`FORGIVEN exit barrier open for ${id} by ${operator ?? "?"}: ${forgiven.note}`);
|
||||||
|
else this.#logger.info(`manual barrier open for ${id}${operator ? ` by ${operator}` : ""}`);
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
} finally {
|
||||||
|
this.#inFlight.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FORGIVEN exit for a car with NO ticket / no session (it came in while the barriers
|
||||||
|
* were up, lost everything, …). Nothing to forgive and nothing to close: a signed,
|
||||||
|
* attributed anomaly with the exit snapshot as evidence, then the pulse. The anomaly's
|
||||||
|
* identity is a fresh `forgiven:<id>` ref so the live feed can show its snapshot.
|
||||||
|
* See wiki/decisions/forgiven-exit.md.
|
||||||
|
*/
|
||||||
|
async forgiveExitNoSession(operator: string, note: string): Promise<BoothReopenResult> {
|
||||||
|
const key = "forgive:nosession";
|
||||||
|
if (this.#inFlight.has(key)) return { ok: false, reason: "a forgiven open is already in progress" };
|
||||||
|
this.#inFlight.add(key);
|
||||||
|
try {
|
||||||
|
const ref = `forgiven:${randomUUID()}`;
|
||||||
|
const resolved = firstRelayByDirection(this.#db, "exit");
|
||||||
|
await this.#log.append({
|
||||||
|
type: "anomaly",
|
||||||
|
identity: ref,
|
||||||
|
payload: {
|
||||||
|
...reasonPayload("exit.forgiven.noSession", { operator, note: note || "(no reason given)" }),
|
||||||
|
source: "booth",
|
||||||
|
barrierReopen: true,
|
||||||
|
forgiven: true,
|
||||||
|
operator,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
this.#fireExitSnapshot(ref);
|
||||||
|
const r = await this.#pulseExit(resolved, ref, operator, `forgiven open (no session) ${ref}`);
|
||||||
|
if (r.opened) this.#logger.warn(`FORGIVEN exit barrier open with no session by ${operator}: ${note}`);
|
||||||
|
return r;
|
||||||
|
} finally {
|
||||||
|
this.#inFlight.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Drive the exit relay for a human-intervention open; the exit/anomaly is already
|
||||||
|
* signed, so every failure is `ok: true, opened: false` + the reason to open by hand. */
|
||||||
|
async #pulseExit(
|
||||||
|
resolved: ResolvedRelay | null,
|
||||||
|
identity: string,
|
||||||
|
operator: string | undefined,
|
||||||
|
label: string,
|
||||||
|
): Promise<Extract<BoothReopenResult, { ok: true }>> {
|
||||||
if (!resolved) {
|
if (!resolved) {
|
||||||
this.#logger.warn(`barrier re-open for ${id}: no exit relay configured`);
|
this.#logger.warn(`${label}: no exit relay configured`);
|
||||||
return { ok: true, opened: false, reason: renderReasonEn("exit.open.noBarrier") };
|
return { ok: true, opened: false, reason: renderReasonEn("exit.open.noBarrier") };
|
||||||
}
|
}
|
||||||
const access = this.#buildAccess(resolved.controller);
|
const access = this.#buildAccess(resolved.controller);
|
||||||
if (!access) {
|
if (!access) {
|
||||||
this.#logger.warn(`barrier re-open for ${id}: exit controller would not build`);
|
this.#logger.warn(`${label}: exit controller would not build`);
|
||||||
return { ok: true, opened: false, reason: renderReasonEn("exit.open.unavailable") };
|
return { ok: true, opened: false, reason: renderReasonEn("exit.open.unavailable") };
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await access.pulseOpen(resolved.relay);
|
await pulseBarrier(this.#db, this.#logger, access, resolved, {
|
||||||
|
direction: "exit",
|
||||||
|
identity,
|
||||||
|
source: "booth",
|
||||||
|
...(operator ? { operator } : {}),
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.#logger.error(`barrier re-open pulseOpen failed (${id}): ${(err as Error).message}`);
|
this.#logger.error(`${label}: pulseOpen failed: ${(err as Error).message}`);
|
||||||
return { ok: true, opened: false, reason: renderReasonEn("exit.open.failed") };
|
return { ok: true, opened: false, reason: renderReasonEn("exit.open.failed") };
|
||||||
}
|
}
|
||||||
this.#logger.info(`manual barrier open for ${id}${operator ? ` by ${operator}` : ""}`);
|
|
||||||
return { ok: true, opened: true };
|
return { ok: true, opened: true };
|
||||||
} finally {
|
|
||||||
this.#inFlight.delete(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#forgivenParams(operator: string | undefined, f: ForgivenExit): Record<string, string | number> {
|
||||||
|
return { operator: operator ?? "?", forgone: (f.forgoneMinor / 100).toFixed(2), currency: f.currency, note: f.note || "(no reason given)" };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handle a transient-ticket read at an exit barrier (the relay pre-resolved by the
|
/** Handle a transient-ticket read at an exit barrier (the relay pre-resolved by the
|
||||||
@@ -411,8 +489,13 @@ export class ExitFlow {
|
|||||||
await this.#signExit(e.value, e.kind === "plate" ? "lpr" : "ticket");
|
await this.#signExit(e.value, e.kind === "plate" ? "lpr" : "ticket");
|
||||||
|
|
||||||
const access = this.#buildAccess(resolved.controller);
|
const access = this.#buildAccess(resolved.controller);
|
||||||
if (access) await access.pulseOpen(resolved.relay);
|
if (access) {
|
||||||
else this.#logger.warn(`exit signed for ${e.value} but the exit relay won't build`);
|
await pulseBarrier(this.#db, this.#logger, access, resolved, {
|
||||||
|
direction: "exit",
|
||||||
|
identity: e.value,
|
||||||
|
source: e.kind === "plate" ? "lpr" : "ticket",
|
||||||
|
});
|
||||||
|
} else this.#logger.warn(`exit signed for ${e.value} but the exit relay won't build`);
|
||||||
|
|
||||||
this.#fireExitSnapshot(e.value);
|
this.#fireExitSnapshot(e.value);
|
||||||
this.#closeSessionCache(e.value);
|
this.#closeSessionCache(e.value);
|
||||||
@@ -422,7 +505,11 @@ export class ExitFlow {
|
|||||||
/** Append the signed vehicle_exit. `source`: "ticket" (booth/reader), "lpr" (plate),
|
/** Append the signed vehicle_exit. `source`: "ticket" (booth/reader), "lpr" (plate),
|
||||||
* or "manual" (a human-intervention barrier re-open that closes an open session —
|
* or "manual" (a human-intervention barrier re-open that closes an open session —
|
||||||
* see reopenBarrier). */
|
* see reopenBarrier). */
|
||||||
async #signExit(identity: string, source: "ticket" | "lpr" | "manual" = "ticket"): Promise<void> {
|
async #signExit(
|
||||||
|
identity: string,
|
||||||
|
source: "ticket" | "lpr" | "manual" = "ticket",
|
||||||
|
reason?: ReturnType<typeof reasonPayload>,
|
||||||
|
): Promise<void> {
|
||||||
await this.#log.append({
|
await this.#log.append({
|
||||||
type: "vehicle_exit",
|
type: "vehicle_exit",
|
||||||
direction: "exit",
|
direction: "exit",
|
||||||
@@ -430,7 +517,7 @@ export class ExitFlow {
|
|||||||
identity,
|
identity,
|
||||||
payload: {
|
payload: {
|
||||||
sessionRef: identity,
|
sessionRef: identity,
|
||||||
...(source === "manual" ? reasonPayload("exit.manualOpen") : {}),
|
...(reason ?? (source === "manual" ? reasonPayload("exit.manualOpen") : {})),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,221 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { type Db } from "@parking/db";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { buildServer } from "./server.js";
|
||||||
|
import { seedUser, login } from "./test-helpers.js";
|
||||||
|
|
||||||
|
// Venue modules — entitled ∩ activated, enforced server-side (wiki/decisions/
|
||||||
|
// venue-modules.md). Boots the real app over an in-memory DB and drives it with
|
||||||
|
// app.inject, like routes.test.ts.
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
const savedEnv = process.env.MODULES_ENTITLED;
|
||||||
|
|
||||||
|
async function boot(): Promise<void> {
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
app = await buildServer({ db });
|
||||||
|
await app.ready();
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
delete process.env.MODULES_ENTITLED;
|
||||||
|
await boot();
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
if (savedEnv === undefined) delete process.env.MODULES_ENTITLED;
|
||||||
|
else process.env.MODULES_ENTITLED = savedEnv;
|
||||||
|
});
|
||||||
|
|
||||||
|
async function admin() {
|
||||||
|
const { username, password } = await seedUser(db, { username: "boss", roleId: "admin" });
|
||||||
|
return login(app, username, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("defaults (no env, nothing activated)", () => {
|
||||||
|
it("every registered module is entitled, activated and effective; /me carries the set", async () => {
|
||||||
|
const { cookie } = await admin();
|
||||||
|
const cfg = await app.inject({ method: "GET", url: "/api/site-config", headers: { cookie } });
|
||||||
|
expect(cfg.statusCode).toBe(200);
|
||||||
|
const body = cfg.json();
|
||||||
|
expect(body.modulesEntitled).toEqual(["parking", "validation", "carwash"]);
|
||||||
|
expect(body.modulesActivated).toEqual(["parking", "validation", "carwash"]);
|
||||||
|
expect(body.modules).toEqual(["parking", "validation", "carwash"]);
|
||||||
|
|
||||||
|
const me = await app.inject({ method: "GET", url: "/api/auth/me", headers: { cookie } });
|
||||||
|
expect(me.json().modules).toEqual(["parking", "validation", "carwash"]);
|
||||||
|
|
||||||
|
// A module route answers normally while the module is on.
|
||||||
|
const programs = await app.inject({ method: "GET", url: "/api/validation/programs", headers: { cookie } });
|
||||||
|
expect(programs.statusCode).toBe(200);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("activation (site admin)", () => {
|
||||||
|
it("deactivating validation 403s its routes with module_disabled, signs a config_change, and is reversible", async () => {
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
const put = await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: ["parking"] },
|
||||||
|
});
|
||||||
|
expect(put.statusCode).toBe(200);
|
||||||
|
expect(put.json().modules).toEqual(["parking"]);
|
||||||
|
expect(put.json().modulesActivated).toEqual(["parking"]);
|
||||||
|
|
||||||
|
// The merchant scan routes are the module → 403; the PROGRAM routes are core (the
|
||||||
|
// discount engine serves Car Wash too) → still 200 with validation off.
|
||||||
|
const off = await app.inject({ method: "GET", url: "/api/validation/mine", headers: { cookie } });
|
||||||
|
expect(off.statusCode).toBe(403);
|
||||||
|
expect(off.json().code).toBe("module_disabled");
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/validation/programs", headers: { cookie } })).statusCode).toBe(200);
|
||||||
|
|
||||||
|
const me = await app.inject({ method: "GET", url: "/api/auth/me", headers: { cookie } });
|
||||||
|
expect(me.json().modules).toEqual(["parking"]);
|
||||||
|
|
||||||
|
// The flip is on the signed ledger, attributed.
|
||||||
|
const events = await app.inject({ method: "GET", url: "/api/events?limit=50", headers: { cookie } });
|
||||||
|
expect(events.statusCode).toBe(200);
|
||||||
|
const list = (events.json().events ?? events.json()) as Array<{ type: string; payload: Record<string, unknown> }>;
|
||||||
|
const flip = list.find((e) => e.type === "config_change" && e.payload?.setting === "modules.validation");
|
||||||
|
expect(flip).toBeTruthy();
|
||||||
|
expect(flip!.payload).toMatchObject({ value: false, prev: true, operator: "boss" });
|
||||||
|
|
||||||
|
// Nothing was deleted: re-enable and the route is back.
|
||||||
|
const back = await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: ["parking", "validation"] },
|
||||||
|
});
|
||||||
|
expect(back.json().modules).toEqual(["parking", "validation"]);
|
||||||
|
const on = await app.inject({ method: "GET", url: "/api/validation/programs", headers: { cookie } });
|
||||||
|
expect(on.statusCode).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("required modules cannot be deactivated (parking is always included)", async () => {
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
const put = await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: [] },
|
||||||
|
});
|
||||||
|
expect(put.statusCode).toBe(200);
|
||||||
|
expect(put.json().modules).toEqual(["parking"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects unknown ids with 400", async () => {
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
const put = await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: ["parking", "bar"] },
|
||||||
|
});
|
||||||
|
expect(put.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("carwash runs without the validation module (the discount engine is core)", async () => {
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
const put = await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: ["parking", "carwash"] },
|
||||||
|
});
|
||||||
|
expect(put.statusCode).toBe(200);
|
||||||
|
expect(put.json().modules).toEqual(["parking", "carwash"]);
|
||||||
|
// The wash's sponsorship program is still composable and readable.
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/validation/programs", headers: { cookie } })).statusCode).toBe(200);
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/carwash/settings", headers: { cookie } })).statusCode).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("dependency rule: a module cannot be on while a module it depends on is off", async () => {
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
// Every non-required module depends on parking, and parking is required — so the rule
|
||||||
|
// is exercised through the effective-set helper directly.
|
||||||
|
const shared = await import("@parking/shared");
|
||||||
|
expect(shared.resolveModuleActivation(["parking", "validation", "carwash"], ["carwash"])).toMatchObject({ ok: true });
|
||||||
|
expect(shared.effectiveModules(["parking", "carwash"], ["parking", "carwash"])).toEqual(["parking", "carwash"]);
|
||||||
|
expect(cookie && csrf).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a no-op resave signs nothing", async () => {
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
const before = await app.inject({ method: "GET", url: "/api/events?limit=50", headers: { cookie } });
|
||||||
|
const countBefore = ((before.json().events ?? before.json()) as unknown[]).length;
|
||||||
|
await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: ["parking", "validation", "carwash"] },
|
||||||
|
});
|
||||||
|
const after = await app.inject({ method: "GET", url: "/api/events?limit=50", headers: { cookie } });
|
||||||
|
expect(((after.json().events ?? after.json()) as unknown[]).length).toBe(countBefore);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("entitlement (vendor env)", () => {
|
||||||
|
it("MODULES_ENTITLED=parking: validation is neither offered nor activatable, and its routes 403", async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
process.env.MODULES_ENTITLED = "parking";
|
||||||
|
await boot();
|
||||||
|
const { cookie, csrf } = await admin();
|
||||||
|
|
||||||
|
const cfg = await app.inject({ method: "GET", url: "/api/site-config", headers: { cookie } });
|
||||||
|
expect(cfg.json().modulesEntitled).toEqual(["parking"]);
|
||||||
|
expect(cfg.json().modules).toEqual(["parking"]);
|
||||||
|
|
||||||
|
const put = await app.inject({
|
||||||
|
method: "PUT", url: "/api/site-config",
|
||||||
|
headers: { cookie, "x-csrf-token": csrf },
|
||||||
|
payload: { modules: ["parking", "validation"] },
|
||||||
|
});
|
||||||
|
expect(put.statusCode).toBe(400);
|
||||||
|
expect(put.json().error).toMatch(/not entitled/);
|
||||||
|
|
||||||
|
const off = await app.inject({ method: "GET", url: "/api/validation/mine", headers: { cookie } });
|
||||||
|
expect(off.statusCode).toBe(403);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("required modules are entitled even when the env omits them; unknown ids are ignored", async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
process.env.MODULES_ENTITLED = "validation,bogus";
|
||||||
|
await boot();
|
||||||
|
const { cookie } = await admin();
|
||||||
|
const cfg = await app.inject({ method: "GET", url: "/api/site-config", headers: { cookie } });
|
||||||
|
expect(cfg.json().modulesEntitled).toEqual(["parking", "validation"]);
|
||||||
|
expect(cfg.json().modules).toEqual(["parking", "validation"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("permissions matrix helpers (venue-modules.md §Permissions matrix)", async () => {
|
||||||
|
const shared = await import("@parking/shared");
|
||||||
|
it("each till is guarded by its own module's permissions", () => {
|
||||||
|
expect(shared.tillGuards("booth")).toEqual({ read: "shift:read", shift: "shift:create", cash: "drawer:create" });
|
||||||
|
expect(shared.tillGuards("carwash")).toEqual({ read: "carwash:read", shift: "carwash:cash", cash: "carwash:cash" });
|
||||||
|
const wash = new Set(["carwash:read", "carwash:cash"]);
|
||||||
|
expect(shared.tillsFor(["parking", "validation", "carwash"], (p) => wash.has(p))).toEqual(["carwash"]);
|
||||||
|
expect(shared.tillsFor(["parking", "validation", "carwash"], (p) => wash.has(p), "shift")).toEqual(["carwash"]);
|
||||||
|
expect(shared.tillsFor(["parking", "validation", "carwash"], (p) => p === "carwash:read", "shift")).toEqual([]);
|
||||||
|
// Module off → its till is not even addressable.
|
||||||
|
expect(shared.tillsFor(["parking"], () => true)).toEqual(["booth"]);
|
||||||
|
});
|
||||||
|
it("the live feed admits by watch permission and filters ledger events by their module", () => {
|
||||||
|
expect(shared.watchPermissions(["parking", "validation", "carwash"])).toEqual(
|
||||||
|
expect.arrayContaining(["event:read", "session:read", "device:read", "carwash:read"]),
|
||||||
|
);
|
||||||
|
expect(shared.watchPermissions(["parking", "validation", "carwash"])).not.toContain("report:read");
|
||||||
|
expect(shared.watchPermissions(["parking"])).not.toContain("carwash:read");
|
||||||
|
expect(shared.feedPermissionFor("carwash_payment")).toBe("carwash:read");
|
||||||
|
expect(shared.feedPermissionFor("payment")).toBe("event:read");
|
||||||
|
expect(shared.feedPermissionFor("validation")).toBe("event:read");
|
||||||
|
});
|
||||||
|
it("every job's permissions exist in the grid", () => {
|
||||||
|
for (const m of shared.MODULES) for (const j of m.jobs) for (const p of j.permissions) expect(shared.PERMISSIONS).toContain(p);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||||
|
import { eq, siteConfig, type Db } from "@parking/db";
|
||||||
|
import {
|
||||||
|
effectiveModules,
|
||||||
|
isModuleId,
|
||||||
|
isTillId,
|
||||||
|
parseEntitledModules,
|
||||||
|
tillGuards,
|
||||||
|
tillsFor,
|
||||||
|
tillsOf,
|
||||||
|
type ModuleId,
|
||||||
|
type TillGuards,
|
||||||
|
type TillId,
|
||||||
|
} from "@parking/shared";
|
||||||
|
import { requireAuth, roleHasPermissions } from "./auth.js";
|
||||||
|
|
||||||
|
declare module "fastify" {
|
||||||
|
interface FastifyRequest {
|
||||||
|
/** Set by requireTill(): the till this request addresses (already authorized). */
|
||||||
|
till?: TillId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Venue modules — the server side of "entitled ∩ activated" (registry + rules live in
|
||||||
|
// @parking/shared; design in wiki/decisions/venue-modules.md).
|
||||||
|
//
|
||||||
|
// entitled MODULES_ENTITLED env (vendor, Komodo stack) — unset = everything.
|
||||||
|
// activated site_config.modules_json (site admin, Setup → Site) — null = everything
|
||||||
|
// entitled.
|
||||||
|
// effective what requireModule() enforces and what /api/auth/me + /api/site-config
|
||||||
|
// hand the SPA so it can hide nav. The web only HIDES; this file ENFORCES.
|
||||||
|
//
|
||||||
|
// Both inputs are re-read per request: one env read and one single-row SELECT on the
|
||||||
|
// site_config singleton — cheap, and it means a change takes effect on the next request
|
||||||
|
// with no cache to invalidate (the same reason the presence-bypass flags aren't cached).
|
||||||
|
|
||||||
|
/** The modules this deployment is entitled to. Unknown ids in the env are ignored
|
||||||
|
* (logged once at boot by registerModules). */
|
||||||
|
export function entitledModules(): ModuleId[] {
|
||||||
|
return parseEntitledModules(process.env.MODULES_ENTITLED).entitled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parse the persisted activation list off a site_config row. null = never set. A
|
||||||
|
* corrupt/unknown value is treated as "never set" rather than locking modules off. */
|
||||||
|
export function activatedModulesOf(row: { modulesJson?: string | null } | undefined): ModuleId[] | null {
|
||||||
|
const raw = row?.modulesJson;
|
||||||
|
if (raw == null) return null;
|
||||||
|
try {
|
||||||
|
const parsed: unknown = JSON.parse(raw);
|
||||||
|
if (!Array.isArray(parsed)) return null;
|
||||||
|
return parsed.filter(isModuleId);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The effective set for this site right now. */
|
||||||
|
export function effectiveModulesFor(db: Db): ModuleId[] {
|
||||||
|
const row = db.select({ modulesJson: siteConfig.modulesJson }).from(siteConfig).where(eq(siteConfig.id, 1)).get();
|
||||||
|
return effectiveModules(entitledModules(), activatedModulesOf(row));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The tills available at this site right now: the booth, plus each effective
|
||||||
|
* money-taking module's own till (registry order). */
|
||||||
|
export function effectiveTillsFor(db: Db): TillId[] {
|
||||||
|
return tillsOf(effectiveModulesFor(db));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The tills a role may SEE (default) or WORK (`shift` / `cash`) here: the effective
|
||||||
|
* tills whose module guard the role holds (each desk's money is guarded by that desk's
|
||||||
|
* own permissions — venue-modules.md §"Permissions matrix"). */
|
||||||
|
export function tillsReadableBy(db: Db, roleId: string, kind: keyof TillGuards = "read"): TillId[] {
|
||||||
|
return tillsFor(effectiveModulesFor(db), (p) => roleHasPermissions(roleId, [p]), kind);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** preHandler factory for the shift/drawer routes: authenticate, parse the `till`
|
||||||
|
* (query on GET, body on POST; absent = booth; 400 `bad_till` when unknown or its
|
||||||
|
* module is off), then require the role to hold THAT TILL's guard for `kind` (403
|
||||||
|
* `till_forbidden`). The authorized till lands on `req.till`. The permission is thus
|
||||||
|
* resolved from the till, never fixed: the booth checks `shift:read`/`shift:create`/
|
||||||
|
* `drawer:create`, the wash `carwash:read`/`carwash:cash`. */
|
||||||
|
export function requireTill(db: Db, kind: keyof TillGuards, from: "query" | "body") {
|
||||||
|
return async (req: FastifyRequest, reply: FastifyReply): Promise<void | FastifyReply> => {
|
||||||
|
await requireAuth(req, reply);
|
||||||
|
const raw = from === "query" ? (req.query as { till?: unknown } | undefined)?.till : (req.body as { till?: unknown } | undefined)?.till;
|
||||||
|
const till = parseTill(db, raw);
|
||||||
|
if (!till) {
|
||||||
|
await reply.code(400).send({ error: "unknown till", code: "bad_till" });
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
if (!roleHasPermissions(req.user.roleId, [tillGuards(till)[kind]])) {
|
||||||
|
await reply
|
||||||
|
.code(403)
|
||||||
|
.send({ error: `your role cannot ${kind === "read" ? "see" : "work"} the ${till} till`, code: "till_forbidden", till });
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
req.till = till;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parse a till from a query/body value. Absent/blank = the booth. Unknown, or a till
|
||||||
|
* whose module is not effective here, → null (the caller answers 400). */
|
||||||
|
export function parseTill(db: Db, raw: unknown): TillId | null {
|
||||||
|
if (raw == null || raw === "") return "booth";
|
||||||
|
if (!isTillId(raw)) return null;
|
||||||
|
return effectiveTillsFor(db).includes(raw) ? raw : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** preHandler: reject the call when `id` is not effective at this site. Compose it
|
||||||
|
* BEFORE requirePermission in a preHandler array so a disabled module answers the
|
||||||
|
* same way for every role — 403 with code "module_disabled" — and never reaches
|
||||||
|
* the permission/CSRF path. */
|
||||||
|
export function requireModule(db: Db, id: ModuleId) {
|
||||||
|
return async (_req: FastifyRequest, _reply: FastifyReply): Promise<void> => {
|
||||||
|
if (!effectiveModulesFor(db).includes(id)) {
|
||||||
|
throw Object.assign(new Error(`module disabled: ${id}`), { statusCode: 403, code: "module_disabled" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,646 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { deviceEvents, type Db } from "@parking/db";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { buildServer } from "../../server.js";
|
||||||
|
import { login, makeLog, minutesAgo, seedTariff, seedUser } from "../../test-helpers.js";
|
||||||
|
|
||||||
|
// Car Wash module, end to end over the real app (wiki/decisions/venue-modules.md):
|
||||||
|
// settings → intake against an open parking session → done applies the sponsorship
|
||||||
|
// validation → bay payment settles the parking session at zero (what the exit reader
|
||||||
|
// checks) / booth payment carries the wash as a charge line → module off = 403.
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
delete process.env.MODULES_ENTITLED;
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
app = await buildServer({ db });
|
||||||
|
await app.ready();
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
|
||||||
|
type Auth = { cookie: string; csrf: string };
|
||||||
|
const hdrs = (a: Auth) => ({ cookie: a.cookie, "x-csrf-token": a.csrf });
|
||||||
|
|
||||||
|
async function admin(): Promise<Auth> {
|
||||||
|
const { username, password } = await seedUser(db, { username: "boss", roleId: "admin" });
|
||||||
|
return login(app, username, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An open transient session that has been parked long enough to owe money. */
|
||||||
|
async function openSession(identity: string, enteredMinutesAgo = 90): Promise<void> {
|
||||||
|
await makeLog(db).append({
|
||||||
|
type: "vehicle_entry",
|
||||||
|
source: "manual",
|
||||||
|
identity,
|
||||||
|
occurredAt: minutesAgo(enteredMinutesAgo),
|
||||||
|
payload: { sessionRef: identity, category: "default" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function seedSettings(a: Auth) {
|
||||||
|
const res = await app.inject({
|
||||||
|
method: "PUT", url: "/api/carwash/settings", headers: hdrs(a),
|
||||||
|
payload: {
|
||||||
|
categories: [{ name: "Car" }, { name: "SUV" }],
|
||||||
|
services: [{ name: "Standard" }, { name: "Inside" }],
|
||||||
|
prices: [],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
const s = res.json();
|
||||||
|
const car = s.categories.find((c: { name: string }) => c.name === "Car").id;
|
||||||
|
const suv = s.categories.find((c: { name: string }) => c.name === "SUV").id;
|
||||||
|
const std = s.services.find((c: { name: string }) => c.name === "Standard").id;
|
||||||
|
const inside = s.services.find((c: { name: string }) => c.name === "Inside").id;
|
||||||
|
const priced = await app.inject({
|
||||||
|
method: "PUT", url: "/api/carwash/settings", headers: hdrs(a),
|
||||||
|
payload: {
|
||||||
|
categories: s.categories, services: s.services,
|
||||||
|
prices: [
|
||||||
|
{ categoryId: car, serviceId: std, priceMinor: 50000 },
|
||||||
|
{ categoryId: suv, serviceId: std, priceMinor: 70000 },
|
||||||
|
{ categoryId: car, serviceId: inside, priceMinor: 30000 },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(priced.statusCode).toBe(200);
|
||||||
|
expect(priced.json().prices).toHaveLength(3);
|
||||||
|
return { car, suv, std, inside };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Flip the site's wash-payment policy (Setup → Car wash). */
|
||||||
|
async function setPayAt(a: Auth, payAt: "booth" | "bay") {
|
||||||
|
const res = await app.inject({ method: "PUT", url: "/api/carwash/settings", headers: hdrs(a), payload: { payAt } });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json().payAt).toBe(payAt);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function seedSponsorship(a: Auth, mode: "comp" | "percent" | "doneTolerance" | "washPrice" = "comp", minutes: number | null = null) {
|
||||||
|
const res = await app.inject({
|
||||||
|
method: "PUT", url: "/api/validation/programs/carwash", headers: hdrs(a),
|
||||||
|
payload: { name: "Lavazh", mode, percent: mode === "percent" ? 50 : null, minutes, active: true, userIds: [] },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBeLessThan(300);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function events(a: Auth) {
|
||||||
|
const r = await app.inject({ method: "GET", url: "/api/events?limit=100", headers: { cookie: a.cookie } });
|
||||||
|
return (r.json().events ?? r.json()) as Array<{ id: string; type: string; identity: string | null; payload: Record<string, unknown> }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("settings", () => {
|
||||||
|
it("round-trips categories, services and the price matrix; signs a config_change; unknown pairs are refused", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
const get = await app.inject({ method: "GET", url: "/api/carwash/settings", headers: { cookie: a.cookie } });
|
||||||
|
expect(get.json().categories.map((c: { name: string }) => c.name)).toEqual(["Car", "SUV"]);
|
||||||
|
expect(get.json().prices.find((p: { categoryId: string; serviceId: string }) => p.categoryId === ids.suv && p.serviceId === ids.std).priceMinor).toBe(70000);
|
||||||
|
const bad = await app.inject({
|
||||||
|
method: "PUT", url: "/api/carwash/settings", headers: hdrs(a),
|
||||||
|
payload: { prices: [{ categoryId: "nope", serviceId: ids.std, priceMinor: 1 }] },
|
||||||
|
});
|
||||||
|
expect(bad.statusCode).toBe(400);
|
||||||
|
const flips = (await events(a)).filter((e) => e.type === "config_change" && e.payload.setting === "carwash.settings");
|
||||||
|
expect(flips.length).toBeGreaterThanOrEqual(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("orders", () => {
|
||||||
|
it("intake needs an open session and a priced pair; the queue is oldest-first", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
const noSession = await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-NONE", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
});
|
||||||
|
expect(noSession.statusCode).toBe(404);
|
||||||
|
|
||||||
|
await openSession("T-1");
|
||||||
|
const noPrice = await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-1", categoryId: ids.suv, serviceId: ids.inside },
|
||||||
|
});
|
||||||
|
expect(noPrice.statusCode).toBe(409);
|
||||||
|
expect(noPrice.json().code).toBe("no_price");
|
||||||
|
|
||||||
|
const created = await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-1", categoryId: ids.suv, serviceId: ids.std },
|
||||||
|
});
|
||||||
|
expect(created.statusCode).toBe(201);
|
||||||
|
expect(created.json()).toMatchObject({ identity: "T-1", categoryName: "SUV", serviceName: "Standard", priceMinor: 70000, payAt: "booth", status: "open", closed: false });
|
||||||
|
|
||||||
|
await openSession("T-2");
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-2", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
});
|
||||||
|
const queue = await app.inject({ method: "GET", url: "/api/carwash/orders", headers: { cookie: a.cookie } });
|
||||||
|
expect(queue.json().orders.map((o: { identity: string }) => o.identity)).toEqual(["T-1", "T-2"]);
|
||||||
|
|
||||||
|
const chain = (await events(a)).filter((e) => e.type === "carwash_order");
|
||||||
|
expect(chain).toHaveLength(2);
|
||||||
|
expect(chain[0]!.payload).toMatchObject({ action: "created", operator: "boss" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("pay at BOOTH: the wash rides the parking quote as a charge line and is marked paid by the booth payment", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await openSession("T-B");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-B", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
|
||||||
|
const look = await app.inject({ method: "GET", url: "/api/session/T-B", headers: { cookie: a.cookie } });
|
||||||
|
const s = look.json();
|
||||||
|
expect(s.chargeLines).toHaveLength(1);
|
||||||
|
expect(s.chargeLines[0]).toMatchObject({ module: "carwash", ref: order.id, amountMinor: 50000 });
|
||||||
|
expect(s.chargesMinor).toBe(50000);
|
||||||
|
expect(s.amountMinor).toBeGreaterThan(50000); // parking fee + the wash
|
||||||
|
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(a) });
|
||||||
|
const pay = await app.inject({ method: "POST", url: "/api/pay", headers: hdrs(a), payload: { identity: "T-B", tender: "cash" } });
|
||||||
|
expect(pay.statusCode).toBeLessThan(300);
|
||||||
|
|
||||||
|
const payment = (await events(a)).find((e) => e.type === "payment" && e.identity === "T-B")!;
|
||||||
|
expect(payment.payload.chargesMinor).toBe(50000);
|
||||||
|
expect((payment.payload.chargeLines as unknown[]).length).toBe(1);
|
||||||
|
expect(payment.payload.amountMinor).toBe((payment.payload.parkingMinor as number) + 50000);
|
||||||
|
|
||||||
|
const recent = await app.inject({ method: "GET", url: "/api/carwash/orders?scope=recent", headers: { cookie: a.cookie } });
|
||||||
|
const o = recent.json().orders.find((x: { id: string }) => x.id === order.id);
|
||||||
|
expect(o.paidAt).toBeTruthy();
|
||||||
|
expect(o.paymentEventId).toBeUndefined(); // not exposed on the view
|
||||||
|
expect(o.tender).toBe("cash");
|
||||||
|
// A second lookup no longer carries the line (it's settled).
|
||||||
|
const again = await app.inject({ method: "GET", url: "/api/session/T-B", headers: { cookie: a.cookie } });
|
||||||
|
expect(again.json().chargeLines).toEqual([]);
|
||||||
|
|
||||||
|
// The booth's Z-report: the wash money is inside cash (it is in the drawer) but
|
||||||
|
// OUT of the ticket bucket, under its own module — Bileta is parking money only.
|
||||||
|
const parking = payment.payload.parkingMinor as number;
|
||||||
|
const z = (await app.inject({ method: "POST", url: "/api/shift/close", headers: hdrs(a) })).json();
|
||||||
|
expect(z).toMatchObject({ till: "booth", cashTotalMinor: parking + 50000, ticketTotalMinor: parking, chargesByModuleMinor: { carwash: 50000 } });
|
||||||
|
expect(z.ticketTotalMinor + z.subscriptionTotalMinor + 50000).toBe(z.cashTotalMinor + z.cardTotalMinor);
|
||||||
|
const summary = (await app.inject({ method: "GET", url: "/api/shifts", headers: { cookie: a.cookie } })).json().shifts[0];
|
||||||
|
expect(summary).toMatchObject({ till: "booth", ticketTotalMinor: parking, chargesByModuleMinor: { carwash: 50000 } });
|
||||||
|
const signed = (await events(a)).find((e) => e.type === "shift_z_report")!;
|
||||||
|
expect(signed.payload.chargesByModuleMinor).toEqual({ carwash: 50000 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("pay at BAY with a comp sponsorship: done applies the validation, bay payment signs carwash_payment and settles parking at zero", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await seedSponsorship(a, "comp");
|
||||||
|
await openSession("T-Y");
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-Y", categoryId: ids.suv, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
|
||||||
|
// Bay money needs an open CARWASH shift — the booth's shift does not count (tills).
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(a) });
|
||||||
|
const noShift = await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/pay`, headers: hdrs(a), payload: { tender: "cash" } });
|
||||||
|
expect(noShift.statusCode).toBe(409);
|
||||||
|
expect(noShift.json()).toMatchObject({ code: "no_shift", till: "carwash" });
|
||||||
|
const openWash = await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(a), payload: { till: "carwash" } });
|
||||||
|
expect(openWash.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const done = await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
expect(done.statusCode).toBe(200);
|
||||||
|
expect(done.json().status).toBe("done");
|
||||||
|
expect(done.json().validationEventId).toBeTruthy();
|
||||||
|
// Sponsorship applied → the parking quote is now zero-due (comp), but NOT yet paid.
|
||||||
|
const mid = await app.inject({ method: "GET", url: "/api/session/T-Y", headers: { cookie: a.cookie } });
|
||||||
|
expect(mid.json().amountMinor).toBe(0);
|
||||||
|
expect(mid.json().paidAt).toBeNull();
|
||||||
|
|
||||||
|
const paid = await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/pay`, headers: hdrs(a), payload: { tender: "card" } });
|
||||||
|
expect(paid.statusCode).toBe(200);
|
||||||
|
expect(paid.json().closed).toBe(true);
|
||||||
|
|
||||||
|
const evs = await events(a);
|
||||||
|
const bay = evs.find((e) => e.type === "carwash_payment")!;
|
||||||
|
expect(bay.payload).toMatchObject({ orderId: order.id, amountMinor: 70000, tender: "card", operator: "boss", till: "carwash" });
|
||||||
|
// The wash Z-report carries the bay money; the booth's carries none of it.
|
||||||
|
const washZ = (await app.inject({ method: "POST", url: "/api/shift/close", headers: hdrs(a), payload: { till: "carwash" } })).json();
|
||||||
|
expect(washZ).toMatchObject({ till: "carwash", cardTotalMinor: 70000, cashTotalMinor: 0, paymentCount: 1 });
|
||||||
|
const boothZ = (await app.inject({ method: "POST", url: "/api/shift/close", headers: hdrs(a) })).json();
|
||||||
|
expect(boothZ.till).toBe("booth");
|
||||||
|
expect(boothZ.cardTotalMinor).toBe(0);
|
||||||
|
expect(boothZ.paymentCount).toBe(1); // the $0 parking settlement is booth money
|
||||||
|
// The $0 parking payment exists → the exit reader's paid+grace check passes.
|
||||||
|
const parkingPay = evs.find((e) => e.type === "payment" && e.identity === "T-Y")!;
|
||||||
|
expect(parkingPay).toBeTruthy();
|
||||||
|
expect(parkingPay.payload.amountMinor).toBe(0);
|
||||||
|
const after = await app.inject({ method: "GET", url: "/api/session/T-Y", headers: { cookie: a.cookie } });
|
||||||
|
expect(after.json().paidAt).toBeTruthy();
|
||||||
|
expect(after.json().withinGrace).toBe(true);
|
||||||
|
|
||||||
|
// The queue is empty (done + paid = closed).
|
||||||
|
const queue = await app.inject({ method: "GET", url: "/api/carwash/orders", headers: { cookie: a.cookie } });
|
||||||
|
expect(queue.json().orders).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("pay at BAY with a PARTIAL sponsorship leaves the remainder for the booth (no $0 payment)", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await seedSponsorship(a, "percent");
|
||||||
|
await openSession("T-P");
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-P", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(a), payload: { till: "carwash" } });
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/pay`, headers: hdrs(a), payload: { tender: "cash" } });
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
const s = (await app.inject({ method: "GET", url: "/api/session/T-P", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(s.paidAt).toBeNull();
|
||||||
|
expect(s.amountMinor).toBeGreaterThan(0);
|
||||||
|
expect(s.discountMinor).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("void takes back a live sponsorship; a paid order cannot be voided", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await seedSponsorship(a, "comp");
|
||||||
|
await openSession("T-V");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-V", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
const before = (await app.inject({ method: "GET", url: "/api/session/T-V", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(before.validationLines).toHaveLength(1);
|
||||||
|
|
||||||
|
const voided = await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/void`, headers: hdrs(a), payload: { reason: "customer left" } });
|
||||||
|
expect(voided.statusCode).toBe(200);
|
||||||
|
expect(voided.json().status).toBe("void");
|
||||||
|
const after = (await app.inject({ method: "GET", url: "/api/session/T-V", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(after.validationLines).toEqual([]);
|
||||||
|
expect(after.chargeLines).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("wash-only discount modes", () => {
|
||||||
|
it("doneTolerance credits only the WASH WINDOW (+ tolerance), never the parking before the order", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
// 100.00 per 60-min increment, no entry grace; parked 95 min → 2 increments.
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60, gracePeriodEntryMin: 0 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await seedSponsorship(a, "doneTolerance", 15);
|
||||||
|
await openSession("T-D", 95);
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-D", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
const before = (await app.inject({ method: "GET", url: "/api/session/T-D", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(before.amountMinor).toBe(20000);
|
||||||
|
// Done right away: the wash window is ~0 min, so the credit is just the tolerance.
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
const v = (await events(a)).find((e) => e.type === "validation" && e.identity === "T-D")!;
|
||||||
|
expect(v.payload.mode).toBe("timeCredit");
|
||||||
|
expect(v.payload.programMode).toBe("doneTolerance");
|
||||||
|
expect(v.payload.minutes as number).toBeGreaterThanOrEqual(15);
|
||||||
|
expect(v.payload.minutes as number).toBeLessThanOrEqual(17);
|
||||||
|
// 95 − ~15 min still spans 2 increments → the long stay is NOT comped away.
|
||||||
|
const after = (await app.inject({ method: "GET", url: "/api/session/T-D", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(after.amountMinor).toBe(20000);
|
||||||
|
expect(after.discountMinor).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("doneTolerance with a tolerance that covers the whole stay does comp it (the credit is real)", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60, gracePeriodEntryMin: 0 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await seedSponsorship(a, "doneTolerance", 120);
|
||||||
|
await openSession("T-D2", 95);
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-D2", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
const after = (await app.inject({ method: "GET", url: "/api/session/T-D2", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(after.amountMinor).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("washPrice: the wash price comes off the parking fee, floored at zero", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
// 1000.00/h, parked 95 min → 2 increments = 200000 owed. Car·Standard wash = 50000.
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 100000, incrementMin: 60, gracePeriodEntryMin: 0 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await seedSponsorship(a, "washPrice");
|
||||||
|
await openSession("T-W", 95);
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-W", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
const before = (await app.inject({ method: "GET", url: "/api/session/T-W", headers: { cookie: a.cookie } })).json();
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
const after = (await app.inject({ method: "GET", url: "/api/session/T-W", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(after.discountMinor).toBe(50000);
|
||||||
|
expect(after.amountMinor).toBe(before.amountMinor - 50000);
|
||||||
|
const v = (await events(a)).find((e) => e.type === "validation" && e.identity === "T-W")!;
|
||||||
|
expect(v.payload).toMatchObject({ mode: "fixed", programMode: "washPrice", amountMinor: 50000 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a merchant scan cannot apply a wash-only program", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
await seedSponsorship(a, "washPrice");
|
||||||
|
// Bind the admin to it so the binding check passes and the MODE check is what refuses.
|
||||||
|
await app.inject({
|
||||||
|
method: "PUT", url: "/api/validation/programs/carwash", headers: hdrs(a),
|
||||||
|
payload: { name: "Lavazh", mode: "washPrice", active: true, userIds: [(await app.inject({ method: "GET", url: "/api/auth/me", headers: { cookie: a.cookie } })).json().id] },
|
||||||
|
});
|
||||||
|
await openSession("T-M");
|
||||||
|
const res = await app.inject({ method: "POST", url: "/api/validation/apply", headers: hdrs(a), payload: { identity: "T-M", programId: "carwash" } });
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
expect(res.json().error).toMatch(/car wash order/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("module gate", () => {
|
||||||
|
it("with carwash deactivated every route 403s and the booth quote carries no wash lines", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await openSession("T-G");
|
||||||
|
await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-G", categoryId: ids.car, serviceId: ids.std },
|
||||||
|
});
|
||||||
|
const off = await app.inject({ method: "PUT", url: "/api/site-config", headers: hdrs(a), payload: { modules: ["parking", "validation"] } });
|
||||||
|
expect(off.json().modules).toEqual(["parking", "validation"]);
|
||||||
|
const q = await app.inject({ method: "GET", url: "/api/carwash/orders", headers: { cookie: a.cookie } });
|
||||||
|
expect(q.statusCode).toBe(403);
|
||||||
|
expect(q.json().code).toBe("module_disabled");
|
||||||
|
const look = (await app.inject({ method: "GET", url: "/api/session/T-G", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(look.chargeLines).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("where the money is taken is a SITE setting", () => {
|
||||||
|
it("defaults to the booth, persists, signs a config_change, and freezes on each order", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/carwash/settings", headers: { cookie: a.cookie } })).json().payAt).toBe("booth");
|
||||||
|
await openSession("T-S1");
|
||||||
|
const o1 = (await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs(a), payload: { identity: "T-S1", categoryId: ids.car, serviceId: ids.std } })).json();
|
||||||
|
expect(o1.payAt).toBe("booth");
|
||||||
|
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
const cfg = (await events(a)).find((e) => e.type === "config_change" && e.payload.setting === "carwash.payAt")!;
|
||||||
|
expect(cfg.payload).toMatchObject({ value: "bay", prev: "booth", operator: "boss" });
|
||||||
|
await openSession("T-S2");
|
||||||
|
const o2 = (await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs(a), payload: { identity: "T-S2", categoryId: ids.car, serviceId: ids.std } })).json();
|
||||||
|
expect(o2.payAt).toBe("bay");
|
||||||
|
expect(o1.payAt).toBe("booth"); // earlier order keeps the policy it was created under
|
||||||
|
|
||||||
|
// A stale client insisting on the other place is refused, never silently overridden.
|
||||||
|
const stale = await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs(a), payload: { identity: "T-S2", categoryId: ids.car, serviceId: ids.std, payAt: "booth" } });
|
||||||
|
expect(stale.statusCode).toBe(409);
|
||||||
|
expect(stale.json().code).toBe("pay_at_policy");
|
||||||
|
const bad = await app.inject({ method: "PUT", url: "/api/carwash/settings", headers: hdrs(a), payload: { payAt: "pocket" } });
|
||||||
|
expect(bad.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("tills are gated by the module permission", () => {
|
||||||
|
it("a wash-only role works the carwash till and never the booth's; a booth role the reverse", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
await seedSettings(a);
|
||||||
|
// The wash-operator JOB: no shift:* / drawer:* at all — the wash till is guarded by
|
||||||
|
// carwash:read / carwash:cash (venue-modules.md §"Permissions matrix").
|
||||||
|
const washer = await seedUser(db, {
|
||||||
|
username: "lavazhier", roleId: "washer",
|
||||||
|
permissions: ["carwash:read", "carwash:create", "carwash:update", "carwash:cash"],
|
||||||
|
});
|
||||||
|
const w = await login(app, washer.username, washer.password);
|
||||||
|
// The desk's category/service pickers come from the settings read — the job has no
|
||||||
|
// site:read, so the module permission must open it (found on park dev, 2026-09-06).
|
||||||
|
const list = await app.inject({ method: "GET", url: "/api/carwash/settings", headers: { cookie: w.cookie } });
|
||||||
|
expect(list.statusCode).toBe(200);
|
||||||
|
expect(list.json().categories.length).toBeGreaterThan(0);
|
||||||
|
expect((await app.inject({ method: "PUT", url: "/api/carwash/settings", headers: hdrs(w), payload: { payAt: "bay" } })).statusCode).toBe(403);
|
||||||
|
// What the UI offers: only the wash till.
|
||||||
|
const tills = await app.inject({ method: "GET", url: "/api/shift/tills", headers: { cookie: w.cookie } });
|
||||||
|
expect(tills.json().tills.map((t: { till: string }) => t.till)).toEqual(["carwash"]);
|
||||||
|
// The booth's shift is refused outright (the role holds no shift:*).
|
||||||
|
const booth = await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(w) });
|
||||||
|
expect(booth.statusCode).toBe(403);
|
||||||
|
expect(booth.json()).toMatchObject({ code: "till_forbidden", till: "booth" });
|
||||||
|
const boothState = await app.inject({ method: "GET", url: "/api/shift/current", headers: { cookie: w.cookie } });
|
||||||
|
expect(boothState.statusCode).toBe(403);
|
||||||
|
const boothCash = await app.inject({ method: "POST", url: "/api/drawer/movement", headers: hdrs(w), payload: { type: "cash_in", amountMinor: 100 } });
|
||||||
|
expect(boothCash.statusCode).toBe(403);
|
||||||
|
// The wash till works.
|
||||||
|
const wash = await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(w), payload: { till: "carwash" } });
|
||||||
|
expect(wash.statusCode).toBe(200);
|
||||||
|
expect(wash.json().till).toBe("carwash");
|
||||||
|
const washCash = await app.inject({ method: "POST", url: "/api/drawer/movement", headers: hdrs(w), payload: { type: "cash_in", amountMinor: 100, till: "carwash" } });
|
||||||
|
expect(washCash.statusCode).toBe(200);
|
||||||
|
|
||||||
|
// A wash user who may look (carwash:read) but not work the till (no carwash:cash)
|
||||||
|
// sees the state and gets canWork=false; opening is refused.
|
||||||
|
const looker = await seedUser(db, { username: "looker", roleId: "wash-look", permissions: ["carwash:read"] });
|
||||||
|
const l = await login(app, looker.username, looker.password);
|
||||||
|
const lookTills = (await app.inject({ method: "GET", url: "/api/shift/tills", headers: { cookie: l.cookie } })).json();
|
||||||
|
expect(lookTills.tills).toMatchObject([{ till: "carwash", canWork: false }]);
|
||||||
|
expect((await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(l), payload: { till: "carwash" } })).statusCode).toBe(403);
|
||||||
|
|
||||||
|
// A booth operator (shift:*, no carwash:*) cannot touch the wash till.
|
||||||
|
const booth1 = await seedUser(db, {
|
||||||
|
username: "boothie", roleId: "booth-op",
|
||||||
|
permissions: ["session:read", "payment:create", "shift:read", "shift:create"],
|
||||||
|
});
|
||||||
|
const b = await login(app, booth1.username, booth1.password);
|
||||||
|
const noWash = await app.inject({ method: "POST", url: "/api/shift/close", headers: hdrs(b), payload: { till: "carwash" } });
|
||||||
|
expect(noWash.statusCode).toBe(403);
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/shift/tills", headers: { cookie: b.cookie } })).json().tills.map((t: { till: string }) => t.till)).toEqual(["booth"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("a role reassignment takes effect without re-login", () => {
|
||||||
|
it("a user moved from a look-only role to the wash-operator role can create an order on the next request", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await openSession("T-R");
|
||||||
|
const looker = await seedUser(db, { username: "moved", roleId: "wash-look", permissions: ["carwash:read"] });
|
||||||
|
// Materialise the target role (seedUser creates the role rows; the user itself is a throwaway).
|
||||||
|
await seedUser(db, { username: "throwaway", roleId: "wash-op", permissions: ["carwash:read", "carwash:create", "carwash:update", "carwash:cash"] });
|
||||||
|
const l = await login(app, looker.username, looker.password);
|
||||||
|
const before = await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs(l), payload: { identity: "T-R", categoryId: ids.car, serviceId: ids.std } });
|
||||||
|
expect(before.statusCode).toBe(403);
|
||||||
|
|
||||||
|
const list = (await app.inject({ method: "GET", url: "/api/users", headers: { cookie: a.cookie } })).json();
|
||||||
|
const id = list.users.find((u: { username: string }) => u.username === "moved").id;
|
||||||
|
const moved = await app.inject({ method: "PUT", url: `/api/users/${id}`, headers: hdrs(a), payload: { roleId: "wash-op" } });
|
||||||
|
expect(moved.statusCode).toBe(200);
|
||||||
|
|
||||||
|
// Same cookie, no re-login: the token's pinned role is refreshed per request.
|
||||||
|
const after = await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs(l), payload: { identity: "T-R", categoryId: ids.car, serviceId: ids.std } });
|
||||||
|
expect(after.statusCode).toBe(201);
|
||||||
|
const me = (await app.inject({ method: "GET", url: "/api/auth/me", headers: { cookie: l.cookie } })).json();
|
||||||
|
expect(me.roleId).toBe("wash-op");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("a shift's activity log is per till", () => {
|
||||||
|
it("/api/events?till= applies tillOfEvent; a feed-only role reads its module's events and nothing else", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000 });
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await openSession("T-L");
|
||||||
|
await setPayAt(a, "bay");
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(a) });
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: hdrs(a), payload: { till: "carwash" } });
|
||||||
|
const order = (await app.inject({
|
||||||
|
method: "POST", url: "/api/carwash/orders", headers: hdrs(a),
|
||||||
|
payload: { identity: "T-L", categoryId: ids.suv, serviceId: ids.std },
|
||||||
|
})).json();
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/done`, headers: hdrs(a) });
|
||||||
|
await app.inject({ method: "POST", url: `/api/carwash/orders/${order.id}/pay`, headers: hdrs(a), payload: { tender: "cash" } });
|
||||||
|
await app.inject({ method: "POST", url: "/api/drawer/movement", headers: hdrs(a), payload: { type: "cash_in", amountMinor: 500, till: "carwash" } });
|
||||||
|
await app.inject({ method: "POST", url: "/api/drawer/movement", headers: hdrs(a), payload: { type: "cash_in", amountMinor: 700 } });
|
||||||
|
|
||||||
|
const types = async (qs: string, auth: Auth = a) => {
|
||||||
|
const r = await app.inject({ method: "GET", url: `/api/events?limit=200${qs}`, headers: { cookie: auth.cookie } });
|
||||||
|
expect(r.statusCode).toBe(200);
|
||||||
|
return (r.json().events as { type: string; payload: Record<string, unknown> }[]).map((e) => `${e.type}${e.payload?.till ? `@${e.payload.till}` : ""}`);
|
||||||
|
};
|
||||||
|
// The wash till's log: its shift, its order (no money moved, but wash-desk activity),
|
||||||
|
// its bay payment and its voucher — none of the booth's.
|
||||||
|
const wash = await types("&till=carwash");
|
||||||
|
expect(wash).toEqual(expect.arrayContaining(["shift_open@carwash", "carwash_order", "carwash_payment@carwash", "cash_in@carwash"]));
|
||||||
|
expect(wash.some((t) => t.startsWith("vehicle_entry") || t === "shift_open@booth" || t === "cash_in@booth")).toBe(false);
|
||||||
|
// The booth's log: entry, its shift, its voucher — and no wash-desk activity.
|
||||||
|
const booth = await types("&till=booth");
|
||||||
|
expect(booth).toEqual(expect.arrayContaining(["vehicle_entry", "shift_open@booth", "cash_in@booth"]));
|
||||||
|
expect(booth.some((t) => t.startsWith("carwash_") || t.endsWith("@carwash"))).toBe(false);
|
||||||
|
// No till → everything (unchanged).
|
||||||
|
const all = await types("");
|
||||||
|
expect(all.length).toBe(wash.length + booth.length);
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/events?till=bar", headers: { cookie: a.cookie } })).statusCode).toBe(400);
|
||||||
|
|
||||||
|
// A wash operator holds carwash:read but not event:read: the log opens for them
|
||||||
|
// with ONLY the module's own event types (the live-socket rule, feedPermissionFor).
|
||||||
|
const washer = await seedUser(db, { username: "lavazhier", roleId: "washer", permissions: ["carwash:read", "carwash:cash"] });
|
||||||
|
const w = await login(app, washer.username, washer.password);
|
||||||
|
const mine = await types("&till=carwash", w);
|
||||||
|
expect(mine).toEqual(expect.arrayContaining(["carwash_order", "carwash_payment@carwash"]));
|
||||||
|
expect(mine.every((t) => t.startsWith("carwash_"))).toBe(true);
|
||||||
|
// A role with neither event:read nor any module feed permission reads nothing.
|
||||||
|
const clerk = await seedUser(db, { username: "clerk", roleId: "clerk", permissions: ["session:read"] });
|
||||||
|
const c = await login(app, clerk.username, clerk.password);
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/events", headers: { cookie: c.cookie } })).statusCode).toBe(403);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("vision category — advisory, flagged, never authoritative", () => {
|
||||||
|
/** What snapshot.ts records when vision classifies the entry frame. */
|
||||||
|
function seeVehicle(identity: string, bodyType: string, bodyConfidence: number) {
|
||||||
|
db.insert(deviceEvents).values({
|
||||||
|
id: `read-${identity}-${bodyType}`, deviceId: "cam-1", category: "camera", kind: "read",
|
||||||
|
detail: { identity, direction: "entry", bodyType, bodyConfidence, snapshotId: "snap-1", source: "entry-exit-snapshot" },
|
||||||
|
occurredAt: new Date().toISOString(),
|
||||||
|
}).run();
|
||||||
|
}
|
||||||
|
async function mapClasses(a: Auth, ids: { car: string; suv: string }) {
|
||||||
|
const cur = (await app.inject({ method: "GET", url: "/api/carwash/settings", headers: { cookie: a.cookie } })).json();
|
||||||
|
const r = await app.inject({
|
||||||
|
method: "PUT", url: "/api/carwash/settings", headers: hdrs(a),
|
||||||
|
payload: {
|
||||||
|
categories: cur.categories.map((c: { id: string }) => ({ ...c, visionClasses: c.id === ids.suv ? ["suv", "pickup"] : c.id === ids.car ? ["car", "sedan", "hatchback"] : [] })),
|
||||||
|
visionThreshold: 0.75,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(r.statusCode).toBe(200);
|
||||||
|
return r.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
it("Setup maps the vocabulary onto site categories; the lookup suggests the mapped category", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
const saved = await mapClasses(a, ids);
|
||||||
|
expect(saved.categories.find((c: { id: string }) => c.id === ids.suv).visionClasses).toEqual(["suv", "pickup"]);
|
||||||
|
expect(saved.visionThreshold).toBe(0.75);
|
||||||
|
expect((await events(a)).some((e) => e.type === "config_change" && e.payload.setting === "carwash.visionThreshold")).toBe(true);
|
||||||
|
const bad = await app.inject({ method: "PUT", url: "/api/carwash/settings", headers: hdrs(a), payload: { categories: [{ id: ids.car, name: "Car", visionClasses: ["spaceship"] }] } });
|
||||||
|
expect(bad.statusCode).toBe(400);
|
||||||
|
|
||||||
|
await openSession("T-V1");
|
||||||
|
seeVehicle("T-V1", "suv", 0.91);
|
||||||
|
const look = (await app.inject({ method: "GET", url: "/api/carwash/session/T-V1", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(look.vision).toMatchObject({ bodyType: "suv", confidence: 0.91, snapshotId: "snap-1" });
|
||||||
|
expect(look.suggestedCategoryId).toBe(ids.suv);
|
||||||
|
// Unmapped class → shown, nothing suggested.
|
||||||
|
await openSession("T-V2");
|
||||||
|
seeVehicle("T-V2", "bus", 0.99);
|
||||||
|
const look2 = (await app.inject({ method: "GET", url: "/api/carwash/session/T-V2", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(look2.vision.bodyType).toBe("bus");
|
||||||
|
expect(look2.suggestedCategoryId).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a confident downgrade signs an anomaly with both categories and the snapshot; equal, upgrade or unsure reads do not; the order is never blocked", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
seedTariff(db);
|
||||||
|
const ids = await seedSettings(a);
|
||||||
|
await mapClasses(a, ids);
|
||||||
|
const order = async (identity: string, categoryId: string) => {
|
||||||
|
const r = await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs(a), payload: { identity, categoryId, serviceId: ids.std } });
|
||||||
|
expect(r.statusCode).toBe(201);
|
||||||
|
return r.json();
|
||||||
|
};
|
||||||
|
// Camera: SUV (0.91) — operator picks Car (cheaper) → flagged, recorded, still created.
|
||||||
|
await openSession("T-D1"); seeVehicle("T-D1", "suv", 0.91);
|
||||||
|
const down = await order("T-D1", ids.car);
|
||||||
|
expect(down).toMatchObject({ visionClass: "suv", visionConfidence: 0.91, visionCategoryId: ids.suv, categoryId: ids.car });
|
||||||
|
expect(down.downgradeEventId).toBeTruthy();
|
||||||
|
const flag = (await events(a)).find((e) => e.type === "anomaly" && e.payload.reasonCode === "carwash.categoryDowngrade")!;
|
||||||
|
expect(flag).toBeTruthy();
|
||||||
|
expect(flag.payload).toMatchObject({
|
||||||
|
visionClass: "suv", visionCategoryName: "SUV", chosenCategoryName: "Car", operator: "boss",
|
||||||
|
visionPriceMinor: 70000, chosenPriceMinor: 50000, snapshotId: "snap-1",
|
||||||
|
});
|
||||||
|
// Same category as the camera → nothing.
|
||||||
|
await openSession("T-D2"); seeVehicle("T-D2", "suv", 0.91);
|
||||||
|
expect((await order("T-D2", ids.suv)).downgradeEventId).toBeNull();
|
||||||
|
// Upgrade (camera Car, operator SUV) → recorded on the order, no anomaly.
|
||||||
|
await openSession("T-D3"); seeVehicle("T-D3", "sedan", 0.95);
|
||||||
|
const up = await order("T-D3", ids.suv);
|
||||||
|
expect(up).toMatchObject({ visionClass: "sedan", visionCategoryId: ids.car, downgradeEventId: null });
|
||||||
|
// Below the site threshold → shown, never flagged.
|
||||||
|
await openSession("T-D4"); seeVehicle("T-D4", "suv", 0.6);
|
||||||
|
expect((await order("T-D4", ids.car)).downgradeEventId).toBeNull();
|
||||||
|
// No read at all → nulls.
|
||||||
|
await openSession("T-D5");
|
||||||
|
expect(await order("T-D5", ids.car)).toMatchObject({ visionClass: null, visionCategoryId: null, downgradeEventId: null });
|
||||||
|
expect((await events(a)).filter((e) => e.type === "anomaly" && e.payload.reasonCode === "carwash.categoryDowngrade")).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { deviceEvents } from "../../device-events.js";
|
||||||
|
import type { ServerModule } from "../index.js";
|
||||||
|
import { ReviewOutbox, reviewUploadConfigFromEnv } from "./review-outbox.js";
|
||||||
|
import { carwashRoutes } from "./routes.js";
|
||||||
|
import { CarwashService } from "./service.js";
|
||||||
|
|
||||||
|
// Car Wash — the pilot venue module (wiki/decisions/venue-modules.md). Everything the
|
||||||
|
// module is lives in this folder: its service (master data, the order queue, the bay
|
||||||
|
// payment, the parking sponsorship + settlement), its routes, and the booth charge
|
||||||
|
// provider it registers with the core's PayStation. The core knows it only through the
|
||||||
|
// registry line in ../index.ts and the manifest in @parking/shared.
|
||||||
|
export const carwashModule: ServerModule = {
|
||||||
|
id: "carwash",
|
||||||
|
async register(app, deps) {
|
||||||
|
// The review outbox (wiki/concepts/vision-review-outbox.md): on when the stack env
|
||||||
|
// names a collector URL, a per-booth token and a pseudonymous booth id; off = no
|
||||||
|
// queueing at all. One-way, background, never on the intake path.
|
||||||
|
const cfg = reviewUploadConfigFromEnv();
|
||||||
|
const outbox = new ReviewOutbox(deps.db, app.log, cfg);
|
||||||
|
app.log.info(cfg ? `carwash review upload: on → ${new URL(cfg.url).host} as ${cfg.boothId}` : "carwash review upload: off");
|
||||||
|
outbox.start();
|
||||||
|
// Entry-stream sampling: one in N entry vehicle reads goes to the reviewer as pure
|
||||||
|
// training material (the gate view, no order attached). The core announces the read;
|
||||||
|
// the module decides. Off unless CARWASH_REVIEW_ENTRY_SAMPLE is set.
|
||||||
|
const offVehicleRead = deviceEvents.onVehicleRead((e) => {
|
||||||
|
if (e.direction === "entry" && outbox.sampleEntry(e.deviceId)) void outbox.enqueueEntry(e.read);
|
||||||
|
});
|
||||||
|
app.addHook("onClose", async () => offVehicleRead());
|
||||||
|
app.addHook("onClose", async () => outbox.stop());
|
||||||
|
const service = new CarwashService(deps, app.log, outbox);
|
||||||
|
// A wash ordered with payAt = "booth" is a charge line on the parking settlement;
|
||||||
|
// the core calls back after the payment is signed so the order is marked paid.
|
||||||
|
deps.payStation.registerChargeProvider(service.chargeProvider());
|
||||||
|
await carwashRoutes(app, deps, service, outbox);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,255 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import sharp from "sharp";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { carwashOrders, carwashReviewOutbox, deviceEvents, snapshots, type Db } from "@parking/db";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { deviceEvents as deviceEventBus } from "../../device-events.js";
|
||||||
|
import { buildServer } from "../../server.js";
|
||||||
|
import { login, makeLog, minutesAgo, seedTariff, seedUser, silentLogger } from "../../test-helpers.js";
|
||||||
|
import { ENTRY_DEDUP_MS, EXPIRE_DAYS, ReviewOutbox, makeReviewCrop, operatorRef, reviewUploadConfigFromEnv } from "./review-outbox.js";
|
||||||
|
|
||||||
|
// The review outbox, booth side (wiki/concepts/vision-review-outbox.md): a plate-blurred
|
||||||
|
// vehicle crop + the operator's choice, queued off the intake path, drained one-way with
|
||||||
|
// backoff, never blocking the wash, never naming the site.
|
||||||
|
|
||||||
|
/** A 400×300 frame: grey ground, a red "car" block, a white "plate" strip inside it. */
|
||||||
|
async function frame(): Promise<Buffer> {
|
||||||
|
return sharp({ create: { width: 400, height: 300, channels: 3, background: { r: 90, g: 90, b: 90 } } })
|
||||||
|
.composite([
|
||||||
|
{ input: { create: { width: 200, height: 120, channels: 3, background: { r: 200, g: 30, b: 30 } } }, left: 100, top: 100 },
|
||||||
|
{ input: { create: { width: 60, height: 16, channels: 3, background: { r: 255, g: 255, b: 255 } } }, left: 170, top: 190 },
|
||||||
|
])
|
||||||
|
.jpeg()
|
||||||
|
.toBuffer();
|
||||||
|
}
|
||||||
|
const CAR = { x1: 100 / 400, y1: 100 / 300, x2: 300 / 400, y2: 220 / 300 };
|
||||||
|
const PLATE = { x1: 170 / 400, y1: 190 / 300, x2: 230 / 400, y2: 206 / 300 };
|
||||||
|
|
||||||
|
/** Mean GREEN over a region — the white plate reads 255, the red car around it 30, so a
|
||||||
|
* blurred plate drops far below 255 as the red bleeds in. */
|
||||||
|
async function meanGreen(buf: Buffer, region: { left: number; top: number; width: number; height: number }): Promise<number> {
|
||||||
|
const { data, info } = await sharp(buf).extract(region).raw().toBuffer({ resolveWithObject: true });
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 1; i < data.length; i += info.channels) sum += data[i]!;
|
||||||
|
return sum / (data.length / info.channels);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("makeReviewCrop", () => {
|
||||||
|
it("cuts the vehicle (with margin), blurs the plate inside it, caps the edge", async () => {
|
||||||
|
const shot = await frame();
|
||||||
|
const crop = await makeReviewCrop(shot, CAR, PLATE);
|
||||||
|
expect(crop.plateBlurred).toBe(true);
|
||||||
|
// Box 200×120 + 8 % margin each side ≈ 232×139; no upscaling.
|
||||||
|
expect(crop.width).toBeGreaterThanOrEqual(228);
|
||||||
|
expect(crop.width).toBeLessThanOrEqual(236);
|
||||||
|
expect(crop.height).toBeGreaterThanOrEqual(135);
|
||||||
|
// The white plate is gone: over the plate strip (crop coords: the frame's 170..230 ×
|
||||||
|
// 190..206 shifted by the crop origin 84,90) the same region cut straight from the
|
||||||
|
// frame is white, the review crop is the red bleeding in.
|
||||||
|
const plain = await sharp(shot).extract({ left: 84, top: 90, width: crop.width, height: crop.height }).jpeg().toBuffer();
|
||||||
|
const strip = { left: 170 - 84, top: 190 - 90, width: 60, height: 16 };
|
||||||
|
expect(await meanGreen(plain, strip)).toBeGreaterThan(240);
|
||||||
|
expect(await meanGreen(crop.bytes, strip)).toBeLessThan(180);
|
||||||
|
// Without a plate box: same crop, nothing blurred.
|
||||||
|
const noPlate = await makeReviewCrop(shot, CAR, null);
|
||||||
|
expect(noPlate.plateBlurred).toBe(false);
|
||||||
|
// A big frame is capped to the max edge.
|
||||||
|
const big = await sharp({ create: { width: 2560, height: 1440, channels: 3, background: "#444" } }).jpeg().toBuffer();
|
||||||
|
const capped = await makeReviewCrop(big, { x1: 0, y1: 0, x2: 1, y2: 1 }, null);
|
||||||
|
expect(Math.max(capped.width, capped.height)).toBe(640);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("config + pseudonyms", () => {
|
||||||
|
it("needs url, token and booth id together; the operator ref is a keyed hash", () => {
|
||||||
|
expect(reviewUploadConfigFromEnv({})).toBeNull();
|
||||||
|
expect(reviewUploadConfigFromEnv({ CARWASH_REVIEW_URL: "https://c/ingest", CARWASH_REVIEW_TOKEN: "t" })).toBeNull();
|
||||||
|
const cfg = reviewUploadConfigFromEnv({ CARWASH_REVIEW_URL: "https://c/ingest", CARWASH_REVIEW_TOKEN: "t", CARWASH_REVIEW_BOOTH_ID: "b7", CARWASH_REVIEW_INTERVAL_SEC: "5" });
|
||||||
|
expect(cfg).toMatchObject({ boothId: "b7", intervalSec: 60 }); // below the 10 s floor → default
|
||||||
|
expect(operatorRef("b7", "lavazhier")).toHaveLength(16);
|
||||||
|
expect(operatorRef("b7", "lavazhier")).not.toBe(operatorRef("b8", "lavazhier"));
|
||||||
|
expect(operatorRef("b7", "lavazhier")).not.toContain("lavazhier");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("queue + drain", () => {
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
beforeEach(() => {
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
});
|
||||||
|
afterEach(() => close());
|
||||||
|
|
||||||
|
const cfg = { url: "https://collector.overlay/ingest", token: "secret-1", boothId: "booth-7", intervalSec: 60, entrySample: 0 };
|
||||||
|
const read = { bodyType: "car" as const, confidence: 0.86, snapshotId: "snap-1", box: CAR, plateBox: PLATE };
|
||||||
|
const item = { orderId: "o-1", createdAt: "2026-09-06T10:00:00.000Z", createdBy: "lavazhier", categoryId: "car", categoryName: "Vetura", categoryClasses: ["car", "sedan"], serviceName: "Standard", visionCategoryId: "car", downgraded: false };
|
||||||
|
|
||||||
|
async function seed(): Promise<void> {
|
||||||
|
db.insert(snapshots).values({ id: "snap-1", direction: "entry", identity: "T-1", contentType: "image/jpeg", bytes: await frame(), capturedAt: new Date().toISOString() }).run();
|
||||||
|
db.insert(carwashOrders).values({
|
||||||
|
id: "o-1", identity: "T-1", plate: null, categoryId: "car", categoryName: "Vetura", serviceId: "std", serviceName: "Standard",
|
||||||
|
priceMinor: 100, currency: "ALL", payAt: "booth", status: "open", createdAt: item.createdAt, createdBy: "lavazhier",
|
||||||
|
}).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
it("enqueues a crop + a payload with no site name, no plate, no operator name; drains with a multipart POST; drops the image once sent", async () => {
|
||||||
|
await seed();
|
||||||
|
const calls: { url: string; init: RequestInit }[] = [];
|
||||||
|
const fetchFn = vi.fn(async (url: string, init: RequestInit) => {
|
||||||
|
calls.push({ url, init });
|
||||||
|
return new Response("ok", { status: 200 });
|
||||||
|
});
|
||||||
|
const ob = new ReviewOutbox(db, silentLogger(), cfg, fetchFn);
|
||||||
|
expect(await ob.enqueue(item, read)).toBe(true);
|
||||||
|
const row = db.select().from(carwashReviewOutbox).all()[0]!;
|
||||||
|
expect(row.status).toBe("queued");
|
||||||
|
expect(row.image!.length).toBeGreaterThan(500);
|
||||||
|
expect(row.payload).toMatchObject({ v: 1, kind: "wash", booth: "booth-7", order: "o-1", operatorCategory: { id: "car", name: "Vetura", classes: ["car", "sedan"] }, vision: { class: "car", confidence: 0.86 }, downgraded: false, image: { plateBlurred: true } });
|
||||||
|
expect(JSON.stringify(row.payload)).not.toContain("lavazhier");
|
||||||
|
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 1, failed: 0, deferred: 0 });
|
||||||
|
expect(calls).toHaveLength(1);
|
||||||
|
expect(calls[0]!.url).toBe(cfg.url);
|
||||||
|
expect((calls[0]!.init.headers as Record<string, string>).authorization).toBe("Bearer secret-1");
|
||||||
|
const form = calls[0]!.init.body as FormData;
|
||||||
|
expect(JSON.parse(form.get("meta") as string).item).toBe(row.id);
|
||||||
|
expect((form.get("image") as File).type).toBe("image/jpeg");
|
||||||
|
const after = db.select().from(carwashReviewOutbox).all()[0]!;
|
||||||
|
expect(after.status).toBe("sent");
|
||||||
|
expect(after.image).toBeNull();
|
||||||
|
expect(after.sentAt).toBeTruthy();
|
||||||
|
expect(ob.status()).toMatchObject({ enabled: true, boothId: "booth-7", queued: 0, sent: 1, failed: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("defers with backoff on collector/network trouble, abandons on a rejection, a void or expiry, skips without a box", async () => {
|
||||||
|
await seed();
|
||||||
|
let status = 503;
|
||||||
|
const fetchFn = vi.fn(async () => (status === 0 ? Promise.reject(new Error("ECONNREFUSED")) : new Response("", { status })));
|
||||||
|
const ob = new ReviewOutbox(db, silentLogger(), cfg, fetchFn);
|
||||||
|
await ob.enqueue(item, read);
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 0, failed: 0, deferred: 1 });
|
||||||
|
let row = db.select().from(carwashReviewOutbox).all()[0]!;
|
||||||
|
expect(row).toMatchObject({ status: "queued", attempts: 1, lastError: "HTTP 503" });
|
||||||
|
expect(Date.parse(row.nextAttemptAt!)).toBeGreaterThan(Date.now() + 60_000);
|
||||||
|
// Not due yet → untouched.
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 0, failed: 0, deferred: 0 });
|
||||||
|
// Due again: a network error defers too; a 422 abandons.
|
||||||
|
db.update(carwashReviewOutbox).set({ nextAttemptAt: null }).run();
|
||||||
|
status = 0;
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 0, failed: 0, deferred: 1 });
|
||||||
|
db.update(carwashReviewOutbox).set({ nextAttemptAt: null }).run();
|
||||||
|
status = 422;
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 0, failed: 1, deferred: 0 });
|
||||||
|
row = db.select().from(carwashReviewOutbox).all()[0]!;
|
||||||
|
expect(row).toMatchObject({ status: "failed", lastError: "rejected: HTTP 422" });
|
||||||
|
expect(row.image).toBeNull();
|
||||||
|
|
||||||
|
// A voided order is not a sample.
|
||||||
|
status = 200;
|
||||||
|
await ob.enqueue({ ...item, orderId: "o-1" }, read);
|
||||||
|
db.update(carwashOrders).set({ status: "void" }).run();
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 0, failed: 1, deferred: 0 });
|
||||||
|
// Expired items are abandoned without a request.
|
||||||
|
await ob.enqueue(item, read);
|
||||||
|
db.update(carwashReviewOutbox).set({ createdAt: new Date(Date.now() - (EXPIRE_DAYS + 1) * 86_400_000).toISOString() }).where(eq(carwashReviewOutbox.status, "queued")).run();
|
||||||
|
db.update(carwashOrders).set({ status: "open" }).run();
|
||||||
|
const before = fetchFn.mock.calls.length;
|
||||||
|
expect(await ob.drain()).toEqual({ sent: 0, failed: 1, deferred: 0 });
|
||||||
|
expect(fetchFn.mock.calls.length).toBe(before);
|
||||||
|
expect(ob.status().failed).toBe(3);
|
||||||
|
|
||||||
|
// Entry sampling: one in N entry reads becomes a package with the crop and the
|
||||||
|
// camera's class only — no order, no operator, no category.
|
||||||
|
const sampler = new ReviewOutbox(db, silentLogger(), { ...cfg, entrySample: 3 }, fetchFn);
|
||||||
|
expect([sampler.sampleEntry(), sampler.sampleEntry(), sampler.sampleEntry(), sampler.sampleEntry()]).toEqual([false, false, true, false]);
|
||||||
|
expect(ob.sampleEntry()).toBe(false); // entrySample 0 = off
|
||||||
|
// Per-camera dedup: the bridge's pass sample and the ticket entry's read, seconds
|
||||||
|
// apart from the same camera, are ONE car — the second is skipped and not counted.
|
||||||
|
vi.useFakeTimers();
|
||||||
|
try {
|
||||||
|
const every = new ReviewOutbox(db, silentLogger(), { ...cfg, entrySample: 1 }, fetchFn);
|
||||||
|
expect(every.sampleEntry("cam-a")).toBe(true);
|
||||||
|
expect(every.sampleEntry("cam-a")).toBe(false);
|
||||||
|
expect(every.sampleEntry("cam-b")).toBe(true); // another camera = another car
|
||||||
|
vi.advanceTimersByTime(ENTRY_DEDUP_MS + 1);
|
||||||
|
expect(every.sampleEntry("cam-a")).toBe(true);
|
||||||
|
expect(every.sampleEntry()).toBe(true); // camera unknown → no dedup
|
||||||
|
} finally {
|
||||||
|
vi.useRealTimers();
|
||||||
|
}
|
||||||
|
expect(await sampler.enqueueEntry(read)).toBe(true);
|
||||||
|
const entryRow = db.select().from(carwashReviewOutbox).where(eq(carwashReviewOutbox.orderId, "entry:snap-1")).get()!;
|
||||||
|
expect(entryRow.payload).toMatchObject({ v: 1, kind: "entry", booth: "booth-7", vision: { class: "car", confidence: 0.86 }, image: { plateBlurred: true } });
|
||||||
|
expect(entryRow.payload).not.toHaveProperty("operator");
|
||||||
|
expect(entryRow.payload).not.toHaveProperty("operatorCategory");
|
||||||
|
expect(entryRow.image!.length).toBeGreaterThan(500);
|
||||||
|
|
||||||
|
// No vehicle box, no snapshot, or upload off → nothing queued.
|
||||||
|
expect(await ob.enqueue(item, { ...read, box: null })).toBe(false);
|
||||||
|
expect(await ob.enqueue(item, { ...read, snapshotId: "gone" })).toBe(false);
|
||||||
|
expect(await new ReviewOutbox(db, silentLogger(), null, fetchFn).enqueue(item, read)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
import { eq } from "@parking/db";
|
||||||
|
|
||||||
|
describe("through the app", () => {
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
const saved = { ...process.env };
|
||||||
|
beforeEach(async () => {
|
||||||
|
delete process.env.MODULES_ENTITLED;
|
||||||
|
process.env.CARWASH_REVIEW_URL = "https://collector.overlay/ingest";
|
||||||
|
process.env.CARWASH_REVIEW_TOKEN = "tok";
|
||||||
|
process.env.CARWASH_REVIEW_BOOTH_ID = "booth-9";
|
||||||
|
process.env.CARWASH_REVIEW_ENTRY_SAMPLE = "1";
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
app = await buildServer({ db });
|
||||||
|
await app.ready();
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
for (const k of ["CARWASH_REVIEW_URL", "CARWASH_REVIEW_TOKEN", "CARWASH_REVIEW_BOOTH_ID", "CARWASH_REVIEW_ENTRY_SAMPLE"]) {
|
||||||
|
if (saved[k] === undefined) delete process.env[k];
|
||||||
|
else process.env[k] = saved[k];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a wash intake with a vehicle read queues a review item; the status route reports it", async () => {
|
||||||
|
const { username, password } = await seedUser(db, { username: "boss", roleId: "admin" });
|
||||||
|
const a = await login(app, username, password);
|
||||||
|
const hdrs = { cookie: a.cookie, "x-csrf-token": a.csrf };
|
||||||
|
seedTariff(db);
|
||||||
|
const s = (await app.inject({ method: "PUT", url: "/api/carwash/settings", headers: hdrs, payload: { categories: [{ name: "Vetura", visionClasses: ["car"] }], services: [{ name: "Standard" }], prices: [] } })).json();
|
||||||
|
const cat = s.categories[0].id, svc = s.services[0].id;
|
||||||
|
await app.inject({ method: "PUT", url: "/api/carwash/settings", headers: hdrs, payload: { prices: [{ categoryId: cat, serviceId: svc, priceMinor: 500 }] } });
|
||||||
|
await makeLog(db).append({ type: "vehicle_entry", source: "manual", identity: "T-R", occurredAt: minutesAgo(30), payload: { sessionRef: "T-R", category: "default" } });
|
||||||
|
db.insert(snapshots).values({ id: "snap-r", direction: "entry", identity: "T-R", contentType: "image/jpeg", bytes: await frame(), capturedAt: new Date().toISOString() }).run();
|
||||||
|
db.insert(deviceEvents).values({
|
||||||
|
id: "read-r", deviceId: "cam-1", category: "camera", kind: "read",
|
||||||
|
detail: { identity: "T-R", direction: "entry", bodyType: "car", bodyConfidence: 0.9, snapshotId: "snap-r", vehicleBox: CAR, plateBox: PLATE },
|
||||||
|
occurredAt: new Date().toISOString(),
|
||||||
|
}).run();
|
||||||
|
|
||||||
|
const order = await app.inject({ method: "POST", url: "/api/carwash/orders", headers: hdrs, payload: { identity: "T-R", categoryId: cat, serviceId: svc } });
|
||||||
|
expect(order.statusCode).toBe(201);
|
||||||
|
// Enqueue is fire-and-forget: give the crop a moment.
|
||||||
|
await vi.waitFor(() => expect(db.select().from(carwashReviewOutbox).all()).toHaveLength(1));
|
||||||
|
const status = (await app.inject({ method: "GET", url: "/api/carwash/review/status", headers: { cookie: a.cookie } })).json();
|
||||||
|
expect(status).toMatchObject({ enabled: true, boothId: "booth-9", queued: 1, sent: 0, entrySample: 1 });
|
||||||
|
|
||||||
|
// An ENTRY vehicle read announced by the core (snapshot.ts) is sampled by the module
|
||||||
|
// (1 in 1 here) into an entry package; an exit read is not.
|
||||||
|
deviceEventBus.emitVehicleRead({ identity: "T-X", direction: "exit", read: { bodyType: "car", confidence: 0.8, snapshotId: "snap-r", box: CAR, plateBox: PLATE } });
|
||||||
|
deviceEventBus.emitVehicleRead({ identity: "T-R", direction: "entry", read: { bodyType: "car", confidence: 0.8, snapshotId: "snap-r", box: CAR, plateBox: PLATE } });
|
||||||
|
await vi.waitFor(() => expect(db.select().from(carwashReviewOutbox).all()).toHaveLength(2));
|
||||||
|
const rows = db.select().from(carwashReviewOutbox).all();
|
||||||
|
expect(rows.map((r) => (r.payload as { kind: string }).kind).sort()).toEqual(["entry", "wash"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,388 @@
|
|||||||
|
import { createHash, randomUUID } from "node:crypto";
|
||||||
|
import sharp from "sharp";
|
||||||
|
import { and, asc, carwashOrders, carwashReviewOutbox, eq, isNull, lte, or, snapshots, sql, type Db } from "@parking/db";
|
||||||
|
import type { NormBox, VehicleRead } from "@parking/shared";
|
||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
|
||||||
|
// The Car Wash REVIEW OUTBOX — booth side (wiki/concepts/vision-review-outbox.md).
|
||||||
|
//
|
||||||
|
// The operator's category choice at intake is a HYPOTHESIS, not truth (the threat model:
|
||||||
|
// the operator may err or cheat). So every wash order that has a vehicle read queues a
|
||||||
|
// small package for a trusted remote reviewer: the vehicle CROP cut out of the entry
|
||||||
|
// snapshot with the plate BLURRED, the operator's choice, and what the camera thought.
|
||||||
|
// The reviewer's verdict becomes the training label for the body-type classifier (phase
|
||||||
|
// B) and, per operator, the honest-mistake / fraud rate.
|
||||||
|
//
|
||||||
|
// Rules that shape this file:
|
||||||
|
// - OFFLINE-FIRST: the wash never waits. Enqueue is fire-and-forget off the intake path;
|
||||||
|
// a background loop drains the queue when the private overlay (Netbird) is up, with
|
||||||
|
// backoff, and gives up loudly after EXPIRE_DAYS.
|
||||||
|
// - ONE-WAY: the booth POSTs; nothing ever comes back into the booth's decisions. The
|
||||||
|
// signed ledger stays the only record of what happened at the wash.
|
||||||
|
// - NOTHING THAT NAMES THE SITE LEAVES: only the crop (no walls, no camera OSD, no
|
||||||
|
// bystanders), the plate blurred in place, a per-booth pseudonymous id set at deploy,
|
||||||
|
// the operator as a keyed hash. The mapping back to people and places stays with the
|
||||||
|
// reviewer, off the collector.
|
||||||
|
// - THE NETWORK IS NOT THE AUTH: a per-booth bearer token on top of the overlay; the
|
||||||
|
// booth can do nothing at the collector but this one POST.
|
||||||
|
|
||||||
|
export interface ReviewUploadConfig {
|
||||||
|
/** The collector's ingest URL (reachable only over the overlay). */
|
||||||
|
readonly url: string;
|
||||||
|
/** Per-booth bearer token. */
|
||||||
|
readonly token: string;
|
||||||
|
/** Pseudonymous booth id — a label the reviewer maps to a site; never the site name. */
|
||||||
|
readonly boothId: string;
|
||||||
|
readonly intervalSec: number;
|
||||||
|
/** Queue one in N ENTRY vehicle reads (no order attached) for the reviewer — the gate
|
||||||
|
* view is exactly what the classifier is trained on, and the entry stream is many times
|
||||||
|
* the wash stream. 0 = off. */
|
||||||
|
readonly entrySample: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** From the server env (Komodo stack env). All three of URL, token and booth id, or off. */
|
||||||
|
export function reviewUploadConfigFromEnv(env: NodeJS.ProcessEnv = process.env): ReviewUploadConfig | null {
|
||||||
|
const url = (env.CARWASH_REVIEW_URL ?? "").trim();
|
||||||
|
const token = (env.CARWASH_REVIEW_TOKEN ?? "").trim();
|
||||||
|
const boothId = (env.CARWASH_REVIEW_BOOTH_ID ?? "").trim();
|
||||||
|
if (!url || !token || !boothId) return null;
|
||||||
|
const raw = Number(env.CARWASH_REVIEW_INTERVAL_SEC ?? 60);
|
||||||
|
const sample = Number(env.CARWASH_REVIEW_ENTRY_SAMPLE ?? 0);
|
||||||
|
return {
|
||||||
|
url, token, boothId,
|
||||||
|
intervalSec: Number.isFinite(raw) && raw >= 10 ? raw : 60,
|
||||||
|
entrySample: Number.isInteger(sample) && sample > 0 ? sample : 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The crop's longest edge, in pixels — enough for a reviewer and a classifier, small
|
||||||
|
* enough that a day of washes is a few megabytes. */
|
||||||
|
export const CROP_MAX_EDGE = 640;
|
||||||
|
/** Margin around the detector's box, as a fraction of the box (context for the reviewer). */
|
||||||
|
const CROP_MARGIN = 0.08;
|
||||||
|
/** Items older than this are abandoned (failed "expired") — a booth cut off for two weeks
|
||||||
|
* should not resurface a fortnight of crops in one burst. */
|
||||||
|
export const EXPIRE_DAYS = 14;
|
||||||
|
/** Two entry reads from the same camera closer than this are one car (the ANPR bridge's
|
||||||
|
* per-detection pass sample, then the ticket entry's session-tied read ~seconds later). */
|
||||||
|
export const ENTRY_DEDUP_MS = 30_000;
|
||||||
|
/** Backoff: 1 min · 2^attempts, capped. */
|
||||||
|
const BACKOFF_BASE_MS = 60_000;
|
||||||
|
const BACKOFF_CAP_MS = 6 * 60 * 60 * 1000;
|
||||||
|
const UPLOAD_TIMEOUT_MS = 20_000;
|
||||||
|
|
||||||
|
/** What one order contributes to the package (the service hands this over at intake). */
|
||||||
|
export interface ReviewItemInput {
|
||||||
|
readonly orderId: string;
|
||||||
|
readonly createdAt: string;
|
||||||
|
readonly createdBy: string;
|
||||||
|
readonly categoryId: string;
|
||||||
|
readonly categoryName: string;
|
||||||
|
/** The vision classes the chosen category covers at this site (its mapping) — lets the
|
||||||
|
* reviewer's class be judged against the operator's category without the site's setup. */
|
||||||
|
readonly categoryClasses: readonly string[];
|
||||||
|
readonly serviceName: string;
|
||||||
|
readonly visionCategoryId: string | null;
|
||||||
|
readonly downgraded: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cut the vehicle out of the snapshot and blur the plate inside it. Boxes are fractions
|
||||||
|
* of the frame, so this works on the stored (downscaled) copy. Returns a JPEG.
|
||||||
|
*/
|
||||||
|
export async function makeReviewCrop(
|
||||||
|
snapshotBytes: Buffer,
|
||||||
|
box: NormBox,
|
||||||
|
plateBox: NormBox | null | undefined,
|
||||||
|
): Promise<{ bytes: Buffer; width: number; height: number; plateBlurred: boolean }> {
|
||||||
|
const img = sharp(snapshotBytes, { failOn: "none" }).rotate();
|
||||||
|
const meta = await img.metadata();
|
||||||
|
const W = meta.width ?? 0;
|
||||||
|
const H = meta.height ?? 0;
|
||||||
|
if (!W || !H) throw new Error("snapshot has no dimensions");
|
||||||
|
const px = (b: NormBox) => ({
|
||||||
|
left: Math.round(b.x1 * W), top: Math.round(b.y1 * H),
|
||||||
|
right: Math.round(b.x2 * W), bottom: Math.round(b.y2 * H),
|
||||||
|
});
|
||||||
|
const v = px(box);
|
||||||
|
const mw = Math.round((v.right - v.left) * CROP_MARGIN);
|
||||||
|
const mh = Math.round((v.bottom - v.top) * CROP_MARGIN);
|
||||||
|
const left = Math.max(0, v.left - mw);
|
||||||
|
const top = Math.max(0, v.top - mh);
|
||||||
|
const right = Math.min(W, v.right + mw);
|
||||||
|
const bottom = Math.min(H, v.bottom + mh);
|
||||||
|
const width = right - left;
|
||||||
|
const height = bottom - top;
|
||||||
|
if (width < 8 || height < 8) throw new Error("vehicle box too small to crop");
|
||||||
|
|
||||||
|
let crop = img.clone().extract({ left, top, width, height });
|
||||||
|
let plateBlurred = false;
|
||||||
|
if (plateBox) {
|
||||||
|
// The plate region, in CROP coordinates, padded a little so the blur eats the edges.
|
||||||
|
const p = px(plateBox);
|
||||||
|
const pad = Math.round(Math.max(p.right - p.left, p.bottom - p.top) * 0.25);
|
||||||
|
const pl = Math.max(0, p.left - pad - left);
|
||||||
|
const pt = Math.max(0, p.top - pad - top);
|
||||||
|
const pr = Math.min(width, p.right + pad - left);
|
||||||
|
const pb = Math.min(height, p.bottom + pad - top);
|
||||||
|
if (pr - pl >= 2 && pb - pt >= 2) {
|
||||||
|
const region = await sharp(await crop.clone().toBuffer())
|
||||||
|
.extract({ left: pl, top: pt, width: pr - pl, height: pb - pt })
|
||||||
|
.blur(Math.max(6, Math.round((pr - pl) / 6)))
|
||||||
|
.toBuffer();
|
||||||
|
crop = sharp(await crop.toBuffer()).composite([{ input: region, left: pl, top: pt }]);
|
||||||
|
plateBlurred = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const out = await crop
|
||||||
|
.resize({ width: CROP_MAX_EDGE, height: CROP_MAX_EDGE, fit: "inside", withoutEnlargement: true })
|
||||||
|
.jpeg({ quality: 85, mozjpeg: true })
|
||||||
|
.toBuffer({ resolveWithObject: true });
|
||||||
|
return { bytes: out.data, width: out.info.width, height: out.info.height, plateBlurred };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The operator as a keyed hash — stable per booth so the reviewer can count per person,
|
||||||
|
* meaningless anywhere else. */
|
||||||
|
export function operatorRef(boothId: string, username: string): string {
|
||||||
|
return createHash("sha256").update(`${boothId}:${username}`).digest("hex").slice(0, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
type FetchLike = (input: string, init: RequestInit) => Promise<Response>;
|
||||||
|
|
||||||
|
export interface OutboxStatus {
|
||||||
|
readonly enabled: boolean;
|
||||||
|
readonly boothId: string | null;
|
||||||
|
readonly queued: number;
|
||||||
|
readonly sent: number;
|
||||||
|
readonly failed: number;
|
||||||
|
readonly lastSentAt: string | null;
|
||||||
|
readonly lastError: string | null;
|
||||||
|
/** 0 = entry sampling off; N = one in N entry reads is queued. */
|
||||||
|
readonly entrySample: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ReviewOutbox {
|
||||||
|
readonly #db: Db;
|
||||||
|
readonly #logger: FastifyBaseLogger;
|
||||||
|
readonly #cfg: ReviewUploadConfig | null;
|
||||||
|
readonly #fetch: FetchLike;
|
||||||
|
#timer: NodeJS.Timeout | null = null;
|
||||||
|
#draining = false;
|
||||||
|
#entrySeen = 0;
|
||||||
|
/** Last entry sample per camera: the bridge's pass sample and the ticket entry's read are
|
||||||
|
* the SAME car seconds apart — one goes, the other is skipped (and not counted). */
|
||||||
|
readonly #lastEntryByDevice = new Map<string, number>();
|
||||||
|
|
||||||
|
constructor(db: Db, logger: FastifyBaseLogger, cfg: ReviewUploadConfig | null, fetchFn?: FetchLike) {
|
||||||
|
this.#db = db;
|
||||||
|
this.#logger = logger;
|
||||||
|
this.#cfg = cfg;
|
||||||
|
this.#fetch = fetchFn ?? ((input, init) => fetch(input, init));
|
||||||
|
}
|
||||||
|
|
||||||
|
get enabled(): boolean {
|
||||||
|
return this.#cfg != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Queue one order's package. Fire-and-forget: the caller does NOT await this on the
|
||||||
|
* intake path; every failure is logged, none is thrown. Skipped when there is no
|
||||||
|
* vehicle box (nothing to crop — a frame without a detected vehicle is no training
|
||||||
|
* sample) or when upload is not configured (an unbounded queue nobody drains). */
|
||||||
|
async enqueue(item: ReviewItemInput, read: VehicleRead): Promise<boolean> {
|
||||||
|
if (!this.#cfg) return false;
|
||||||
|
return this.#queue(item.orderId, read, (id, crop) => ({
|
||||||
|
v: 1,
|
||||||
|
kind: "wash",
|
||||||
|
booth: this.#cfg!.boothId,
|
||||||
|
item: id,
|
||||||
|
order: item.orderId,
|
||||||
|
at: item.createdAt,
|
||||||
|
operator: operatorRef(this.#cfg!.boothId, item.createdBy),
|
||||||
|
operatorCategory: { id: item.categoryId, name: item.categoryName, classes: [...item.categoryClasses] },
|
||||||
|
service: item.serviceName,
|
||||||
|
vision: { class: read.bodyType, confidence: read.confidence, categoryId: item.visionCategoryId },
|
||||||
|
downgraded: item.downgraded,
|
||||||
|
image: crop,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Every Nth entry read is a sample (N = entrySample); the caller queues it. Counted
|
||||||
|
* in-process, so "1 in 5" is exactly that across a booth's day. */
|
||||||
|
sampleEntry(deviceId?: string): boolean {
|
||||||
|
const n = this.#cfg?.entrySample ?? 0;
|
||||||
|
if (n <= 0) return false;
|
||||||
|
if (deviceId) {
|
||||||
|
const now = Date.now();
|
||||||
|
const last = this.#lastEntryByDevice.get(deviceId);
|
||||||
|
if (last != null && now - last < ENTRY_DEDUP_MS) return false;
|
||||||
|
this.#lastEntryByDevice.set(deviceId, now);
|
||||||
|
}
|
||||||
|
this.#entrySeen += 1;
|
||||||
|
return this.#entrySeen % n === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Queue an ENTRY sample: the crop and the camera's class only — no order, no operator,
|
||||||
|
* no category. Pure training material in the gate view; the reviewer labels it. */
|
||||||
|
async enqueueEntry(read: VehicleRead): Promise<boolean> {
|
||||||
|
if (!this.#cfg) return false;
|
||||||
|
return this.#queue(`entry:${read.snapshotId ?? "?"}`, read, (id, crop) => ({
|
||||||
|
v: 1,
|
||||||
|
kind: "entry",
|
||||||
|
booth: this.#cfg!.boothId,
|
||||||
|
item: id,
|
||||||
|
at: new Date().toISOString(),
|
||||||
|
vision: { class: read.bodyType, confidence: read.confidence },
|
||||||
|
image: crop,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async #queue(
|
||||||
|
ref: string,
|
||||||
|
read: VehicleRead,
|
||||||
|
build: (id: string, image: { width: number; height: number; plateBlurred: boolean }) => Record<string, unknown>,
|
||||||
|
): Promise<boolean> {
|
||||||
|
if (!read.box || !read.snapshotId) return false;
|
||||||
|
try {
|
||||||
|
const snap = this.#db.select().from(snapshots).where(eq(snapshots.id, read.snapshotId)).get();
|
||||||
|
if (!snap) {
|
||||||
|
this.#logger.info(`carwash review: snapshot ${read.snapshotId} gone (pruned) — ${ref} not queued`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const crop = await makeReviewCrop(snap.bytes, read.box, read.plateBox);
|
||||||
|
const id = randomUUID();
|
||||||
|
const payload = build(id, { width: crop.width, height: crop.height, plateBlurred: crop.plateBlurred });
|
||||||
|
this.#db
|
||||||
|
.insert(carwashReviewOutbox)
|
||||||
|
.values({ id, orderId: ref, createdAt: new Date().toISOString(), status: "queued", attempts: 0, nextAttemptAt: null, image: crop.bytes, payload })
|
||||||
|
.run();
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
this.#logger.warn(`carwash review: could not queue ${ref}: ${(err as Error).message}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
start(): void {
|
||||||
|
if (!this.#cfg || this.#timer) return;
|
||||||
|
const tick = () => {
|
||||||
|
void this.drain().catch((err) => this.#logger.warn(`carwash review: drain failed: ${(err as Error).message}`));
|
||||||
|
};
|
||||||
|
this.#timer = setInterval(tick, this.#cfg.intervalSec * 1000);
|
||||||
|
this.#timer.unref?.();
|
||||||
|
setTimeout(tick, 5_000).unref?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
if (this.#timer) clearInterval(this.#timer);
|
||||||
|
this.#timer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Send what is due, oldest first. Returns the tally; never throws for a single item. */
|
||||||
|
async drain(limit = 20): Promise<{ sent: number; failed: number; deferred: number }> {
|
||||||
|
const tally = { sent: 0, failed: 0, deferred: 0 };
|
||||||
|
if (!this.#cfg || this.#draining) return tally;
|
||||||
|
this.#draining = true;
|
||||||
|
try {
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const due = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashReviewOutbox)
|
||||||
|
.where(and(eq(carwashReviewOutbox.status, "queued"), or(isNull(carwashReviewOutbox.nextAttemptAt), lte(carwashReviewOutbox.nextAttemptAt, now))))
|
||||||
|
.orderBy(asc(carwashReviewOutbox.createdAt))
|
||||||
|
.limit(limit)
|
||||||
|
.all();
|
||||||
|
for (const row of due) {
|
||||||
|
const outcome = await this.#send(row);
|
||||||
|
tally[outcome] += 1;
|
||||||
|
}
|
||||||
|
if (tally.sent || tally.failed) this.#logger.info(`carwash review: sent ${tally.sent}, failed ${tally.failed}, deferred ${tally.deferred}`);
|
||||||
|
} finally {
|
||||||
|
this.#draining = false;
|
||||||
|
}
|
||||||
|
return tally;
|
||||||
|
}
|
||||||
|
|
||||||
|
async #send(row: typeof carwashReviewOutbox.$inferSelect): Promise<"sent" | "failed" | "deferred"> {
|
||||||
|
const cfg = this.#cfg!;
|
||||||
|
const ageMs = Date.now() - Date.parse(row.createdAt);
|
||||||
|
if (ageMs > EXPIRE_DAYS * 24 * 60 * 60 * 1000) return this.#fail(row, `expired after ${EXPIRE_DAYS} days`);
|
||||||
|
// A wash voided before delivery is not a sample (and not a decision to review).
|
||||||
|
const order = this.#db.select({ status: carwashOrders.status }).from(carwashOrders).where(eq(carwashOrders.id, row.orderId)).get();
|
||||||
|
if (order?.status === "void") return this.#fail(row, "order voided");
|
||||||
|
if (!row.image) return this.#fail(row, "image missing");
|
||||||
|
|
||||||
|
const form = new FormData();
|
||||||
|
form.set("meta", JSON.stringify(row.payload));
|
||||||
|
form.set("image", new Blob([new Uint8Array(row.image)], { type: "image/jpeg" }), `${row.id}.jpg`);
|
||||||
|
const ac = new AbortController();
|
||||||
|
const t = setTimeout(() => ac.abort(), UPLOAD_TIMEOUT_MS);
|
||||||
|
try {
|
||||||
|
const res = await this.#fetch(cfg.url, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { authorization: `Bearer ${cfg.token}`, "x-booth-id": cfg.boothId },
|
||||||
|
body: form,
|
||||||
|
signal: ac.signal,
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
this.#db
|
||||||
|
.update(carwashReviewOutbox)
|
||||||
|
.set({ status: "sent", sentAt: new Date().toISOString(), image: null, lastError: null, attempts: row.attempts + 1 })
|
||||||
|
.where(eq(carwashReviewOutbox.id, row.id))
|
||||||
|
.run();
|
||||||
|
return "sent";
|
||||||
|
}
|
||||||
|
// The collector refused the package itself → no retry will help.
|
||||||
|
if ([400, 404, 413, 415, 422].includes(res.status)) return this.#fail(row, `rejected: HTTP ${res.status}`);
|
||||||
|
// Everything else (auth not yet fixed, throttled, collector down) → try again later.
|
||||||
|
return this.#defer(row, `HTTP ${res.status}`);
|
||||||
|
} catch (err) {
|
||||||
|
return this.#defer(row, (err as Error).name === "AbortError" ? "timeout" : (err as Error).message);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#fail(row: typeof carwashReviewOutbox.$inferSelect, why: string): "failed" {
|
||||||
|
this.#db
|
||||||
|
.update(carwashReviewOutbox)
|
||||||
|
.set({ status: "failed", lastError: why, image: null, attempts: row.attempts + 1 })
|
||||||
|
.where(eq(carwashReviewOutbox.id, row.id))
|
||||||
|
.run();
|
||||||
|
this.#logger.warn(`carwash review: item ${row.id} (order ${row.orderId}) abandoned — ${why}`);
|
||||||
|
return "failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
#defer(row: typeof carwashReviewOutbox.$inferSelect, why: string): "deferred" {
|
||||||
|
const attempts = row.attempts + 1;
|
||||||
|
const wait = Math.min(BACKOFF_BASE_MS * 2 ** Math.min(attempts, 20), BACKOFF_CAP_MS);
|
||||||
|
this.#db
|
||||||
|
.update(carwashReviewOutbox)
|
||||||
|
.set({ attempts, lastError: why, nextAttemptAt: new Date(Date.now() + wait).toISOString() })
|
||||||
|
.where(eq(carwashReviewOutbox.id, row.id))
|
||||||
|
.run();
|
||||||
|
return "deferred";
|
||||||
|
}
|
||||||
|
|
||||||
|
status(): OutboxStatus {
|
||||||
|
const count = (s: "queued" | "sent" | "failed") =>
|
||||||
|
this.#db.select({ n: sql<number>`count(*)` }).from(carwashReviewOutbox).where(eq(carwashReviewOutbox.status, s)).get()?.n ?? 0;
|
||||||
|
const lastSent = this.#db.select({ at: sql<string | null>`max(${carwashReviewOutbox.sentAt})` }).from(carwashReviewOutbox).get()?.at ?? null;
|
||||||
|
const lastErr = this.#db
|
||||||
|
.select({ e: carwashReviewOutbox.lastError })
|
||||||
|
.from(carwashReviewOutbox)
|
||||||
|
.where(sql`${carwashReviewOutbox.lastError} is not null`)
|
||||||
|
.orderBy(sql`coalesce(${carwashReviewOutbox.sentAt}, ${carwashReviewOutbox.nextAttemptAt}, ${carwashReviewOutbox.createdAt}) desc`)
|
||||||
|
.limit(1)
|
||||||
|
.get()?.e ?? null;
|
||||||
|
return {
|
||||||
|
enabled: this.enabled,
|
||||||
|
boothId: this.#cfg?.boothId ?? null,
|
||||||
|
entrySample: this.#cfg?.entrySample ?? 0,
|
||||||
|
queued: count("queued"),
|
||||||
|
sent: count("sent"),
|
||||||
|
failed: count("failed"),
|
||||||
|
lastSentAt: lastSent,
|
||||||
|
lastError: lastErr,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import type { FastifyInstance, FastifyReply } from "fastify";
|
||||||
|
import type { Tender } from "@parking/shared";
|
||||||
|
import { requireAnyPermission, requirePermission } from "../../auth.js";
|
||||||
|
import { requireModule } from "../../modules.js";
|
||||||
|
import { NoShiftOpenError } from "../../shift-service.js";
|
||||||
|
import type { ServerModuleDeps } from "../index.js";
|
||||||
|
import type { ReviewOutbox } from "./review-outbox.js";
|
||||||
|
import { CarwashError, CarwashService, isPayAt, type SettingsBody } from "./service.js";
|
||||||
|
|
||||||
|
// HTTP surface of the Car Wash module. Every route is behind the venue-module gate
|
||||||
|
// FIRST (403 module_disabled), then a permission:
|
||||||
|
// settings (master data) site:read / site:update — the site admin's job
|
||||||
|
// queue / ticket lookup carwash:read — the wash desk
|
||||||
|
// intake carwash:create
|
||||||
|
// done / bay payment / void carwash:update
|
||||||
|
// The sponsorship PROGRAM itself is a validation program row (id "carwash") and is
|
||||||
|
// composed through the existing /api/validation/programs/:id route (site:update).
|
||||||
|
|
||||||
|
function sendError(reply: FastifyReply, err: unknown): FastifyReply {
|
||||||
|
if (err instanceof CarwashError) {
|
||||||
|
return reply.code(err.status).send({ error: err.message, ...(err.code ? { code: err.code } : {}) });
|
||||||
|
}
|
||||||
|
if (err instanceof NoShiftOpenError) {
|
||||||
|
// The bay takes money on the CARWASH till: the wash operator's own shift must be
|
||||||
|
// open (the booth's does not count). The desk shows its shift control on this code.
|
||||||
|
return reply.code(409).send({ error: err.message, code: "no_shift", till: err.till });
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function carwashRoutes(app: FastifyInstance, deps: ServerModuleDeps, service: CarwashService, outbox?: ReviewOutbox): Promise<void> {
|
||||||
|
const moduleOn = requireModule(deps.db, "carwash");
|
||||||
|
// The price list is the desk's working data as much as Setup's: the wash operator
|
||||||
|
// reads it under the module's own permission (the Wash operator job holds no site:*).
|
||||||
|
const settingsRead = [moduleOn, requireAnyPermission("carwash:read", "site:read")];
|
||||||
|
const settingsWrite = [moduleOn, requirePermission("site:update")];
|
||||||
|
const read = [moduleOn, requirePermission("carwash:read")];
|
||||||
|
const create = [moduleOn, requirePermission("carwash:create")];
|
||||||
|
const update = [moduleOn, requirePermission("carwash:update")];
|
||||||
|
|
||||||
|
app.get("/api/carwash/settings", { preHandler: settingsRead }, async () => service.settings());
|
||||||
|
// The review outbox's health (Setup → Car wash): how many decisions wait for the
|
||||||
|
// reviewer, how many went, the last error. Site admin's read.
|
||||||
|
app.get("/api/carwash/review/status", { preHandler: settingsRead }, async () =>
|
||||||
|
outbox?.status() ?? { enabled: false, boothId: null, queued: 0, sent: 0, failed: 0, lastSentAt: null, lastError: null, entrySample: 0 },
|
||||||
|
);
|
||||||
|
|
||||||
|
app.put<{ Body: SettingsBody }>("/api/carwash/settings", { preHandler: settingsWrite }, async (req, reply) => {
|
||||||
|
try {
|
||||||
|
return await service.saveSettings(req.body ?? {}, req.user.username);
|
||||||
|
} catch (err) {
|
||||||
|
return sendError(reply, err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get<{ Params: { identity: string } }>("/api/carwash/session/:identity", { preHandler: read }, async (req) =>
|
||||||
|
service.lookup(req.params.identity),
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get<{ Querystring: { scope?: string; limit?: string } }>("/api/carwash/orders", { preHandler: read }, async (req) => {
|
||||||
|
if (req.query.scope === "recent") return { orders: service.recentOrders(Number(req.query.limit) || 100) };
|
||||||
|
return { orders: service.openOrders() };
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post<{ Body: { identity?: string; categoryId?: string; serviceId?: string; payAt?: string } }>(
|
||||||
|
"/api/carwash/orders",
|
||||||
|
{ preHandler: create },
|
||||||
|
async (req, reply) => {
|
||||||
|
const b = req.body ?? {};
|
||||||
|
// payAt is a SITE setting now; the desk no longer sends it. Accept it only when it
|
||||||
|
// matches (the service refuses a mismatch) so a stale client cannot pick the till.
|
||||||
|
if (b.payAt !== undefined && !isPayAt(b.payAt)) return reply.code(400).send({ error: "payAt must be booth|bay" });
|
||||||
|
try {
|
||||||
|
const order = await service.createOrder({
|
||||||
|
identity: String(b.identity ?? ""),
|
||||||
|
categoryId: String(b.categoryId ?? ""),
|
||||||
|
serviceId: String(b.serviceId ?? ""),
|
||||||
|
...(b.payAt !== undefined ? { payAt: b.payAt } : {}),
|
||||||
|
actor: req.user.username,
|
||||||
|
});
|
||||||
|
return reply.code(201).send(order);
|
||||||
|
} catch (err) {
|
||||||
|
return sendError(reply, err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
app.post<{ Params: { id: string } }>("/api/carwash/orders/:id/done", { preHandler: update }, async (req, reply) => {
|
||||||
|
try {
|
||||||
|
return await service.markDone(req.params.id, req.user.username);
|
||||||
|
} catch (err) {
|
||||||
|
return sendError(reply, err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post<{ Params: { id: string }; Body: { tender?: Tender } }>(
|
||||||
|
"/api/carwash/orders/:id/pay",
|
||||||
|
{ preHandler: update },
|
||||||
|
async (req, reply) => {
|
||||||
|
try {
|
||||||
|
return await service.payAtBay(req.params.id, (req.body?.tender ?? "cash") as Tender, req.user.username);
|
||||||
|
} catch (err) {
|
||||||
|
return sendError(reply, err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
app.post<{ Params: { id: string }; Body: { reason?: string } }>(
|
||||||
|
"/api/carwash/orders/:id/void",
|
||||||
|
{ preHandler: update },
|
||||||
|
async (req, reply) => {
|
||||||
|
try {
|
||||||
|
return await service.voidOrder(req.params.id, String(req.body?.reason ?? "").trim(), req.user.username);
|
||||||
|
} catch (err) {
|
||||||
|
return sendError(reply, err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,765 @@
|
|||||||
|
import { randomUUID } from "node:crypto";
|
||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import {
|
||||||
|
and,
|
||||||
|
asc,
|
||||||
|
carwashCategories,
|
||||||
|
carwashConfig,
|
||||||
|
carwashOrders,
|
||||||
|
carwashPrices,
|
||||||
|
carwashServices,
|
||||||
|
desc,
|
||||||
|
eq,
|
||||||
|
inArray,
|
||||||
|
isNull,
|
||||||
|
type CarwashOrderRow,
|
||||||
|
type Db,
|
||||||
|
} from "@parking/db";
|
||||||
|
import {
|
||||||
|
CARWASH_PAY_AT,
|
||||||
|
CARWASH_PAY_AT_DEFAULT,
|
||||||
|
CARWASH_PROGRAM_ID,
|
||||||
|
type CarWashPayAt,
|
||||||
|
type CarwashOrderView,
|
||||||
|
type CarwashSettingsView,
|
||||||
|
type ChargeLine,
|
||||||
|
CARWASH_VISION_THRESHOLD_DEFAULT,
|
||||||
|
isVehicleClass,
|
||||||
|
reasonPayload,
|
||||||
|
type VehicleClass,
|
||||||
|
type VehicleRead,
|
||||||
|
type Tender,
|
||||||
|
type TillId,
|
||||||
|
} from "@parking/shared";
|
||||||
|
import type { EventLog } from "../../event-log.js";
|
||||||
|
import { vehicleForIdentity } from "../../plate-lookup.js";
|
||||||
|
import type { ReviewOutbox } from "./review-outbox.js";
|
||||||
|
import { effectiveModulesFor } from "../../modules.js";
|
||||||
|
import type { ChargeProvider, PayStation } from "../../pay-station.js";
|
||||||
|
import type { ShiftService } from "../../shift-service.js";
|
||||||
|
import { applyValidation, liveValidations } from "../../validations.js";
|
||||||
|
import type { ServerModuleDeps } from "../index.js";
|
||||||
|
|
||||||
|
// Car Wash — the module's whole behaviour (wiki/decisions/venue-modules.md, "Car Wash —
|
||||||
|
// the pilot module" + "v1 answers"). Master data is mutable rows; every order freezes
|
||||||
|
// what it sold (names + price) and signs its life onto the ledger; money at the bay is
|
||||||
|
// a signed `carwash_payment`; money at the booth rides the parking `payment` as a
|
||||||
|
// charge line (ChargeProvider below). The parking sponsorship is the site's "carwash"
|
||||||
|
// VALIDATION program, applied through the shared applyValidation() when a wash is done
|
||||||
|
// — the wash never touches parking code, it talks to the core through ServerModuleDeps.
|
||||||
|
|
||||||
|
/** A refusal the route maps to an HTTP status. */
|
||||||
|
/** The till bay money lands on — declared by the module manifest (MODULES). */
|
||||||
|
const CARWASH_TILL: TillId = "carwash";
|
||||||
|
|
||||||
|
export class CarwashError extends Error {
|
||||||
|
constructor(
|
||||||
|
readonly status: 400 | 404 | 409,
|
||||||
|
message: string,
|
||||||
|
readonly code?: string,
|
||||||
|
) {
|
||||||
|
super(message);
|
||||||
|
this.name = "CarwashError";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SettingsBody {
|
||||||
|
categories?: { id?: string; name?: string; active?: boolean; visionClasses?: unknown }[];
|
||||||
|
services?: { id?: string; name?: string; active?: boolean }[];
|
||||||
|
prices?: { categoryId?: string; serviceId?: string; priceMinor?: number }[];
|
||||||
|
/** Where wash money is taken at this site (site-level policy). */
|
||||||
|
payAt?: unknown;
|
||||||
|
visionThreshold?: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreateOrderInput {
|
||||||
|
identity: string;
|
||||||
|
categoryId: string;
|
||||||
|
serviceId: string;
|
||||||
|
/** Optional — the SITE policy decides; a stale client that sends a different value
|
||||||
|
* is refused (409 pay_at_policy) rather than silently overridden. */
|
||||||
|
payAt?: CarWashPayAt;
|
||||||
|
actor: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TicketLookup {
|
||||||
|
identity: string;
|
||||||
|
found: boolean;
|
||||||
|
open: boolean;
|
||||||
|
subscription: boolean;
|
||||||
|
plate: string | null;
|
||||||
|
enteredAt: string | null;
|
||||||
|
currency: string | null;
|
||||||
|
orders: CarwashOrderView[];
|
||||||
|
/** What the camera saw at entry (advisory) and the category the site mapping
|
||||||
|
* suggests for it — the desk pre-selects it; the operator may change it. */
|
||||||
|
vision: VehicleRead | null;
|
||||||
|
suggestedCategoryId: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
||||||
|
|
||||||
|
/** Stable slug for a new master-data row: from the name, else a random id. */
|
||||||
|
function slugify(name: string): string {
|
||||||
|
const s = name
|
||||||
|
.toLowerCase()
|
||||||
|
.normalize("NFD")
|
||||||
|
.replace(/[\u0300-\u036f]/g, "")
|
||||||
|
.replace(/[^a-z0-9]+/g, "-")
|
||||||
|
.replace(/^-+|-+$/g, "")
|
||||||
|
.slice(0, 40);
|
||||||
|
return s || randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CarwashService {
|
||||||
|
readonly #db: Db;
|
||||||
|
readonly #log: EventLog;
|
||||||
|
readonly #pay: PayStation;
|
||||||
|
readonly #shift: ShiftService;
|
||||||
|
readonly #logger: FastifyBaseLogger;
|
||||||
|
readonly #outbox: ReviewOutbox | null;
|
||||||
|
|
||||||
|
constructor(deps: ServerModuleDeps, logger: FastifyBaseLogger, outbox: ReviewOutbox | null = null) {
|
||||||
|
this.#db = deps.db;
|
||||||
|
this.#log = deps.eventLog;
|
||||||
|
this.#pay = deps.payStation;
|
||||||
|
this.#shift = deps.shiftService;
|
||||||
|
this.#logger = logger;
|
||||||
|
this.#outbox = outbox;
|
||||||
|
}
|
||||||
|
|
||||||
|
#enabled(): boolean {
|
||||||
|
return effectiveModulesFor(this.#db).includes("carwash");
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Settings (master data) -------------------------------------------------
|
||||||
|
|
||||||
|
settings(): CarwashSettingsView {
|
||||||
|
const categories = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashCategories)
|
||||||
|
.where(isNull(carwashCategories.deletedAt))
|
||||||
|
.orderBy(asc(carwashCategories.sortOrder), asc(carwashCategories.name))
|
||||||
|
.all()
|
||||||
|
.map((r) => ({ id: r.id, name: r.name, sortOrder: r.sortOrder, active: r.active, visionClasses: r.visionClasses.filter(isVehicleClass) }));
|
||||||
|
const services = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashServices)
|
||||||
|
.where(isNull(carwashServices.deletedAt))
|
||||||
|
.orderBy(asc(carwashServices.sortOrder), asc(carwashServices.name))
|
||||||
|
.all()
|
||||||
|
.map((r) => ({ id: r.id, name: r.name, sortOrder: r.sortOrder, active: r.active }));
|
||||||
|
const live = new Set([...categories.map((c) => c.id), ...services.map((s) => s.id)]);
|
||||||
|
const prices = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashPrices)
|
||||||
|
.all()
|
||||||
|
.filter((p) => live.has(p.categoryId) && live.has(p.serviceId))
|
||||||
|
.map((p) => ({ categoryId: p.categoryId, serviceId: p.serviceId, priceMinor: p.priceMinor }));
|
||||||
|
return { categories, services, prices, currency: this.#currency(), payAt: this.payAt(), visionThreshold: this.visionThreshold() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The site's wash-payment policy (Setup → Car wash). Missing row = the default. */
|
||||||
|
payAt(): CarWashPayAt {
|
||||||
|
const row = this.#db.select().from(carwashConfig).where(eq(carwashConfig.id, 1)).get();
|
||||||
|
return row?.payAt ?? CARWASH_PAY_AT_DEFAULT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Confidence floor for a vision class to flag a category downgrade (site config). */
|
||||||
|
visionThreshold(): number {
|
||||||
|
const row = this.#db.select().from(carwashConfig).where(eq(carwashConfig.id, 1)).get();
|
||||||
|
return row?.visionThreshold ?? CARWASH_VISION_THRESHOLD_DEFAULT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The category the site mapping suggests for a vision class (first active category
|
||||||
|
* listing it, in display order), or null when unmapped. */
|
||||||
|
#categoryForClass(cls: VehicleClass): { id: string; name: string } | null {
|
||||||
|
const rows = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashCategories)
|
||||||
|
.where(isNull(carwashCategories.deletedAt))
|
||||||
|
.orderBy(asc(carwashCategories.sortOrder), asc(carwashCategories.name))
|
||||||
|
.all();
|
||||||
|
const hit = rows.find((r) => r.active && r.visionClasses.includes(cls));
|
||||||
|
return hit ? { id: hit.id, name: hit.name } : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The site's currency = the active tariff's (the wash is priced in the same money
|
||||||
|
* the booth takes). null when no tariff is published yet. */
|
||||||
|
#currency(): string | null {
|
||||||
|
try {
|
||||||
|
// Any open session's quote carries it; without one, fall back to the tariff table.
|
||||||
|
const row = this.#db.select().from(carwashOrders).orderBy(desc(carwashOrders.createdAt)).limit(1).get();
|
||||||
|
if (row) return row.currency;
|
||||||
|
} catch {
|
||||||
|
/* fall through */
|
||||||
|
}
|
||||||
|
return this.#pay.activeCurrency();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Full-replacement save of the three lists. Rows missing from the body are
|
||||||
|
* soft-deleted (orders already reference names + prices by value, so nothing
|
||||||
|
* historical changes). Signs one config_change. */
|
||||||
|
async saveSettings(body: SettingsBody, actor: string): Promise<CarwashSettingsView> {
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const upsertList = (
|
||||||
|
table: typeof carwashCategories | typeof carwashServices,
|
||||||
|
items: { id?: string; name?: string; active?: boolean; visionClasses?: unknown }[] | undefined,
|
||||||
|
label: string,
|
||||||
|
): string[] => {
|
||||||
|
if (items === undefined) {
|
||||||
|
return this.#db.select({ id: table.id }).from(table).where(isNull(table.deletedAt)).all().map((r) => r.id);
|
||||||
|
}
|
||||||
|
if (!Array.isArray(items)) throw new CarwashError(400, `${label} must be an array`);
|
||||||
|
const keep: string[] = [];
|
||||||
|
let sort = 0;
|
||||||
|
const seen = new Set<string>();
|
||||||
|
for (const it of items) {
|
||||||
|
const name = String(it?.name ?? "").trim();
|
||||||
|
if (!name) throw new CarwashError(400, `${label}: every item needs a name`);
|
||||||
|
let id = typeof it.id === "string" && it.id.trim() ? it.id.trim() : slugify(name);
|
||||||
|
if (!ID_RE.test(id)) throw new CarwashError(400, `${label}: bad id "${id}"`);
|
||||||
|
// Two new items slugging to the same id → disambiguate rather than merge.
|
||||||
|
while (seen.has(id)) id = `${id}-${sort}`;
|
||||||
|
seen.add(id);
|
||||||
|
const active = it.active !== false;
|
||||||
|
// Vision mapping lives on CATEGORIES only; absent = keep what the row has.
|
||||||
|
let visionClasses: string[] | undefined;
|
||||||
|
if (table === carwashCategories && it.visionClasses !== undefined) {
|
||||||
|
if (!Array.isArray(it.visionClasses) || !it.visionClasses.every(isVehicleClass)) {
|
||||||
|
throw new CarwashError(400, `${label}: visionClasses must be an array of vehicle classes`);
|
||||||
|
}
|
||||||
|
visionClasses = [...new Set(it.visionClasses as string[])];
|
||||||
|
}
|
||||||
|
const existing = this.#db.select().from(table).where(eq(table.id, id)).get();
|
||||||
|
if (existing) {
|
||||||
|
this.#db.update(table).set({ name, sortOrder: sort, active, deletedAt: null, deletedBy: null, ...(visionClasses ? { visionClasses } : {}) }).where(eq(table.id, id)).run();
|
||||||
|
} else {
|
||||||
|
this.#db.insert(table).values({ id, name, sortOrder: sort, active, ...(visionClasses ? { visionClasses } : {}) }).run();
|
||||||
|
}
|
||||||
|
keep.push(id);
|
||||||
|
sort += 1;
|
||||||
|
}
|
||||||
|
const live = this.#db.select({ id: table.id }).from(table).where(isNull(table.deletedAt)).all();
|
||||||
|
for (const r of live) {
|
||||||
|
if (!keep.includes(r.id)) {
|
||||||
|
this.#db.update(table).set({ deletedAt: now, deletedBy: actor }).where(eq(table.id, r.id)).run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keep;
|
||||||
|
};
|
||||||
|
|
||||||
|
const categoryIds = upsertList(carwashCategories, body.categories, "categories");
|
||||||
|
const serviceIds = upsertList(carwashServices, body.services, "services");
|
||||||
|
|
||||||
|
if (body.prices !== undefined) {
|
||||||
|
if (!Array.isArray(body.prices)) throw new CarwashError(400, "prices must be an array");
|
||||||
|
const rows: { categoryId: string; serviceId: string; priceMinor: number }[] = [];
|
||||||
|
for (const p of body.prices) {
|
||||||
|
const categoryId = String(p?.categoryId ?? "");
|
||||||
|
const serviceId = String(p?.serviceId ?? "");
|
||||||
|
const priceMinor = p?.priceMinor;
|
||||||
|
if (!categoryIds.includes(categoryId)) throw new CarwashError(400, `prices: unknown category "${categoryId}"`);
|
||||||
|
if (!serviceIds.includes(serviceId)) throw new CarwashError(400, `prices: unknown service "${serviceId}"`);
|
||||||
|
if (!Number.isInteger(priceMinor) || (priceMinor as number) < 0) {
|
||||||
|
throw new CarwashError(400, "prices: priceMinor must be a non-negative integer");
|
||||||
|
}
|
||||||
|
rows.push({ categoryId, serviceId, priceMinor: priceMinor as number });
|
||||||
|
}
|
||||||
|
this.#db.delete(carwashPrices).run();
|
||||||
|
for (const r of rows) this.#db.insert(carwashPrices).values(r).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.#log.append({
|
||||||
|
type: "config_change",
|
||||||
|
source: "manual",
|
||||||
|
identity: "module:carwash",
|
||||||
|
payload: {
|
||||||
|
setting: "carwash.settings",
|
||||||
|
value: { categories: categoryIds.length, services: serviceIds.length, prices: body.prices?.length ?? null },
|
||||||
|
operator: actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Where the money is taken — a site policy, signed on its own when it flips (it
|
||||||
|
// decides which till the cash lands on and whether the booth barrier or the exit
|
||||||
|
// reader releases the car; fraud-relevant, so it is attributed like other config).
|
||||||
|
if (body.payAt !== undefined) {
|
||||||
|
if (!isPayAt(body.payAt)) throw new CarwashError(400, "payAt must be booth|bay");
|
||||||
|
const prev = this.payAt();
|
||||||
|
if (body.payAt !== prev) {
|
||||||
|
this.#db
|
||||||
|
.insert(carwashConfig)
|
||||||
|
.values({ id: 1, payAt: body.payAt, updatedAt: now, updatedBy: actor })
|
||||||
|
.onConflictDoUpdate({ target: carwashConfig.id, set: { payAt: body.payAt, updatedAt: now, updatedBy: actor } })
|
||||||
|
.run();
|
||||||
|
await this.#log.append({
|
||||||
|
type: "config_change",
|
||||||
|
source: "manual",
|
||||||
|
identity: "module:carwash",
|
||||||
|
payload: { setting: "carwash.payAt", value: body.payAt, prev, operator: actor },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (body.visionThreshold !== undefined) {
|
||||||
|
const v = Number(body.visionThreshold);
|
||||||
|
if (!Number.isFinite(v) || v < 0 || v > 1) throw new CarwashError(400, "visionThreshold must be between 0 and 1");
|
||||||
|
const prev = this.visionThreshold();
|
||||||
|
if (v !== prev) {
|
||||||
|
this.#db
|
||||||
|
.insert(carwashConfig)
|
||||||
|
.values({ id: 1, visionThreshold: v, updatedAt: now, updatedBy: actor })
|
||||||
|
.onConflictDoUpdate({ target: carwashConfig.id, set: { visionThreshold: v, updatedAt: now, updatedBy: actor } })
|
||||||
|
.run();
|
||||||
|
await this.#log.append({
|
||||||
|
type: "config_change",
|
||||||
|
source: "manual",
|
||||||
|
identity: "module:carwash",
|
||||||
|
payload: { setting: "carwash.visionThreshold", value: v, prev, operator: actor },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.settings();
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Orders ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
#view(r: CarwashOrderRow): CarwashOrderView {
|
||||||
|
return {
|
||||||
|
id: r.id,
|
||||||
|
identity: r.identity,
|
||||||
|
plate: r.plate,
|
||||||
|
categoryId: r.categoryId,
|
||||||
|
categoryName: r.categoryName,
|
||||||
|
serviceId: r.serviceId,
|
||||||
|
serviceName: r.serviceName,
|
||||||
|
priceMinor: r.priceMinor,
|
||||||
|
currency: r.currency,
|
||||||
|
payAt: r.payAt,
|
||||||
|
status: r.status,
|
||||||
|
createdAt: r.createdAt,
|
||||||
|
createdBy: r.createdBy,
|
||||||
|
doneAt: r.doneAt,
|
||||||
|
doneBy: r.doneBy,
|
||||||
|
paidAt: r.paidAt,
|
||||||
|
paidBy: r.paidBy,
|
||||||
|
tender: (r.tender as Tender | null) ?? null,
|
||||||
|
closed: r.status === "void" || (r.status === "done" && r.paidAt != null),
|
||||||
|
validationEventId: r.validationEventId,
|
||||||
|
voidBy: r.voidBy,
|
||||||
|
voidReason: r.voidReason,
|
||||||
|
visionClass: isVehicleClass(r.visionClass) ? r.visionClass : null,
|
||||||
|
visionConfidence: r.visionConfidence,
|
||||||
|
visionCategoryId: r.visionCategoryId,
|
||||||
|
downgradeEventId: r.downgradeEventId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#row(id: string): CarwashOrderRow {
|
||||||
|
const r = this.#db.select().from(carwashOrders).where(eq(carwashOrders.id, id)).get();
|
||||||
|
if (!r) throw new CarwashError(404, "order not found");
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The desk's queue: every order still needing something, oldest first. */
|
||||||
|
openOrders(): CarwashOrderView[] {
|
||||||
|
return this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashOrders)
|
||||||
|
.where(inArray(carwashOrders.status, ["open", "done"]))
|
||||||
|
.orderBy(asc(carwashOrders.createdAt))
|
||||||
|
.all()
|
||||||
|
.map((r) => this.#view(r))
|
||||||
|
.filter((o) => !o.closed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Recent history (closed included), newest first. */
|
||||||
|
recentOrders(limit = 100): CarwashOrderView[] {
|
||||||
|
return this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashOrders)
|
||||||
|
.orderBy(desc(carwashOrders.createdAt))
|
||||||
|
.limit(Math.min(Math.max(limit, 1), 500))
|
||||||
|
.all()
|
||||||
|
.map((r) => this.#view(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
#ordersFor(identity: string): CarwashOrderView[] {
|
||||||
|
return this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashOrders)
|
||||||
|
.where(eq(carwashOrders.identity, identity))
|
||||||
|
.orderBy(asc(carwashOrders.createdAt))
|
||||||
|
.all()
|
||||||
|
.map((r) => this.#view(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ticket → session facts the desk needs (the parking ticket IS the customer). */
|
||||||
|
lookup(identity: string): TicketLookup {
|
||||||
|
const id = identity.trim();
|
||||||
|
const s = this.#pay.lookup(id);
|
||||||
|
const vision = s.found ? vehicleForIdentity(this.#db, id) : null;
|
||||||
|
return {
|
||||||
|
identity: id,
|
||||||
|
found: s.found,
|
||||||
|
open: s.open,
|
||||||
|
subscription: s.subscription,
|
||||||
|
plate: s.plate,
|
||||||
|
enteredAt: s.enteredAt,
|
||||||
|
currency: s.currency,
|
||||||
|
orders: this.#ordersFor(id),
|
||||||
|
vision,
|
||||||
|
suggestedCategoryId: vision ? (this.#categoryForClass(vision.bodyType)?.id ?? null) : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async createOrder(input: CreateOrderInput): Promise<CarwashOrderView> {
|
||||||
|
const identity = input.identity.trim();
|
||||||
|
if (!identity) throw new CarwashError(400, "identity (ticket) required");
|
||||||
|
|
||||||
|
const s = this.#pay.lookup(identity);
|
||||||
|
if (!s.found) throw new CarwashError(404, "no session for ticket");
|
||||||
|
if (!s.open) throw new CarwashError(409, "session is closed");
|
||||||
|
if (s.subscription) throw new CarwashError(409, "subscription sessions: order the wash with payAt=bay", "subscription");
|
||||||
|
|
||||||
|
const category = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashCategories)
|
||||||
|
.where(and(eq(carwashCategories.id, input.categoryId), isNull(carwashCategories.deletedAt)))
|
||||||
|
.get();
|
||||||
|
if (!category || !category.active) throw new CarwashError(404, "category not found or inactive");
|
||||||
|
const service = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashServices)
|
||||||
|
.where(and(eq(carwashServices.id, input.serviceId), isNull(carwashServices.deletedAt)))
|
||||||
|
.get();
|
||||||
|
if (!service || !service.active) throw new CarwashError(404, "service not found or inactive");
|
||||||
|
const price = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashPrices)
|
||||||
|
.where(and(eq(carwashPrices.categoryId, category.id), eq(carwashPrices.serviceId, service.id)))
|
||||||
|
.get();
|
||||||
|
if (!price) throw new CarwashError(409, `no price for ${category.name} · ${service.name}`, "no_price");
|
||||||
|
// The SITE decides where wash money is taken (Setup → Car wash); the order freezes
|
||||||
|
// the policy in force. A client that still sends a different value is stale.
|
||||||
|
const payAt = this.payAt();
|
||||||
|
if (input.payAt !== undefined && input.payAt !== payAt) {
|
||||||
|
throw new CarwashError(409, `this site takes wash money at the ${payAt === "bay" ? "bay" : "booth"}`, "pay_at_policy");
|
||||||
|
}
|
||||||
|
const currency = s.currency ?? this.#pay.activeCurrency();
|
||||||
|
if (!currency) throw new CarwashError(409, "no active tariff (currency unknown)", "no_tariff");
|
||||||
|
|
||||||
|
// Vision, advisory: what the camera saw at entry and the category the site maps it
|
||||||
|
// to. A DOWNGRADE — the operator chose a category that prices LOWER than the mapped
|
||||||
|
// one for this service, with the read above the site threshold — is signed as an
|
||||||
|
// anomaly for the reviewer (both categories, operator, snapshot). Recorded only:
|
||||||
|
// never blocks, no reason prompt (user, 2026-09-06).
|
||||||
|
const vision = vehicleForIdentity(this.#db, identity);
|
||||||
|
const visionCategory = vision ? this.#categoryForClass(vision.bodyType) : null;
|
||||||
|
let downgradeEventId: string | null = null;
|
||||||
|
if (vision && visionCategory && visionCategory.id !== category.id && vision.confidence >= this.visionThreshold()) {
|
||||||
|
const visionPrice = this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashPrices)
|
||||||
|
.where(and(eq(carwashPrices.categoryId, visionCategory.id), eq(carwashPrices.serviceId, service.id)))
|
||||||
|
.get();
|
||||||
|
if (visionPrice && visionPrice.priceMinor > price.priceMinor) {
|
||||||
|
const ev = await this.#log.append({
|
||||||
|
type: "anomaly",
|
||||||
|
source: "manual",
|
||||||
|
identity,
|
||||||
|
payload: {
|
||||||
|
...reasonPayload("carwash.categoryDowngrade", {
|
||||||
|
visionClass: vision.bodyType,
|
||||||
|
visionCategory: visionCategory.name,
|
||||||
|
operator: input.actor,
|
||||||
|
chosenCategory: category.name,
|
||||||
|
}),
|
||||||
|
sessionRef: identity,
|
||||||
|
visionClass: vision.bodyType,
|
||||||
|
visionConfidence: vision.confidence,
|
||||||
|
visionCategoryId: visionCategory.id,
|
||||||
|
visionCategoryName: visionCategory.name,
|
||||||
|
chosenCategoryId: category.id,
|
||||||
|
chosenCategoryName: category.name,
|
||||||
|
serviceName: service.name,
|
||||||
|
visionPriceMinor: visionPrice.priceMinor,
|
||||||
|
chosenPriceMinor: price.priceMinor,
|
||||||
|
currency,
|
||||||
|
snapshotId: vision.snapshotId,
|
||||||
|
operator: input.actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
downgradeEventId = ev.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const row: CarwashOrderRow = {
|
||||||
|
id: randomUUID(),
|
||||||
|
identity,
|
||||||
|
plate: s.plate,
|
||||||
|
categoryId: category.id,
|
||||||
|
categoryName: category.name,
|
||||||
|
serviceId: service.id,
|
||||||
|
serviceName: service.name,
|
||||||
|
priceMinor: price.priceMinor,
|
||||||
|
currency,
|
||||||
|
payAt,
|
||||||
|
status: "open",
|
||||||
|
createdAt: now,
|
||||||
|
createdBy: input.actor,
|
||||||
|
doneAt: null,
|
||||||
|
doneBy: null,
|
||||||
|
paidAt: null,
|
||||||
|
paidBy: null,
|
||||||
|
tender: null,
|
||||||
|
paymentEventId: null,
|
||||||
|
validationEventId: null,
|
||||||
|
voidAt: null,
|
||||||
|
voidBy: null,
|
||||||
|
voidReason: null,
|
||||||
|
visionClass: vision?.bodyType ?? null,
|
||||||
|
visionConfidence: vision?.confidence ?? null,
|
||||||
|
visionCategoryId: visionCategory?.id ?? null,
|
||||||
|
downgradeEventId,
|
||||||
|
};
|
||||||
|
this.#db.insert(carwashOrders).values(row).run();
|
||||||
|
// Hand the decision to the remote reviewer (crop + choice), off the intake path.
|
||||||
|
if (vision && this.#outbox?.enabled) {
|
||||||
|
void this.#outbox.enqueue(
|
||||||
|
{
|
||||||
|
orderId: row.id,
|
||||||
|
createdAt: now,
|
||||||
|
createdBy: input.actor,
|
||||||
|
categoryId: category.id,
|
||||||
|
categoryName: category.name,
|
||||||
|
categoryClasses: category.visionClasses,
|
||||||
|
serviceName: service.name,
|
||||||
|
visionCategoryId: visionCategory?.id ?? null,
|
||||||
|
downgraded: downgradeEventId != null,
|
||||||
|
},
|
||||||
|
vision,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await this.#log.append({
|
||||||
|
type: "carwash_order",
|
||||||
|
source: "manual",
|
||||||
|
identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: identity,
|
||||||
|
orderId: row.id,
|
||||||
|
action: "created",
|
||||||
|
categoryName: row.categoryName,
|
||||||
|
serviceName: row.serviceName,
|
||||||
|
priceMinor: row.priceMinor,
|
||||||
|
currency,
|
||||||
|
payAt: row.payAt,
|
||||||
|
operator: input.actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return this.#view(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The wash is finished: apply the site's sponsorship program to the parking session
|
||||||
|
* (if one is configured and active), then — for a bay order already paid — settle
|
||||||
|
* the parking session so the exit reader opens. */
|
||||||
|
async markDone(id: string, actor: string): Promise<CarwashOrderView> {
|
||||||
|
const r = this.#row(id);
|
||||||
|
if (r.status === "void") throw new CarwashError(409, "order is void");
|
||||||
|
if (r.status === "done") throw new CarwashError(409, "order is already done");
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
|
||||||
|
let validationEventId: string | null = null;
|
||||||
|
// Wash context for the wash-only discount modes: the WASH WINDOW in minutes — from
|
||||||
|
// the order's intake to now (= done) — and the order's frozen price. NOT the time
|
||||||
|
// since entry: a car parked for hours before it asks for a wash still pays for those
|
||||||
|
// hours (found 2026-09-05 on a long-open ticket that would have been fully comped).
|
||||||
|
// The credit lands at the start of the billed period (that is how timeCredit
|
||||||
|
// folds), so for a flat tariff the money is identical; a stepped/daily-cap tariff
|
||||||
|
// may differ by an increment. See applyValidation().
|
||||||
|
const washMinutes = Math.max(0, Math.ceil((Date.now() - Date.parse(r.createdAt)) / 60_000));
|
||||||
|
const applied = await applyValidation(this.#db, this.#log, {
|
||||||
|
programId: CARWASH_PROGRAM_ID,
|
||||||
|
identity: r.identity,
|
||||||
|
actor,
|
||||||
|
wash: { washMinutes, priceMinor: r.priceMinor },
|
||||||
|
});
|
||||||
|
if (applied.ok) validationEventId = applied.eventId;
|
||||||
|
else if (applied.status !== 404 && !/already applied/.test(applied.error)) {
|
||||||
|
// A real refusal (session closed, daily cap …) — the wash is still done; the
|
||||||
|
// customer simply gets no sponsorship. Keep it visible in the log.
|
||||||
|
this.#logger.warn(`carwash sponsorship not applied for ${r.identity}: ${applied.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#db
|
||||||
|
.update(carwashOrders)
|
||||||
|
.set({ status: "done", doneAt: now, doneBy: actor, validationEventId })
|
||||||
|
.where(eq(carwashOrders.id, id))
|
||||||
|
.run();
|
||||||
|
await this.#log.append({
|
||||||
|
type: "carwash_order",
|
||||||
|
source: "manual",
|
||||||
|
identity: r.identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: r.identity,
|
||||||
|
orderId: id,
|
||||||
|
action: "done",
|
||||||
|
categoryName: r.categoryName,
|
||||||
|
serviceName: r.serviceName,
|
||||||
|
priceMinor: r.priceMinor,
|
||||||
|
currency: r.currency,
|
||||||
|
payAt: r.payAt,
|
||||||
|
...(validationEventId ? { validationEventId } : {}),
|
||||||
|
operator: actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const updated = this.#row(id);
|
||||||
|
if (updated.payAt === "bay" && updated.paidAt != null) await this.#settleParkingIfFree(updated, actor);
|
||||||
|
return this.#view(updated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Money taken AT THE BAY. Needs an open CARWASH shift (it is the wash operator's
|
||||||
|
* drawer money, never the booth's — wiki/concepts/shift.md "Tills"); signs a
|
||||||
|
* carwash_payment on that till; then, if the wash is also done, settles the
|
||||||
|
* parking session. */
|
||||||
|
async payAtBay(id: string, tender: Tender, actor: string): Promise<CarwashOrderView> {
|
||||||
|
const r = this.#row(id);
|
||||||
|
if (r.status === "void") throw new CarwashError(409, "order is void");
|
||||||
|
if (r.payAt !== "bay") throw new CarwashError(409, "this order is paid at the booth", "pay_at_booth");
|
||||||
|
if (r.paidAt != null) throw new CarwashError(409, "order is already paid");
|
||||||
|
if (tender !== "cash" && tender !== "card") throw new CarwashError(400, "tender must be cash|card");
|
||||||
|
this.#shift.requireOpenShift(CARWASH_TILL);
|
||||||
|
|
||||||
|
const ev = await this.#log.append({
|
||||||
|
type: "carwash_payment",
|
||||||
|
source: "manual",
|
||||||
|
identity: r.identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: r.identity,
|
||||||
|
orderId: id,
|
||||||
|
amountMinor: r.priceMinor,
|
||||||
|
currency: r.currency,
|
||||||
|
tender,
|
||||||
|
till: CARWASH_TILL,
|
||||||
|
categoryName: r.categoryName,
|
||||||
|
serviceName: r.serviceName,
|
||||||
|
operator: actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
this.#db
|
||||||
|
.update(carwashOrders)
|
||||||
|
.set({ paidAt: now, paidBy: actor, tender, paymentEventId: ev.id })
|
||||||
|
.where(eq(carwashOrders.id, id))
|
||||||
|
.run();
|
||||||
|
const updated = this.#row(id);
|
||||||
|
if (updated.status === "done") await this.#settleParkingIfFree(updated, actor, tender);
|
||||||
|
return this.#view(updated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A bay-paid, done wash: if the sponsorship made the parking session zero-due, sign
|
||||||
|
* the $0 parking payment now — that is what the exit READER checks (a validation
|
||||||
|
* alone opens nothing; see exit-flow.ts). A remaining balance stays for the booth. */
|
||||||
|
async #settleParkingIfFree(r: CarwashOrderRow, actor: string, tender: Tender = "cash"): Promise<void> {
|
||||||
|
try {
|
||||||
|
const s = this.#pay.lookup(r.identity);
|
||||||
|
if (!s.open || s.subscription || s.paidAt != null) return;
|
||||||
|
const q = this.#pay.quote(r.identity);
|
||||||
|
if (q.amountMinor !== 0) return;
|
||||||
|
await this.#pay.pay(r.identity, tender);
|
||||||
|
this.#logger.info(`carwash: parking session ${r.identity} settled at zero after bay payment (by ${actor})`);
|
||||||
|
} catch (err) {
|
||||||
|
this.#logger.warn(`carwash: could not settle parking for ${r.identity}: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async voidOrder(id: string, reason: string, actor: string): Promise<CarwashOrderView> {
|
||||||
|
const r = this.#row(id);
|
||||||
|
if (r.status === "void") throw new CarwashError(409, "order is already void");
|
||||||
|
if (r.paidAt != null) throw new CarwashError(409, "a paid order cannot be voided", "paid");
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
// Take back the sponsorship if it is still live (not consumed by a payment).
|
||||||
|
if (r.validationEventId) {
|
||||||
|
const live = liveValidations(this.#db, r.identity).find((v) => v.eventId === r.validationEventId);
|
||||||
|
if (live) {
|
||||||
|
await this.#log.append({
|
||||||
|
type: "validation",
|
||||||
|
source: "manual",
|
||||||
|
identity: r.identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: r.identity,
|
||||||
|
refId: r.validationEventId,
|
||||||
|
programId: live.programId,
|
||||||
|
programLabel: live.label,
|
||||||
|
operator: actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.#db
|
||||||
|
.update(carwashOrders)
|
||||||
|
.set({ status: "void", voidAt: now, voidBy: actor, voidReason: reason || null })
|
||||||
|
.where(eq(carwashOrders.id, id))
|
||||||
|
.run();
|
||||||
|
await this.#log.append({
|
||||||
|
type: "carwash_order",
|
||||||
|
source: "manual",
|
||||||
|
identity: r.identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: r.identity,
|
||||||
|
orderId: id,
|
||||||
|
action: "void",
|
||||||
|
categoryName: r.categoryName,
|
||||||
|
serviceName: r.serviceName,
|
||||||
|
priceMinor: r.priceMinor,
|
||||||
|
currency: r.currency,
|
||||||
|
payAt: r.payAt,
|
||||||
|
reason: reason || undefined,
|
||||||
|
operator: actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return this.#view(this.#row(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Booth settlement hook ------------------------------------------------------
|
||||||
|
|
||||||
|
/** Orders with payAt = "booth" ride the parking payment as charge lines; the core
|
||||||
|
* calls back after the payment is signed so they are marked paid. Off = no lines. */
|
||||||
|
chargeProvider(): ChargeProvider {
|
||||||
|
return {
|
||||||
|
lines: (identity) => {
|
||||||
|
if (!this.#enabled()) return [];
|
||||||
|
return this.#db
|
||||||
|
.select()
|
||||||
|
.from(carwashOrders)
|
||||||
|
.where(and(eq(carwashOrders.identity, identity), eq(carwashOrders.payAt, "booth"), isNull(carwashOrders.paidAt)))
|
||||||
|
.all()
|
||||||
|
.filter((r) => r.status !== "void")
|
||||||
|
.map((r) => ({
|
||||||
|
module: "carwash" as const,
|
||||||
|
ref: r.id,
|
||||||
|
label: `Lavazh — ${r.categoryName} · ${r.serviceName}`,
|
||||||
|
amountMinor: r.priceMinor,
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
onPaid: async (_identity, lines, payment) => {
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
for (const l of lines) {
|
||||||
|
if (l.module !== "carwash") continue;
|
||||||
|
this.#db
|
||||||
|
.update(carwashOrders)
|
||||||
|
.set({ paidAt: now, paidBy: payment.operator ?? "booth", tender: payment.tender, paymentEventId: payment.eventId })
|
||||||
|
.where(and(eq(carwashOrders.id, l.ref), isNull(carwashOrders.paidAt)))
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Type guard for the void body etc. */
|
||||||
|
export function isPayAt(v: unknown): v is CarWashPayAt {
|
||||||
|
return typeof v === "string" && (CARWASH_PAY_AT as readonly string[]).includes(v);
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import type { Db } from "@parking/db";
|
||||||
|
import { MODULES, parseEntitledModules, type ModuleId } from "@parking/shared";
|
||||||
|
import type { EventLog } from "../event-log.js";
|
||||||
|
import type { PayStation } from "../pay-station.js";
|
||||||
|
import type { ShiftService } from "../shift-service.js";
|
||||||
|
import { effectiveModulesFor } from "../modules.js";
|
||||||
|
import { carwashModule } from "./carwash/index.js";
|
||||||
|
import { validationModule } from "./validation/index.js";
|
||||||
|
|
||||||
|
// The server-side module registry. A module's routes live in its own folder
|
||||||
|
// (apps/server/src/modules/<id>/index.ts) and are registered by iterating
|
||||||
|
// @parking/shared's MODULES — so adding a module is one manifest entry + one folder +
|
||||||
|
// one line in SERVER_MODULES below, with nothing else in the core touched
|
||||||
|
// (wiki/decisions/venue-modules.md, "A module = a manifest + three folders").
|
||||||
|
//
|
||||||
|
// `parking` is registered in the manifest but has NO folder yet: its routes are still
|
||||||
|
// the flat list in server.ts. That is deliberate — the seam is drawn, the code moves
|
||||||
|
// across it subsystem by subsystem as each is touched, not in one big move.
|
||||||
|
|
||||||
|
/** What the core hands a module at registration. Modules reach the core ONLY through
|
||||||
|
* these (never by importing another module): the DB, the signed ledger, the booth
|
||||||
|
* settlement (to fold charges in / settle a session — PayStation.registerChargeProvider,
|
||||||
|
* quote, pay) and the shift service (money needs an open shift). */
|
||||||
|
export interface ServerModuleDeps {
|
||||||
|
db: Db;
|
||||||
|
eventLog: EventLog;
|
||||||
|
payStation: PayStation;
|
||||||
|
shiftService: ShiftService;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServerModule {
|
||||||
|
id: ModuleId;
|
||||||
|
register(app: FastifyInstance, deps: ServerModuleDeps): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SERVER_MODULES: Partial<Record<ModuleId, ServerModule>> = {
|
||||||
|
validation: validationModule,
|
||||||
|
carwash: carwashModule,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Register every folder-based module in registry order, then log what this site
|
||||||
|
* is entitled to / has effective, so a "why is X missing" question is answerable
|
||||||
|
* from the container log alone. */
|
||||||
|
export async function registerModules(app: FastifyInstance, deps: ServerModuleDeps): Promise<void> {
|
||||||
|
for (const manifest of MODULES) {
|
||||||
|
const impl = SERVER_MODULES[manifest.id];
|
||||||
|
if (impl) {
|
||||||
|
if (impl.id !== manifest.id) throw new Error(`module registry mismatch: ${impl.id} registered under ${manifest.id}`);
|
||||||
|
await impl.register(app, deps);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const { entitled, unknown } = parseEntitledModules(process.env.MODULES_ENTITLED);
|
||||||
|
if (unknown.length > 0) {
|
||||||
|
app.log.warn({ unknown }, "MODULES_ENTITLED names unknown module ids — ignored");
|
||||||
|
}
|
||||||
|
app.log.info(
|
||||||
|
{ entitled, effective: effectiveModulesFor(deps.db) },
|
||||||
|
"venue modules (entitled = MODULES_ENTITLED env; effective = entitled ∩ site activation)",
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { validationRoutes } from "../../routes/validations.js";
|
||||||
|
import type { ServerModule } from "../index.js";
|
||||||
|
|
||||||
|
// Merchant-scan ticket validation as a venue module. Kept for the Bar until a Bar
|
||||||
|
// module absorbs it (wiki/decisions/venue-modules.md, decision 1). The routes
|
||||||
|
// themselves still live in routes/validations.ts (unchanged location, now guarded by
|
||||||
|
// requireModule("validation")); this folder is the registry hook.
|
||||||
|
export const validationModule: ServerModule = {
|
||||||
|
id: "validation",
|
||||||
|
async register(app, { db, eventLog }) {
|
||||||
|
await validationRoutes(app, db, eventLog);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -28,6 +28,24 @@ async function enter(identity: string, enteredAt: string, payload?: Record<strin
|
|||||||
await log.append({ type: "vehicle_entry", direction: "entry", identity, occurredAt: enteredAt, payload: payload ?? null });
|
await log.append({ type: "vehicle_entry", direction: "entry", identity, occurredAt: enteredAt, payload: payload ?? null });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe("PayStation.forgive (forgiven exit)", () => {
|
||||||
|
it("signs a $0 payment with the quoted fee as forgoneMinor and marks the session paid", async () => {
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60 });
|
||||||
|
await enter("T1", minutesAgo(90));
|
||||||
|
const r = await pay.forgive("T1", { operator: "owner", note: "lost ticket, known customer" });
|
||||||
|
expect(r).toEqual({ ok: true, forgoneMinor: 20000, currency: expect.any(String) });
|
||||||
|
const ev = db.select().from(ledgerEvents).where(eq(ledgerEvents.identity, "T1")).all().find((e) => e.type === "payment")!;
|
||||||
|
expect(ev.payload).toMatchObject({ amountMinor: 0, forgoneMinor: 20000, operator: "owner", reasonCode: "exit.forgiven" });
|
||||||
|
expect((ev.payload as { tender?: string }).tender).toBeUndefined(); // nothing was tendered
|
||||||
|
expect(pay.lookup("T1").paidAt).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws like quote() for an unknown ticket", async () => {
|
||||||
|
seedTariff(db);
|
||||||
|
await expect(pay.forgive("nope", { operator: "owner", note: "x" })).rejects.toThrow(NoOpenSessionError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("PayStation.quote", () => {
|
describe("PayStation.quote", () => {
|
||||||
it("throws NoOpenSessionError for an unknown ticket", () => {
|
it("throws NoOpenSessionError for an unknown ticket", () => {
|
||||||
seedTariff(db);
|
seedTariff(db);
|
||||||
@@ -134,4 +152,25 @@ describe("PayStation.activeSessions", () => {
|
|||||||
expect(list.map((s) => s.identity)).toEqual(["NEW", "OLD"]);
|
expect(list.map((s) => s.identity)).toEqual(["NEW", "OLD"]);
|
||||||
expect(list.every((s) => s.open)).toBe(true);
|
expect(list.every((s) => s.open)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The list prices each open transient from ONE whole-ledger scan (no per-row ledger
|
||||||
|
// reads — those froze park-2's server, 2026-10-24). The amounts must match the
|
||||||
|
// single-session quote() exactly, including after a payment (overstay repricing) and
|
||||||
|
// for a subscription (never quoted).
|
||||||
|
it("prices every row identically to quote() without per-session ledger reads", async () => {
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60, gracePeriodExitMin: 0 });
|
||||||
|
await enter("UNPAID", minutesAgo(150));
|
||||||
|
await enter("PAID", minutesAgo(300));
|
||||||
|
await pay.pay("PAID", "cash"); // grace 0 → paid-but-open ⇒ overstay, re-priced from now
|
||||||
|
await enter("SUB", minutesAgo(10), { permit: true, permitId: "sub-1" });
|
||||||
|
|
||||||
|
const list = pay.activeSessions();
|
||||||
|
const by = new Map(list.map((s) => [s.identity, s]));
|
||||||
|
expect(by.get("UNPAID")!.amountMinor).toBe(pay.quote("UNPAID").amountMinor);
|
||||||
|
expect(by.get("UNPAID")!.currency).toBe(pay.quote("UNPAID").currency);
|
||||||
|
expect(by.get("PAID")!.overstay).toBe(true);
|
||||||
|
expect(by.get("PAID")!.amountMinor).toBe(pay.quote("PAID").amountMinor);
|
||||||
|
expect(by.get("SUB")!.subscription).toBe(true);
|
||||||
|
expect(by.get("SUB")!.amountMinor).toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+188
-24
@@ -1,10 +1,16 @@
|
|||||||
import { desc, eq, ledgerEvents, sessions, subscriptions, tariffVersions, tariffs, type Db } from "@parking/db";
|
import { desc, eq, ledgerEvents, sessions, subscriptions, tariffVersions, tariffs, type Db } from "@parking/db";
|
||||||
import { priceSession, type TariffStructure, type Tender, type ValidationLine } from "@parking/shared";
|
import { BOOTH_TILL, priceSession, reasonPayload, type ChargeLine, type TariffStructure, type Tender, type ValidationLine } from "@parking/shared";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
import type { EventLog } from "./event-log.js";
|
import type { EventLog } from "./event-log.js";
|
||||||
import { plateForIdentity, platesForIdentities } from "./plate-lookup.js";
|
import { plateForIdentity, platesForIdentities } from "./plate-lookup.js";
|
||||||
import { windowOwedBetween } from "./subscription-window.js";
|
import { windowOwedBetween } from "./subscription-window.js";
|
||||||
import { liveValidations } from "./validations.js";
|
import { liveValidationsFromRows } from "./validations.js";
|
||||||
|
|
||||||
|
/** A `ledger_events` row as the pricing fold reads it. */
|
||||||
|
type LedgerRow = typeof ledgerEvents.$inferSelect;
|
||||||
|
/** Resolves the tariff version in force at an instant. Built once per request so a list
|
||||||
|
* view does not re-read the tariff tables for every row. */
|
||||||
|
type TariffResolver = (at: string) => typeof tariffVersions.$inferSelect | null;
|
||||||
|
|
||||||
// The PAY STATION: a customer pays for an open session BEFORE walking back to the
|
// The PAY STATION: a customer pays for an open session BEFORE walking back to the
|
||||||
// car (pay-on-foot — payment is decoupled from exit). Two steps:
|
// car (pay-on-foot — payment is decoupled from exit). Two steps:
|
||||||
@@ -29,6 +35,18 @@ export class NoTariffError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A module that folds its own charges into a booth settlement (venue-modules.md):
|
||||||
|
* `lines(identity)` returns the open charges for the session (e.g. wash orders with
|
||||||
|
* payAt = "booth"); after the `payment` is signed, `onPaid` lets the module mark them
|
||||||
|
* settled. Registered by the module at boot (registerChargeProvider) — PayStation
|
||||||
|
* never imports a module.
|
||||||
|
*/
|
||||||
|
export interface ChargeProvider {
|
||||||
|
lines(identity: string): ChargeLine[];
|
||||||
|
onPaid(identity: string, lines: ChargeLine[], payment: { eventId: string; tender: Tender; operator?: string }): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Quote {
|
export interface Quote {
|
||||||
readonly identity: string;
|
readonly identity: string;
|
||||||
/** Vehicle entry time (the session's original entry; for display/audit). */
|
/** Vehicle entry time (the session's original entry; for display/audit). */
|
||||||
@@ -39,8 +57,14 @@ export interface Quote {
|
|||||||
* is priced as a fresh stay from there → now, with its own daily-cap ladder, NOT
|
* is priced as a fresh stay from there → now, with its own daily-cap ladder, NOT
|
||||||
* "full stay minus paid" (which a daily cap collapses toward zero). */
|
* "full stay minus paid" (which a daily cap collapses toward zero). */
|
||||||
readonly periodStart: string;
|
readonly periodStart: string;
|
||||||
/** Amount owed now: the fee for [periodStart → now], NET of merchant validations. */
|
/** Amount owed now: the parking fee for [periodStart → now] NET of merchant
|
||||||
|
* validations, PLUS any module charge lines (a wash paid at the booth). */
|
||||||
readonly amountMinor: number;
|
readonly amountMinor: number;
|
||||||
|
/** The parking-only net (amountMinor − chargesMinor). */
|
||||||
|
readonly parkingMinor: number;
|
||||||
|
/** Non-parking charges folded in by modules (see ChargeProvider). */
|
||||||
|
readonly chargeLines: ChargeLine[];
|
||||||
|
readonly chargesMinor: number;
|
||||||
/** The pre-validation fee (= amountMinor when no validations apply). */
|
/** The pre-validation fee (= amountMinor when no validations apply). */
|
||||||
readonly grossMinor: number;
|
readonly grossMinor: number;
|
||||||
/** Total the merchant validations took off (gross − net). */
|
/** Total the merchant validations took off (gross − net). */
|
||||||
@@ -133,12 +157,16 @@ export interface SessionLookup {
|
|||||||
readonly grossMinor: number | null;
|
readonly grossMinor: number | null;
|
||||||
readonly discountMinor: number | null;
|
readonly discountMinor: number | null;
|
||||||
readonly validationLines: ValidationLine[];
|
readonly validationLines: ValidationLine[];
|
||||||
|
/** Module charge lines folded into `amountMinor` (e.g. a wash paid at the booth). */
|
||||||
|
readonly chargeLines: ChargeLine[];
|
||||||
|
readonly chargesMinor: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PayStation {
|
export class PayStation {
|
||||||
readonly #db: Db;
|
readonly #db: Db;
|
||||||
readonly #log: EventLog;
|
readonly #log: EventLog;
|
||||||
readonly #logger: FastifyBaseLogger;
|
readonly #logger: FastifyBaseLogger;
|
||||||
|
readonly #chargeProviders: ChargeProvider[] = [];
|
||||||
|
|
||||||
constructor(db: Db, log: EventLog, logger: FastifyBaseLogger) {
|
constructor(db: Db, log: EventLog, logger: FastifyBaseLogger) {
|
||||||
this.#db = db;
|
this.#db = db;
|
||||||
@@ -146,6 +174,30 @@ export class PayStation {
|
|||||||
this.#logger = logger;
|
this.#logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Let a module fold its charges into booth settlements (see ChargeProvider). */
|
||||||
|
registerChargeProvider(p: ChargeProvider): void {
|
||||||
|
this.#chargeProviders.push(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The currency of the tariff in force right now (null = none published). Modules
|
||||||
|
* price their own goods in the same money the booth takes. */
|
||||||
|
activeCurrency(): string | null {
|
||||||
|
return this.#tariffVersionFor(new Date().toISOString())?.currency ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chargeLines(identity: string): ChargeLine[] {
|
||||||
|
const out: ChargeLine[] = [];
|
||||||
|
for (const p of this.#chargeProviders) {
|
||||||
|
try {
|
||||||
|
out.push(...p.lines(identity));
|
||||||
|
} catch (err) {
|
||||||
|
// A module's fault must never block a parking settlement — log and price without it.
|
||||||
|
this.#logger.error(`charge provider failed for ${identity}: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
/** Price an open session. Normally the period is entry→now. But for an OVERSTAY — a
|
/** Price an open session. Normally the period is entry→now. But for an OVERSTAY — a
|
||||||
* paid session whose walk-back grace has lapsed (the car re-parked, or a new period
|
* paid session whose walk-back grace has lapsed (the car re-parked, or a new period
|
||||||
* began) — the customer is billed for a FRESH period from grace-expiry→now, with its
|
* began) — the customer is billed for a FRESH period from grace-expiry→now, with its
|
||||||
@@ -154,12 +206,23 @@ export class PayStation {
|
|||||||
* 0 and a multi-day overstay would exit free (ticket 1245791632490). A new period
|
* 0 and a multi-day overstay would exit free (ticket 1245791632490). A new period
|
||||||
* reflects the reality and re-accrues the fee. No side effect. */
|
* reflects the reality and re-accrues the fee. No side effect. */
|
||||||
quote(identity: string): Quote {
|
quote(identity: string): Quote {
|
||||||
const entry = this.#openEntry(identity);
|
return this.#quoteFromRows(identity, this.#identityRows(identity), this.#tariffResolver());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** `quote` over the identity's ledger rows the caller already holds (oldest first) and
|
||||||
|
* a prebuilt tariff resolver. The single-session `quote()` wraps this with one
|
||||||
|
* identity-scoped read; `activeSessions()` feeds it per-identity groups of ONE whole-
|
||||||
|
* ledger scan. Both paths MUST price identically — pinned by pay-station.test.ts.
|
||||||
|
* Why: the list view used to call `quote()` per open unpaid row, each doing 3–4
|
||||||
|
* ledger scans; with ~660 such rows at park-2 the synchronous handler ran ~2 s and
|
||||||
|
* froze the event loop — relay replies then "timed out" (2026-10-24). */
|
||||||
|
#quoteFromRows(identity: string, rows: readonly LedgerRow[], tariffFor: TariffResolver): Quote {
|
||||||
|
const entry = PayStation.#openEntryFromRows(rows);
|
||||||
if (!entry) throw new NoOpenSessionError(identity);
|
if (!entry) throw new NoOpenSessionError(identity);
|
||||||
|
|
||||||
// The tariff in force is keyed to ENTRY (the version frozen for this session), even
|
// The tariff in force is keyed to ENTRY (the version frozen for this session), even
|
||||||
// for an overstay period — the customer keeps the rate card they entered under.
|
// for an overstay period — the customer keeps the rate card they entered under.
|
||||||
const tv = this.#tariffVersionFor(entry.occurredAt);
|
const tv = tariffFor(entry.occurredAt);
|
||||||
if (!tv) throw new NoTariffError();
|
if (!tv) throw new NoTariffError();
|
||||||
const structure = tv.structure as unknown as TariffStructure;
|
const structure = tv.structure as unknown as TariffStructure;
|
||||||
|
|
||||||
@@ -174,8 +237,8 @@ export class PayStation {
|
|||||||
// Merchant validations: fold the LIVE ones (applied, unvoided, not consumed by a
|
// Merchant validations: fold the LIVE ones (applied, unvoided, not consumed by a
|
||||||
// prior payment) so the quote is NET — the payment then stamps their ids as
|
// prior payment) so the quote is NET — the payment then stamps their ids as
|
||||||
// consumed. See wiki/concepts/validation-discounts.md.
|
// consumed. See wiki/concepts/validation-discounts.md.
|
||||||
const last = this.#lastPayment(identity);
|
const last = PayStation.#lastPaymentFromRows(rows);
|
||||||
const validations = liveValidations(this.#db, identity);
|
const validations = liveValidationsFromRows(rows);
|
||||||
const p = priceSession(
|
const p = priceSession(
|
||||||
entry.occurredAt,
|
entry.occurredAt,
|
||||||
new Date().toISOString(),
|
new Date().toISOString(),
|
||||||
@@ -184,11 +247,16 @@ export class PayStation {
|
|||||||
category,
|
category,
|
||||||
validations,
|
validations,
|
||||||
);
|
);
|
||||||
|
const chargeLines = this.#chargeLines(identity);
|
||||||
|
const chargesMinor = chargeLines.reduce((sum, l) => sum + l.amountMinor, 0);
|
||||||
return {
|
return {
|
||||||
identity,
|
identity,
|
||||||
enteredAt: entry.occurredAt,
|
enteredAt: entry.occurredAt,
|
||||||
periodStart: p.periodStart,
|
periodStart: p.periodStart,
|
||||||
amountMinor: p.amountMinor,
|
amountMinor: p.amountMinor + chargesMinor,
|
||||||
|
parkingMinor: p.amountMinor,
|
||||||
|
chargeLines,
|
||||||
|
chargesMinor,
|
||||||
grossMinor: p.grossMinor,
|
grossMinor: p.grossMinor,
|
||||||
discountMinor: p.discountMinor,
|
discountMinor: p.discountMinor,
|
||||||
validationLines: p.validationLines,
|
validationLines: p.validationLines,
|
||||||
@@ -203,12 +271,17 @@ export class PayStation {
|
|||||||
/** The latest signed `payment` for this session (time + the grace window it granted),
|
/** The latest signed `payment` for this session (time + the grace window it granted),
|
||||||
* or null if never paid. Folds the append-only ledger. */
|
* or null if never paid. Folds the append-only ledger. */
|
||||||
#lastPayment(identity: string): { paidAt: string; graceExitMin: number | null } | null {
|
#lastPayment(identity: string): { paidAt: string; graceExitMin: number | null } | null {
|
||||||
const rows = this.#db
|
return PayStation.#lastPaymentFromRows(this.#identityRows(identity));
|
||||||
.select({ type: ledgerEvents.type, occurredAt: ledgerEvents.occurredAt, payload: ledgerEvents.payload })
|
}
|
||||||
.from(ledgerEvents)
|
|
||||||
.where(eq(ledgerEvents.identity, identity))
|
/** All ledger rows of one identity, oldest first (the fold order every helper expects). */
|
||||||
.orderBy(ledgerEvents.index)
|
#identityRows(identity: string): LedgerRow[] {
|
||||||
.all();
|
return this.#db.select().from(ledgerEvents).where(eq(ledgerEvents.identity, identity)).orderBy(ledgerEvents.index).all();
|
||||||
|
}
|
||||||
|
|
||||||
|
static #lastPaymentFromRows(
|
||||||
|
rows: readonly Pick<LedgerRow, "type" | "occurredAt" | "payload">[],
|
||||||
|
): { paidAt: string; graceExitMin: number | null } | null {
|
||||||
let last: { paidAt: string; graceExitMin: number | null } | null = null;
|
let last: { paidAt: string; graceExitMin: number | null } | null = null;
|
||||||
for (const r of rows) {
|
for (const r of rows) {
|
||||||
if (r.type !== "payment") continue;
|
if (r.type !== "payment") continue;
|
||||||
@@ -246,6 +319,7 @@ export class PayStation {
|
|||||||
amountMinor,
|
amountMinor,
|
||||||
currency: subWindow.currency ?? undefined,
|
currency: subWindow.currency ?? undefined,
|
||||||
tender,
|
tender,
|
||||||
|
till: BOOTH_TILL,
|
||||||
...(subWindow.tariffVersionId ? { tariffVersionId: subWindow.tariffVersionId } : {}),
|
...(subWindow.tariffVersionId ? { tariffVersionId: subWindow.tariffVersionId } : {}),
|
||||||
subscriptionWindowCharge: true,
|
subscriptionWindowCharge: true,
|
||||||
...(overrideMinor != null ? { reason: "operator-set amount", quotedMinor: subWindow.dueMinor } : {}),
|
...(overrideMinor != null ? { reason: "operator-set amount", quotedMinor: subWindow.dueMinor } : {}),
|
||||||
@@ -258,7 +332,7 @@ export class PayStation {
|
|||||||
const q = this.quote(identity);
|
const q = this.quote(identity);
|
||||||
const amountMinor = overrideMinor ?? q.amountMinor;
|
const amountMinor = overrideMinor ?? q.amountMinor;
|
||||||
|
|
||||||
await this.#log.append({
|
const paymentEvent = await this.#log.append({
|
||||||
type: "payment",
|
type: "payment",
|
||||||
source: "manual",
|
source: "manual",
|
||||||
identity,
|
identity,
|
||||||
@@ -267,7 +341,19 @@ export class PayStation {
|
|||||||
amountMinor,
|
amountMinor,
|
||||||
currency: q.currency,
|
currency: q.currency,
|
||||||
tender,
|
tender,
|
||||||
|
// Parking money is BOOTH money (a wash paid at the booth rides along as
|
||||||
|
// chargeLines, so it is booth money too). See wiki/concepts/shift.md "Tills".
|
||||||
|
till: BOOTH_TILL,
|
||||||
tariffVersionId: q.tariffVersionId,
|
tariffVersionId: q.tariffVersionId,
|
||||||
|
// Module charges (e.g. a wash paid at the booth): frozen as lines so the
|
||||||
|
// receipt reproduces and reporting can split parking from the rest.
|
||||||
|
...(q.chargeLines.length
|
||||||
|
? {
|
||||||
|
chargeLines: q.chargeLines.map((l) => ({ ...l })),
|
||||||
|
chargesMinor: q.chargesMinor,
|
||||||
|
parkingMinor: q.parkingMinor,
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
// The exit flow reads graceExitMin off the payment to validate the
|
// The exit flow reads graceExitMin off the payment to validate the
|
||||||
// walk-back window without re-resolving the tariff.
|
// walk-back window without re-resolving the tariff.
|
||||||
graceExitMin: q.graceExitMin,
|
graceExitMin: q.graceExitMin,
|
||||||
@@ -294,10 +380,68 @@ export class PayStation {
|
|||||||
this.#logger.error(`session-cache mark-paid failed for ${identity}: ${(err as Error).message}`);
|
this.#logger.error(`session-cache mark-paid failed for ${identity}: ${(err as Error).message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Let each module mark the charge lines it contributed as settled by this payment.
|
||||||
|
if (q.chargeLines.length) {
|
||||||
|
for (const p of this.#chargeProviders) {
|
||||||
|
try {
|
||||||
|
await p.onPaid(identity, q.chargeLines, { eventId: paymentEvent.id, tender });
|
||||||
|
} catch (err) {
|
||||||
|
this.#logger.error(`charge provider onPaid failed for ${identity}: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.#logger.info(`payment ${amountMinor} ${q.currency} (${tender}) for ${identity}`);
|
this.#logger.info(`payment ${amountMinor} ${q.currency} (${tender}) for ${identity}`);
|
||||||
return { amountMinor, currency: q.currency };
|
return { amountMinor, currency: q.currency };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FORGIVEN EXIT (wiki/decisions/forgiven-exit.md): the owner lets a ticketed car out
|
||||||
|
* with no money. Signs a ZERO-amount payment that carries the fee the car owed as
|
||||||
|
* `forgoneMinor`, so the shift's cash/card/drawer are untouched by construction while
|
||||||
|
* the Z-report shows the forgone amount as its own line; the exit that follows closes
|
||||||
|
* the ticket (no reuse). Refused when the quote carries module charge lines (a wash on
|
||||||
|
* the ticket): parking is forgiven, a wash is not — settle it first. Throws
|
||||||
|
* NoOpenSessionError / NoTariffError like `quote()`.
|
||||||
|
*/
|
||||||
|
async forgive(
|
||||||
|
identity: string,
|
||||||
|
opts: { operator: string; note: string },
|
||||||
|
): Promise<{ ok: true; forgoneMinor: number; currency: string } | { ok: false; reason: string }> {
|
||||||
|
const q = this.quote(identity);
|
||||||
|
if (q.chargesMinor > 0) {
|
||||||
|
return { ok: false, reason: "ticket carries a module charge (e.g. a wash) — settle it first; only parking can be forgiven" };
|
||||||
|
}
|
||||||
|
await this.#log.append({
|
||||||
|
type: "payment",
|
||||||
|
source: "manual",
|
||||||
|
identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: identity,
|
||||||
|
amountMinor: 0,
|
||||||
|
forgoneMinor: q.amountMinor,
|
||||||
|
currency: q.currency,
|
||||||
|
till: BOOTH_TILL,
|
||||||
|
tariffVersionId: q.tariffVersionId,
|
||||||
|
graceExitMin: q.graceExitMin,
|
||||||
|
operator: opts.operator,
|
||||||
|
...reasonPayload("exit.forgiven", {
|
||||||
|
operator: opts.operator,
|
||||||
|
forgone: (q.amountMinor / 100).toFixed(2),
|
||||||
|
currency: q.currency,
|
||||||
|
note: opts.note || "(no reason given)",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
this.#db.update(sessions).set({ state: "paid" }).where(eq(sessions.id, identity)).run();
|
||||||
|
} catch (err) {
|
||||||
|
this.#logger.error(`session-cache mark-paid failed for ${identity}: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
this.#logger.warn(`FORGIVEN exit for ${identity} by ${opts.operator}: ${q.amountMinor} ${q.currency} waived — ${opts.note}`);
|
||||||
|
return { ok: true, forgoneMinor: q.amountMinor, currency: q.currency };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One-read session view for the booth pay/exit modal: entry/exit times, paid
|
* One-read session view for the booth pay/exit modal: entry/exit times, paid
|
||||||
* state, amount owed now, and walk-back-grace status. Read-only — folds the
|
* state, amount owed now, and walk-back-grace status. Read-only — folds the
|
||||||
@@ -320,6 +464,7 @@ export class PayStation {
|
|||||||
withinGrace: false, graceExpiresAt: null,
|
withinGrace: false, graceExpiresAt: null,
|
||||||
overstay: false, subscription: false, subscriptionId: null, subscriptionHolder: null, plate: null,
|
overstay: false, subscription: false, subscriptionId: null, subscriptionHolder: null, plate: null,
|
||||||
grossMinor: null, discountMinor: null, validationLines: [],
|
grossMinor: null, discountMinor: null, validationLines: [],
|
||||||
|
chargeLines: [], chargesMinor: null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Subscription occurrence? The entry payload carries permit:true + permitId.
|
// Subscription occurrence? The entry payload carries permit:true + permitId.
|
||||||
@@ -360,6 +505,8 @@ export class PayStation {
|
|||||||
let grossMinor: number | null = null;
|
let grossMinor: number | null = null;
|
||||||
let discountMinor: number | null = null;
|
let discountMinor: number | null = null;
|
||||||
let validationLines: ValidationLine[] = [];
|
let validationLines: ValidationLine[] = [];
|
||||||
|
let chargeLines: ChargeLine[] = [];
|
||||||
|
let chargesMinor: number | null = null;
|
||||||
if (open && !isSubscription) {
|
if (open && !isSubscription) {
|
||||||
try {
|
try {
|
||||||
const q = this.quote(id);
|
const q = this.quote(id);
|
||||||
@@ -368,6 +515,8 @@ export class PayStation {
|
|||||||
grossMinor = q.grossMinor;
|
grossMinor = q.grossMinor;
|
||||||
discountMinor = q.discountMinor;
|
discountMinor = q.discountMinor;
|
||||||
validationLines = q.validationLines;
|
validationLines = q.validationLines;
|
||||||
|
chargeLines = q.chargeLines;
|
||||||
|
chargesMinor = q.chargesMinor;
|
||||||
} catch {
|
} catch {
|
||||||
/* no active tariff — leave null; modal shows session without a price */
|
/* no active tariff — leave null; modal shows session without a price */
|
||||||
}
|
}
|
||||||
@@ -389,6 +538,7 @@ export class PayStation {
|
|||||||
subscriptionHolder: this.#holderOf(subscriptionId),
|
subscriptionHolder: this.#holderOf(subscriptionId),
|
||||||
plate: plateForIdentity(this.#db, id)?.plate ?? null,
|
plate: plateForIdentity(this.#db, id)?.plate ?? null,
|
||||||
grossMinor, discountMinor, validationLines,
|
grossMinor, discountMinor, validationLines,
|
||||||
|
chargeLines, chargesMinor,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,9 +563,15 @@ export class PayStation {
|
|||||||
subscriptionId?: string | null;
|
subscriptionId?: string | null;
|
||||||
};
|
};
|
||||||
const byId = new Map<string, Acc>();
|
const byId = new Map<string, Acc>();
|
||||||
|
// Every row per identity (oldest first — `rows` is index-ordered), so the quotes
|
||||||
|
// below price from THIS scan instead of re-reading the ledger per session.
|
||||||
|
const rowsById = new Map<string, LedgerRow[]>();
|
||||||
for (const r of rows) {
|
for (const r of rows) {
|
||||||
const id = r.identity;
|
const id = r.identity;
|
||||||
if (!id) continue;
|
if (!id) continue;
|
||||||
|
const group = rowsById.get(id);
|
||||||
|
if (group) group.push(r);
|
||||||
|
else rowsById.set(id, [r]);
|
||||||
if (r.type === "vehicle_entry") {
|
if (r.type === "vehicle_entry") {
|
||||||
const a = byId.get(id) ?? { source: r.source ?? null };
|
const a = byId.get(id) ?? { source: r.source ?? null };
|
||||||
a.enteredAt = r.occurredAt;
|
a.enteredAt = r.occurredAt;
|
||||||
@@ -442,6 +598,8 @@ export class PayStation {
|
|||||||
// Resolve advisory plates for all candidate identities in ONE device_events scan
|
// Resolve advisory plates for all candidate identities in ONE device_events scan
|
||||||
// (cheaper than one lookup per row).
|
// (cheaper than one lookup per row).
|
||||||
const plates = platesForIdentities(this.#db, byId.keys());
|
const plates = platesForIdentities(this.#db, byId.keys());
|
||||||
|
// One tariff read for the whole list (quotes below reuse it).
|
||||||
|
const tariffFor = this.#tariffResolver();
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const out: ActiveSession[] = [];
|
const out: ActiveSession[] = [];
|
||||||
@@ -479,7 +637,9 @@ export class PayStation {
|
|||||||
let currency: string | null = null;
|
let currency: string | null = null;
|
||||||
if (open && !isSubscription && (a.paidAt == null || overstay)) {
|
if (open && !isSubscription && (a.paidAt == null || overstay)) {
|
||||||
try {
|
try {
|
||||||
const q = this.quote(identity);
|
// Priced from the rows already in hand — no per-session ledger reads. The
|
||||||
|
// charge providers (car wash) still run one indexed lookup per row.
|
||||||
|
const q = this.#quoteFromRows(identity, rowsById.get(identity) ?? [], tariffFor);
|
||||||
amountMinor = q.amountMinor;
|
amountMinor = q.amountMinor;
|
||||||
currency = q.currency;
|
currency = q.currency;
|
||||||
} catch {
|
} catch {
|
||||||
@@ -582,12 +742,10 @@ export class PayStation {
|
|||||||
|
|
||||||
/** The vehicle_entry of an OPEN session for this identity (no later exit), or null. */
|
/** The vehicle_entry of an OPEN session for this identity (no later exit), or null. */
|
||||||
#openEntry(identity: string) {
|
#openEntry(identity: string) {
|
||||||
const rows = this.#db
|
return PayStation.#openEntryFromRows(this.#identityRows(identity));
|
||||||
.select()
|
}
|
||||||
.from(ledgerEvents)
|
|
||||||
.where(eq(ledgerEvents.identity, identity))
|
static #openEntryFromRows(rows: readonly LedgerRow[]): LedgerRow | null {
|
||||||
.orderBy(ledgerEvents.index)
|
|
||||||
.all();
|
|
||||||
const entry = rows.find((r) => r.type === "vehicle_entry");
|
const entry = rows.find((r) => r.type === "vehicle_entry");
|
||||||
if (!entry) return null;
|
if (!entry) return null;
|
||||||
if (rows.some((r) => r.type === "vehicle_exit")) return null; // already closed
|
if (rows.some((r) => r.type === "vehicle_exit")) return null; // already closed
|
||||||
@@ -597,14 +755,20 @@ export class PayStation {
|
|||||||
/** The tariff version in force at `at` — latest effectiveFrom ≤ at, for the
|
/** The tariff version in force at `at` — latest effectiveFrom ≤ at, for the
|
||||||
* (single, for now) active site tariff. */
|
* (single, for now) active site tariff. */
|
||||||
#tariffVersionFor(at: string) {
|
#tariffVersionFor(at: string) {
|
||||||
|
return this.#tariffResolver()(at);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read the site tariff + its versions ONCE and return a resolver over them. A list
|
||||||
|
* view builds one resolver per request instead of two tariff reads per row. */
|
||||||
|
#tariffResolver(): TariffResolver {
|
||||||
const tariff = this.#db.select().from(tariffs).where(eq(tariffs.scope, "site")).get();
|
const tariff = this.#db.select().from(tariffs).where(eq(tariffs.scope, "site")).get();
|
||||||
if (!tariff) return null;
|
if (!tariff) return () => null;
|
||||||
const versions = this.#db
|
const versions = this.#db
|
||||||
.select()
|
.select()
|
||||||
.from(tariffVersions)
|
.from(tariffVersions)
|
||||||
.where(eq(tariffVersions.tariffId, tariff.id))
|
.where(eq(tariffVersions.tariffId, tariff.id))
|
||||||
.orderBy(desc(tariffVersions.effectiveFrom))
|
.orderBy(desc(tariffVersions.effectiveFrom))
|
||||||
.all();
|
.all();
|
||||||
return versions.find((v) => v.effectiveFrom <= at) ?? null;
|
return (at) => versions.find((v) => v.effectiveFrom <= at) ?? null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { and, desc, deviceEvents, eq, type Db } from "@parking/db";
|
import { and, desc, deviceEvents, eq, type Db } from "@parking/db";
|
||||||
|
import { isNormBox, isVehicleClass, type VehicleRead } from "@parking/shared";
|
||||||
|
|
||||||
// READ-TIME plate resolution. A recognized licence plate is ADVISORY evidence — it
|
// READ-TIME plate resolution. A recognized licence plate is ADVISORY evidence — it
|
||||||
// lives in the unsigned, prunable `device_events` (kind="read") stream written by the
|
// lives in the unsigned, prunable `device_events` (kind="read") stream written by the
|
||||||
@@ -23,6 +24,38 @@ interface ReadDetail {
|
|||||||
plate?: string;
|
plate?: string;
|
||||||
confidence?: number;
|
confidence?: number;
|
||||||
direction?: string;
|
direction?: string;
|
||||||
|
bodyType?: string;
|
||||||
|
bodyConfidence?: number;
|
||||||
|
snapshotId?: string;
|
||||||
|
vehicleBox?: unknown;
|
||||||
|
plateBox?: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The advisory VEHICLE read (body type) for a session — the same stream and the same
|
||||||
|
* preference as the plate (entry over exit, newest first). Null when vision never
|
||||||
|
* classified the vehicle. See venue-modules.md §Vehicle category from vision. */
|
||||||
|
export function vehicleForIdentity(db: Db, identity: string): VehicleRead | null {
|
||||||
|
const rows = db
|
||||||
|
.select({ detail: deviceEvents.detail })
|
||||||
|
.from(deviceEvents)
|
||||||
|
.where(and(eq(deviceEvents.category, "camera"), eq(deviceEvents.kind, "read")))
|
||||||
|
.orderBy(desc(deviceEvents.occurredAt))
|
||||||
|
.all();
|
||||||
|
let fallback: VehicleRead | null = null;
|
||||||
|
for (const r of rows) {
|
||||||
|
const d = (r.detail ?? {}) as ReadDetail;
|
||||||
|
if (d.identity !== identity || !isVehicleClass(d.bodyType) || typeof d.bodyConfidence !== "number") continue;
|
||||||
|
const v: VehicleRead = {
|
||||||
|
bodyType: d.bodyType,
|
||||||
|
confidence: d.bodyConfidence,
|
||||||
|
snapshotId: d.snapshotId ?? null,
|
||||||
|
box: isNormBox(d.vehicleBox) ? d.vehicleBox : null,
|
||||||
|
plateBox: isNormBox(d.plateBox) ? d.plateBox : null,
|
||||||
|
};
|
||||||
|
if (d.direction === "entry") return v;
|
||||||
|
if (!fallback) fallback = v;
|
||||||
|
}
|
||||||
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Best plate for one identity, or null. Prefers an entry read, then the newest read. */
|
/** Best plate for one identity, or null. Prefers an entry read, then the newest read. */
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
import { beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { devices, deviceEvents as deviceEventsTable, ledgerEvents, subscriptionCredentials, subscriptions, type Db } from "@parking/db";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { ReadDispatcher, readDebounceMs, DEFAULT_READ_DEBOUNCE_MS } from "./read-dispatch.js";
|
||||||
|
import { ExitFlow } from "./exit-flow.js";
|
||||||
|
import { SubscriptionFlow } from "./subscription-flow.js";
|
||||||
|
import type { DeviceReadEvent } from "./device-events.js";
|
||||||
|
import { makeLog, silentLogger } from "./test-helpers.js";
|
||||||
|
|
||||||
|
// SAME-CODE DEBOUNCE at the dispatcher (2026-10-11): the same value from the same reader
|
||||||
|
// inside the reader's `debounceMs` window is ONE presentation — the first verdict is
|
||||||
|
// replayed, nothing is signed twice, the repeat lands in device_events as telemetry.
|
||||||
|
// Born at park-2: the DT-008 reported one card twice 560 ms apart; the permit flow opened
|
||||||
|
// two sessions and pulsed the barrier twice. Every lane, every credential kind.
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
|
||||||
|
function seed(readerCfg: Record<string, unknown>, direction: "entry" | "exit" = "exit"): ReadDispatcher {
|
||||||
|
({ db } = createTestDb());
|
||||||
|
db.insert(devices).values({
|
||||||
|
id: "ctl",
|
||||||
|
category: "access",
|
||||||
|
driverId: "stub-access",
|
||||||
|
config: { relays: [{ relay: 1, direction }] },
|
||||||
|
enabled: true,
|
||||||
|
}).run();
|
||||||
|
db.insert(devices).values({
|
||||||
|
id: "reader",
|
||||||
|
category: "reader",
|
||||||
|
driverId: "dingtian-qr-reader",
|
||||||
|
config: { serial: "H05MA5B0", controllerId: "ctl", relay: 1, ...readerCfg },
|
||||||
|
enabled: true,
|
||||||
|
}).run();
|
||||||
|
const log = makeLog(db);
|
||||||
|
return new ReadDispatcher(db, new ExitFlow(db, log, silentLogger()), new SubscriptionFlow(db, log, silentLogger()), silentLogger());
|
||||||
|
}
|
||||||
|
|
||||||
|
function read(value: string, opts: { kind?: DeviceReadEvent["kind"]; channel?: DeviceReadEvent["channel"] } = {}): DeviceReadEvent {
|
||||||
|
return {
|
||||||
|
driverId: "dingtian-qr-reader",
|
||||||
|
deviceId: "reader",
|
||||||
|
value,
|
||||||
|
kind: opts.kind ?? "qr",
|
||||||
|
...(opts.channel ? { channel: opts.channel } : {}),
|
||||||
|
at: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const ledger = () => db.select().from(ledgerEvents).all();
|
||||||
|
const debounced = () =>
|
||||||
|
db.select().from(deviceEventsTable).all()
|
||||||
|
.map((r) => r.detail as { debounced?: boolean; value?: string; sinceMs?: number; replayedAccepted?: boolean })
|
||||||
|
.filter((d) => d.debounced === true);
|
||||||
|
|
||||||
|
describe("readDebounceMs", () => {
|
||||||
|
it("defaults to 5000 when the reader has no value; honours numbers and form strings; 0 = off", () => {
|
||||||
|
expect(readDebounceMs({})).toBe(DEFAULT_READ_DEBOUNCE_MS);
|
||||||
|
expect(readDebounceMs({ debounceMs: "" })).toBe(5000);
|
||||||
|
expect(readDebounceMs({ debounceMs: 2500 })).toBe(2500);
|
||||||
|
expect(readDebounceMs({ debounceMs: "800" })).toBe(800);
|
||||||
|
expect(readDebounceMs({ debounceMs: 0 })).toBe(0);
|
||||||
|
expect(readDebounceMs({ debounceMs: "nope" })).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("read-dispatch same-code debounce", () => {
|
||||||
|
it("a repeat of the same unknown ticket at exit signs ONE refusal, replays the verdict, logs telemetry", async () => {
|
||||||
|
const d = seed({}); // default window
|
||||||
|
const first = await d.dispatch(read("00000000000")); // Luhn-valid, no session → refused + signed
|
||||||
|
const second = await d.dispatch(read("00000000000"));
|
||||||
|
expect(ledger()).toHaveLength(1); // not a second red row for the same scan
|
||||||
|
expect(second).toEqual(first); // same verdict → same beep
|
||||||
|
expect(debounced()).toHaveLength(1);
|
||||||
|
expect(debounced()[0]).toMatchObject({ value: "00000000000", replayedAccepted: false });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a double-read SUBSCRIBER card at ENTRY opens ONE occurrence and the repeat is accepted (replayed)", async () => {
|
||||||
|
const d = seed({}, "entry");
|
||||||
|
db.insert(subscriptions).values({ id: "s1", status: "active", quantity: 1, period: "month" }).run();
|
||||||
|
db.insert(subscriptionCredentials).values({ id: "c1", subscriptionId: "s1", kind: "rf", value: "3F3AFC" }).run();
|
||||||
|
const first = await d.dispatch(read("3F3AFC", { kind: "card", channel: "rf" }));
|
||||||
|
const second = await d.dispatch(read("3F3AFC", { kind: "card", channel: "rf" }));
|
||||||
|
expect(first.accepted).toBe(true);
|
||||||
|
expect(second.accepted).toBe(true); // the card WAS read; beep ok, but no second entry
|
||||||
|
expect(ledger().filter((e) => e.type === "vehicle_entry")).toHaveLength(1);
|
||||||
|
expect(debounced()).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a DIFFERENT code inside the window is not debounced", async () => {
|
||||||
|
const d = seed({});
|
||||||
|
await d.dispatch(read("00000000000"));
|
||||||
|
await d.dispatch(read("00000000026")); // another Luhn-valid shape
|
||||||
|
expect(ledger()).toHaveLength(2);
|
||||||
|
expect(debounced()).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("the window comes from the reader's config and expires", async () => {
|
||||||
|
const d = seed({ debounceMs: 30 });
|
||||||
|
await d.dispatch(read("00000000000"));
|
||||||
|
await new Promise((r) => setTimeout(r, 45));
|
||||||
|
await d.dispatch(read("00000000000")); // outside the 30ms window → a fresh decision
|
||||||
|
expect(ledger()).toHaveLength(2);
|
||||||
|
expect(debounced()).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("debounceMs = 0 turns it off", async () => {
|
||||||
|
const d = seed({ debounceMs: 0 });
|
||||||
|
await d.dispatch(read("00000000000"));
|
||||||
|
await d.dispatch(read("00000000000"));
|
||||||
|
expect(ledger()).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a FAILED first decision is not replayed: the repeat decides afresh, no unhandled rejection", async () => {
|
||||||
|
seed({});
|
||||||
|
// Make the exit flow blow up once (e.g. a signer hiccup), then work.
|
||||||
|
const log = makeLog(db);
|
||||||
|
const flaky = new ExitFlow(db, log, silentLogger());
|
||||||
|
let calls = 0;
|
||||||
|
const orig = flaky.handleAt.bind(flaky);
|
||||||
|
flaky.handleAt = async (...args: Parameters<ExitFlow["handleAt"]>) => {
|
||||||
|
calls++;
|
||||||
|
if (calls === 1) throw new Error("signer busy");
|
||||||
|
return orig(...args);
|
||||||
|
};
|
||||||
|
const d2 = new ReadDispatcher(db, flaky, new SubscriptionFlow(db, log, silentLogger()), silentLogger());
|
||||||
|
const rejections: unknown[] = [];
|
||||||
|
const onUnhandled = (r: unknown) => rejections.push(r);
|
||||||
|
process.on("unhandledRejection", onUnhandled);
|
||||||
|
try {
|
||||||
|
await expect(d2.dispatch(read("00000000000"))).rejects.toThrow("signer busy");
|
||||||
|
const second = await d2.dispatch(read("00000000000")); // inside the window, but nothing to replay
|
||||||
|
expect(second.accepted).toBe(false); // a real decision (refused: no session), signed once
|
||||||
|
expect(ledger()).toHaveLength(1);
|
||||||
|
await new Promise((r) => setTimeout(r, 10));
|
||||||
|
expect(rejections).toEqual([]);
|
||||||
|
} finally {
|
||||||
|
process.off("unhandledRejection", onUnhandled);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a repeat arriving while the first read is still deciding gets the SAME decision", async () => {
|
||||||
|
const d = seed({});
|
||||||
|
const [a, b] = await Promise.all([d.dispatch(read("00000000000")), d.dispatch(read("00000000000"))]);
|
||||||
|
expect(b).toEqual(a);
|
||||||
|
expect(ledger()).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,7 +5,7 @@ import type { DeviceReadEvent, ReadOutcome } from "./device-events.js";
|
|||||||
import type { ExitFlow } from "./exit-flow.js";
|
import type { ExitFlow } from "./exit-flow.js";
|
||||||
import { validateTicketCode } from "./entry-flow.js";
|
import { validateTicketCode } from "./entry-flow.js";
|
||||||
import type { SubscriptionFlow } from "./subscription-flow.js";
|
import type { SubscriptionFlow } from "./subscription-flow.js";
|
||||||
import { relayForDevice } from "./device-resolve.js";
|
import { relayForDevice, type ResolvedRelay } from "./device-resolve.js";
|
||||||
|
|
||||||
// Routes a credential read (ticket scan / plate / card) to the right flow. A read
|
// Routes a credential read (ticket scan / plate / card) to the right flow. A read
|
||||||
// can mean a subscription entry/exit OR a transient exit, so we dispatch by WHAT the
|
// can mean a subscription entry/exit OR a transient exit, so we dispatch by WHAT the
|
||||||
@@ -35,12 +35,49 @@ import { relayForDevice } from "./device-resolve.js";
|
|||||||
// - ANY read on a CONFIRMED RF channel (a physically present card, enrolled or
|
// - ANY read on a CONFIRMED RF channel (a physically present card, enrolled or
|
||||||
// not, is a real event — RF is never sun noise),
|
// not, is a real event — RF is never sun noise),
|
||||||
// - plates (different population; never shape-filtered here).
|
// - plates (different population; never shape-filtered here).
|
||||||
|
//
|
||||||
|
// SAME-CODE DEBOUNCE (2026-10-11, park-2). The DT-008 reported one card TWICE 560 ms
|
||||||
|
// apart; the permit flow opened two sessions on the same permit and pulsed the barrier
|
||||||
|
// twice. Any reader can double-read (a card held a beat too long, a QR re-decoded while
|
||||||
|
// the hand moves), and at exit a second scan of a ticket inside its grace period would
|
||||||
|
// sign a noSession refusal for a car that just left. So the SAME value from the SAME
|
||||||
|
// reader within the reader's `debounceMs` (Setup → reader; default 5000; 0 = off) is one
|
||||||
|
// presentation: the first verdict is REPLAYED (same beep, no second decision, nothing
|
||||||
|
// signed twice) and the repeat is kept as unsigned telemetry. Every lane, every kind
|
||||||
|
// (card / QR / ticket / plate) — the ANPR bridge debounces plates itself, longer.
|
||||||
|
|
||||||
|
/** Default same-code debounce when a reader's config has none (ms). */
|
||||||
|
export const DEFAULT_READ_DEBOUNCE_MS = 5000;
|
||||||
|
/** Recent-reads map housekeeping: prune above the soft cap, never exceed the hard cap, and
|
||||||
|
* treat any entry older than this as expired whatever the reader's configured window. */
|
||||||
|
const RECENT_READS_SOFT_CAP = 512;
|
||||||
|
const RECENT_READS_HARD_CAP = 4096;
|
||||||
|
const RECENT_READS_MAX_AGE_MS = 60_000;
|
||||||
|
|
||||||
|
interface ReaderConfig {
|
||||||
|
readonly debounceMs?: number | string | null;
|
||||||
|
readonly [k: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The reader's debounce window: its config value (the Setup form may store a string),
|
||||||
|
* else the default; 0 (or a negative/garbage value coerced to 0) disables. */
|
||||||
|
export function readDebounceMs(config: unknown): number {
|
||||||
|
const v = (config as ReaderConfig | null)?.debounceMs;
|
||||||
|
if (v == null || v === "") return DEFAULT_READ_DEBOUNCE_MS;
|
||||||
|
const n = Number(v);
|
||||||
|
return Number.isFinite(n) && n > 0 ? n : 0;
|
||||||
|
}
|
||||||
|
|
||||||
export class ReadDispatcher {
|
export class ReadDispatcher {
|
||||||
readonly #db: Db;
|
readonly #db: Db;
|
||||||
readonly #exit: ExitFlow;
|
readonly #exit: ExitFlow;
|
||||||
readonly #subscription: SubscriptionFlow;
|
readonly #subscription: SubscriptionFlow;
|
||||||
readonly #logger: FastifyBaseLogger;
|
readonly #logger: FastifyBaseLogger;
|
||||||
|
/** Recent reads keyed `deviceId:kind:value` → when last seen + the FIRST verdict (a
|
||||||
|
* promise, so a repeat arriving while the first is still deciding gets that same
|
||||||
|
* decision). `at` slides on each repeat: a card held against the reader stays one
|
||||||
|
* presentation for as long as it keeps re-reading. */
|
||||||
|
readonly #recent = new Map<string, { at: number; outcome: Promise<ReadOutcome> }>();
|
||||||
|
|
||||||
constructor(db: Db, exit: ExitFlow, subscription: SubscriptionFlow, logger: FastifyBaseLogger) {
|
constructor(db: Db, exit: ExitFlow, subscription: SubscriptionFlow, logger: FastifyBaseLogger) {
|
||||||
this.#db = db;
|
this.#db = db;
|
||||||
@@ -59,6 +96,50 @@ export class ReadDispatcher {
|
|||||||
return { accepted: false, reason: "reader not bound to a barrier (no relay to open)" };
|
return { accepted: false, reason: "reader not bound to a barrier (no relay to open)" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Same code, same reader, inside the window → replay the first verdict. Checked
|
||||||
|
// BEFORE matching so a repeat never reaches a flow (no second entry, no second exit,
|
||||||
|
// no refusal anomaly for a car that was just admitted).
|
||||||
|
const debounceMs = readDebounceMs(reader.config);
|
||||||
|
const key = `${e.deviceId}:${e.kind}:${e.value}`;
|
||||||
|
const now = Date.now();
|
||||||
|
const prev = this.#recent.get(key);
|
||||||
|
if (debounceMs > 0 && prev && now - prev.at < debounceMs) {
|
||||||
|
const sinceMs = now - prev.at;
|
||||||
|
prev.at = now;
|
||||||
|
this.#logger.info(
|
||||||
|
`read debounced: '${e.value}' (${e.kind}) again on ${e.deviceId} after ${sinceMs}ms (window ${debounceMs}ms) — first verdict replayed`,
|
||||||
|
);
|
||||||
|
prev.outcome.then(
|
||||||
|
(o) => this.#recordDebounced(e, sinceMs, debounceMs, o),
|
||||||
|
() => undefined, // the first decision failed: the caller gets that rejection, not us
|
||||||
|
);
|
||||||
|
return prev.outcome;
|
||||||
|
}
|
||||||
|
const outcome = this.#decide(resolved, e);
|
||||||
|
if (debounceMs > 0) {
|
||||||
|
this.#recent.set(key, { at: now, outcome });
|
||||||
|
// A failed decision is NOT a verdict to replay — forget it so the next read retries.
|
||||||
|
outcome.catch(() => this.#recent.delete(key));
|
||||||
|
this.#pruneRecent(now);
|
||||||
|
}
|
||||||
|
return outcome;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Keep the recent-reads map small: drop expired entries, and if a flood of distinct
|
||||||
|
* values inside one window still leaves it over the cap, drop the oldest. Bounded
|
||||||
|
* memory whatever a misbehaving (or hostile) reader sends. */
|
||||||
|
#pruneRecent(now: number): void {
|
||||||
|
if (this.#recent.size <= RECENT_READS_SOFT_CAP) return;
|
||||||
|
for (const [k, r] of this.#recent) if (now - r.at >= RECENT_READS_MAX_AGE_MS) this.#recent.delete(k);
|
||||||
|
while (this.#recent.size > RECENT_READS_HARD_CAP) {
|
||||||
|
const oldest = this.#recent.keys().next().value; // insertion order = oldest first
|
||||||
|
if (oldest === undefined) break;
|
||||||
|
this.#recent.delete(oldest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The actual routing of a FRESH read (not a debounced repeat). */
|
||||||
|
async #decide(resolved: ResolvedRelay, e: DeviceReadEvent): Promise<ReadOutcome> {
|
||||||
const sub = this.#subscription.match(e);
|
const sub = this.#subscription.match(e);
|
||||||
if (sub) {
|
if (sub) {
|
||||||
return this.#subscription.run(resolved, e, sub);
|
return this.#subscription.run(resolved, e, sub);
|
||||||
@@ -86,6 +167,34 @@ export class ReadDispatcher {
|
|||||||
return this.#exit.handleAt(resolved, e);
|
return this.#exit.handleAt(resolved, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Unsigned telemetry for a debounced repeat — auditable in device_events (the double
|
||||||
|
* read is still a fact about the hardware), out of the signed feed. */
|
||||||
|
#recordDebounced(e: DeviceReadEvent, sinceMs: number, debounceMs: number, replayed: ReadOutcome): void {
|
||||||
|
try {
|
||||||
|
this.#db
|
||||||
|
.insert(deviceEventsTable)
|
||||||
|
.values({
|
||||||
|
id: randomUUID(),
|
||||||
|
deviceId: e.deviceId,
|
||||||
|
category: "reader",
|
||||||
|
kind: "read",
|
||||||
|
detail: {
|
||||||
|
debounced: true,
|
||||||
|
value: e.value,
|
||||||
|
readKind: e.kind,
|
||||||
|
...(e.channel ? { channel: e.channel } : {}),
|
||||||
|
sinceMs,
|
||||||
|
debounceMs,
|
||||||
|
replayedAccepted: replayed.accepted,
|
||||||
|
},
|
||||||
|
occurredAt: e.at,
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
} catch (err) {
|
||||||
|
this.#logger.error(`debounced-read telemetry insert failed: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Unsigned telemetry for a filtered read — auditable in device_events, out of the
|
/** Unsigned telemetry for a filtered read — auditable in device_events, out of the
|
||||||
* signed feed. Mirrors the entry flow's suppressed-press pattern. */
|
* signed feed. Mirrors the entry flow's suppressed-press pattern. */
|
||||||
#recordUnrecognized(e: DeviceReadEvent): void {
|
#recordUnrecognized(e: DeviceReadEvent): void {
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { devices, deviceEvents as deviceEventsTable, type Db, type DeviceRow } from "@parking/db";
|
||||||
|
import { pulseBarrier } from "./relay-pulse.js";
|
||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
|
||||||
|
const silentLogger = { info() {}, warn() {}, error() {}, debug() {} } as unknown as FastifyBaseLogger;
|
||||||
|
|
||||||
|
// Every barrier pulse — success or failure — must leave an UNSIGNED device_events row
|
||||||
|
// (kind "relay") naming the relay, the direction, the identity and who asked, so a
|
||||||
|
// "did the server open the barrier?" question is answerable from telemetry alone
|
||||||
|
// (park-2, 2026-10-09). A failure is recorded AND re-thrown (each flow keeps its own
|
||||||
|
// anomaly handling).
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let controller: DeviceRow;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
db.insert(devices)
|
||||||
|
.values({
|
||||||
|
id: "ctl-1",
|
||||||
|
category: "access",
|
||||||
|
driverId: "stub-access",
|
||||||
|
config: { relays: [{ relay: 2, direction: "exit" }] },
|
||||||
|
enabled: true,
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
controller = db.select().from(devices).get()!;
|
||||||
|
});
|
||||||
|
afterEach(() => close());
|
||||||
|
|
||||||
|
function relayRows() {
|
||||||
|
return db
|
||||||
|
.select()
|
||||||
|
.from(deviceEventsTable)
|
||||||
|
.all()
|
||||||
|
.filter((r) => r.kind === "relay")
|
||||||
|
.map((r) => ({ deviceId: r.deviceId, category: r.category, detail: r.detail as Record<string, unknown> }));
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("pulseBarrier", () => {
|
||||||
|
it("pulses the relay and records a successful relay event with the context", async () => {
|
||||||
|
const fired: number[] = [];
|
||||||
|
const access = { pulseOpen: async (door: number) => void fired.push(door) };
|
||||||
|
await pulseBarrier(db, silentLogger, access, { controller, relay: 2 }, {
|
||||||
|
direction: "exit",
|
||||||
|
identity: "forgiven:abc",
|
||||||
|
source: "booth",
|
||||||
|
operator: "admin",
|
||||||
|
});
|
||||||
|
expect(fired).toEqual([2]);
|
||||||
|
const rows = relayRows();
|
||||||
|
expect(rows).toHaveLength(1);
|
||||||
|
expect(rows[0].deviceId).toBe("ctl-1");
|
||||||
|
expect(rows[0].category).toBe("access");
|
||||||
|
expect(rows[0].detail).toMatchObject({
|
||||||
|
driverId: "stub-access",
|
||||||
|
relay: 2,
|
||||||
|
direction: "exit",
|
||||||
|
identity: "forgiven:abc",
|
||||||
|
source: "booth",
|
||||||
|
operator: "admin",
|
||||||
|
ok: true,
|
||||||
|
});
|
||||||
|
expect(typeof rows[0].detail.tookMs).toBe("number");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("records a failed pulse with the error, then re-throws it", async () => {
|
||||||
|
const access = {
|
||||||
|
pulseOpen: async () => {
|
||||||
|
throw new Error("timeout");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
await expect(
|
||||||
|
pulseBarrier(db, silentLogger, access, { controller, relay: 2 }, { direction: "exit", identity: "T1", source: "ticket" }),
|
||||||
|
).rejects.toThrow("timeout");
|
||||||
|
const rows = relayRows();
|
||||||
|
expect(rows).toHaveLength(1);
|
||||||
|
expect(rows[0].detail).toMatchObject({ relay: 2, identity: "T1", source: "ticket", ok: false, error: "timeout" });
|
||||||
|
expect(rows[0].detail.operator).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import { randomUUID } from "node:crypto";
|
||||||
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import { deviceEvents as deviceEventsTable, type Db, type DeviceRow } from "@parking/db";
|
||||||
|
import type { AccessControlDevice } from "@parking/devices";
|
||||||
|
import type { Direction } from "./device-resolve.js";
|
||||||
|
|
||||||
|
// The ONE place a barrier relay is pulsed from business logic. Every pulse is logged at
|
||||||
|
// info (relay, controller, who asked and for which identity) and echoed into the UNSIGNED
|
||||||
|
// device_events stream (kind "relay") whether it succeeded or failed — so "did the server
|
||||||
|
// open the barrier at 15:50?" is answerable from `docker logs` or the telemetry table
|
||||||
|
// without reading the ledger. The signed ledger stays the authority on WHY (the
|
||||||
|
// vehicle_entry/exit/anomaly is appended BEFORE the pulse); this is the device-side echo
|
||||||
|
// (wiki/concepts/device-events.md). Failures are recorded, then re-thrown so each flow keeps
|
||||||
|
// its own anomaly/result handling. park-2 2026-10-09: a "barrier opened by itself" claim
|
||||||
|
// took ledger + snapshots to settle because pulses were only logged when they failed.
|
||||||
|
|
||||||
|
export interface PulseContext {
|
||||||
|
/** The barrier's direction (the relay's configured event). */
|
||||||
|
readonly direction: Direction;
|
||||||
|
/** The session/ticket/card/ref the open is for (matches the signed event's identity). */
|
||||||
|
readonly identity: string;
|
||||||
|
/** Who asked: ticket | manual | booth | lpr | subscription | relay-test … (free-form). */
|
||||||
|
readonly source: string;
|
||||||
|
readonly operator?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function pulseBarrier(
|
||||||
|
db: Db,
|
||||||
|
logger: FastifyBaseLogger,
|
||||||
|
access: Pick<AccessControlDevice, "pulseOpen">,
|
||||||
|
target: { readonly controller: DeviceRow; readonly relay: number },
|
||||||
|
ctx: PulseContext,
|
||||||
|
): Promise<void> {
|
||||||
|
const { controller, relay } = target;
|
||||||
|
const who = ctx.operator ? ` by ${ctx.operator}` : "";
|
||||||
|
logger.info(
|
||||||
|
`relay pulse: ${ctx.direction} relay ${relay} on ${controller.driverId} ${controller.id} ← ${ctx.source} ${ctx.identity}${who}`,
|
||||||
|
);
|
||||||
|
const startedAt = Date.now();
|
||||||
|
const firedAt = new Date(startedAt).toISOString();
|
||||||
|
try {
|
||||||
|
await access.pulseOpen(relay);
|
||||||
|
record(db, logger, controller, relay, ctx, firedAt, { ok: true, tookMs: Date.now() - startedAt });
|
||||||
|
} catch (err) {
|
||||||
|
const error = (err as Error).message;
|
||||||
|
logger.error(`relay pulse FAILED: ${ctx.direction} relay ${relay} on ${controller.id} for ${ctx.identity}: ${error}`);
|
||||||
|
record(db, logger, controller, relay, ctx, firedAt, { ok: false, error, tookMs: Date.now() - startedAt });
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function record(
|
||||||
|
db: Db,
|
||||||
|
logger: FastifyBaseLogger,
|
||||||
|
controller: DeviceRow,
|
||||||
|
relay: number,
|
||||||
|
ctx: PulseContext,
|
||||||
|
firedAt: string,
|
||||||
|
outcome: { ok: boolean; error?: string; tookMs: number },
|
||||||
|
): void {
|
||||||
|
// Telemetry must never block or fail the open — swallow insert errors (logged).
|
||||||
|
try {
|
||||||
|
db.insert(deviceEventsTable)
|
||||||
|
.values({
|
||||||
|
id: randomUUID(),
|
||||||
|
deviceId: controller.id,
|
||||||
|
category: "access",
|
||||||
|
kind: "relay",
|
||||||
|
detail: {
|
||||||
|
driverId: controller.driverId,
|
||||||
|
relay,
|
||||||
|
direction: ctx.direction,
|
||||||
|
identity: ctx.identity,
|
||||||
|
source: ctx.source,
|
||||||
|
...(ctx.operator ? { operator: ctx.operator } : {}),
|
||||||
|
...outcome,
|
||||||
|
},
|
||||||
|
occurredAt: firedAt,
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`relay pulse telemetry insert failed: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { buildServer } from "./server.js";
|
||||||
|
|
||||||
|
// Request logging shape (2026-10-08 noise cut, wiki/concepts/app-logs.md §Noise cut):
|
||||||
|
// - /health logs NOTHING (Docker probes it every 30 s — it was half a quiet booth's log),
|
||||||
|
// - every other request logs ONE line "METHOD url → status Nms" (not pino's two),
|
||||||
|
// - a handler failure still logs the error with its stack, because
|
||||||
|
// `disableRequestLogging` also silences Fastify's default error-handler logging.
|
||||||
|
// The logger's stream is process.stdout (pinoDbStream tees warn+ into app_logs), so
|
||||||
|
// capture stdout writes and look at the JSON lines.
|
||||||
|
|
||||||
|
const lines: Record<string, unknown>[] = [];
|
||||||
|
let restore: (() => void) | null = null;
|
||||||
|
|
||||||
|
function captureStdout() {
|
||||||
|
const spy = vi.spyOn(process.stdout, "write").mockImplementation(((chunk: unknown) => {
|
||||||
|
const text = typeof chunk === "string" ? chunk : Buffer.from(chunk as Uint8Array).toString("utf8");
|
||||||
|
for (const l of text.split("\n")) {
|
||||||
|
if (!l.trim()) continue;
|
||||||
|
try {
|
||||||
|
lines.push(JSON.parse(l));
|
||||||
|
} catch {
|
||||||
|
/* non-JSON stdout noise — ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}) as typeof process.stdout.write);
|
||||||
|
restore = () => spy.mockRestore();
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
restore?.();
|
||||||
|
restore = null;
|
||||||
|
lines.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
const msgs = () => lines.map((l) => String(l.msg ?? ""));
|
||||||
|
|
||||||
|
describe("request logging", () => {
|
||||||
|
it("/health is silent; other requests log exactly one line; 5xx keeps the error", async () => {
|
||||||
|
const t = createTestDb();
|
||||||
|
captureStdout();
|
||||||
|
// vitest.config forces LOG_LEVEL=silent for every test; this one needs the real level.
|
||||||
|
const prevLevel = process.env.LOG_LEVEL;
|
||||||
|
process.env.LOG_LEVEL = "info";
|
||||||
|
const app = await buildServer({ db: t.db });
|
||||||
|
process.env.LOG_LEVEL = prevLevel;
|
||||||
|
app.get("/boom", async () => {
|
||||||
|
throw new Error("kaboom");
|
||||||
|
});
|
||||||
|
await app.ready();
|
||||||
|
try {
|
||||||
|
lines.length = 0; // drop startup lines
|
||||||
|
|
||||||
|
expect((await app.inject({ method: "GET", url: "/health" })).statusCode).toBe(200);
|
||||||
|
expect(msgs().filter((m) => m.includes("/health"))).toEqual([]);
|
||||||
|
|
||||||
|
expect((await app.inject({ method: "GET", url: "/api/nope" })).statusCode).toBe(404);
|
||||||
|
const notFound = msgs().filter((m) => m.includes("/api/nope"));
|
||||||
|
expect(notFound).toHaveLength(1);
|
||||||
|
expect(notFound[0]).toMatch(/^GET \/api\/nope → 404 \d+ms$/);
|
||||||
|
expect(msgs().some((m) => /incoming request|request completed/.test(m))).toBe(false);
|
||||||
|
|
||||||
|
expect((await app.inject({ method: "GET", url: "/boom" })).statusCode).toBe(500);
|
||||||
|
const boom = lines.filter((l) => String(l.msg).includes("/boom"));
|
||||||
|
// one error line (with the err object) + the one-line request summary
|
||||||
|
expect(boom.map((l) => [l.level, String(l.msg).replace(/\d+ms/, "Nms")])).toEqual([
|
||||||
|
["error", "GET /boom failed: kaboom"],
|
||||||
|
["info", "GET /boom → 500 Nms"],
|
||||||
|
]);
|
||||||
|
expect((boom[0]!.err as { message?: string })?.message).toBe("kaboom");
|
||||||
|
} finally {
|
||||||
|
await app.close();
|
||||||
|
t.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcrypt";
|
||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { eq, roles, users, type Db } from "@parking/db";
|
import { eq, roles, users, type Db } from "@parking/db";
|
||||||
|
import { effectiveModulesFor } from "../modules.js";
|
||||||
import {
|
import {
|
||||||
clearAuthCookies,
|
clearAuthCookies,
|
||||||
newCsrfToken,
|
newCsrfToken,
|
||||||
@@ -36,6 +37,24 @@ interface FontScaleBody {
|
|||||||
fontScale: number;
|
fontScale: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Booth camera tiles the user left open (camera device ids, in opening order). Bounded:
|
||||||
|
// a booth has a handful of cameras; the cap stops a client from storing junk.
|
||||||
|
const OPEN_CAMERAS_MAX = 8;
|
||||||
|
const DEVICE_ID_MAX = 64;
|
||||||
|
interface OpenCamerasBody {
|
||||||
|
openCameras: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** users.open_cameras (JSON text) → string[]; anything malformed reads as none open. */
|
||||||
|
function parseOpenCameras(raw: string | null | undefined): string[] {
|
||||||
|
try {
|
||||||
|
const v: unknown = JSON.parse(raw ?? "[]");
|
||||||
|
return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string").slice(0, OPEN_CAMERAS_MAX) : [];
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Self-service profile: a signed-in user edits their OWN display name + email. This is
|
// Self-service profile: a signed-in user edits their OWN display name + email. This is
|
||||||
// NOT the admin user-management path (routes/users.ts) — it only ever touches the caller
|
// NOT the admin user-management path (routes/users.ts) — it only ever touches the caller
|
||||||
// (req.user.sub), needs no `user:*` permission, and can't change username, role, or any
|
// (req.user.sub), needs no `user:*` permission, and can't change username, role, or any
|
||||||
@@ -89,6 +108,7 @@ function sessionView(
|
|||||||
language: string;
|
language: string;
|
||||||
theme: string;
|
theme: string;
|
||||||
fontScale: number;
|
fontScale: number;
|
||||||
|
openCameras?: string | null;
|
||||||
fullName?: string | null;
|
fullName?: string | null;
|
||||||
email?: string | null;
|
email?: string | null;
|
||||||
},
|
},
|
||||||
@@ -105,8 +125,12 @@ function sessionView(
|
|||||||
language: user.language,
|
language: user.language,
|
||||||
theme: user.theme,
|
theme: user.theme,
|
||||||
fontScale: user.fontScale,
|
fontScale: user.fontScale,
|
||||||
|
openCameras: parseOpenCameras(user.openCameras),
|
||||||
fullName: user.fullName ?? null,
|
fullName: user.fullName ?? null,
|
||||||
email: user.email ?? null,
|
email: user.email ?? null,
|
||||||
|
// Effective venue modules (entitled ∩ activated) so the SPA can hide nav/routes
|
||||||
|
// on first paint. The server still enforces via requireModule — this is display.
|
||||||
|
modules: effectiveModulesFor(db),
|
||||||
...(csrf ? { csrfToken: csrf } : {}),
|
...(csrf ? { csrfToken: csrf } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -215,6 +239,33 @@ export async function authRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Which booth camera tiles I left open (any signed-in user; only touches the caller).
|
||||||
|
// Persisted like `theme`, restored on the next login from any booth. Ids are not
|
||||||
|
// checked against `devices`: a removed camera's tile is simply not rendered.
|
||||||
|
app.put<{ Body: OpenCamerasBody }>(
|
||||||
|
"/api/auth/open-cameras",
|
||||||
|
{ preHandler: requireAuth },
|
||||||
|
async (req, reply) => {
|
||||||
|
const raw = req.body?.openCameras;
|
||||||
|
if (
|
||||||
|
!Array.isArray(raw) ||
|
||||||
|
raw.length > OPEN_CAMERAS_MAX ||
|
||||||
|
!raw.every((x) => typeof x === "string" && x.length > 0 && x.length <= DEVICE_ID_MAX)
|
||||||
|
) {
|
||||||
|
return reply
|
||||||
|
.code(400)
|
||||||
|
.send({ error: `openCameras must be up to ${OPEN_CAMERAS_MAX} device ids` });
|
||||||
|
}
|
||||||
|
const openCameras = [...new Set(raw as string[])];
|
||||||
|
await db
|
||||||
|
.update(users)
|
||||||
|
.set({ openCameras: JSON.stringify(openCameras) })
|
||||||
|
.where(eq(users.id, req.user.sub))
|
||||||
|
.run();
|
||||||
|
return { openCameras };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// Edit MY own display name / email (any signed-in user; no permission needed — it only
|
// Edit MY own display name / email (any signed-in user; no permission needed — it only
|
||||||
// touches the caller). Cannot change username or role — those stay admin-only (users.ts).
|
// touches the caller). Cannot change username or role — those stay admin-only (users.ts).
|
||||||
app.put<{ Body: ProfileBody }>(
|
app.put<{ Body: ProfileBody }>(
|
||||||
|
|||||||
@@ -0,0 +1,137 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { devices, snapshots, type Db } from "@parking/db";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { buildServer } from "../server.js";
|
||||||
|
import { login, seedUser } from "../test-helpers.js";
|
||||||
|
|
||||||
|
// Booth camera tiles: latest-snapshot lookup (session:read), the live socket's auth gate
|
||||||
|
// (same handshake as /api/ws, then device:read), and the per-user open-tiles preference.
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
app = await buildServer({ db });
|
||||||
|
await app.ready();
|
||||||
|
// A Dahua camera: no live-view capability, so the socket answers without any network.
|
||||||
|
db.insert(devices)
|
||||||
|
.values({ id: "cam-dahua", category: "camera", driverId: "dahua", config: { host: "192.0.2.1" } })
|
||||||
|
.run();
|
||||||
|
db.insert(devices).values({ id: "relay-1", category: "access", driverId: "stub-access", config: {} }).run();
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
|
||||||
|
function shot(id: string, deviceId: string, capturedAt: string) {
|
||||||
|
db.insert(snapshots)
|
||||||
|
.values({ id, direction: "entry", deviceId, identity: "x", contentType: "image/jpeg", bytes: Buffer.from([1]), capturedAt })
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("GET /api/cameras/:id/latest-snapshot", () => {
|
||||||
|
it("returns the camera's newest snapshot", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const auth = await login(app, "admin", "pw");
|
||||||
|
shot("s1", "cam-dahua", "2026-11-17T10:00:00.000Z");
|
||||||
|
shot("s2", "cam-dahua", "2026-11-17T11:00:00.000Z");
|
||||||
|
shot("s3", "other", "2026-11-17T12:00:00.000Z");
|
||||||
|
const res = await app.inject({ url: "/api/cameras/cam-dahua/latest-snapshot", headers: { cookie: auth.cookie } });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json()).toMatchObject({ id: "s2", direction: "entry" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("404s when the camera has none, 403s without session:read", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const admin = await login(app, "admin", "pw");
|
||||||
|
const none = await app.inject({ url: "/api/cameras/cam-dahua/latest-snapshot", headers: { cookie: admin.cookie } });
|
||||||
|
expect(none.statusCode).toBe(404);
|
||||||
|
|
||||||
|
await seedUser(db, { username: "dev", password: "pw", roleId: "devonly", permissions: ["device:read"] });
|
||||||
|
const dev = await login(app, "dev", "pw");
|
||||||
|
const res = await app.inject({ url: "/api/cameras/cam-dahua/latest-snapshot", headers: { cookie: dev.cookie } });
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("WS /api/cameras/:id/live", () => {
|
||||||
|
const origin = "http://booth.local:8080";
|
||||||
|
|
||||||
|
it("rejects a cross-origin handshake", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const auth = await login(app, "admin", "pw");
|
||||||
|
await expect(
|
||||||
|
app.injectWS("/api/cameras/cam-dahua/live", { headers: { cookie: auth.cookie, origin: "http://evil.example", host: "booth.local:8080" } }),
|
||||||
|
).rejects.toThrow(/403/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects a role without device:read", async () => {
|
||||||
|
await seedUser(db, { username: "cash", password: "pw", roleId: "cashier2", permissions: ["session:read"] });
|
||||||
|
const auth = await login(app, "cash", "pw");
|
||||||
|
await expect(
|
||||||
|
app.injectWS("/api/cameras/cam-dahua/live", { headers: { cookie: auth.cookie, origin, host: "booth.local:8080" } }),
|
||||||
|
).rejects.toThrow(/403/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("404s a device that is not a camera", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const auth = await login(app, "admin", "pw");
|
||||||
|
await expect(
|
||||||
|
app.injectWS("/api/cameras/relay-1/live", { headers: { cookie: auth.cookie, origin, host: "booth.local:8080" } }),
|
||||||
|
).rejects.toThrow(/404/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("tells the viewer when the camera has no live view", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const auth = await login(app, "admin", "pw");
|
||||||
|
let resolveMsg!: (m: string) => void;
|
||||||
|
const first = new Promise<string>((r) => (resolveMsg = r));
|
||||||
|
const ws = await app.injectWS(
|
||||||
|
"/api/cameras/cam-dahua/live",
|
||||||
|
{ headers: { cookie: auth.cookie, origin, host: "booth.local:8080" } },
|
||||||
|
{ onInit: (sock) => sock.once("message", (d) => resolveMsg(String(d))) },
|
||||||
|
);
|
||||||
|
const msg = await first;
|
||||||
|
expect(JSON.parse(msg)).toEqual({
|
||||||
|
kind: "status",
|
||||||
|
state: "unavailable",
|
||||||
|
reason: "live view not supported by this camera driver",
|
||||||
|
});
|
||||||
|
ws.terminate();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("PUT /api/auth/open-cameras", () => {
|
||||||
|
it("persists the open tiles and returns them on /me", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const auth = await login(app, "admin", "pw");
|
||||||
|
const put = await app.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/api/auth/open-cameras",
|
||||||
|
headers: { cookie: auth.cookie, "x-csrf-token": auth.csrf },
|
||||||
|
payload: { openCameras: ["cam-b", "cam-a", "cam-b"] },
|
||||||
|
});
|
||||||
|
expect(put.statusCode).toBe(200);
|
||||||
|
expect(put.json()).toEqual({ openCameras: ["cam-b", "cam-a"] });
|
||||||
|
const me = await app.inject({ url: "/api/auth/me", headers: { cookie: auth.cookie } });
|
||||||
|
expect(me.json().openCameras).toEqual(["cam-b", "cam-a"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects a malformed list", async () => {
|
||||||
|
await seedUser(db, { username: "admin", password: "pw" });
|
||||||
|
const auth = await login(app, "admin", "pw");
|
||||||
|
const res = await app.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/api/auth/open-cameras",
|
||||||
|
headers: { cookie: auth.cookie, "x-csrf-token": auth.csrf },
|
||||||
|
payload: { openCameras: Array.from({ length: 9 }, (_, i) => `c${i}`) },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { desc, devices, eq, snapshots, type Db } from "@parking/db";
|
||||||
|
import { isLiveStreamDevice } from "@parking/devices";
|
||||||
|
import { requirePermission, roleHasPermissions } from "../auth.js";
|
||||||
|
import { buildCamera } from "../snapshot.js";
|
||||||
|
import type { CameraLiveHub, LiveResolver, LiveStatus } from "../camera-live.js";
|
||||||
|
import { authenticateWsHandshake } from "./ws.js";
|
||||||
|
|
||||||
|
// The booth's camera tiles (wiki/decisions/camera-live-view.md):
|
||||||
|
// - GET /api/cameras/:deviceId/latest-snapshot — the tile's default picture: the newest
|
||||||
|
// entry/exit evidence still this camera captured (metadata; bytes stay at
|
||||||
|
// /api/snapshots/:id). Evidence → `session:read`, like every snapshot read.
|
||||||
|
// - WS /api/cameras/:deviceId/live — live video while the camera sees a vehicle.
|
||||||
|
// Same handshake auth as /api/ws (Origin allowlist, then desktop ticket or cookie),
|
||||||
|
// then `device:read` (the footer's permission). Server → client: text JSON
|
||||||
|
// `{kind:"status", ...LiveStatus}` and binary messages, each one complete JPEG.
|
||||||
|
// Advisory viewing only: no route here writes anything.
|
||||||
|
|
||||||
|
/** Bytes a viewer's socket may hold unsent before frames are skipped for it. */
|
||||||
|
const MAX_BUFFERED_BYTES = 256 * 1024;
|
||||||
|
|
||||||
|
/** Resolve a camera id to its live capability, or the reason it has none. */
|
||||||
|
export function liveResolverFor(db: Db): LiveResolver {
|
||||||
|
return (deviceId) => {
|
||||||
|
const row = db.select().from(devices).where(eq(devices.id, deviceId)).get();
|
||||||
|
if (!row || row.category !== "camera") return "unknown camera";
|
||||||
|
if (!row.enabled) return "camera disabled";
|
||||||
|
const camera = buildCamera(row);
|
||||||
|
if (!camera) return "camera driver unavailable";
|
||||||
|
if (!isLiveStreamDevice(camera)) return "live view not supported by this camera driver";
|
||||||
|
return camera;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cameraRoutes(app: FastifyInstance, db: Db, hub: CameraLiveHub): Promise<void> {
|
||||||
|
app.get<{ Params: { deviceId: string } }>(
|
||||||
|
"/api/cameras/:deviceId/latest-snapshot",
|
||||||
|
{ preHandler: requirePermission("session:read") },
|
||||||
|
async (req, reply) => {
|
||||||
|
// Uses snapshots_device_time_idx (migration 0033): one index seek, not a scan.
|
||||||
|
const row = db
|
||||||
|
.select({ id: snapshots.id, direction: snapshots.direction, capturedAt: snapshots.capturedAt })
|
||||||
|
.from(snapshots)
|
||||||
|
.where(eq(snapshots.deviceId, req.params.deviceId))
|
||||||
|
.orderBy(desc(snapshots.capturedAt))
|
||||||
|
.limit(1)
|
||||||
|
.get();
|
||||||
|
if (!row) return reply.code(404).send({ error: "no snapshot for this camera yet" });
|
||||||
|
return row;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get<{ Params: { deviceId: string } }>(
|
||||||
|
"/api/cameras/:deviceId/live",
|
||||||
|
{
|
||||||
|
websocket: true,
|
||||||
|
preHandler: async (req) => {
|
||||||
|
const roleId = await authenticateWsHandshake(req);
|
||||||
|
if (!roleHasPermissions(roleId, ["device:read"])) {
|
||||||
|
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
||||||
|
}
|
||||||
|
const row = db.select({ category: devices.category }).from(devices).where(eq(devices.id, req.params.deviceId)).get();
|
||||||
|
if (!row || row.category !== "camera") {
|
||||||
|
throw Object.assign(new Error("unknown camera"), { statusCode: 404 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
(socket, req) => {
|
||||||
|
const open = () => socket.readyState === 1;
|
||||||
|
const unsubscribe = hub.subscribe(req.params.deviceId, {
|
||||||
|
sendFrame: (jpeg) => {
|
||||||
|
if (open()) socket.send(jpeg, { binary: true });
|
||||||
|
},
|
||||||
|
sendStatus: (status: LiveStatus) => {
|
||||||
|
if (open()) socket.send(JSON.stringify({ kind: "status", ...status }));
|
||||||
|
},
|
||||||
|
busy: () => !open() || socket.bufferedAmount > MAX_BUFFERED_BYTES,
|
||||||
|
});
|
||||||
|
socket.on("close", unsubscribe);
|
||||||
|
socket.on("error", unsubscribe);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,19 +1,26 @@
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { requirePermission, roleHasPermissions } from "../auth.js";
|
import type { Db } from "@parking/db";
|
||||||
|
import type { TillId } from "@parking/shared";
|
||||||
|
import { requireAuth, requirePermission, roleHasPermissions } from "../auth.js";
|
||||||
|
import { parseTill, requireTill, tillsReadableBy } from "../modules.js";
|
||||||
import { InvalidCashMovementError, type MovementStatus, type ShiftService } from "../shift-service.js";
|
import { InvalidCashMovementError, type MovementStatus, type ShiftService } from "../shift-service.js";
|
||||||
|
|
||||||
// Drawer cash movements (manned mode). Redesigned 2026-07-01: an operator RECORDS a
|
// Drawer cash movements (manned mode). Redesigned 2026-07-01: an operator RECORDS a
|
||||||
// receipt/disbursement FREELY (no admin sign-off at creation); an admin REVIEWS it after
|
// receipt/disbursement FREELY (no admin sign-off at creation); an admin REVIEWS it after
|
||||||
// the fact (authorize/deny — a flag that never moves cash). See wiki/concepts/shift.md.
|
// the fact (authorize/deny — a flag that never moves cash). See wiki/concepts/shift.md.
|
||||||
// - POST /api/drawer/movement : operator records a cash_in/cash_out. (drawer:create)
|
// - POST /api/drawer/movement : operator records a cash_in/cash_out on a till.
|
||||||
// - GET /api/drawer/movements: list with review status. Operators see (shift:read)
|
// Guard = the till's `cash` (booth drawer:create,
|
||||||
// only their own; reviewers see all + can filter status.
|
// wash carwash:cash).
|
||||||
|
// - GET /api/drawer/movements: list with review status, over the tills the role may
|
||||||
|
// read (own movements); reviewers (drawer:review) see all
|
||||||
|
// tills + all operators and can filter status.
|
||||||
// - POST /api/drawer/review : admin authorize/deny a movement. (drawer:review)
|
// - POST /api/drawer/review : admin authorize/deny a movement. (drawer:review)
|
||||||
// - GET /api/drawer/balance : the physical drawer balance NOW (cash (shift:read)
|
// - GET /api/drawer/balance : a till's physical balance NOW (guard = the till's read).
|
||||||
// payments + vouchers over the whole chain — the
|
|
||||||
// amount that carries across shifts).
|
|
||||||
// The drawer BALANCE math is unchanged — a movement counts immediately; a denial is a
|
// The drawer BALANCE math is unchanged — a movement counts immediately; a denial is a
|
||||||
// judgment about the operator settled outside the app, never a cash reversal.
|
// judgment about the operator settled outside the app, never a cash reversal.
|
||||||
|
// TILLS: a movement names the drawer it moved in/out of (`till`, default booth); each
|
||||||
|
// desk's cash is guarded by that desk's own permissions (venue-modules.md §"Permissions
|
||||||
|
// matrix").
|
||||||
|
|
||||||
interface MovementBody {
|
interface MovementBody {
|
||||||
/** Direction is the document TYPE, not a sign: cash_in = Mandat Arkëtimi (pay-IN),
|
/** Direction is the document TYPE, not a sign: cash_in = Mandat Arkëtimi (pay-IN),
|
||||||
@@ -23,6 +30,8 @@ interface MovementBody {
|
|||||||
amountMinor: number;
|
amountMinor: number;
|
||||||
reason?: string;
|
reason?: string;
|
||||||
currency?: string;
|
currency?: string;
|
||||||
|
/** Which drawer (default: the booth). */
|
||||||
|
till?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ReviewBody {
|
interface ReviewBody {
|
||||||
@@ -36,15 +45,15 @@ interface ReviewBody {
|
|||||||
interface MovementsQuery {
|
interface MovementsQuery {
|
||||||
/** Reviewers only: filter to pending/authorized/denied. Ignored for non-reviewers. */
|
/** Reviewers only: filter to pending/authorized/denied. Ignored for non-reviewers. */
|
||||||
status?: MovementStatus;
|
status?: MovementStatus;
|
||||||
|
/** Filter to one till; absent = every till the role may read (reviewers: every till). */
|
||||||
|
till?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function drawerRoutes(app: FastifyInstance, shift: ShiftService): Promise<void> {
|
export async function drawerRoutes(app: FastifyInstance, db: Db, shift: ShiftService): Promise<void> {
|
||||||
const createGuard = requirePermission("drawer:create");
|
|
||||||
const reviewGuard = requirePermission("drawer:review");
|
const reviewGuard = requirePermission("drawer:review");
|
||||||
const readGuard = requirePermission("shift:read");
|
|
||||||
|
|
||||||
// Operator RECORDS a movement — freely, no authorizer. It counts in the drawer at once.
|
// Operator RECORDS a movement — freely, no authorizer. It counts in the drawer at once.
|
||||||
app.post<{ Body: MovementBody }>("/api/drawer/movement", { preHandler: createGuard }, async (req, reply) => {
|
app.post<{ Body: MovementBody }>("/api/drawer/movement", { preHandler: requireTill(db, "cash", "body") }, async (req, reply) => {
|
||||||
const b = req.body ?? ({} as MovementBody);
|
const b = req.body ?? ({} as MovementBody);
|
||||||
if (b.type !== "cash_in" && b.type !== "cash_out") {
|
if (b.type !== "cash_in" && b.type !== "cash_out") {
|
||||||
return reply.code(400).send({ error: "type must be cash_in or cash_out" });
|
return reply.code(400).send({ error: "type must be cash_in or cash_out" });
|
||||||
@@ -56,6 +65,7 @@ export async function drawerRoutes(app: FastifyInstance, shift: ShiftService): P
|
|||||||
amountMinor: b.amountMinor,
|
amountMinor: b.amountMinor,
|
||||||
reason: b.reason ?? "",
|
reason: b.reason ?? "",
|
||||||
currency: b.currency,
|
currency: b.currency,
|
||||||
|
till: req.till!,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof InvalidCashMovementError) return reply.code(400).send({ error: err.message });
|
if (err instanceof InvalidCashMovementError) return reply.code(400).send({ error: err.message });
|
||||||
@@ -63,22 +73,37 @@ export async function drawerRoutes(app: FastifyInstance, shift: ShiftService): P
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// List movements + review status. Operators are hard-scoped to their OWN movements; a
|
// List movements + review status. Operators are hard-scoped to their OWN movements on
|
||||||
// reviewer sees ALL and may filter by status (the pending review queue).
|
// the tills they may read; a reviewer sees ALL and may filter by status (the pending
|
||||||
app.get<{ Querystring: MovementsQuery }>("/api/drawer/movements", { preHandler: readGuard }, async (req) => {
|
// review queue).
|
||||||
|
app.get<{ Querystring: MovementsQuery }>("/api/drawer/movements", { preHandler: requireAuth }, async (req, reply) => {
|
||||||
const canReview = roleHasPermissions(req.user.roleId, ["drawer:review"]);
|
const canReview = roleHasPermissions(req.user.roleId, ["drawer:review"]);
|
||||||
|
const readable = tillsReadableBy(db, req.user.roleId);
|
||||||
|
if (!canReview && readable.length === 0) return reply.code(403).send({ error: "forbidden" });
|
||||||
const q = req.query ?? {};
|
const q = req.query ?? {};
|
||||||
const status = canReview && ["pending", "authorized", "denied"].includes(q.status ?? "") ? q.status : undefined;
|
const status = canReview && ["pending", "authorized", "denied"].includes(q.status ?? "") ? q.status : undefined;
|
||||||
const movements = shift.movementsWithStatus({
|
let tills: TillId[] | undefined = canReview ? undefined : readable;
|
||||||
operator: canReview ? undefined : req.user.username,
|
if (q.till?.trim()) {
|
||||||
status,
|
const parsed = parseTill(db, q.till.trim());
|
||||||
});
|
if (!parsed) return reply.code(400).send({ error: "unknown till", code: "bad_till" });
|
||||||
|
if (!canReview && !readable.includes(parsed)) {
|
||||||
|
return reply.code(403).send({ error: `your role cannot see the ${parsed} till`, code: "till_forbidden", till: parsed });
|
||||||
|
}
|
||||||
|
tills = [parsed];
|
||||||
|
}
|
||||||
|
const operator = canReview ? undefined : req.user.username;
|
||||||
|
const movements = tills
|
||||||
|
? tills.flatMap((till) => shift.movementsWithStatus({ operator, status, till })).sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : 0))
|
||||||
|
: shift.movementsWithStatus({ operator, status });
|
||||||
return { movements, scope: canReview ? "all" : "self" };
|
return { movements, scope: canReview ? "all" : "self" };
|
||||||
});
|
});
|
||||||
|
|
||||||
// The physical drawer balance now. Same visibility as the open shift's X-report
|
// A till's physical drawer balance now. Same visibility as the open shift's X-report
|
||||||
// (shift:read) — the drawer is a single site-wide till, not per-operator data.
|
// (the till's read guard) — a drawer is a shared till, not per-operator data.
|
||||||
app.get("/api/drawer/balance", { preHandler: readGuard }, async () => shift.drawerBalance());
|
app.get("/api/drawer/balance", { preHandler: requireTill(db, "read", "query") }, async (req) => ({
|
||||||
|
till: req.till!,
|
||||||
|
...shift.drawerBalance(req.till!),
|
||||||
|
}));
|
||||||
|
|
||||||
// Admin AUTHORIZES or DENIES a recorded movement. A flag only — no cash reversal.
|
// Admin AUTHORIZES or DENIES a recorded movement. A flag only — no cash reversal.
|
||||||
app.post<{ Body: ReviewBody }>("/api/drawer/review", { preHandler: reviewGuard }, async (req, reply) => {
|
app.post<{ Body: ReviewBody }>("/api/drawer/review", { preHandler: reviewGuard }, async (req, reply) => {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { and, desc, gte, lte, ledgerEvents, type Db } from "@parking/db";
|
import { and, desc, gte, inArray, lte, sql, ledgerEvents, type Db } from "@parking/db";
|
||||||
import type { LedgerEvent } from "@parking/shared";
|
import { BOOTH_TILL, MODULES, feedPermissionFor, isTillId, type LedgerEvent, type LedgerEventType } from "@parking/shared";
|
||||||
import { requirePermission } from "../auth.js";
|
import { requireAuth, requirePermission, roleHasPermissions } from "../auth.js";
|
||||||
|
import { effectiveModulesFor } from "../modules.js";
|
||||||
import { enrichEvents } from "../event-enrich.js";
|
import { enrichEvents } from "../event-enrich.js";
|
||||||
import type { EventLog } from "../event-log.js";
|
import type { EventLog } from "../event-log.js";
|
||||||
|
|
||||||
@@ -15,25 +16,59 @@ export async function eventRoutes(
|
|||||||
db: Db,
|
db: Db,
|
||||||
eventLog: EventLog,
|
eventLog: EventLog,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// Reading the log (the audit trail).
|
// Reading the log (the audit trail). `event:read` reads everything; a role WITHOUT it
|
||||||
const guard = requirePermission("event:read");
|
// may still hold a module's feed permission (a wash operator's `carwash:read`) and
|
||||||
|
// then reads ONLY that module's event types — the same rule the live socket applies
|
||||||
|
// (feedPermissionFor; venue-modules.md §Permissions matrix, move 3).
|
||||||
|
|
||||||
|
/** The event types a role may read, or null for "everything" (event:read). Empty =
|
||||||
|
* the role reads nothing → 403 at the route. */
|
||||||
|
function readableTypes(roleId: string): LedgerEventType[] | null {
|
||||||
|
if (roleHasPermissions(roleId, ["event:read"])) return null;
|
||||||
|
const effective = effectiveModulesFor(db);
|
||||||
|
const out: LedgerEventType[] = [];
|
||||||
|
for (const m of MODULES) {
|
||||||
|
if (!m.feedPermission || !effective.includes(m.id)) continue;
|
||||||
|
if (roleHasPermissions(roleId, [m.feedPermission])) out.push(...m.ledgerEventTypes);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** SQL form of the shared `tillOfEvent` rule: the payload's `till`, else the till of
|
||||||
|
* the module owning the event type, else the booth. Computed in the query so the
|
||||||
|
* page limit applies AFTER the till filter (a shift's window can hold thousands of
|
||||||
|
* device events). */
|
||||||
|
const tillExpr = (() => {
|
||||||
|
const cases = MODULES.filter((m) => m.till && m.till !== BOOTH_TILL && m.ledgerEventTypes.length > 0).map(
|
||||||
|
(m) => sql`when ${ledgerEvents.type} in (${sql.join(m.ledgerEventTypes.map((t) => sql`${t}`), sql`, `)}) then ${m.till}`,
|
||||||
|
);
|
||||||
|
return sql`coalesce(json_extract(${ledgerEvents.payload}, '$.till'), case ${sql.join(cases, sql` `)} else ${BOOTH_TILL} end)`;
|
||||||
|
})();
|
||||||
|
|
||||||
// Recent events, newest first. `limit` caps the page (default 100, max 1000).
|
// Recent events, newest first. `limit` caps the page (default 100, max 1000).
|
||||||
// Optional `since` (ISO) scopes to events at/after that instant — the booth passes
|
// Optional `since` (ISO) scopes to events at/after that instant — the booth passes
|
||||||
// the current shift's start so the live feed shows ONLY this shift's activity. An
|
// the current shift's start so the live feed shows ONLY this shift's activity. An
|
||||||
// optional `until` (ISO) closes the upper bound — the shift-history screen passes a
|
// optional `until` (ISO) closes the upper bound — the shift-history screen passes a
|
||||||
// selected shift's [start, end] to show just that shift's signed activity log.
|
// selected shift's [start, end] to show just that shift's signed activity log.
|
||||||
// (logs are per-shift, not all history). See wiki/concepts/shift.md.
|
// (logs are per-shift, not all history). An optional `till` keeps only that till's
|
||||||
app.get<{ Querystring: { limit?: string; since?: string; until?: string } }>(
|
// activity (tillOfEvent) — a booth shift's log no longer shows the wash desk's, and
|
||||||
|
// vice versa. See wiki/concepts/shift.md §Tills.
|
||||||
|
app.get<{ Querystring: { limit?: string; since?: string; until?: string; till?: string } }>(
|
||||||
"/api/events",
|
"/api/events",
|
||||||
{ preHandler: guard },
|
{ preHandler: requireAuth },
|
||||||
async (req) => {
|
async (req, reply) => {
|
||||||
|
const types = readableTypes(req.user?.roleId ?? "");
|
||||||
|
if (types && types.length === 0) return reply.code(403).send({ error: "forbidden" });
|
||||||
const limit = Math.min(Math.max(Number(req.query.limit) || 100, 1), 1000);
|
const limit = Math.min(Math.max(Number(req.query.limit) || 100, 1), 1000);
|
||||||
const since = (req.query.since ?? "").trim();
|
const since = (req.query.since ?? "").trim();
|
||||||
const until = (req.query.until ?? "").trim();
|
const until = (req.query.until ?? "").trim();
|
||||||
|
const till = (req.query.till ?? "").trim();
|
||||||
|
if (till && !isTillId(till)) return reply.code(400).send({ error: "unknown till", code: "bad_till" });
|
||||||
const bounds = [
|
const bounds = [
|
||||||
since ? gte(ledgerEvents.occurredAt, since) : undefined,
|
since ? gte(ledgerEvents.occurredAt, since) : undefined,
|
||||||
until ? lte(ledgerEvents.occurredAt, until) : undefined,
|
until ? lte(ledgerEvents.occurredAt, until) : undefined,
|
||||||
|
till ? sql`${tillExpr} = ${till}` : undefined,
|
||||||
|
types ? inArray(ledgerEvents.type, types) : undefined,
|
||||||
].filter(Boolean);
|
].filter(Boolean);
|
||||||
const rows = db
|
const rows = db
|
||||||
.select()
|
.select()
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { ledgerEvents, eq, type Db } from "@parking/db";
|
||||||
|
import { buildServer } from "../server.js";
|
||||||
|
import { login, makeLog, minutesAgo, seedTariff, seedUser } from "../test-helpers.js";
|
||||||
|
|
||||||
|
// HTTP integration for the FORGIVEN exit (wiki/decisions/forgiven-exit.md): the route is
|
||||||
|
// gated on `barrier:create` (admin by default, grantable per role), needs an open shift
|
||||||
|
// and a reason, and signs a $0 payment carrying the forgone fee + the exit. Drives the
|
||||||
|
// REAL app over a fresh in-memory DB via app.inject.
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
app = await buildServer({ db });
|
||||||
|
await app.ready();
|
||||||
|
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60 });
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
|
||||||
|
async function as(opts: { username: string; roleId?: string; permissions?: Parameters<typeof seedUser>[1]["permissions"] }) {
|
||||||
|
const { username, password } = await seedUser(db, opts);
|
||||||
|
const { cookie, csrf } = await login(app, username, password);
|
||||||
|
return { cookie, "x-csrf-token": csrf };
|
||||||
|
}
|
||||||
|
async function enter(identity: string) {
|
||||||
|
await makeLog(db).append({ type: "vehicle_entry", direction: "entry", identity, occurredAt: minutesAgo(90), payload: null });
|
||||||
|
}
|
||||||
|
function events(identity: string) {
|
||||||
|
return db.select().from(ledgerEvents).where(eq(ledgerEvents.identity, identity)).all();
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("POST /api/barrier/forgive", () => {
|
||||||
|
it("is refused without barrier:create (an operator who can take payment but was not granted it)", async () => {
|
||||||
|
const h = await as({ username: "op", roleId: "operator", permissions: ["payment:create", "session:read", "shift:create"] });
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: h, payload: {} });
|
||||||
|
await enter("T1");
|
||||||
|
const r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: { identity: "T1", note: "friend" } });
|
||||||
|
expect(r.statusCode).toBe(403);
|
||||||
|
expect(events("T1").map((e) => e.type)).toEqual(["vehicle_entry"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a role the owner granted barrier:create can forgive; the reason is optional; a shift must be open", async () => {
|
||||||
|
const h = await as({ username: "trusted", roleId: "senior", permissions: ["payment:create", "session:read", "shift:create", "shift:read", "barrier:create"] });
|
||||||
|
await enter("T1");
|
||||||
|
// No shift yet → the money-path gate refuses.
|
||||||
|
let r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: { identity: "T1", note: "x" } });
|
||||||
|
expect(r.statusCode).toBe(409);
|
||||||
|
expect(r.json()).toMatchObject({ code: "no_shift" });
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: h, payload: {} });
|
||||||
|
// Forgiven: $0 payment with the fee as forgone, then the exit. No relay configured →
|
||||||
|
// ok:true, opened:false with the open-by-hand reason (the ledger side is complete).
|
||||||
|
r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: { identity: "T1", note: "known customer, lost ticket" } });
|
||||||
|
expect(r.statusCode).toBe(200);
|
||||||
|
expect(r.json()).toMatchObject({ ok: true, opened: false, forgoneMinor: 20000 });
|
||||||
|
const types = events("T1").map((e) => e.type);
|
||||||
|
expect(types).toEqual(["vehicle_entry", "payment", "anomaly", "vehicle_exit"]);
|
||||||
|
const payment = events("T1").find((e) => e.type === "payment")!;
|
||||||
|
expect(payment.payload).toMatchObject({ amountMinor: 0, forgoneMinor: 20000, operator: "trusted", reasonCode: "exit.forgiven" });
|
||||||
|
// The Z-report carries it as its own line, and the takings stay at zero.
|
||||||
|
const rep = (await app.inject({ method: "GET", url: "/api/shift/report", headers: h })).json() as Record<string, unknown>;
|
||||||
|
expect(rep).toMatchObject({ cashTotalMinor: 0, cardTotalMinor: 0, forgoneTotalMinor: 20000, forgoneCount: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("no ticket, no reason: admin opens the exit for a car with no session → anomaly only, readable sentence", async () => {
|
||||||
|
const h = await as({ username: "boss", roleId: "admin" });
|
||||||
|
await app.inject({ method: "POST", url: "/api/shift/open", headers: h, payload: {} });
|
||||||
|
const r = await app.inject({ method: "POST", url: "/api/barrier/forgive", headers: h, payload: {} });
|
||||||
|
expect(r.statusCode).toBe(200);
|
||||||
|
const all = db.select().from(ledgerEvents).all();
|
||||||
|
const anomaly = all.find((e) => e.type === "anomaly")!;
|
||||||
|
expect(anomaly.identity).toMatch(/^forgiven:/);
|
||||||
|
expect(anomaly.payload).toMatchObject({ reasonCode: "exit.forgiven.noSession", operator: "boss" });
|
||||||
|
expect((anomaly.payload as { reason: string }).reason).toContain("(no reason given)");
|
||||||
|
expect(all.filter((e) => e.type === "payment" || e.type === "vehicle_exit")).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -6,6 +6,7 @@ import type { FastifyInstance } from "fastify";
|
|||||||
import { buildServer } from "../server.js";
|
import { buildServer } from "../server.js";
|
||||||
import { hikvisionAlarmRoutes } from "./hikvision-alarm.js";
|
import { hikvisionAlarmRoutes } from "./hikvision-alarm.js";
|
||||||
import type { AnprBridge } from "../anpr-entry.js";
|
import type { AnprBridge } from "../anpr-entry.js";
|
||||||
|
import type { CameraActivity } from "../camera-activity.js";
|
||||||
import { seedUser, login } from "../test-helpers.js";
|
import { seedUser, login } from "../test-helpers.js";
|
||||||
|
|
||||||
// Hikvision Alarm Server push ingress. Verifies the discovery endpoint: a vehicle-
|
// Hikvision Alarm Server push ingress. Verifies the discovery endpoint: a vehicle-
|
||||||
@@ -235,6 +236,7 @@ describe("Hikvision Alarm Server → ANPR bridge wiring", () => {
|
|||||||
let rawDb: Db;
|
let rawDb: Db;
|
||||||
let rawClose: () => void;
|
let rawClose: () => void;
|
||||||
let onVehicleDetected: ReturnType<typeof vi.fn>;
|
let onVehicleDetected: ReturnType<typeof vi.fn>;
|
||||||
|
let cameraSaw: ReturnType<typeof vi.fn>;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const t = createTestDb();
|
const t = createTestDb();
|
||||||
@@ -243,7 +245,9 @@ describe("Hikvision Alarm Server → ANPR bridge wiring", () => {
|
|||||||
onVehicleDetected = vi.fn(async () => {});
|
onVehicleDetected = vi.fn(async () => {});
|
||||||
const bridge = { onVehicleDetected } as unknown as AnprBridge;
|
const bridge = { onVehicleDetected } as unknown as AnprBridge;
|
||||||
rawApp = Fastify();
|
rawApp = Fastify();
|
||||||
await hikvisionAlarmRoutes(rawApp, rawDb, undefined, bridge);
|
cameraSaw = vi.fn();
|
||||||
|
const activity = { vehicleDetected: cameraSaw } as unknown as CameraActivity;
|
||||||
|
await hikvisionAlarmRoutes(rawApp, rawDb, undefined, bridge, activity);
|
||||||
await rawApp.ready();
|
await rawApp.ready();
|
||||||
rawDb.insert(devices).values({
|
rawDb.insert(devices).values({
|
||||||
id: CAM_ID,
|
id: CAM_ID,
|
||||||
@@ -275,10 +279,16 @@ describe("Hikvision Alarm Server → ANPR bridge wiring", () => {
|
|||||||
expect(onVehicleDetected).toHaveBeenCalledWith(CAM_ID);
|
expect(onVehicleDetected).toHaveBeenCalledWith(CAM_ID);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("marks THIS camera active for the booth camera tile", async () => {
|
||||||
|
await post(VEHICLE_XML);
|
||||||
|
expect(cameraSaw).toHaveBeenCalledWith(CAM_ID);
|
||||||
|
});
|
||||||
|
|
||||||
it("does NOT call the bridge for a human target", async () => {
|
it("does NOT call the bridge for a human target", async () => {
|
||||||
const human = VEHICLE_XML.replace("vehicle", "human");
|
const human = VEHICLE_XML.replace("vehicle", "human");
|
||||||
await post(human);
|
await post(human);
|
||||||
expect(onVehicleDetected).not.toHaveBeenCalled();
|
expect(onVehicleDetected).not.toHaveBeenCalled();
|
||||||
|
expect(cameraSaw).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does NOT call the bridge on an `inactive` (leave) vehicle event", async () => {
|
it("does NOT call the bridge on an `inactive` (leave) vehicle event", async () => {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { requirePermission } from "../auth.js";
|
|||||||
import { verifyDigest } from "../digest-auth.js";
|
import { verifyDigest } from "../digest-auth.js";
|
||||||
import type { LaneStatus } from "../lane-status.js";
|
import type { LaneStatus } from "../lane-status.js";
|
||||||
import type { AnprBridge } from "../anpr-entry.js";
|
import type { AnprBridge } from "../anpr-entry.js";
|
||||||
|
import type { CameraActivity } from "../camera-activity.js";
|
||||||
|
|
||||||
// Hikvision "Alarm Server" event PUSH ingress. The newer-firmware cameras (Event →
|
// Hikvision "Alarm Server" event PUSH ingress. The newer-firmware cameras (Event →
|
||||||
// Smart/VCA with "Detection Target: Human/Vehicle", Notify Surveillance Center, Alarm
|
// Smart/VCA with "Detection Target: Human/Vehicle", Notify Surveillance Center, Alarm
|
||||||
@@ -101,6 +102,7 @@ export async function hikvisionAlarmRoutes(
|
|||||||
db: Db,
|
db: Db,
|
||||||
laneStatus?: LaneStatus,
|
laneStatus?: LaneStatus,
|
||||||
anprBridge?: AnprBridge,
|
anprBridge?: AnprBridge,
|
||||||
|
cameraActivity?: CameraActivity,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// Accept ANY content-type as a raw Buffer (the camera may POST application/xml,
|
// Accept ANY content-type as a raw Buffer (the camera may POST application/xml,
|
||||||
// multipart/form-data with a JPEG, or text). Fastify's default JSON parser would 415
|
// multipart/form-data with a JPEG, or text). Fastify's default JSON parser would 415
|
||||||
@@ -159,9 +161,11 @@ export async function hikvisionAlarmRoutes(
|
|||||||
const contentType = String(req.headers["content-type"] ?? "");
|
const contentType = String(req.headers["content-type"] ?? "");
|
||||||
const raw: Buffer = Buffer.isBuffer(req.body) ? (req.body as Buffer) : Buffer.from("");
|
const raw: Buffer = Buffer.isBuffer(req.body) ? (req.body as Buffer) : Buffer.from("");
|
||||||
const summary = summarize(raw.toString("utf8"));
|
const summary = summarize(raw.toString("utf8"));
|
||||||
// Log EVERY hit immediately (method + ip + size), before any guard — so even a probe
|
// Log EVERY hit immediately (method + ip + size), before any guard — so under
|
||||||
// that gets rejected is visible in the dev log the instant it arrives.
|
// LOG_LEVEL=debug even a probe that gets rejected is visible the instant it arrives.
|
||||||
app.log.info(`[hik-alarm:${deviceId}] HIT ${method} from ${ip} (${contentType || "no-ct"} ${raw.length}B)`);
|
// debug, not info: every accepted hit is followed by the ACCEPTED line (which carries
|
||||||
|
// all of this plus the verdict) and every rejected one by the REJECTED warn.
|
||||||
|
app.log.debug(`[hik-alarm:${deviceId}] HIT ${method} from ${ip} (${contentType || "no-ct"} ${raw.length}B)`);
|
||||||
|
|
||||||
// Guard: must be a known hikvision device with alarm-push enabled, posting from its
|
// Guard: must be a known hikvision device with alarm-push enabled, posting from its
|
||||||
// configured host IP. Source-IP is the primary guard on the LAN (like the Dingtian).
|
// configured host IP. Source-IP is the primary guard on the LAN (like the Dingtian).
|
||||||
@@ -211,6 +215,10 @@ export async function hikvisionAlarmRoutes(
|
|||||||
if (laneStatus && isVehicleActive) {
|
if (laneStatus && isVehicleActive) {
|
||||||
laneStatus.vehicleDetected(deviceId);
|
laneStatus.vehicleDetected(deviceId);
|
||||||
}
|
}
|
||||||
|
// The same signal per camera: the booth's tile for THIS camera switches to live video.
|
||||||
|
if (cameraActivity && isVehicleActive) {
|
||||||
|
cameraActivity.vehicleDetected(deviceId);
|
||||||
|
}
|
||||||
|
|
||||||
// ANPR BRIDGE: on a vehicle detection, if this camera opts into ANPR (config.anpr),
|
// ANPR BRIDGE: on a vehicle detection, if this camera opts into ANPR (config.anpr),
|
||||||
// pull a snapshot → read the plate → if it matches a SUBSCRIBER, emit a plate read
|
// pull a snapshot → read the plate → if it matches a SUBSCRIBER, emit a plate read
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ export async function payRoutes(
|
|||||||
const guard = requirePermission("payment:create");
|
const guard = requirePermission("payment:create");
|
||||||
const readGuard = requirePermission("session:read");
|
const readGuard = requirePermission("session:read");
|
||||||
const voidGuard = requirePermission("event:void");
|
const voidGuard = requirePermission("event:void");
|
||||||
|
const forgiveGuard = requirePermission("barrier:create");
|
||||||
|
|
||||||
// Money-path gate: a shift must be open site-wide before any payment/exit/voucher/
|
// Money-path gate: a shift must be open site-wide before any payment/exit/voucher/
|
||||||
// re-open is processed, so every taking is attributed to a shift (one operator's
|
// re-open is processed, so every taking is attributed to a shift (one operator's
|
||||||
@@ -144,6 +145,37 @@ export async function payRoutes(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// FORGIVEN exit (wiki/decisions/forgiven-exit.md): an operator holding barrier:create
|
||||||
|
// lets a car out with NO money. With a ticket: PayStation.forgive signs a $0 payment
|
||||||
|
// carrying the forgone fee (never in cash/card), then the human-intervention re-open
|
||||||
|
// signs the exit + pulses. Without a ticket: anomaly + snapshot + pulse. A free-text
|
||||||
|
// reason is OPTIONAL (user, 2026-10-09 — the signature + snapshot + Z-report line are
|
||||||
|
// the accountability); an open shift is required so it lands on someone's Z-report.
|
||||||
|
// Prints nothing.
|
||||||
|
app.post<{ Body: { identity?: string; note?: string } }>(
|
||||||
|
"/api/barrier/forgive",
|
||||||
|
{ preHandler: [forgiveGuard, requireShift] },
|
||||||
|
async (req, reply) => {
|
||||||
|
const note = (req.body?.note ?? "").trim();
|
||||||
|
const identity = (req.body?.identity ?? "").trim();
|
||||||
|
const operator = req.user?.username ?? "?";
|
||||||
|
if (!identity) {
|
||||||
|
const r = await exitFlow.forgiveExitNoSession(operator, note);
|
||||||
|
if (!r.ok) return reply.code(409).send({ error: r.reason });
|
||||||
|
return reply.code(200).send(r);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const f = await payStation.forgive(identity, { operator, note });
|
||||||
|
if (!f.ok) return reply.code(409).send({ error: f.reason });
|
||||||
|
const r = await exitFlow.reopenBarrier(identity, operator, { forgiven: { note, forgoneMinor: f.forgoneMinor, currency: f.currency } });
|
||||||
|
if (!r.ok) return reply.code(409).send({ error: r.reason });
|
||||||
|
return reply.code(200).send({ ...r, forgoneMinor: f.forgoneMinor, currency: f.currency });
|
||||||
|
} catch (err) {
|
||||||
|
return mapError(reply, err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// Cancel (void) a wrongly-printed transient ticket. Appends a SIGNED `void` event
|
// Cancel (void) a wrongly-printed transient ticket. Appends a SIGNED `void` event
|
||||||
// referencing the entry, with the operator + a REQUIRED reason — the entry itself is
|
// referencing the entry, with the operator + a REQUIRED reason — the entry itself is
|
||||||
// never edited/deleted (append-only). The session projection folds the void to CLOSED,
|
// never edited/deleted (append-only). The session projection folds the void to CLOSED,
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import { createTestDb } from "@parking/db/testing";
|
||||||
|
import { type Db } from "@parking/db";
|
||||||
|
import type { FastifyInstance } from "fastify";
|
||||||
|
import { jobsBehind } from "@parking/shared";
|
||||||
|
import { buildServer } from "../server.js";
|
||||||
|
import { login, seedUser } from "../test-helpers.js";
|
||||||
|
|
||||||
|
// Roles are data composed from the permission grid (venue-modules.md §Permissions
|
||||||
|
// matrix): every edit is SIGNED as a config_change, and a role remembers the manifest
|
||||||
|
// JOBS it was built from so a grown job can be surfaced and re-applied.
|
||||||
|
|
||||||
|
let db: Db;
|
||||||
|
let close: () => void;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
beforeEach(async () => {
|
||||||
|
delete process.env.MODULES_ENTITLED;
|
||||||
|
const t = createTestDb();
|
||||||
|
db = t.db;
|
||||||
|
close = t.close;
|
||||||
|
app = await buildServer({ db });
|
||||||
|
await app.ready();
|
||||||
|
});
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
type Auth = { cookie: string; csrf: string };
|
||||||
|
const hdrs = (a: Auth) => ({ cookie: a.cookie, "x-csrf-token": a.csrf });
|
||||||
|
async function admin(): Promise<Auth> {
|
||||||
|
const { username, password } = await seedUser(db, { username: "boss", roleId: "admin" });
|
||||||
|
return login(app, username, password);
|
||||||
|
}
|
||||||
|
async function roleChanges(a: Auth) {
|
||||||
|
const r = await app.inject({ method: "GET", url: "/api/events?limit=100", headers: { cookie: a.cookie } });
|
||||||
|
return (r.json().events as { type: string; payload: Record<string, unknown> }[]).filter(
|
||||||
|
(e) => e.type === "config_change" && String(e.payload.setting).startsWith("role."),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("role edits are signed and jobs are remembered", () => {
|
||||||
|
it("create / update / delete each sign one config_change with prev + value + operator; a no-op resave signs nothing", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
const created = await app.inject({
|
||||||
|
method: "POST", url: "/api/roles", headers: hdrs(a),
|
||||||
|
payload: { name: "Lavazh", permissions: ["carwash:read", "carwash:create", "carwash:update", "carwash:cash"], jobs: ["wash-operator"] },
|
||||||
|
});
|
||||||
|
expect(created.statusCode).toBe(201);
|
||||||
|
const role = created.json();
|
||||||
|
expect(role.jobs).toEqual(["wash-operator"]);
|
||||||
|
let evs = await roleChanges(a);
|
||||||
|
expect(evs).toHaveLength(1);
|
||||||
|
expect(evs[0]!.payload).toMatchObject({
|
||||||
|
setting: `role.${role.id}`, prev: null, operator: "boss",
|
||||||
|
value: { name: "Lavazh", jobs: ["wash-operator"] },
|
||||||
|
});
|
||||||
|
expect((evs[0]!.payload.value as { permissions: string[] }).permissions).toEqual(["carwash:cash", "carwash:create", "carwash:read", "carwash:update"]);
|
||||||
|
|
||||||
|
// Same content again → nothing new on the chain.
|
||||||
|
const same = await app.inject({
|
||||||
|
method: "PUT", url: `/api/roles/${role.id}`, headers: hdrs(a),
|
||||||
|
payload: { permissions: ["carwash:read", "carwash:create", "carwash:update", "carwash:cash"], jobs: ["wash-operator"] },
|
||||||
|
});
|
||||||
|
expect(same.statusCode).toBe(200);
|
||||||
|
expect(await roleChanges(a)).toHaveLength(1);
|
||||||
|
|
||||||
|
// A real change: prev is the old shape, value the new.
|
||||||
|
const renamed = await app.inject({ method: "PUT", url: `/api/roles/${role.id}`, headers: hdrs(a), payload: { name: "Lavazh NEW" } });
|
||||||
|
expect(renamed.statusCode).toBe(200);
|
||||||
|
evs = await roleChanges(a);
|
||||||
|
expect(evs).toHaveLength(2);
|
||||||
|
expect(evs[0]!.payload).toMatchObject({ prev: { name: "Lavazh" }, value: { name: "Lavazh NEW" } });
|
||||||
|
|
||||||
|
const gone = await app.inject({ method: "DELETE", url: `/api/roles/${role.id}`, headers: hdrs(a) });
|
||||||
|
expect(gone.statusCode).toBe(200);
|
||||||
|
evs = await roleChanges(a);
|
||||||
|
expect(evs).toHaveLength(3);
|
||||||
|
expect(evs[0]!.payload).toMatchObject({ prev: { name: "Lavazh NEW" }, value: null });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("unknown jobs are refused; a role built from a job that later grew reports what it is missing", async () => {
|
||||||
|
const a = await admin();
|
||||||
|
const bad = await app.inject({ method: "POST", url: "/api/roles", headers: hdrs(a), payload: { name: "X", permissions: [], jobs: ["bar-tender"] } });
|
||||||
|
expect(bad.statusCode).toBe(400);
|
||||||
|
// Compose "behind": the role follows wash-operator but holds only part of today's bundle
|
||||||
|
// — exactly what an older release's chip would have left once the job grew.
|
||||||
|
const r = (await app.inject({
|
||||||
|
method: "POST", url: "/api/roles", headers: hdrs(a),
|
||||||
|
payload: { name: "Old wash", permissions: ["carwash:read", "carwash:create"], jobs: ["wash-operator"] },
|
||||||
|
})).json();
|
||||||
|
const view = (await app.inject({ method: "GET", url: "/api/roles", headers: { cookie: a.cookie } })).json().roles.find((x: { id: string }) => x.id === r.id);
|
||||||
|
const has = new Set<string>(view.permissions);
|
||||||
|
expect(jobsBehind(view.jobs, (p) => has.has(p))).toEqual([{ job: "wash-operator", missing: ["carwash:update", "carwash:cash"] }]);
|
||||||
|
// Re-apply = the union; then nothing is behind.
|
||||||
|
const fixed = (await app.inject({
|
||||||
|
method: "PUT", url: `/api/roles/${r.id}`, headers: hdrs(a),
|
||||||
|
payload: { permissions: [...has, "carwash:update", "carwash:cash"] },
|
||||||
|
})).json();
|
||||||
|
const has2 = new Set<string>(fixed.permissions);
|
||||||
|
expect(jobsBehind(fixed.jobs, (p) => has2.has(p))).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { and, eq, isNull, rolePermissions, roles, users, type Db } from "@parking/db";
|
import { and, eq, isNull, roleJobs, rolePermissions, roles, users, type Db } from "@parking/db";
|
||||||
import { ADMIN_ROLE_ID, PERMISSIONS, type Permission } from "@parking/shared";
|
import { ADMIN_ROLE_ID, PERMISSIONS, jobById, type Permission } from "@parking/shared";
|
||||||
import { bumpPermsCache, permissionsFor, requirePermission } from "../auth.js";
|
import { bumpPermsCache, permissionsFor, requirePermission } from "../auth.js";
|
||||||
|
import type { EventLog } from "../event-log.js";
|
||||||
import { softDelete } from "../recycle-bin.js";
|
import { softDelete } from "../recycle-bin.js";
|
||||||
|
|
||||||
// Role management (admin). Roles are DATA: an admin composes a role from the
|
// Role management (admin). Roles are DATA: an admin composes a role from the
|
||||||
@@ -18,14 +19,30 @@ import { softDelete } from "../recycle-bin.js";
|
|||||||
// that grants admin-equivalent powers, and escalate. So a non-admin caller may
|
// that grants admin-equivalent powers, and escalate. So a non-admin caller may
|
||||||
// only put permissions they ALREADY hold onto a role. An admin (full set) is
|
// only put permissions they ALREADY hold onto a role. An admin (full set) is
|
||||||
// unrestricted, which is the intended behaviour.
|
// unrestricted, which is the intended behaviour.
|
||||||
|
//
|
||||||
|
// EVERY role edit is SIGNED on the ledger as a `config_change` (setting `role.<id>`,
|
||||||
|
// value/prev = the role's name + permissions + jobs, operator = who) — a role edit is a
|
||||||
|
// privilege change, and under this threat model the only setting an admin could alter
|
||||||
|
// without a trace. A role also REMEMBERS the manifest JOBS it was composed from
|
||||||
|
// (role_jobs) so a later release that grows a job's bundle can be surfaced and
|
||||||
|
// re-applied — the grid is never expanded silently (venue-modules.md §Permissions matrix).
|
||||||
|
|
||||||
interface RoleBody {
|
interface RoleBody {
|
||||||
name: string;
|
name: string;
|
||||||
permissions: string[];
|
permissions: string[];
|
||||||
|
jobs?: string[];
|
||||||
}
|
}
|
||||||
interface UpdateBody {
|
interface UpdateBody {
|
||||||
name?: string;
|
name?: string;
|
||||||
permissions?: string[];
|
permissions?: string[];
|
||||||
|
jobs?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What a signed role change records (before/after). */
|
||||||
|
interface RoleShape {
|
||||||
|
name: string;
|
||||||
|
permissions: Permission[];
|
||||||
|
jobs: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const VALID = new Set<string>(PERMISSIONS);
|
const VALID = new Set<string>(PERMISSIONS);
|
||||||
@@ -41,7 +58,19 @@ function cleanPermissions(input: unknown): { ok: true; perms: Permission[] } | {
|
|||||||
return { ok: true, perms: [...out] };
|
return { ok: true, perms: [...out] };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function roleRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
/** Validate + dedupe a requested job list against the registry's job presets. */
|
||||||
|
function cleanJobs(input: unknown): { ok: true; jobs: string[] } | { ok: false; bad: string } {
|
||||||
|
if (input == null) return { ok: true, jobs: [] };
|
||||||
|
if (!Array.isArray(input)) return { ok: false, bad: "jobs must be an array" };
|
||||||
|
const out = new Set<string>();
|
||||||
|
for (const j of input) {
|
||||||
|
if (typeof j !== "string" || !jobById(j)) return { ok: false, bad: `unknown job: ${String(j)}` };
|
||||||
|
out.add(j);
|
||||||
|
}
|
||||||
|
return { ok: true, jobs: [...out] };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function roleRoutes(app: FastifyInstance, db: Db, eventLog?: EventLog): Promise<void> {
|
||||||
const readGuard = requirePermission("role:read");
|
const readGuard = requirePermission("role:read");
|
||||||
const createGuard = requirePermission("role:create");
|
const createGuard = requirePermission("role:create");
|
||||||
const updateGuard = requirePermission("role:update");
|
const updateGuard = requirePermission("role:update");
|
||||||
@@ -64,10 +93,39 @@ export async function roleRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
name: role.name,
|
name: role.name,
|
||||||
builtin: role.builtin === 1,
|
builtin: role.builtin === 1,
|
||||||
permissions: role.id === ADMIN_ROLE_ID ? [...PERMISSIONS] : perms,
|
permissions: role.id === ADMIN_ROLE_ID ? [...PERMISSIONS] : perms,
|
||||||
|
jobs: jobsOf(roleId),
|
||||||
userCount,
|
userCount,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function jobsOf(roleId: string): string[] {
|
||||||
|
return db.select({ jobId: roleJobs.jobId }).from(roleJobs).where(eq(roleJobs.roleId, roleId)).all().map((r) => r.jobId).sort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The role as the ledger records it (sorted so two identical shapes compare equal). */
|
||||||
|
function shapeOf(roleId: string): RoleShape | null {
|
||||||
|
const v = roleView(roleId);
|
||||||
|
if (!v) return null;
|
||||||
|
return { name: v.name, permissions: [...v.permissions].sort() as Permission[], jobs: v.jobs };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Replace a role's remembered jobs. */
|
||||||
|
function setJobs(roleId: string, jobs: string[]): void {
|
||||||
|
db.delete(roleJobs).where(eq(roleJobs.roleId, roleId)).run();
|
||||||
|
for (const jobId of jobs) db.insert(roleJobs).values({ roleId, jobId }).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Sign a role change. `prev` null = created; `value` null = deleted. Skipped when
|
||||||
|
* nothing changed (a no-op resave leaves no trace, like the site-config flips). */
|
||||||
|
async function signRoleChange(req: { user?: { username?: string } }, roleId: string, prev: RoleShape | null, value: RoleShape | null): Promise<void> {
|
||||||
|
if (JSON.stringify(prev) === JSON.stringify(value)) return;
|
||||||
|
await eventLog?.append({
|
||||||
|
type: "config_change",
|
||||||
|
source: "manual",
|
||||||
|
payload: { setting: `role.${roleId}`, value, prev, operator: req.user?.username ?? "unknown" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** Replace a role's permission rows with `perms` (in a single pass). */
|
/** Replace a role's permission rows with `perms` (in a single pass). */
|
||||||
function setPermissions(roleId: string, perms: Permission[]): void {
|
function setPermissions(roleId: string, perms: Permission[]): void {
|
||||||
db.delete(rolePermissions).where(eq(rolePermissions.roleId, roleId)).run();
|
db.delete(rolePermissions).where(eq(rolePermissions.roleId, roleId)).run();
|
||||||
@@ -104,13 +162,17 @@ export async function roleRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
}
|
}
|
||||||
const cleaned = cleanPermissions(req.body?.permissions ?? []);
|
const cleaned = cleanPermissions(req.body?.permissions ?? []);
|
||||||
if (!cleaned.ok) return reply.code(400).send({ error: cleaned.bad });
|
if (!cleaned.ok) return reply.code(400).send({ error: cleaned.bad });
|
||||||
|
const jobs = cleanJobs(req.body?.jobs);
|
||||||
|
if (!jobs.ok) return reply.code(400).send({ error: jobs.bad });
|
||||||
const over = escalates(req.user.roleId, cleaned.perms);
|
const over = escalates(req.user.roleId, cleaned.perms);
|
||||||
if (over) return reply.code(403).send({ error: `cannot grant a permission you do not hold: ${over}` });
|
if (over) return reply.code(403).send({ error: `cannot grant a permission you do not hold: ${over}` });
|
||||||
|
|
||||||
const id = randomUUID();
|
const id = randomUUID();
|
||||||
db.insert(roles).values({ id, name, builtin: 0 }).run();
|
db.insert(roles).values({ id, name, builtin: 0 }).run();
|
||||||
setPermissions(id, cleaned.perms);
|
setPermissions(id, cleaned.perms);
|
||||||
|
setJobs(id, jobs.jobs);
|
||||||
bumpPermsCache();
|
bumpPermsCache();
|
||||||
|
await signRoleChange(req, id, null, shapeOf(id));
|
||||||
return reply.code(201).send(roleView(id));
|
return reply.code(201).send(roleView(id));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -125,6 +187,7 @@ export async function roleRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
if (role.builtin === 1) {
|
if (role.builtin === 1) {
|
||||||
return reply.code(409).send({ error: "the built-in admin role cannot be edited" });
|
return reply.code(409).send({ error: "the built-in admin role cannot be edited" });
|
||||||
}
|
}
|
||||||
|
const prev = shapeOf(id);
|
||||||
|
|
||||||
if (req.body?.name != null) {
|
if (req.body?.name != null) {
|
||||||
const name = req.body.name.trim();
|
const name = req.body.name.trim();
|
||||||
@@ -140,7 +203,13 @@ export async function roleRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
if (over) return reply.code(403).send({ error: `cannot grant a permission you do not hold: ${over}` });
|
if (over) return reply.code(403).send({ error: `cannot grant a permission you do not hold: ${over}` });
|
||||||
setPermissions(id, cleaned.perms);
|
setPermissions(id, cleaned.perms);
|
||||||
}
|
}
|
||||||
|
if (req.body?.jobs != null) {
|
||||||
|
const jobs = cleanJobs(req.body.jobs);
|
||||||
|
if (!jobs.ok) return reply.code(400).send({ error: jobs.bad });
|
||||||
|
setJobs(id, jobs.jobs);
|
||||||
|
}
|
||||||
bumpPermsCache();
|
bumpPermsCache();
|
||||||
|
await signRoleChange(req, id, prev, shapeOf(id));
|
||||||
return roleView(id);
|
return roleView(id);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -163,8 +232,10 @@ export async function roleRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
if (holders > 0) {
|
if (holders > 0) {
|
||||||
return reply.code(409).send({ error: `cannot delete a role still assigned to ${holders} user(s)` });
|
return reply.code(409).send({ error: `cannot delete a role still assigned to ${holders} user(s)` });
|
||||||
}
|
}
|
||||||
|
const prev = shapeOf(id);
|
||||||
softDelete(db, "role", id, req.user.sub);
|
softDelete(db, "role", id, req.user.sub);
|
||||||
bumpPermsCache();
|
bumpPermsCache();
|
||||||
|
await signRoleChange(req, id, prev, null);
|
||||||
return { ok: true };
|
return { ok: true };
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ describe("health + login", () => {
|
|||||||
it("GET /health is open", async () => {
|
it("GET /health is open", async () => {
|
||||||
const res = await app.inject({ method: "GET", url: "/health" });
|
const res = await app.inject({ method: "GET", url: "/health" });
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.json()).toEqual({ status: "ok" });
|
expect(res.json()).toEqual({ status: "ok", app: "parking-system" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("login with bad credentials is rejected", async () => {
|
it("login with bad credentials is rejected", async () => {
|
||||||
@@ -142,7 +142,7 @@ describe("drawer balance (the till NOW)", () => {
|
|||||||
const { cookie } = await login(app, viewer.username, viewer.password);
|
const { cookie } = await login(app, viewer.username, viewer.password);
|
||||||
const ok = await app.inject({ method: "GET", url: "/api/drawer/balance", headers: { cookie } });
|
const ok = await app.inject({ method: "GET", url: "/api/drawer/balance", headers: { cookie } });
|
||||||
expect(ok.statusCode).toBe(200);
|
expect(ok.statusCode).toBe(200);
|
||||||
expect(ok.json()).toEqual({ balanceMinor: 0, currency: null });
|
expect(ok.json()).toEqual({ till: "booth", balanceMinor: 0, currency: null });
|
||||||
|
|
||||||
const outsider = await seedUser(db, { username: "noshift", roleId: "noshift", permissions: ["site:read"] });
|
const outsider = await seedUser(db, { username: "noshift", roleId: "noshift", permissions: ["site:read"] });
|
||||||
const other = await login(app, outsider.username, outsider.password);
|
const other = await login(app, outsider.username, outsider.password);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { randomBytes, randomUUID } from "node:crypto";
|
import { randomBytes, randomUUID } from "node:crypto";
|
||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { eq, devices, setupState, type Db } from "@parking/db";
|
import { eq, devices, setupState, type Db } from "@parking/db";
|
||||||
|
import { pulseBarrier } from "../relay-pulse.js";
|
||||||
import {
|
import {
|
||||||
hasPreconditions,
|
hasPreconditions,
|
||||||
hasPushConfig,
|
hasPushConfig,
|
||||||
@@ -191,6 +192,14 @@ async function configureDevice(
|
|||||||
|
|
||||||
if (hasPushConfig(device)) {
|
if (hasPushConfig(device)) {
|
||||||
const host = String(config.host ?? "");
|
const host = String(config.host ?? "");
|
||||||
|
if (!host) {
|
||||||
|
// Only reachable for a push device whose host is OPTIONAL (the DT-008 reader:
|
||||||
|
// scans resolve by serial, the IP is what lets us configure it). Nothing to talk
|
||||||
|
// to → save, but say so: the reader must then be set up by hand.
|
||||||
|
hardenWarnings.push(
|
||||||
|
"no IP given — the reader was NOT configured: set its output type to HTTP and its server to this booth with the vendor tool (or add its IP here and save again)",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
// Admin-provided backend IP wins; else auto-derive (on-subnet NIC).
|
// Admin-provided backend IP wins; else auto-derive (on-subnet NIC).
|
||||||
const pushHost = backendIp ?? backendIpForDevice(host);
|
const pushHost = backendIp ?? backendIpForDevice(host);
|
||||||
if (!pushHost) {
|
if (!pushHost) {
|
||||||
@@ -211,12 +220,17 @@ async function configureDevice(
|
|||||||
pathBase: `/api/devices/${driverId}/${id}/input`,
|
pathBase: `/api/devices/${driverId}/${id}/input`,
|
||||||
auth: { user: pushUser, password: pushPassword },
|
auth: { user: pushUser, password: pushPassword },
|
||||||
});
|
});
|
||||||
|
// A device whose push carries no credentials (the DT-008 scan GET) has no use
|
||||||
|
// for these — don't persist a secret nothing checks.
|
||||||
|
if (device.usesPushAuth !== false) {
|
||||||
fullConfig.pushUser = pushUser;
|
fullConfig.pushUser = pushUser;
|
||||||
fullConfig.pushPassword = pushPassword;
|
fullConfig.pushPassword = pushPassword;
|
||||||
|
}
|
||||||
// Record the backend IP the device was told to push to — lets us detect
|
// Record the backend IP the device was told to push to — lets us detect
|
||||||
// a later mismatch if the host's IP changes.
|
// a later mismatch if the host's IP changes.
|
||||||
fullConfig.backendIp = pushHost;
|
fullConfig.backendIp = pushHost;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return { error: { code: 502, message: `device configuration failed: ${(err as Error).message}` } };
|
return { error: { code: 502, message: `device configuration failed: ${(err as Error).message}` } };
|
||||||
}
|
}
|
||||||
@@ -231,7 +245,7 @@ export async function setupRoutes(
|
|||||||
eventLog?: EventLog | null,
|
eventLog?: EventLog | null,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
registerBuiltinDrivers();
|
registerBuiltinDrivers();
|
||||||
setDeviceLogSink((line) => app.log.info(line));
|
setDeviceLogSink((line, level) => app.log[level](line));
|
||||||
|
|
||||||
// Device setup is site administration — it changes which hardware the site runs
|
// Device setup is site administration — it changes which hardware the site runs
|
||||||
// and how readers bind to relays. Gated on site:update. See ../auth.ts.
|
// and how readers bind to relays. Gated on site:update. See ../auth.ts.
|
||||||
@@ -494,7 +508,7 @@ export async function setupRoutes(
|
|||||||
return reply.code(400).send({ error: `device ${id} is not a controller` });
|
return reply.code(400).send({ error: `device ${id} is not a controller` });
|
||||||
}
|
}
|
||||||
const cfg = (row.config ?? {}) as Record<string, unknown>;
|
const cfg = (row.config ?? {}) as Record<string, unknown>;
|
||||||
const relays = Array.isArray(cfg.relays) ? (cfg.relays as { relay?: number }[]) : [];
|
const relays = Array.isArray(cfg.relays) ? (cfg.relays as { relay?: number; direction?: string }[]) : [];
|
||||||
if (!relays.some((r) => r.relay === relay)) {
|
if (!relays.some((r) => r.relay === relay)) {
|
||||||
return reply.code(400).send({ error: `controller ${id} has no relay ${relay}` });
|
return reply.code(400).send({ error: `controller ${id} has no relay ${relay}` });
|
||||||
}
|
}
|
||||||
@@ -532,8 +546,14 @@ export async function setupRoutes(
|
|||||||
});
|
});
|
||||||
|
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
|
const relayDir = relays.find((r) => r.relay === relay)?.direction;
|
||||||
try {
|
try {
|
||||||
await device.pulseOpen(relay);
|
await pulseBarrier(db, req.log, device, { controller: row, relay }, {
|
||||||
|
direction: relayDir === "entry" || relayDir === "exit" ? relayDir : "both",
|
||||||
|
identity: `relay-test:${id}:${relay}`,
|
||||||
|
source: "relay-test",
|
||||||
|
operator,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// The failure we're testing for (relay unreachable, wrong password). The open is
|
// The failure we're testing for (relay unreachable, wrong password). The open is
|
||||||
// already signed; report the pulse failure, don't 500.
|
// already signed; report the pulse failure, don't 500.
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance, FastifyReply } from "fastify";
|
||||||
import { requirePermission, roleHasPermissions } from "../auth.js";
|
import type { Db } from "@parking/db";
|
||||||
import { NoOpenShiftError, ShiftAlreadyOpenError, type ShiftService } from "../shift-service.js";
|
import { tillGuards, type TillId } from "@parking/shared";
|
||||||
|
import { requireAuth, roleHasPermissions } from "../auth.js";
|
||||||
|
import { parseTill, requireTill, tillsReadableBy } from "../modules.js";
|
||||||
|
import { NoOpenShiftError, ShiftAlreadyOpenError, type ShiftService, type ShiftSummary } from "../shift-service.js";
|
||||||
|
|
||||||
interface ShiftsQuery {
|
interface ShiftsQuery {
|
||||||
/** Filter to one operator (admin-only; non-admins are forced to themselves). */
|
/** Filter to one operator (admin-only; non-admins are forced to themselves). */
|
||||||
@@ -8,85 +11,124 @@ interface ShiftsQuery {
|
|||||||
/** ISO window over shift START time. */
|
/** ISO window over shift START time. */
|
||||||
from?: string;
|
from?: string;
|
||||||
to?: string;
|
to?: string;
|
||||||
|
/** Filter to one till; absent = every till the role may read. */
|
||||||
|
till?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shift endpoints (manned mode). The operator is the logged-in user; a shift is
|
// 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
|
// 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.
|
// local-jwt-auth.md "until logout"). End Shift signs a shift_z_report + prints it.
|
||||||
|
//
|
||||||
|
// TILLS + PERMISSIONS: every endpoint addresses a `till` (query on GET, body on POST;
|
||||||
|
// default booth) and its guard is resolved FROM THE TILL (requireTill): the booth's shift
|
||||||
|
// is `shift:read` / `shift:create`, the wash's is `carwash:read` / `carwash:cash` — each
|
||||||
|
// desk's money is guarded by that desk's own permissions, so a wash role holds no
|
||||||
|
// `shift:*` at all and cannot touch the booth. See venue-modules.md §"Permissions matrix".
|
||||||
|
|
||||||
export async function shiftRoutes(app: FastifyInstance, shift: ShiftService): Promise<void> {
|
export async function shiftRoutes(app: FastifyInstance, db: Db, shift: ShiftService): Promise<void> {
|
||||||
// Reading the shift state vs. opening/closing one's own shift.
|
const statusOf = (till: TillId, me: string, roleId: string) => {
|
||||||
const readGuard = requirePermission("shift:read");
|
const open = shift.currentOpenShift(till);
|
||||||
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 heldBy = open?.identity ?? null;
|
||||||
const drawer = shift.drawerBalance();
|
const drawer = shift.drawerBalance(till);
|
||||||
return {
|
return {
|
||||||
operator: me,
|
till,
|
||||||
open: open ? { startedAt: open.occurredAt, operator: heldBy } : null,
|
open: open ? { startedAt: open.occurredAt, operator: heldBy } : null,
|
||||||
isMine: open != null && heldBy === me,
|
isMine: open != null && heldBy === me,
|
||||||
|
/** May this role open/close this till's shift? (The UI offers the button only then.) */
|
||||||
|
canWork: roleHasPermissions(roleId, [tillGuards(till).shift]),
|
||||||
drawerMinor: drawer.balanceMinor,
|
drawerMinor: drawer.balanceMinor,
|
||||||
currency: drawer.currency,
|
currency: drawer.currency,
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// The shift state of ONE till (at most one shift open per till). The UI uses this
|
||||||
|
// to render a till's control: no shift → "Open"; my shift → "Close" (enabled);
|
||||||
|
// someone else's shift → disabled. Also returns the live drawer balance.
|
||||||
|
// - till: which till this describes
|
||||||
|
// - open: the open shift { startedAt, operator } or null
|
||||||
|
// - isMine: true iff the open shift belongs to the requesting operator
|
||||||
|
// - canWork: may this role open/close it
|
||||||
|
// - operator: the requesting user (for the UI's own identity)
|
||||||
|
// - tills: every till THIS ROLE may read — what the UI offers controls for
|
||||||
|
app.get("/api/shift/current", { preHandler: requireTill(db, "read", "query") }, async (req) => ({
|
||||||
|
operator: req.user.username,
|
||||||
|
tills: tillsReadableBy(db, req.user.roleId),
|
||||||
|
...statusOf(req.till!, req.user.username, req.user.roleId),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// The state of every till this role may read, in one read — the shift hub lists
|
||||||
|
// each open shift and offers "start" for the idle ones it may work.
|
||||||
|
app.get("/api/shift/tills", { preHandler: requireAuth }, async (req) => {
|
||||||
|
const me = req.user.username;
|
||||||
|
return { operator: me, tills: tillsReadableBy(db, req.user.roleId).map((t) => statusOf(t, me, req.user.roleId)) };
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mid-shift X-report: a READ-ONLY "so far" snapshot of the OPEN shift's takings +
|
// 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
|
// 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
|
// 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.
|
// (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) => {
|
app.get("/api/shift/report", { preHandler: requireTill(db, "read", "query") }, async (req, reply) => {
|
||||||
const report = shift.currentReport();
|
const report = shift.currentReport(req.till!);
|
||||||
if (!report) return reply.code(204).send();
|
if (!report) return reply.code(204).send();
|
||||||
return report;
|
return report;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Completed shift history. SCOPED by permission:
|
// Completed shift history. SCOPED by permission:
|
||||||
// - `shift:read` (operators) → own shifts only; operator/from/to params ignored.
|
// - a till's `read` guard (operators) → own shifts only, on the tills they may read;
|
||||||
|
// operator/from/to params ignored.
|
||||||
// - `shift:cash` (admin-grade) → all operators, optionally filtered by
|
// - `shift:cash` (admin-grade) → all operators, optionally filtered by
|
||||||
// `operator` and a `from`/`to` time window over each shift's START.
|
// `operator` and a `from`/`to` time window over each shift's START.
|
||||||
// This keeps one operator from reading another's takings while letting admins
|
// 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.
|
// reconcile across the site. The data is the signed shift_z_report chain. Both
|
||||||
app.get<{ Querystring: ShiftsQuery }>("/api/shifts", { preHandler: readGuard }, async (req) => {
|
// scopes may filter by `till` (must be one the role may read).
|
||||||
|
app.get<{ Querystring: ShiftsQuery }>("/api/shifts", { preHandler: requireAuth }, async (req, reply) => {
|
||||||
const canSeeAll = roleHasPermissions(req.user.roleId, ["shift:cash"]);
|
const canSeeAll = roleHasPermissions(req.user.roleId, ["shift:cash"]);
|
||||||
|
const readable = tillsReadableBy(db, req.user.roleId);
|
||||||
|
if (!canSeeAll && readable.length === 0) return reply.code(403).send({ error: "forbidden" });
|
||||||
const q = req.query ?? {};
|
const q = req.query ?? {};
|
||||||
// Non-admins are hard-scoped to themselves regardless of any operator param.
|
// Non-admins are hard-scoped to themselves regardless of any operator param.
|
||||||
const operator = canSeeAll ? (q.operator?.trim() || undefined) : req.user.username;
|
const operator = canSeeAll ? (q.operator?.trim() || undefined) : req.user.username;
|
||||||
const from = canSeeAll ? q.from?.trim() || undefined : undefined;
|
const from = canSeeAll ? q.from?.trim() || undefined : undefined;
|
||||||
const to = canSeeAll ? q.to?.trim() || undefined : undefined;
|
const to = canSeeAll ? q.to?.trim() || undefined : undefined;
|
||||||
const shifts = shift.listShifts({ operator, from, to });
|
let tills: TillId[] = canSeeAll ? [] : readable; // [] = no till filter (admin)
|
||||||
|
if (q.till?.trim()) {
|
||||||
|
const parsed = parseTill(db, q.till.trim());
|
||||||
|
if (!parsed) return reply.code(400).send({ error: "unknown till", code: "bad_till" });
|
||||||
|
if (!canSeeAll && !readable.includes(parsed)) return badTill(reply, parsed);
|
||||||
|
tills = [parsed];
|
||||||
|
}
|
||||||
|
const shifts: ShiftSummary[] =
|
||||||
|
tills.length === 0
|
||||||
|
? shift.listShifts({ operator, from, to })
|
||||||
|
: tills.flatMap((till) => shift.listShifts({ operator, from, to, till })).sort((a, b) => b.index - a.index);
|
||||||
// Admins also get the distinct operator list (unfiltered) for the filter
|
// Admins also get the distinct operator list (unfiltered) for the filter
|
||||||
// dropdown — operators don't see other names, so it's scope-gated.
|
// dropdown — operators don't see other names, so it's scope-gated.
|
||||||
if (canSeeAll) return { shifts, scope: "all", operators: shift.listOperators() };
|
if (canSeeAll) return { shifts, scope: "all", operators: shift.listOperators(), tills: tillsReadableBy(db, req.user.roleId) };
|
||||||
return { shifts, scope: "self" };
|
return { shifts, scope: "self", tills: readable };
|
||||||
});
|
});
|
||||||
|
|
||||||
// NB: drawer cash movements (record/review) moved to routes/drawer.ts (2026-07-01) — the
|
// 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.
|
// feature is no longer part of the shift route. See wiki/concepts/shift.md.
|
||||||
|
|
||||||
app.post("/api/shift/open", { preHandler: guard }, async (req, reply) => {
|
app.post("/api/shift/open", { preHandler: requireTill(db, "shift", "body") }, async (req, reply) => {
|
||||||
try {
|
try {
|
||||||
return await shift.open(req.user.username);
|
return await shift.open(req.user.username, req.till!);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof ShiftAlreadyOpenError) return reply.code(409).send({ error: err.message });
|
if (err instanceof ShiftAlreadyOpenError) return reply.code(409).send({ error: err.message });
|
||||||
return reply.code(500).send({ error: (err as Error).message });
|
return reply.code(500).send({ error: (err as Error).message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/api/shift/close", { preHandler: guard }, async (req, reply) => {
|
app.post("/api/shift/close", { preHandler: requireTill(db, "shift", "body") }, async (req, reply) => {
|
||||||
try {
|
try {
|
||||||
return await shift.close(req.user.username);
|
return await shift.close(req.user.username, req.till!);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof NoOpenShiftError) return reply.code(409).send({ error: err.message });
|
if (err instanceof NoOpenShiftError) return reply.code(409).send({ error: err.message });
|
||||||
return reply.code(500).send({ error: (err as Error).message });
|
return reply.code(500).send({ error: (err as Error).message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function badTill(reply: FastifyReply, till: TillId): FastifyReply {
|
||||||
|
return reply.code(403).send({ error: `your role cannot see the ${till} till`, code: "till_forbidden", till });
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { eq, siteConfig, type Db } from "@parking/db";
|
import { eq, siteConfig, type Db } from "@parking/db";
|
||||||
|
import { MODULES, effectiveModules, isModuleId, resolveModuleActivation, type ModuleId } from "@parking/shared";
|
||||||
import { requirePermission } from "../auth.js";
|
import { requirePermission } from "../auth.js";
|
||||||
import type { EventLog } from "../event-log.js";
|
import type { EventLog } from "../event-log.js";
|
||||||
|
import { activatedModulesOf, entitledModules } from "../modules.js";
|
||||||
import { getOccupancy } from "../occupancy.js";
|
import { getOccupancy } from "../occupancy.js";
|
||||||
|
|
||||||
// Site config (capacity) + live occupancy. Occupancy is a fold over the signed
|
// Site config (capacity) + live occupancy. Occupancy is a fold over the signed
|
||||||
@@ -36,6 +38,10 @@ interface SiteConfigBody extends Partial<Record<TextField, string | null>> {
|
|||||||
/** Master switch for the ANPR subscriber-entry bridge (auto-open on a subscriber's
|
/** Master switch for the ANPR subscriber-entry bridge (auto-open on a subscriber's
|
||||||
* plate read). OFF → subscribers fall back to card/QR; advisory ANPR still records. */
|
* plate read). OFF → subscribers fall back to card/QR; advisory ANPR still records. */
|
||||||
anprEntryEnabled?: boolean;
|
anprEntryEnabled?: boolean;
|
||||||
|
/** Venue modules to ACTIVATE (full desired set). Validated against the entitlement
|
||||||
|
* and the registry's dependency rules; required modules are always included. Each
|
||||||
|
* module that actually flips signs a config_change. See wiki/decisions/venue-modules.md. */
|
||||||
|
modules?: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Shape returned by GET/PUT: capacity + the booth flag + the subscription default
|
/** Shape returned by GET/PUT: capacity + the booth flag + the subscription default
|
||||||
@@ -48,6 +54,13 @@ type SiteConfig = {
|
|||||||
anprEntryEnabled: boolean;
|
anprEntryEnabled: boolean;
|
||||||
bypassPresenceRadar: boolean;
|
bypassPresenceRadar: boolean;
|
||||||
bypassPresenceCamera: boolean;
|
bypassPresenceCamera: boolean;
|
||||||
|
/** Effective venue modules = entitled ∩ activated (what the server enforces). */
|
||||||
|
modules: ModuleId[];
|
||||||
|
/** What this deployment is entitled to (MODULES_ENTITLED env) — the Setup → Site
|
||||||
|
* panel offers exactly these to toggle. */
|
||||||
|
modulesEntitled: ModuleId[];
|
||||||
|
/** What the site admin has activated (null in storage = everything entitled). */
|
||||||
|
modulesActivated: ModuleId[];
|
||||||
} & Record<TextField, string | null>;
|
} & Record<TextField, string | null>;
|
||||||
|
|
||||||
function toSiteConfig(row: typeof siteConfig.$inferSelect | undefined): SiteConfig {
|
function toSiteConfig(row: typeof siteConfig.$inferSelect | undefined): SiteConfig {
|
||||||
@@ -59,11 +72,22 @@ function toSiteConfig(row: typeof siteConfig.$inferSelect | undefined): SiteConf
|
|||||||
anprEntryEnabled: row?.anprEntryEnabled ?? true,
|
anprEntryEnabled: row?.anprEntryEnabled ?? true,
|
||||||
bypassPresenceRadar: row?.bypassPresenceRadar ?? false,
|
bypassPresenceRadar: row?.bypassPresenceRadar ?? false,
|
||||||
bypassPresenceCamera: row?.bypassPresenceCamera ?? false,
|
bypassPresenceCamera: row?.bypassPresenceCamera ?? false,
|
||||||
|
...moduleView(row),
|
||||||
} as SiteConfig;
|
} as SiteConfig;
|
||||||
for (const f of TEXT_FIELDS) out[f] = row?.[f] ?? null;
|
for (const f of TEXT_FIELDS) out[f] = row?.[f] ?? null;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function moduleView(row: typeof siteConfig.$inferSelect | undefined) {
|
||||||
|
const entitled = entitledModules();
|
||||||
|
const activated = activatedModulesOf(row) ?? entitled;
|
||||||
|
return {
|
||||||
|
modules: effectiveModules(entitled, activated),
|
||||||
|
modulesEntitled: entitled,
|
||||||
|
modulesActivated: activated,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/** Trim a text field; empty string becomes null so blank input clears it. */
|
/** Trim a text field; empty string becomes null so blank input clears it. */
|
||||||
function normText(v: unknown): string | null {
|
function normText(v: unknown): string | null {
|
||||||
if (v == null) return null;
|
if (v == null) return null;
|
||||||
@@ -136,6 +160,40 @@ export async function siteRoutes(app: FastifyInstance, db: Db, eventLog?: EventL
|
|||||||
}
|
}
|
||||||
|
|
||||||
const existing = db.select().from(siteConfig).where(eq(siteConfig.id, 1)).get();
|
const existing = db.select().from(siteConfig).where(eq(siteConfig.id, 1)).get();
|
||||||
|
|
||||||
|
// Venue-module activation. The body carries the full DESIRED set; the shared rules
|
||||||
|
// (required always on, must be entitled, dependencies effective) decide, and every
|
||||||
|
// module whose effective state actually flips is signed as a config_change — the
|
||||||
|
// same attribution pattern as the presence-bypass endpoint below. Disabling never
|
||||||
|
// deletes anything: tables/history/grants stay, routes 403, UI hides.
|
||||||
|
if ("modules" in body) {
|
||||||
|
const requested = body.modules;
|
||||||
|
if (!Array.isArray(requested) || !requested.every(isModuleId)) {
|
||||||
|
return reply.code(400).send({
|
||||||
|
error: `modules must be an array of module ids (${MODULES.map((m) => m.id).join(", ")})`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const entitled = entitledModules();
|
||||||
|
const result = resolveModuleActivation(entitled, requested);
|
||||||
|
if (!result.ok) return reply.code(400).send({ error: result.error });
|
||||||
|
const prevEffective = new Set(effectiveModules(entitled, activatedModulesOf(existing) ?? entitled));
|
||||||
|
const nextEffective = new Set(effectiveModules(entitled, result.modules));
|
||||||
|
const operator = req.user?.username ?? "unknown";
|
||||||
|
for (const m of MODULES) {
|
||||||
|
const was = prevEffective.has(m.id);
|
||||||
|
const now = nextEffective.has(m.id);
|
||||||
|
if (was !== now) {
|
||||||
|
await eventLog?.append({
|
||||||
|
type: "config_change",
|
||||||
|
source: "manual",
|
||||||
|
identity: `module:${m.id}`,
|
||||||
|
payload: { setting: `modules.${m.id}`, value: now, prev: was, operator },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
patch.modulesJson = JSON.stringify(result.modules);
|
||||||
|
}
|
||||||
|
|
||||||
const updatedAt = new Date().toISOString();
|
const updatedAt = new Date().toISOString();
|
||||||
if (existing) {
|
if (existing) {
|
||||||
db.update(siteConfig).set({ ...patch, updatedAt }).where(eq(siteConfig.id, 1)).run();
|
db.update(siteConfig).set({ ...patch, updatedAt }).where(eq(siteConfig.id, 1)).run();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { randomBytes, randomUUID } from "node:crypto";
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { and, eq, isNull, devices, subscriptionCredentials, subscriptionPlans, subscriptionPlates, subscriptions, type Db } from "@parking/db";
|
import { and, eq, isNull, devices, subscriptionCredentials, subscriptionPlans, subscriptionPlates, subscriptions, type Db } from "@parking/db";
|
||||||
import { NoPrinterAvailableError } from "@parking/devices";
|
import { NoPrinterAvailableError } from "@parking/devices";
|
||||||
import type { SubscriptionPlan, SubscriptionQuote, Tender } from "@parking/shared";
|
import { BOOTH_TILL, type SubscriptionPlan, type SubscriptionQuote, type Tender } from "@parking/shared";
|
||||||
import { requirePermission, roleHasPermissions } from "../auth.js";
|
import { requirePermission, roleHasPermissions } from "../auth.js";
|
||||||
import { softDelete } from "../recycle-bin.js";
|
import { softDelete } from "../recycle-bin.js";
|
||||||
import { invalidateHolder } from "../event-enrich.js";
|
import { invalidateHolder } from "../event-enrich.js";
|
||||||
@@ -381,6 +381,7 @@ export async function subscriptionRoutes(
|
|||||||
amountMinor,
|
amountMinor,
|
||||||
currency,
|
currency,
|
||||||
tender,
|
tender,
|
||||||
|
till: BOOTH_TILL,
|
||||||
operator,
|
operator,
|
||||||
// Flags this `payment` as a subscription SALE (not a parking payment) so the
|
// Flags this `payment` as a subscription SALE (not a parking payment) so the
|
||||||
// live feed / activity log can label it distinctly. plan + periods for audit
|
// live feed / activity log can label it distinctly. plan + periods for audit
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import bcrypt from "bcrypt";
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { and, eq, isNull, roles, users, type Db } from "@parking/db";
|
import { and, eq, isNull, roles, users, type Db } from "@parking/db";
|
||||||
import { ADMIN_ROLE_ID } from "@parking/shared";
|
import { ADMIN_ROLE_ID } from "@parking/shared";
|
||||||
import { permissionsFor, requirePermission } from "../auth.js";
|
import { bumpPermsCache, permissionsFor, requirePermission } from "../auth.js";
|
||||||
import { softDelete } from "../recycle-bin.js";
|
import { softDelete } from "../recycle-bin.js";
|
||||||
|
|
||||||
// User management (admin). Users are created/edited at runtime here — the
|
// User management (admin). Users are created/edited at runtime here — the
|
||||||
@@ -202,6 +202,8 @@ export async function userRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
return reply.code(400).send({ error: "nothing to update" });
|
return reply.code(400).send({ error: "nothing to update" });
|
||||||
}
|
}
|
||||||
db.update(users).set(next).where(eq(users.id, id)).run();
|
db.update(users).set(next).where(eq(users.id, id)).run();
|
||||||
|
// A role reassignment takes effect on the user's NEXT request (auth.ts refreshRole).
|
||||||
|
if (next.roleId) bumpPermsCache();
|
||||||
return publicUser(db.select().from(users).where(eq(users.id, id)).get()!);
|
return publicUser(db.select().from(users).where(eq(users.id, id)).get()!);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -251,6 +253,7 @@ export async function userRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
|||||||
return reply.code(409).send({ error: "cannot delete the last admin" });
|
return reply.code(409).send({ error: "cannot delete the last admin" });
|
||||||
}
|
}
|
||||||
softDelete(db, "user", id, req.user.sub);
|
softDelete(db, "user", id, req.user.sub);
|
||||||
|
bumpPermsCache(); // their live session ends on its next request
|
||||||
return { ok: true };
|
return { ok: true };
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,10 +10,11 @@ import {
|
|||||||
validationPrograms,
|
validationPrograms,
|
||||||
type Db,
|
type Db,
|
||||||
} from "@parking/db";
|
} from "@parking/db";
|
||||||
import { VALIDATION_MODES, type ValidationMode } from "@parking/shared";
|
import { MERCHANT_VALIDATION_MODES, VALIDATION_MODES, type ValidationMode } from "@parking/shared";
|
||||||
import { requirePermission } from "../auth.js";
|
import { requirePermission } from "../auth.js";
|
||||||
|
import { requireModule } from "../modules.js";
|
||||||
import type { EventLog } from "../event-log.js";
|
import type { EventLog } from "../event-log.js";
|
||||||
import { liveValidations, sessionValidations } from "../validations.js";
|
import { applyValidation, liveValidations, sessionValidations } from "../validations.js";
|
||||||
|
|
||||||
// Merchant validations (bar / lavazh). The merchant is VALIDATION-ONLY: they scan the
|
// Merchant validations (bar / lavazh). The merchant is VALIDATION-ONLY: they scan the
|
||||||
// customer's ticket on their own device and apply their program — all money and paper
|
// customer's ticket on their own device and apply their program — all money and paper
|
||||||
@@ -63,21 +64,33 @@ function validateProgram(b: ProgramBody): string | null {
|
|||||||
if (!b.name || !String(b.name).trim()) return "name is required";
|
if (!b.name || !String(b.name).trim()) return "name is required";
|
||||||
if (!VALIDATION_MODES.includes(b.mode as ValidationMode)) return "mode must be comp|timeCredit|fixed|percent";
|
if (!VALIDATION_MODES.includes(b.mode as ValidationMode)) return "mode must be comp|timeCredit|fixed|percent";
|
||||||
const intOrNull = (v: unknown) => v == null || (Number.isInteger(v) && (v as number) > 0);
|
const intOrNull = (v: unknown) => v == null || (Number.isInteger(v) && (v as number) > 0);
|
||||||
if (!intOrNull(b.minutes)) return "minutes must be a positive integer";
|
// doneTolerance's minutes is a TOLERANCE — zero is a legitimate "free until done, not a
|
||||||
|
// minute more"; every other minutes use is a positive credit.
|
||||||
|
const minutesOk = b.mode === "doneTolerance"
|
||||||
|
? b.minutes == null || (Number.isInteger(b.minutes) && (b.minutes as number) >= 0)
|
||||||
|
: intOrNull(b.minutes);
|
||||||
|
if (!minutesOk) return b.mode === "doneTolerance" ? "minutes must be a non-negative integer" : "minutes must be a positive integer";
|
||||||
if (!intOrNull(b.maxAmountMinor)) return "maxAmountMinor must be a positive integer";
|
if (!intOrNull(b.maxAmountMinor)) return "maxAmountMinor must be a positive integer";
|
||||||
if (!intOrNull(b.maxPerDay)) return "maxPerDay must be a positive integer";
|
if (!intOrNull(b.maxPerDay)) return "maxPerDay must be a positive integer";
|
||||||
if (b.percent != null && (!Number.isInteger(b.percent) || b.percent < 1 || b.percent > 100))
|
if (b.percent != null && (!Number.isInteger(b.percent) || b.percent < 1 || b.percent > 100))
|
||||||
return "percent must be 1..100";
|
return "percent must be 1..100";
|
||||||
if (b.mode === "timeCredit" && b.minutes == null) return "timeCredit needs minutes";
|
if (b.mode === "timeCredit" && b.minutes == null) return "timeCredit needs minutes";
|
||||||
|
if (b.mode === "doneTolerance" && b.minutes == null) return "doneTolerance needs minutes (the tolerance; 0 allowed)";
|
||||||
if (b.mode === "percent" && b.percent == null) return "percent mode needs percent";
|
if (b.mode === "percent" && b.percent == null) return "percent mode needs percent";
|
||||||
if (b.mode === "fixed" && b.maxAmountMinor == null) return "fixed mode needs maxAmountMinor";
|
if (b.mode === "fixed" && b.maxAmountMinor == null) return "fixed mode needs maxAmountMinor";
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function validationRoutes(app: FastifyInstance, db: Db, eventLog: EventLog): Promise<void> {
|
export async function validationRoutes(app: FastifyInstance, db: Db, eventLog: EventLog): Promise<void> {
|
||||||
|
// The PROGRAM routes (compose / read discount programs) are CORE: the discount engine
|
||||||
|
// serves every module that grants a parking discount (Car Wash's "carwash" program
|
||||||
|
// rides it), so they are never behind the validation module gate — plain site:read /
|
||||||
|
// site:update. The MERCHANT routes (mine / lookup / apply / void — the scan screen)
|
||||||
|
// are the validation module itself: module gate FIRST (403 module_disabled when the
|
||||||
|
// site has validation off — see ../modules.ts), then the permission.
|
||||||
const siteRead = requirePermission("site:read");
|
const siteRead = requirePermission("site:read");
|
||||||
const siteWrite = requirePermission("site:update");
|
const siteWrite = requirePermission("site:update");
|
||||||
const applyGuard = requirePermission("validation:create");
|
const applyGuard = [requireModule(db, "validation"), requirePermission("validation:create")];
|
||||||
|
|
||||||
const liveProgram = (id: string) =>
|
const liveProgram = (id: string) =>
|
||||||
db
|
db
|
||||||
@@ -243,88 +256,21 @@ export async function validationRoutes(app: FastifyInstance, db: Db, eventLog: E
|
|||||||
if (!boundUserIds(programId).includes(req.user.sub)) {
|
if (!boundUserIds(programId).includes(req.user.sub)) {
|
||||||
return reply.code(403).send({ error: "you are not bound to this program" });
|
return reply.code(403).send({ error: "you are not bound to this program" });
|
||||||
}
|
}
|
||||||
|
if (!MERCHANT_VALIDATION_MODES.includes(program.mode)) {
|
||||||
// Session state — an open transient (subscriptions are prepaid; nothing to discount).
|
return reply.code(400).send({ error: "this program's discount is resolved by a car wash order, not at scan" });
|
||||||
const rows = db
|
|
||||||
.select({ type: ledgerEvents.type, payload: ledgerEvents.payload })
|
|
||||||
.from(ledgerEvents)
|
|
||||||
.where(eq(ledgerEvents.identity, identity))
|
|
||||||
.orderBy(ledgerEvents.index)
|
|
||||||
.all();
|
|
||||||
const entry = rows.find((r) => r.type === "vehicle_entry");
|
|
||||||
if (!entry) return reply.code(404).send({ error: "no session for ticket" });
|
|
||||||
const entryPl = (entry.payload ?? {}) as { permit?: boolean; permitId?: string };
|
|
||||||
if (entryPl.permit === true || entryPl.permitId != null) {
|
|
||||||
return reply.code(409).send({ error: "subscription sessions cannot be validated" });
|
|
||||||
}
|
|
||||||
if (rows.some((r) => r.type === "vehicle_exit" || r.type === "void")) {
|
|
||||||
return reply.code(409).send({ error: "session is closed" });
|
|
||||||
}
|
|
||||||
if (liveValidations(db, identity).some((v) => v.programId === programId)) {
|
|
||||||
return reply.code(409).send({ error: "this program is already applied to the ticket" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Per-day cap: unvoided applications of this program since LOCAL midnight (the
|
// The decision chain + the signed append live in ../validations.ts (applyValidation)
|
||||||
// appliance runs in site time).
|
// — shared with the Car Wash module, which applies its own sponsorship program with
|
||||||
if (program.maxPerDay != null) {
|
// no user binding. Only the binding check above is merchant-specific.
|
||||||
const midnight = new Date();
|
const result = await applyValidation(db, eventLog, {
|
||||||
midnight.setHours(0, 0, 0, 0);
|
programId,
|
||||||
const todays = db
|
|
||||||
.select({ id: ledgerEvents.id, occurredAt: ledgerEvents.occurredAt, payload: ledgerEvents.payload, type: ledgerEvents.type })
|
|
||||||
.from(ledgerEvents)
|
|
||||||
.where(eq(ledgerEvents.type, "validation"))
|
|
||||||
.all()
|
|
||||||
.filter((r) => Date.parse(r.occurredAt) >= midnight.getTime());
|
|
||||||
const voidedIds = new Set(
|
|
||||||
todays.map((r) => (r.payload as { refId?: string } | null)?.refId).filter(Boolean) as string[],
|
|
||||||
);
|
|
||||||
const count = todays.filter((r) => {
|
|
||||||
const p = (r.payload ?? {}) as { programId?: string; refId?: string };
|
|
||||||
return p.programId === programId && !p.refId && !voidedIds.has(r.id);
|
|
||||||
}).length;
|
|
||||||
if (count >= program.maxPerDay) {
|
|
||||||
return reply.code(409).send({ error: "daily cap reached for this program" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolve the values off the program row (frozen into the signed event).
|
|
||||||
let amountMinor: number | undefined;
|
|
||||||
if (program.mode === "fixed") {
|
|
||||||
const a = req.body?.amountMinor;
|
|
||||||
if (a == null || !Number.isInteger(a) || a <= 0) {
|
|
||||||
return reply.code(400).send({ error: "amountMinor (positive integer) required for this program" });
|
|
||||||
}
|
|
||||||
if (program.maxAmountMinor != null && a > program.maxAmountMinor) {
|
|
||||||
return reply.code(400).send({ error: `amount exceeds the program cap (${program.maxAmountMinor})` });
|
|
||||||
}
|
|
||||||
amountMinor = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ev = await eventLog.append({
|
|
||||||
type: "validation",
|
|
||||||
source: "manual",
|
|
||||||
identity,
|
identity,
|
||||||
payload: {
|
actor: req.user.username,
|
||||||
sessionRef: identity,
|
amountMinor: req.body?.amountMinor,
|
||||||
programId,
|
|
||||||
programLabel: program.name,
|
|
||||||
mode: program.mode,
|
|
||||||
...(program.mode === "timeCredit" && program.minutes != null ? { minutes: program.minutes } : {}),
|
|
||||||
...(program.mode === "percent" && program.percent != null ? { percent: program.percent } : {}),
|
|
||||||
...(amountMinor != null ? { amountMinor } : {}),
|
|
||||||
operator: req.user.username,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return reply.code(201).send({
|
|
||||||
ok: true,
|
|
||||||
eventId: ev.id,
|
|
||||||
programId,
|
|
||||||
label: program.name,
|
|
||||||
mode: program.mode,
|
|
||||||
minutes: program.mode === "timeCredit" ? program.minutes : undefined,
|
|
||||||
percent: program.mode === "percent" ? program.percent : undefined,
|
|
||||||
amountMinor,
|
|
||||||
});
|
});
|
||||||
|
if (!result.ok) return reply.code(result.status).send({ error: result.error });
|
||||||
|
return reply.code(201).send(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
// VOID my own UNUSED validation (fat-fingered amount / wrong ticket). Append-only:
|
// VOID my own UNUSED validation (fat-fingered amount / wrong ticket). Append-only:
|
||||||
|
|||||||
+142
-31
@@ -1,13 +1,17 @@
|
|||||||
import type { FastifyInstance } from "fastify";
|
import { randomBytes } from "node:crypto";
|
||||||
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
import type { Db } from "@parking/db";
|
import type { Db } from "@parking/db";
|
||||||
import type { LedgerEvent } from "@parking/shared";
|
import { feedPermissionFor, watchPermissions, type LedgerEvent, type Permission } from "@parking/shared";
|
||||||
import { roleHasPermissions } from "../auth.js";
|
import { currentRoleId, requireAuth, roleHasPermissions } from "../auth.js";
|
||||||
|
import { effectiveModulesFor } from "../modules.js";
|
||||||
import {
|
import {
|
||||||
deviceEvents,
|
deviceEvents,
|
||||||
type LaneStatusEvent,
|
type LaneStatusEvent,
|
||||||
type LanePresenceEvent,
|
type LanePresenceEvent,
|
||||||
type PlateRecognizedEvent,
|
type PlateRecognizedEvent,
|
||||||
|
type CameraActivityEvent,
|
||||||
} from "../device-events.js";
|
} from "../device-events.js";
|
||||||
|
import type { CameraActivity } from "../camera-activity.js";
|
||||||
import { enrichEvent } from "../event-enrich.js";
|
import { enrichEvent } from "../event-enrich.js";
|
||||||
import type { DeviceMonitor } from "../device-monitor.js";
|
import type { DeviceMonitor } from "../device-monitor.js";
|
||||||
import type { LaneStatus } from "../lane-status.js";
|
import type { LaneStatus } from "../lane-status.js";
|
||||||
@@ -31,10 +35,61 @@ import { getOccupancy } from "../occupancy.js";
|
|||||||
// an Origin allowlist: the handshake's Origin must be same-origin (or an explicitly
|
// an Origin allowlist: the handshake's Origin must be same-origin (or an explicitly
|
||||||
// allowed booth UI origin). Non-browser clients (no Origin) are rejected too.
|
// allowed booth UI origin). Non-browser clients (no Origin) are rejected too.
|
||||||
// See auth.ts, event-log.ts (emitLedger), capacity-occupancy.md.
|
// See auth.ts, event-log.ts (emitLedger), capacity-occupancy.md.
|
||||||
|
//
|
||||||
|
// Desktop shell (Tauri) exception — the WS TICKET. The desktop app's HTTP goes
|
||||||
|
// through tauri-plugin-http (reqwest, its own cookie jar) and its WebSocket
|
||||||
|
// through tauri-plugin-websocket (bare tungstenite, NO cookie jar at all), so
|
||||||
|
// the JWT cookie set at login can never ride on the WS handshake — jwtVerify()
|
||||||
|
// would 401 every connect (found 2026-09-04: the desktop live feed reconnected
|
||||||
|
// every 10s forever). The JWT is HttpOnly and must stay out of JS, so instead
|
||||||
|
// the desktop client POSTs /api/ws/ticket (normal cookie + CSRF auth) to get a
|
||||||
|
// single-use, 30-second random ticket bound to its user, and presents it in an
|
||||||
|
// `x-ws-ticket` header on the handshake. A browser page cannot set custom
|
||||||
|
// headers on a WebSocket, so this path is unreachable from a browser and adds
|
||||||
|
// no CSWSH surface; the Origin allowlist still applies to both paths.
|
||||||
|
|
||||||
/** Permission required to watch the live feed (a read-only stream of ledger +
|
// WHO may watch, and WHAT they see (venue-modules.md §"Permissions matrix", move 3):
|
||||||
* device status). Any role granted `report:read` may watch. */
|
// a role connects if it holds ANY watch permission — the core feed/occupancy/device
|
||||||
const WATCH_PERMISSION = "report:read" as const;
|
// ones or an effective module's own (carwash:read) — and every pushed message is then
|
||||||
|
// FILTERED per role: a ledger event needs feedPermissionFor(type) (the owning module's,
|
||||||
|
// else event:read); occupancy + the plate backfill need session:read; device / printer /
|
||||||
|
// lane / radar need device:read. `report:read` is the REPORTS screen, not the socket: the
|
||||||
|
// wash desk gets a live queue without the booth's ledger, the booth a feed without reports.
|
||||||
|
type Viewer = { has: (p: Permission) => boolean };
|
||||||
|
|
||||||
|
/** Handshake header carrying a desktop WS ticket (see file header). */
|
||||||
|
const WS_TICKET_HEADER = "x-ws-ticket";
|
||||||
|
/** A ticket is only good for the connect that immediately follows its issue. */
|
||||||
|
const WS_TICKET_TTL_MS = 30_000;
|
||||||
|
|
||||||
|
interface WsTicket {
|
||||||
|
sub: string;
|
||||||
|
roleId: string;
|
||||||
|
expiresAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Outstanding tickets. Tiny (one per desktop connect attempt), in-memory only —
|
||||||
|
* a server restart invalidates them, which is fine: the client just asks for
|
||||||
|
* another on its next reconnect. */
|
||||||
|
const tickets = new Map<string, WsTicket>();
|
||||||
|
|
||||||
|
function issueWsTicket(sub: string, roleId: string): string {
|
||||||
|
const now = Date.now();
|
||||||
|
for (const [key, t] of tickets) {
|
||||||
|
if (t.expiresAt <= now) tickets.delete(key);
|
||||||
|
}
|
||||||
|
const ticket = randomBytes(32).toString("hex");
|
||||||
|
tickets.set(ticket, { sub, roleId, expiresAt: now + WS_TICKET_TTL_MS });
|
||||||
|
return ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Single-use: the ticket is removed whether or not it turns out to be valid. */
|
||||||
|
function consumeWsTicket(ticket: string): WsTicket | null {
|
||||||
|
const t = tickets.get(ticket);
|
||||||
|
if (!t) return null;
|
||||||
|
tickets.delete(ticket);
|
||||||
|
return t.expiresAt > Date.now() ? t : null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the handshake's Origin trusted? Same-origin (Origin host === Host header) is
|
* Is the handshake's Origin trusted? Same-origin (Origin host === Host header) is
|
||||||
@@ -58,46 +113,93 @@ function isAllowedOrigin(origin: string | undefined, host: string | undefined):
|
|||||||
return allow.includes(origin);
|
return allow.includes(origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authenticate a WebSocket handshake and return the caller's role id: Origin allowlist
|
||||||
|
* FIRST (anti-CSWSH), then a desktop ticket (x-ws-ticket) or the JWT cookie. Shared by
|
||||||
|
* every socket route (/api/ws, /api/cameras/:id/live) so they cannot drift apart.
|
||||||
|
* Permission checks stay with each route.
|
||||||
|
*/
|
||||||
|
export async function authenticateWsHandshake(req: FastifyRequest): Promise<string> {
|
||||||
|
if (!isAllowedOrigin(req.headers.origin, req.headers.host)) {
|
||||||
|
throw Object.assign(new Error("forbidden origin"), { statusCode: 403 });
|
||||||
|
}
|
||||||
|
const rawTicket = req.headers[WS_TICKET_HEADER];
|
||||||
|
const ticket = Array.isArray(rawTicket) ? rawTicket[0] : rawTicket;
|
||||||
|
if (ticket !== undefined) {
|
||||||
|
const t = consumeWsTicket(ticket);
|
||||||
|
if (!t) {
|
||||||
|
throw Object.assign(new Error("invalid or expired ws ticket"), { statusCode: 401 });
|
||||||
|
}
|
||||||
|
return t.roleId;
|
||||||
|
}
|
||||||
|
await req.jwtVerify(); // reads the HttpOnly cookie (browser path)
|
||||||
|
if (!req.user) {
|
||||||
|
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
||||||
|
}
|
||||||
|
return currentRoleId(req.user.sub) ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
type OutMsg =
|
type OutMsg =
|
||||||
| {
|
| {
|
||||||
kind: "hello";
|
kind: "hello";
|
||||||
occupancy: ReturnType<typeof getOccupancy>;
|
occupancy: ReturnType<typeof getOccupancy> | null;
|
||||||
devices: unknown;
|
devices: unknown;
|
||||||
lanes: LaneStatusEvent;
|
lanes: LaneStatusEvent | null;
|
||||||
radar: LanePresenceEvent;
|
radar: LanePresenceEvent | null;
|
||||||
|
/** Camera ids currently seeing a vehicle (camera tiles go live). */
|
||||||
|
cameras: string[] | null;
|
||||||
}
|
}
|
||||||
| { kind: "ledger"; event: unknown; occupancy: ReturnType<typeof getOccupancy> }
|
| { kind: "ledger"; event: unknown; occupancy: ReturnType<typeof getOccupancy> | null }
|
||||||
| { kind: "printer-status"; event: unknown }
|
| { kind: "printer-status"; event: unknown }
|
||||||
| { kind: "device-status"; event: unknown }
|
| { kind: "device-status"; event: unknown }
|
||||||
| { kind: "lane-status"; lanes: LaneStatusEvent }
|
| { kind: "lane-status"; lanes: LaneStatusEvent }
|
||||||
| { kind: "lane-presence"; radar: LanePresenceEvent }
|
| { kind: "lane-presence"; radar: LanePresenceEvent }
|
||||||
|
| { kind: "camera-activity"; camera: CameraActivityEvent }
|
||||||
| { kind: "plate-recognized"; plate: PlateRecognizedEvent };
|
| { kind: "plate-recognized"; plate: PlateRecognizedEvent };
|
||||||
|
|
||||||
|
declare module "fastify" {
|
||||||
|
interface FastifyRequest {
|
||||||
|
/** The role the WS preHandler authenticated (ticket or cookie path) — for the handler's filter. */
|
||||||
|
wsRoleId?: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function wsRoutes(
|
export async function wsRoutes(
|
||||||
app: FastifyInstance,
|
app: FastifyInstance,
|
||||||
db: Db,
|
db: Db,
|
||||||
deviceMonitor: DeviceMonitor,
|
deviceMonitor: DeviceMonitor,
|
||||||
laneStatus: LaneStatus,
|
laneStatus: LaneStatus,
|
||||||
lanePresence: LanePresence,
|
lanePresence: LanePresence,
|
||||||
|
cameraActivity: CameraActivity,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
// Desktop-only: mint a WS ticket for the signed-in session (see file header).
|
||||||
|
// Ordinary cookie + CSRF auth — the desktop client CAN do that over HTTP (via
|
||||||
|
// tauri-plugin-http), it just can't carry the cookie onto the WebSocket.
|
||||||
|
app.post("/api/ws/ticket", { preHandler: requireAuth }, async (req) => ({
|
||||||
|
ticket: issueWsTicket(req.user.sub, req.user.roleId),
|
||||||
|
expiresInMs: WS_TICKET_TTL_MS,
|
||||||
|
}));
|
||||||
|
|
||||||
app.get(
|
app.get(
|
||||||
"/api/ws",
|
"/api/ws",
|
||||||
{
|
{
|
||||||
websocket: true,
|
websocket: true,
|
||||||
// Origin allowlist (anti-CSWSH, replaces CSRF — see file header) THEN JWT +
|
// Origin allowlist (anti-CSWSH, replaces CSRF — see file header) THEN
|
||||||
// role. Reject a cross/absent origin before touching the token, so a hijack
|
// session (JWT cookie, or a desktop WS ticket) THEN role. Reject a
|
||||||
// attempt never reaches an authenticated socket. jwtVerify reads the cookie.
|
// cross/absent origin before touching either credential, so a hijack
|
||||||
|
// attempt never reaches an authenticated socket.
|
||||||
preHandler: async (req) => {
|
preHandler: async (req) => {
|
||||||
if (!isAllowedOrigin(req.headers.origin, req.headers.host)) {
|
const roleId = await authenticateWsHandshake(req);
|
||||||
throw Object.assign(new Error("forbidden origin"), { statusCode: 403 });
|
const may = watchPermissions(effectiveModulesFor(db)).some((p) => roleHasPermissions(roleId, [p]));
|
||||||
}
|
if (!may) throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
||||||
await req.jwtVerify();
|
req.wsRoleId = roleId;
|
||||||
if (!req.user || !roleHasPermissions(req.user.roleId, [WATCH_PERMISSION])) {
|
|
||||||
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
(socket) => {
|
(socket, req) => {
|
||||||
|
const roleId = req.wsRoleId ?? req.user?.roleId ?? "";
|
||||||
|
const viewer: Viewer = { has: (p) => roleHasPermissions(roleId, [p]) };
|
||||||
|
const seesOccupancy = viewer.has("session:read");
|
||||||
|
const seesDevices = viewer.has("device:read");
|
||||||
const send = (msg: OutMsg) => {
|
const send = (msg: OutMsg) => {
|
||||||
// readyState 1 = OPEN; never throw out of an event-bus callback.
|
// readyState 1 = OPEN; never throw out of an event-bus callback.
|
||||||
if (socket.readyState === 1) {
|
if (socket.readyState === 1) {
|
||||||
@@ -111,40 +213,48 @@ export async function wsRoutes(
|
|||||||
|
|
||||||
// Initial snapshot so the client renders immediately, before any event:
|
// Initial snapshot so the client renders immediately, before any event:
|
||||||
// occupancy AND the current device-status set (for the footer).
|
// occupancy AND the current device-status set (for the footer).
|
||||||
|
// Each part of the snapshot only for a role that may see it (null otherwise).
|
||||||
send({
|
send({
|
||||||
kind: "hello",
|
kind: "hello",
|
||||||
occupancy: getOccupancy(db),
|
occupancy: seesOccupancy ? getOccupancy(db) : null,
|
||||||
devices: deviceMonitor.snapshot(),
|
devices: seesDevices ? deviceMonitor.snapshot() : null,
|
||||||
lanes: laneStatus.snapshot(),
|
lanes: seesDevices ? laneStatus.snapshot() : null,
|
||||||
radar: lanePresence.snapshot(),
|
radar: seesDevices ? lanePresence.snapshot() : null,
|
||||||
|
cameras: seesDevices ? cameraActivity.snapshot() : null,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Subscribe to the live buses. Each handler recomputes occupancy from the
|
// Subscribe to the live buses. Each handler recomputes occupancy from the
|
||||||
// ledger (cheap fold) so the pushed count is always authoritative.
|
// ledger (cheap fold) so the pushed count is always authoritative.
|
||||||
const offLedger = deviceEvents.onLedger((event) => {
|
const offLedger = deviceEvents.onLedger((event) => {
|
||||||
|
// Per-role filter: the event type's feed permission (module's own, else event:read).
|
||||||
|
if (!viewer.has(feedPermissionFor((event as { type: LedgerEvent["type"] }).type))) return;
|
||||||
// Enrich with read-time display fields (subscriber name) before fan-out.
|
// Enrich with read-time display fields (subscriber name) before fan-out.
|
||||||
const enriched = enrichEvent(db, event as unknown as LedgerEvent);
|
const enriched = enrichEvent(db, event as unknown as LedgerEvent);
|
||||||
send({ kind: "ledger", event: enriched, occupancy: getOccupancy(db) });
|
send({ kind: "ledger", event: enriched, occupancy: seesOccupancy ? getOccupancy(db) : null });
|
||||||
});
|
});
|
||||||
const offPrinter = deviceEvents.onPrinterStatus((event) => {
|
const offPrinter = deviceEvents.onPrinterStatus((event) => {
|
||||||
send({ kind: "printer-status", event });
|
if (seesDevices) send({ kind: "printer-status", event });
|
||||||
});
|
});
|
||||||
// Unified device status (all categories) for the booth footer — pushed on
|
// Unified device status (all categories) for the booth footer — pushed on
|
||||||
// change; the initial set rode the hello above.
|
// change; the initial set rode the hello above.
|
||||||
const offDevice = deviceEvents.onDeviceStatus((event) => {
|
const offDevice = deviceEvents.onDeviceStatus((event) => {
|
||||||
send({ kind: "device-status", event });
|
if (seesDevices) send({ kind: "device-status", event });
|
||||||
});
|
});
|
||||||
// Lane busy/free (camera vehicle detection → booth barrier lights). Advisory.
|
// Lane busy/free (camera vehicle detection → booth barrier lights). Advisory.
|
||||||
const offLane = deviceEvents.onLaneStatus((lanes) => {
|
const offLane = deviceEvents.onLaneStatus((lanes) => {
|
||||||
send({ kind: "lane-status", lanes });
|
if (seesDevices) send({ kind: "lane-status", lanes });
|
||||||
});
|
});
|
||||||
// Lane RADAR presence (presence-input edge → barrier-light blink). Advisory.
|
// Lane RADAR presence (presence-input edge → barrier-light blink). Advisory.
|
||||||
const offPresence = deviceEvents.onLanePresence((radar) => {
|
const offPresence = deviceEvents.onLanePresence((radar) => {
|
||||||
send({ kind: "lane-presence", radar });
|
if (seesDevices) send({ kind: "lane-presence", radar });
|
||||||
|
});
|
||||||
|
// A camera started/stopped seeing a vehicle → its booth tile switches live/snapshot.
|
||||||
|
const offCamera = deviceEvents.onCameraActivity((camera) => {
|
||||||
|
if (seesDevices) send({ kind: "camera-activity", camera });
|
||||||
});
|
});
|
||||||
// A late async plate recognition → backfill the badge on the matching feed row. Advisory.
|
// A late async plate recognition → backfill the badge on the matching feed row. Advisory.
|
||||||
const offPlate = deviceEvents.onPlateRecognized((plate) => {
|
const offPlate = deviceEvents.onPlateRecognized((plate) => {
|
||||||
send({ kind: "plate-recognized", plate });
|
if (seesOccupancy) send({ kind: "plate-recognized", plate });
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("close", () => {
|
socket.on("close", () => {
|
||||||
@@ -154,6 +264,7 @@ export async function wsRoutes(
|
|||||||
offLane();
|
offLane();
|
||||||
offPresence();
|
offPresence();
|
||||||
offPlate();
|
offPlate();
|
||||||
|
offCamera();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
+49
-11
@@ -45,8 +45,11 @@ import { shiftRoutes } from "./routes/shift.js";
|
|||||||
import { drawerRoutes } from "./routes/drawer.js";
|
import { drawerRoutes } from "./routes/drawer.js";
|
||||||
import { entryRoutes } from "./routes/entry.js";
|
import { entryRoutes } from "./routes/entry.js";
|
||||||
import { siteRoutes } from "./routes/site.js";
|
import { siteRoutes } from "./routes/site.js";
|
||||||
import { validationRoutes } from "./routes/validations.js";
|
import { registerModules } from "./modules/index.js";
|
||||||
import { snapshotRoutes } from "./routes/snapshots.js";
|
import { snapshotRoutes } from "./routes/snapshots.js";
|
||||||
|
import { cameraRoutes, liveResolverFor } from "./routes/cameras.js";
|
||||||
|
import { CameraActivity } from "./camera-activity.js";
|
||||||
|
import { CameraLiveHub } from "./camera-live.js";
|
||||||
import { tariffRoutes } from "./routes/tariffs.js";
|
import { tariffRoutes } from "./routes/tariffs.js";
|
||||||
import { printerRoutes } from "./routes/printers.js";
|
import { printerRoutes } from "./routes/printers.js";
|
||||||
import { setupRoutes } from "./routes/setup.js";
|
import { setupRoutes } from "./routes/setup.js";
|
||||||
@@ -82,6 +85,25 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
formatters: { level: (label) => ({ level: label }) },
|
formatters: { level: (label) => ({ level: label }) },
|
||||||
stream: pinoDbStream(logService, process.stdout),
|
stream: pinoDbStream(logService, process.stdout),
|
||||||
},
|
},
|
||||||
|
// ONE line per request (the onResponse hook below) instead of Fastify's default
|
||||||
|
// "incoming request" + "request completed" pair — on a quiet booth the pair was
|
||||||
|
// most of the log. NB: with this flag Fastify's default error handler ALSO stops
|
||||||
|
// logging handler errors, so the onError hook below re-adds that for 5xx.
|
||||||
|
disableRequestLogging: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Request log, one line: method, url, status, elapsed. Routes declared with
|
||||||
|
// `logLevel: "silent"` (the /health probe) are skipped because `req.log` is theirs.
|
||||||
|
app.addHook("onResponse", async (req, reply) => {
|
||||||
|
req.log.info(`${req.method} ${req.url} → ${reply.statusCode} ${Math.round(reply.elapsedTime)}ms`);
|
||||||
|
});
|
||||||
|
// Handler failures (5xx) keep their stack in the log — disableRequestLogging would
|
||||||
|
// otherwise swallow them (fastify/lib/error-handler.js checks the same flag).
|
||||||
|
app.addHook("onError", async (req, _reply, err) => {
|
||||||
|
// onError runs BEFORE the error handler sets the reply status, so the reply still
|
||||||
|
// says 200 here; an error without its own statusCode is a 500 by definition.
|
||||||
|
const code = (err as { statusCode?: number }).statusCode ?? 500;
|
||||||
|
if (code >= 500) req.log.error({ err }, `${req.method} ${req.url} failed: ${err.message}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wire the RBAC permission resolver to this DB (route guards resolve a user's
|
// Wire the RBAC permission resolver to this DB (route guards resolve a user's
|
||||||
@@ -106,7 +128,12 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
cookie: { cookieName: TOKEN_COOKIE, signed: false },
|
cookie: { cookieName: TOKEN_COOKIE, signed: false },
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/health", async () => ({ status: "ok" }));
|
// Unauthenticated liveness probe. `app` lets a client (the desktop ConnectScreen
|
||||||
|
// test — apps/web/src/lib/backend-config.ts) tell THIS server apart from any
|
||||||
|
// other service that happens to answer on the address the operator typed.
|
||||||
|
// `logLevel: "silent"`: Docker's HEALTHCHECK hits this every 30 s; logging each
|
||||||
|
// probe was half of a quiet booth's output (park-2, 2026-10-08).
|
||||||
|
app.get("/health", { logLevel: "silent" }, async () => ({ status: "ok", app: "parking-system" }));
|
||||||
|
|
||||||
// Local username/password login → JWT in an HttpOnly cookie + CSRF cookie.
|
// Local username/password login → JWT in an HttpOnly cookie + CSRF cookie.
|
||||||
await authRoutes(app, db);
|
await authRoutes(app, db);
|
||||||
@@ -114,7 +141,6 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
// RBAC administration: compose roles (role:*) + manage users (user:*). The
|
// RBAC administration: compose roles (role:*) + manage users (user:*). The
|
||||||
// built-in admin role is protected; the last admin can't be removed. See auth.ts.
|
// built-in admin role is protected; the last admin can't be removed. See auth.ts.
|
||||||
await userRoutes(app, db);
|
await userRoutes(app, db);
|
||||||
await roleRoutes(app, db);
|
|
||||||
|
|
||||||
// Vision (ANPR) client — built early so the device monitor can include the vision
|
// Vision (ANPR) client — built early so the device monitor can include the vision
|
||||||
// service's health in the footer, AND so the setup wizard's "Test ANPR" can run a
|
// service's health in the footer, AND so the setup wizard's "Test ANPR" can run a
|
||||||
@@ -141,6 +167,7 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
// no lane — a parking lot is one pool with a flexible set of entry/exit points.
|
// no lane — a parking lot is one pool with a flexible set of entry/exit points.
|
||||||
// See wiki/concepts/first-run-setup.md, entry-exit-points.md.
|
// See wiki/concepts/first-run-setup.md, entry-exit-points.md.
|
||||||
await setupRoutes(app, db, visionClient, eventLog);
|
await setupRoutes(app, db, visionClient, eventLog);
|
||||||
|
await roleRoutes(app, db, eventLog);
|
||||||
|
|
||||||
// Inbound device pushes (e.g. Dingtian Input Link URL → button events),
|
// Inbound device pushes (e.g. Dingtian Input Link URL → button events),
|
||||||
// guarded by source-IP allowlist + a shared-secret path token, both read from
|
// guarded by source-IP allowlist + a shared-secret path token, both read from
|
||||||
@@ -152,6 +179,14 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
const laneStatus = new LaneStatus(db, app.log);
|
const laneStatus = new LaneStatus(db, app.log);
|
||||||
app.addHook("onClose", async () => laneStatus.stop());
|
app.addHook("onClose", async () => laneStatus.stop());
|
||||||
|
|
||||||
|
// Per-CAMERA vehicle activity (same pushes, keyed by camera) + the live-video hub for
|
||||||
|
// the booth camera tiles: a tile goes live while its camera sees a vehicle. Advisory
|
||||||
|
// viewing only. See camera-live.ts, wiki/decisions/camera-live-view.md.
|
||||||
|
const cameraActivity = new CameraActivity(app.log);
|
||||||
|
app.addHook("onClose", async () => cameraActivity.stop());
|
||||||
|
const cameraLive = new CameraLiveHub(liveResolverFor(db), app.log);
|
||||||
|
app.addHook("onClose", async () => cameraLive.stopAll());
|
||||||
|
|
||||||
// Per-lane RADAR presence (presence-input edges → barrier-light blink). Mirrors the
|
// Per-lane RADAR presence (presence-input edges → barrier-light blink). Mirrors the
|
||||||
// physical button lamp (relay 3): the SAME presence signal, surfaced to the booth UI.
|
// physical button lamp (relay 3): the SAME presence signal, surfaced to the booth UI.
|
||||||
const lanePresence = new LanePresence(db, app.log);
|
const lanePresence = new LanePresence(db, app.log);
|
||||||
@@ -192,10 +227,11 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
|
|
||||||
// Live booth feed: server-pushed ledger + occupancy + printer-status over a
|
// Live booth feed: server-pushed ledger + occupancy + printer-status over a
|
||||||
// single authenticated WebSocket (/api/ws). See routes/ws.ts.
|
// single authenticated WebSocket (/api/ws). See routes/ws.ts.
|
||||||
await wsRoutes(app, db, deviceMonitor, laneStatus, lanePresence);
|
await wsRoutes(app, db, deviceMonitor, laneStatus, lanePresence, cameraActivity);
|
||||||
|
|
||||||
// Entry/exit camera snapshots (BLOB-in-DB), read-only. See snapshot.ts.
|
// Entry/exit camera snapshots (BLOB-in-DB), read-only. See snapshot.ts.
|
||||||
await snapshotRoutes(app, db);
|
await snapshotRoutes(app, db);
|
||||||
|
await cameraRoutes(app, db, cameraLive);
|
||||||
|
|
||||||
// Entry flow: a button press → print ticket → signed vehicle_entry → pulseOpen.
|
// Entry flow: a button press → print ticket → signed vehicle_entry → pulseOpen.
|
||||||
// Subscribes to the SAME input bus as the telemetry writer below; the two are
|
// Subscribes to the SAME input bus as the telemetry writer below; the two are
|
||||||
@@ -246,7 +282,7 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
// each detected target (vehicle). Source-IP guarded + optional Digest; records the raw
|
// each detected target (vehicle). Source-IP guarded + optional Digest; records the raw
|
||||||
// payload as a `kind:"alarm"` device_event, drives lane busy/free, AND hands a vehicle
|
// payload as a `kind:"alarm"` device_event, drives lane busy/free, AND hands a vehicle
|
||||||
// detection to the ANPR bridge above. See routes/hikvision-alarm.ts.
|
// detection to the ANPR bridge above. See routes/hikvision-alarm.ts.
|
||||||
await hikvisionAlarmRoutes(app, db, laneStatus, anprBridge);
|
await hikvisionAlarmRoutes(app, db, laneStatus, anprBridge, cameraActivity);
|
||||||
|
|
||||||
// Credential capture ("enroll a card"): lets the operator present an RFID card to a
|
// Credential capture ("enroll a card"): lets the operator present an RFID card to a
|
||||||
// CHOSEN reader to populate a subscription credential, without blocking the other
|
// CHOSEN reader to populate a subscription credential, without blocking the other
|
||||||
@@ -283,9 +319,9 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
await subscriptionPlanRoutes(app, db);
|
await subscriptionPlanRoutes(app, db);
|
||||||
|
|
||||||
// Shift open/close (shiftService constructed above).
|
// Shift open/close (shiftService constructed above).
|
||||||
await shiftRoutes(app, shiftService);
|
await shiftRoutes(app, db, shiftService);
|
||||||
// Drawer cash movements — operator records, admin reviews (routes/drawer.ts).
|
// Drawer cash movements — operator records, admin reviews (routes/drawer.ts).
|
||||||
await drawerRoutes(app, shiftService);
|
await drawerRoutes(app, db, shiftService);
|
||||||
// Operator-issued entry (broken physical button) — flagged mint, presence-gated.
|
// Operator-issued entry (broken physical button) — flagged mint, presence-gated.
|
||||||
await entryRoutes(app, entryFlow, laneStatus, shiftService);
|
await entryRoutes(app, entryFlow, laneStatus, shiftService);
|
||||||
|
|
||||||
@@ -293,10 +329,12 @@ export async function buildServer(opts: BuildOptions = {}): Promise<FastifyInsta
|
|||||||
// at capacity) is in the entry flow. See wiki/concepts/capacity-occupancy.md.
|
// at capacity) is in the entry flow. See wiki/concepts/capacity-occupancy.md.
|
||||||
await siteRoutes(app, db, eventLog);
|
await siteRoutes(app, db, eventLog);
|
||||||
|
|
||||||
// Merchant validations (bar / lavazh): setup panel config + the merchant user's
|
// Venue modules (wiki/decisions/venue-modules.md): folder-based modules register
|
||||||
// scan-and-apply. The booth settlement folds the applied validations into its
|
// here by iterating the shared registry — today that is `validation` (merchant
|
||||||
// quote (pay-station.ts). See wiki/concepts/validation-discounts.md.
|
// validations for the Bar; the booth settlement folds applied validations into its
|
||||||
await validationRoutes(app, db, eventLog);
|
// quote, pay-station.ts). `parking` is in the registry too but its routes are still
|
||||||
|
// the flat list above; they move behind the seam subsystem by subsystem.
|
||||||
|
await registerModules(app, { db, eventLog, payStation, shiftService });
|
||||||
|
|
||||||
// Application logs: ingest frontend errors (POST /api/logs, any signed-in user) +
|
// Application logs: ingest frontend errors (POST /api/logs, any signed-in user) +
|
||||||
// read the store (GET /api/logs, log:read). See wiki/concepts/app-logs.md.
|
// read the store (GET /api/logs, log:read). See wiki/concepts/app-logs.md.
|
||||||
|
|||||||
@@ -34,12 +34,14 @@ afterEach(() => close());
|
|||||||
/** Append a signed payment with source-split flags, as the booth/pay paths do. */
|
/** Append a signed payment with source-split flags, as the booth/pay paths do. */
|
||||||
async function payment(
|
async function payment(
|
||||||
amountMinor: number,
|
amountMinor: number,
|
||||||
opts: { tender?: "cash" | "card"; subscriptionSale?: boolean; subscriptionWindowCharge?: boolean } = {},
|
opts: { tender?: "cash" | "card"; subscriptionSale?: boolean; subscriptionWindowCharge?: boolean; forgoneMinor?: number } = {},
|
||||||
) {
|
) {
|
||||||
await log.append({
|
await log.append({
|
||||||
type: "payment", source: "manual", identity: "T",
|
type: "payment", source: "manual", identity: "T",
|
||||||
payload: {
|
payload: {
|
||||||
sessionRef: "T", amountMinor, currency: "ALL", tender: opts.tender ?? "cash",
|
sessionRef: "T", amountMinor, currency: "ALL",
|
||||||
|
// A forgiven exit's $0 payment carries no tender (nothing was tendered).
|
||||||
|
...(opts.forgoneMinor != null ? { forgoneMinor: opts.forgoneMinor } : { tender: opts.tender ?? "cash" }),
|
||||||
...(opts.subscriptionSale ? { subscriptionSale: true } : {}),
|
...(opts.subscriptionSale ? { subscriptionSale: true } : {}),
|
||||||
...(opts.subscriptionWindowCharge ? { subscriptionWindowCharge: true } : {}),
|
...(opts.subscriptionWindowCharge ? { subscriptionWindowCharge: true } : {}),
|
||||||
},
|
},
|
||||||
@@ -74,6 +76,29 @@ describe("single-open invariant", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("forgiven exits on the Z-report", () => {
|
||||||
|
it("a $0 forgiven payment adds nothing to cash/card/drawer and is reported as its own line", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
await payment(10000); // a real ticket, cash
|
||||||
|
await payment(0, { forgoneMinor: 25000 });
|
||||||
|
await payment(0, { forgoneMinor: 5000 });
|
||||||
|
|
||||||
|
const r = shift.currentReport()!;
|
||||||
|
expect(r.cashTotalMinor).toBe(10000);
|
||||||
|
expect(r.cardTotalMinor).toBe(0);
|
||||||
|
expect(r.ticketTotalMinor).toBe(10000);
|
||||||
|
expect(r.forgoneTotalMinor).toBe(30000);
|
||||||
|
expect(r.forgoneCount).toBe(2);
|
||||||
|
expect(r.discountTotalMinor).toBe(0); // not a merchant validation
|
||||||
|
expect(r.expectedDrawerMinor).toBe(r.openingFloatMinor + 10000);
|
||||||
|
|
||||||
|
const closed = await shift.close("alice");
|
||||||
|
expect(closed).toMatchObject({ forgoneTotalMinor: 30000, forgoneCount: 2, cashTotalMinor: 10000 });
|
||||||
|
const hist = shift.listShifts();
|
||||||
|
expect(hist[0]).toMatchObject({ forgoneTotalMinor: 30000, forgoneCount: 2 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("takings split by source", () => {
|
describe("takings split by source", () => {
|
||||||
it("separates subscription sales, out-of-window charges, and transient tickets", async () => {
|
it("separates subscription sales, out-of-window charges, and transient tickets", async () => {
|
||||||
await shift.open("alice");
|
await shift.open("alice");
|
||||||
@@ -242,3 +267,93 @@ describe("close signs a Z-report; listShifts reads it back", () => {
|
|||||||
expect(shift.listOperators()).toEqual(["alice", "bob"]);
|
expect(shift.listOperators()).toEqual(["alice", "bob"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("tills: one shift per till, one drawer per till", () => {
|
||||||
|
/** A bay payment as the Car Wash module signs it (till = carwash). */
|
||||||
|
async function bayPayment(amountMinor: number, tender: "cash" | "card" = "cash") {
|
||||||
|
await log.append({
|
||||||
|
type: "carwash_payment", source: "manual", identity: "T",
|
||||||
|
payload: { sessionRef: "T", orderId: "o1", amountMinor, currency: "ALL", tender, till: "carwash" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it("the booth and the carwash till can both be open at once, by different operators", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
await expect(shift.open("wanda", "carwash")).resolves.toMatchObject({ till: "carwash" });
|
||||||
|
expect(shift.currentOpenShift()?.identity).toBe("alice");
|
||||||
|
expect(shift.currentOpenShift("carwash")?.identity).toBe("wanda");
|
||||||
|
// Each till keeps its own single-open rule.
|
||||||
|
await expect(shift.open("bob", "carwash")).rejects.toBeInstanceOf(ShiftAlreadyOpenError);
|
||||||
|
await expect(shift.open("bob")).rejects.toBeInstanceOf(ShiftAlreadyOpenError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("requireOpenShift is per till: a booth shift does not cover the bay", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
expect(() => shift.requireOpenShift("carwash")).toThrow(NoShiftOpenError);
|
||||||
|
await shift.open("wanda", "carwash");
|
||||||
|
expect(shift.requireOpenShift("carwash").identity).toBe("wanda");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("money folds into ITS till only: bay cash is the wash operator's, not the booth's", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
await shift.open("wanda", "carwash");
|
||||||
|
await payment(10000); // booth (payment events carry till=booth or nothing)
|
||||||
|
await bayPayment(70000);
|
||||||
|
await bayPayment(20000, "card");
|
||||||
|
|
||||||
|
const booth = shift.currentReport()!;
|
||||||
|
expect(booth.till).toBe("booth");
|
||||||
|
expect(booth.cashTotalMinor).toBe(10000);
|
||||||
|
expect(booth.paymentCount).toBe(1);
|
||||||
|
expect(booth.expectedDrawerMinor).toBe(10000);
|
||||||
|
|
||||||
|
const wash = shift.currentReport("carwash")!;
|
||||||
|
expect(wash.till).toBe("carwash");
|
||||||
|
expect(wash.cashTotalMinor).toBe(70000);
|
||||||
|
expect(wash.cardTotalMinor).toBe(20000);
|
||||||
|
expect(wash.paymentCount).toBe(2);
|
||||||
|
expect(wash.expectedDrawerMinor).toBe(70000);
|
||||||
|
|
||||||
|
expect(shift.drawerBalance().balanceMinor).toBe(10000);
|
||||||
|
expect(shift.drawerBalance("carwash").balanceMinor).toBe(70000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("vouchers name their till; each till's expected drawer carries forward on its own", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
await shift.open("wanda", "carwash");
|
||||||
|
await shift.recordVoucher({ type: "cash_in", operator: "wanda", amountMinor: 5000, reason: "float", till: "carwash" });
|
||||||
|
await shift.recordVoucher({ type: "cash_in", operator: "alice", amountMinor: 100000, reason: "float" });
|
||||||
|
await bayPayment(70000);
|
||||||
|
expect(shift.movementsWithStatus({ till: "carwash" }).map((m) => m.amountMinor)).toEqual([5000]);
|
||||||
|
|
||||||
|
const washZ = await shift.close("wanda", "carwash");
|
||||||
|
expect(washZ).toMatchObject({ till: "carwash", cashAddedMinor: 5000, cashTotalMinor: 70000, expectedDrawerMinor: 75000 });
|
||||||
|
const boothZ = await shift.close("alice");
|
||||||
|
expect(boothZ).toMatchObject({ till: "booth", cashAddedMinor: 100000, cashTotalMinor: 0, expectedDrawerMinor: 100000 });
|
||||||
|
|
||||||
|
// Next shift on each till inherits that till's drawer only.
|
||||||
|
expect((await shift.open("wanda", "carwash")).openingFloatMinor).toBe(75000);
|
||||||
|
expect((await shift.open("bob")).openingFloatMinor).toBe(100000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("close is per till: closing the booth never closes the wash desk", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
await shift.open("alice", "carwash");
|
||||||
|
await shift.close("alice");
|
||||||
|
expect(shift.currentOpenShift()).toBeNull();
|
||||||
|
expect(shift.currentOpenShift("carwash")?.identity).toBe("alice");
|
||||||
|
await expect(shift.close("alice")).rejects.toBeInstanceOf(NoOpenShiftError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("history lists both tills, filterable; pre-till reports read as booth", async () => {
|
||||||
|
await shift.open("alice");
|
||||||
|
await shift.open("wanda", "carwash");
|
||||||
|
await shift.close("wanda", "carwash");
|
||||||
|
await shift.close("alice");
|
||||||
|
const all = shift.listShifts();
|
||||||
|
expect(all.map((s) => s.till).sort()).toEqual(["booth", "carwash"]);
|
||||||
|
expect(shift.listShifts({ till: "carwash" }).map((s) => s.operator)).toEqual(["wanda"]);
|
||||||
|
expect(shift.listShifts({ till: "booth" }).map((s) => s.operator)).toEqual(["alice"]);
|
||||||
|
expect(shift.listOperators("carwash")).toEqual(["wanda"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
+265
-120
@@ -1,6 +1,6 @@
|
|||||||
import { eq, devices, ledgerEvents, type Db } from "@parking/db";
|
import { eq, devices, ledgerEvents, type Db, inArray } from "@parking/db";
|
||||||
import { registry, formatStampSq as zStamp, type PrinterDevice } from "@parking/devices";
|
import { orderForRole, printerRoleOf, registry, formatStampSq as zStamp, type PrinterDevice, type PrinterInstance, type PrinterRole } from "@parking/devices";
|
||||||
import type { LedgerPayload } from "@parking/shared";
|
import { BOOTH_TILL, tillOf, type ChargeLine, type LedgerPayload, type ModuleId, type TillId } from "@parking/shared";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
import type { EventLog } from "./event-log.js";
|
import type { EventLog } from "./event-log.js";
|
||||||
|
|
||||||
@@ -8,33 +8,46 @@ import type { EventLog } from "./event-log.js";
|
|||||||
// delimited by EXPLICIT marks — not a clock. Represented entirely as signed ledger
|
// delimited by EXPLICIT marks — not a clock. Represented entirely as signed ledger
|
||||||
// events (no mutable table): `shift_open` … `shift_z_report`. At close, sum the
|
// events (no mutable table): `shift_open` … `shift_z_report`. At close, sum the
|
||||||
// `payment` events taken during the shift by tender and print a Z-report.
|
// `payment` events taken during the shift by tender and print a Z-report.
|
||||||
|
//
|
||||||
|
// TILLS (2026-09-05): a shift is opened ON A TILL — the booth, or a money-taking
|
||||||
|
// module's own desk (Car Wash → "carwash"). One shift may be open PER TILL, each with
|
||||||
|
// its own operator, opening float, expected drawer and Z-report. Every money event
|
||||||
|
// names its till (`payload.till`; absent = booth, which is what every pre-till event
|
||||||
|
// is), and every fold in this file filters by it. Every public method takes the till,
|
||||||
|
// defaulting to the booth so the parking paths read as they always did.
|
||||||
// See wiki/concepts/shift.md.
|
// See wiki/concepts/shift.md.
|
||||||
|
|
||||||
export class ShiftAlreadyOpenError extends Error {
|
export class ShiftAlreadyOpenError extends Error {
|
||||||
/** The operator who currently holds the open shift (may be someone else). */
|
/** The operator who currently holds the open shift (may be someone else). */
|
||||||
readonly heldBy: string;
|
readonly heldBy: string;
|
||||||
constructor(operator: string, heldBy: string) {
|
constructor(operator: string, heldBy: string, till: TillId = BOOTH_TILL) {
|
||||||
super(
|
super(
|
||||||
heldBy === operator
|
heldBy === operator
|
||||||
? `operator ${operator} already has an open shift`
|
? `operator ${operator} already has an open ${till} shift`
|
||||||
: `another operator (${heldBy}) has an open shift; only one shift may be open at a time`,
|
: `another operator (${heldBy}) has an open ${till} shift; only one shift may be open per till`,
|
||||||
);
|
);
|
||||||
this.name = "ShiftAlreadyOpenError";
|
this.name = "ShiftAlreadyOpenError";
|
||||||
this.heldBy = heldBy;
|
this.heldBy = heldBy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class NoOpenShiftError extends Error {
|
export class NoOpenShiftError extends Error {
|
||||||
constructor(operator: string) {
|
constructor(operator: string, till: TillId = BOOTH_TILL) {
|
||||||
super(`operator ${operator} has no open shift`);
|
super(`operator ${operator} has no open ${till} shift`);
|
||||||
this.name = "NoOpenShiftError";
|
this.name = "NoOpenShiftError";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/** Thrown by the booth money path when NO shift is open site-wide — an operator
|
/** Thrown by a money path when NO shift is open on its till — an operator must open
|
||||||
* must open a shift before any payment/exit can be attributed to a shift. */
|
* a shift there before any payment/exit can be attributed to one. */
|
||||||
export class NoShiftOpenError extends Error {
|
export class NoShiftOpenError extends Error {
|
||||||
constructor() {
|
readonly till: TillId;
|
||||||
super("no shift is open — open a shift before processing tickets");
|
constructor(till: TillId = BOOTH_TILL) {
|
||||||
|
super(
|
||||||
|
till === BOOTH_TILL
|
||||||
|
? "no shift is open — open a shift before processing tickets"
|
||||||
|
: `no ${till} shift is open — open one before taking money there`,
|
||||||
|
);
|
||||||
this.name = "NoShiftOpenError";
|
this.name = "NoShiftOpenError";
|
||||||
|
this.till = till;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +57,8 @@ export class NoShiftOpenError extends Error {
|
|||||||
export interface ShiftSummary {
|
export interface ShiftSummary {
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
readonly index: number;
|
readonly index: number;
|
||||||
|
/** The till this shift reconciled (booth for every pre-till report). */
|
||||||
|
readonly till: TillId;
|
||||||
readonly operator: string;
|
readonly operator: string;
|
||||||
readonly startedAt: string;
|
readonly startedAt: string;
|
||||||
readonly endedAt: string;
|
readonly endedAt: string;
|
||||||
@@ -56,13 +71,23 @@ export interface ShiftSummary {
|
|||||||
readonly subscriptionSalesMinor: number;
|
readonly subscriptionSalesMinor: number;
|
||||||
readonly subscriptionWindowMinor: number;
|
readonly subscriptionWindowMinor: number;
|
||||||
readonly discountTotalMinor: number;
|
readonly discountTotalMinor: number;
|
||||||
|
readonly forgoneTotalMinor: number;
|
||||||
|
readonly forgoneCount: number;
|
||||||
|
readonly chargesByModuleMinor: ChargesByModule;
|
||||||
readonly openingFloatMinor: number;
|
readonly openingFloatMinor: number;
|
||||||
readonly cashAddedMinor: number;
|
readonly cashAddedMinor: number;
|
||||||
readonly cashRemovedMinor: number;
|
readonly cashRemovedMinor: number;
|
||||||
readonly expectedDrawerMinor: number;
|
readonly expectedDrawerMinor: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Module money folded into this till's payments as `chargeLines`, by owning module —
|
||||||
|
* a wash paid on the parking ticket lands here as `{ carwash: <minor> }`. Only modules
|
||||||
|
* that actually charged in the window appear. Cash+card already contain it; it is
|
||||||
|
* broken OUT of the ticket bucket so "Bileta" is parking money only. */
|
||||||
|
export type ChargesByModule = Partial<Record<ModuleId, number>>;
|
||||||
|
|
||||||
export interface ShiftReport {
|
export interface ShiftReport {
|
||||||
|
readonly till: TillId;
|
||||||
readonly operator: string;
|
readonly operator: string;
|
||||||
readonly startedAt: string;
|
readonly startedAt: string;
|
||||||
readonly endedAt: string;
|
readonly endedAt: string;
|
||||||
@@ -82,6 +107,12 @@ export interface ShiftReport {
|
|||||||
/** Merchant-validation DISCOUNT total given away in the window (leakage — the
|
/** Merchant-validation DISCOUNT total given away in the window (leakage — the
|
||||||
* cash/card figures above are already NET of it). See validation-discounts.md. */
|
* cash/card figures above are already NET of it). See validation-discounts.md. */
|
||||||
readonly discountTotalMinor: number;
|
readonly discountTotalMinor: number;
|
||||||
|
/** FORGIVEN exits (wiki/decisions/forgiven-exit.md): fees waived by an operator holding
|
||||||
|
* barrier:create, and how many. $0 payments — never inside cash/card; its own line. */
|
||||||
|
readonly forgoneTotalMinor: number;
|
||||||
|
readonly forgoneCount: number;
|
||||||
|
/** Module charges settled on this till's payments (a booth-paid wash), by module. */
|
||||||
|
readonly chargesByModuleMinor: ChargesByModule;
|
||||||
// --- Drawer (physical cash till; carries across shifts) ---
|
// --- Drawer (physical cash till; carries across shifts) ---
|
||||||
/** Cash in the drawer at shift start = prior shift's expected closing drawer. */
|
/** Cash in the drawer at shift start = prior shift's expected closing drawer. */
|
||||||
readonly openingFloatMinor: number;
|
readonly openingFloatMinor: number;
|
||||||
@@ -102,6 +133,8 @@ export type MovementStatus = "pending" | "authorized" | "denied";
|
|||||||
export interface DrawerMovement {
|
export interface DrawerMovement {
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
readonly type: "cash_in" | "cash_out";
|
readonly type: "cash_in" | "cash_out";
|
||||||
|
/** Which drawer the cash moved in/out of. */
|
||||||
|
readonly till: TillId;
|
||||||
/** Positive magnitude; direction is the `type`. */
|
/** Positive magnitude; direction is the `type`. */
|
||||||
readonly amountMinor: number;
|
readonly amountMinor: number;
|
||||||
readonly currency: string | null;
|
readonly currency: string | null;
|
||||||
@@ -122,6 +155,18 @@ export class InvalidCashMovementError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Printed (Albanian) name of a till on Z-reports and voucher slips. */
|
||||||
|
const TILL_PRINT_LABEL: Record<TillId, string> = { booth: "Kabina", carwash: "Lavazhi" };
|
||||||
|
/** The takings line a till's OWN money prints under (the booth sells tickets; the wash
|
||||||
|
* desk sells washes) and the label a module's charge gets when it rides another
|
||||||
|
* till's ticket ("Lavazh (në biletë)"). Printed slips are Albanian (i18n.md). */
|
||||||
|
const TILL_TAKINGS_LABEL: Record<TillId, string> = { booth: "Bileta", carwash: "Lavazh" };
|
||||||
|
const MODULE_PRINT_LABEL: Partial<Record<ModuleId, string>> = { carwash: "Lavazh", validation: "Validime" };
|
||||||
|
/** Which printer a till's slips (Z-report, vouchers) want. The wash desk falls back to
|
||||||
|
* the booth printer when it has none of its own (orderForRole); the booth never falls
|
||||||
|
* back to the desk. See wiki/concepts/printer-roles-failover.md. */
|
||||||
|
const TILL_PRINTER_ROLE: Record<TillId, PrinterRole> = { booth: "booth-receipt", carwash: "wash-desk" };
|
||||||
|
|
||||||
export class ShiftService {
|
export class ShiftService {
|
||||||
readonly #db: Db;
|
readonly #db: Db;
|
||||||
readonly #log: EventLog;
|
readonly #log: EventLog;
|
||||||
@@ -133,41 +178,42 @@ export class ShiftService {
|
|||||||
this.#logger = logger;
|
this.#logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Current physical drawer balance (cash payments + cash_movements, by time). For
|
/** Current physical drawer balance of a till (cash payments + cash_movements, by
|
||||||
* the UI to show "inherited / in the drawer now". */
|
* time). For the UI to show "inherited / in the drawer now". */
|
||||||
drawerBalance(): { balanceMinor: number; currency: string | null } {
|
drawerBalance(till: TillId = BOOTH_TILL): { balanceMinor: number; currency: string | null } {
|
||||||
return this.#drawerBalanceAt(new Date().toISOString());
|
return this.#drawerBalanceAt(new Date().toISOString(), till);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Is there an open shift for this operator? Returns the open `shift_open` row or null. */
|
/** The shift-boundary events (shift_open / shift_z_report) of ONE till, chain order. */
|
||||||
openShiftFor(operator: string) {
|
#shiftEvents(till: TillId) {
|
||||||
// Scan shift events for this operator; the shift is open if the most recent
|
return this.#db
|
||||||
// shift event for them is a `shift_open` (not yet closed by a z_report).
|
|
||||||
const rows = this.#db
|
|
||||||
.select()
|
.select()
|
||||||
.from(ledgerEvents)
|
.from(ledgerEvents)
|
||||||
.where(eq(ledgerEvents.identity, operator))
|
.where(inArray(ledgerEvents.type, ["shift_open", "shift_z_report"]))
|
||||||
.orderBy(ledgerEvents.index)
|
.orderBy(ledgerEvents.index)
|
||||||
.all()
|
.all()
|
||||||
.filter((r) => r.type === "shift_open" || r.type === "shift_z_report");
|
.filter((r) => tillOf(r.payload as LedgerPayload | null) === till);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Is there an open shift for this operator on this till? Returns the open
|
||||||
|
* `shift_open` row or null. */
|
||||||
|
openShiftFor(operator: string, till: TillId = BOOTH_TILL) {
|
||||||
|
// The shift is open if the operator's most recent shift event on the till is a
|
||||||
|
// `shift_open` (not yet closed by a z_report).
|
||||||
|
const rows = this.#shiftEvents(till).filter((r) => r.identity === operator);
|
||||||
const last = rows[rows.length - 1];
|
const last = rows[rows.length - 1];
|
||||||
return last && last.type === "shift_open" ? last : null;
|
return last && last.type === "shift_open" ? last : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The SINGLE site-wide open shift, or null. A shift is a site-wide accountability
|
* The SINGLE open shift of a till, or null. A shift is the till's accountability
|
||||||
* period: at most ONE may be open at a time (so booth takings are unambiguously
|
* period: at most ONE may be open per till at a time (so its takings are
|
||||||
* attributed to one operator). It's open iff the most recent shift event on the
|
* unambiguously attributed to one operator). It's open iff the till's most recent
|
||||||
* whole chain is a `shift_open` (the matching `shift_z_report` hasn't been
|
* shift event is a `shift_open` (the matching `shift_z_report` hasn't been appended
|
||||||
* appended yet). Returns that row so callers can read its operator/startedAt.
|
* yet). Returns that row so callers can read its operator/startedAt.
|
||||||
*/
|
*/
|
||||||
currentOpenShift() {
|
currentOpenShift(till: TillId = BOOTH_TILL) {
|
||||||
const rows = this.#db
|
const rows = this.#shiftEvents(till);
|
||||||
.select()
|
|
||||||
.from(ledgerEvents)
|
|
||||||
.orderBy(ledgerEvents.index)
|
|
||||||
.all()
|
|
||||||
.filter((r) => r.type === "shift_open" || r.type === "shift_z_report");
|
|
||||||
const last = rows[rows.length - 1];
|
const last = rows[rows.length - 1];
|
||||||
return last && last.type === "shift_open" ? last : null;
|
return last && last.type === "shift_open" ? last : null;
|
||||||
}
|
}
|
||||||
@@ -186,24 +232,25 @@ export class ShiftService {
|
|||||||
* distinct + sorted — feeds the admin filter dropdown so it can only ever ask
|
* distinct + sorted — feeds the admin filter dropdown so it can only ever ask
|
||||||
* for an operator that exists (the filter is an exact username match).
|
* for an operator that exists (the filter is an exact username match).
|
||||||
*/
|
*/
|
||||||
listOperators(): string[] {
|
listOperators(till?: TillId): string[] {
|
||||||
const rows = this.#db
|
const rows = this.#db
|
||||||
.select()
|
.select()
|
||||||
.from(ledgerEvents)
|
.from(ledgerEvents)
|
||||||
.where(eq(ledgerEvents.type, "shift_z_report"))
|
.where(inArray(ledgerEvents.type, ["shift_z_report", "shift_open"]))
|
||||||
.all();
|
.all()
|
||||||
|
.filter((r) => till == null || tillOf(r.payload as LedgerPayload | null) === till);
|
||||||
|
// Every operator with a closed report, plus the holder of each open shift (an
|
||||||
|
// open shift is the last shift_open on its till — but any shift_open's operator
|
||||||
|
// has or had a shift, which is all the dropdown needs).
|
||||||
const names = new Set<string>();
|
const names = new Set<string>();
|
||||||
for (const r of rows) {
|
for (const r of rows) {
|
||||||
const op = ((r.payload ?? {}) as { operator?: string }).operator ?? r.identity;
|
const op = ((r.payload ?? {}) as { operator?: string }).operator ?? r.identity;
|
||||||
if (op) names.add(op);
|
if (op) names.add(op);
|
||||||
}
|
}
|
||||||
const open = this.currentOpenShift();
|
|
||||||
const openOp = open ? (((open.payload ?? {}) as { operator?: string }).operator ?? open.identity) : null;
|
|
||||||
if (openOp) names.add(openOp);
|
|
||||||
return [...names].sort((a, b) => a.localeCompare(b));
|
return [...names].sort((a, b) => a.localeCompare(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
listShifts(opts: { operator?: string; from?: string; to?: string } = {}): ShiftSummary[] {
|
listShifts(opts: { operator?: string; from?: string; to?: string; till?: TillId } = {}): ShiftSummary[] {
|
||||||
const rows = this.#db
|
const rows = this.#db
|
||||||
.select()
|
.select()
|
||||||
.from(ledgerEvents)
|
.from(ledgerEvents)
|
||||||
@@ -225,6 +272,9 @@ export class ShiftService {
|
|||||||
subscriptionSalesMinor?: number;
|
subscriptionSalesMinor?: number;
|
||||||
subscriptionWindowMinor?: number;
|
subscriptionWindowMinor?: number;
|
||||||
discountTotalMinor?: number;
|
discountTotalMinor?: number;
|
||||||
|
forgoneTotalMinor?: number;
|
||||||
|
forgoneCount?: number;
|
||||||
|
chargesByModuleMinor?: ChargesByModule;
|
||||||
openingFloatMinor?: number;
|
openingFloatMinor?: number;
|
||||||
cashAddedMinor?: number;
|
cashAddedMinor?: number;
|
||||||
cashRemovedMinor?: number;
|
cashRemovedMinor?: number;
|
||||||
@@ -232,12 +282,15 @@ export class ShiftService {
|
|||||||
};
|
};
|
||||||
const operator = pl.operator ?? r.identity ?? "?";
|
const operator = pl.operator ?? r.identity ?? "?";
|
||||||
const startedAt = pl.startedAt ?? r.occurredAt;
|
const startedAt = pl.startedAt ?? r.occurredAt;
|
||||||
|
const till = tillOf(pl);
|
||||||
|
if (opts.till && till !== opts.till) continue;
|
||||||
if (opts.operator && operator !== opts.operator) continue;
|
if (opts.operator && operator !== opts.operator) continue;
|
||||||
if (opts.from && startedAt < opts.from) continue;
|
if (opts.from && startedAt < opts.from) continue;
|
||||||
if (opts.to && startedAt > opts.to) continue;
|
if (opts.to && startedAt > opts.to) continue;
|
||||||
out.push({
|
out.push({
|
||||||
id: r.id,
|
id: r.id,
|
||||||
index: r.index,
|
index: r.index,
|
||||||
|
till,
|
||||||
operator,
|
operator,
|
||||||
startedAt,
|
startedAt,
|
||||||
endedAt: pl.endedAt ?? r.occurredAt,
|
endedAt: pl.endedAt ?? r.occurredAt,
|
||||||
@@ -257,6 +310,11 @@ export class ShiftService {
|
|||||||
(pl.cashTotalMinor ?? 0) + (pl.cardTotalMinor ?? 0) - (pl.subscriptionTotalMinor ?? 0),
|
(pl.cashTotalMinor ?? 0) + (pl.cardTotalMinor ?? 0) - (pl.subscriptionTotalMinor ?? 0),
|
||||||
// Merchant-validation leakage (added 2026-07-13). Old reports lack it → 0.
|
// Merchant-validation leakage (added 2026-07-13). Old reports lack it → 0.
|
||||||
discountTotalMinor: pl.discountTotalMinor ?? 0,
|
discountTotalMinor: pl.discountTotalMinor ?? 0,
|
||||||
|
// Forgiven exits (added 2026-10-08). Old reports lack it → 0.
|
||||||
|
forgoneTotalMinor: pl.forgoneTotalMinor ?? 0,
|
||||||
|
forgoneCount: pl.forgoneCount ?? 0,
|
||||||
|
// Module charges on the ticket (added 2026-09-06). Old reports lack it → none.
|
||||||
|
chargesByModuleMinor: pl.chargesByModuleMinor ?? {},
|
||||||
openingFloatMinor: pl.openingFloatMinor ?? 0,
|
openingFloatMinor: pl.openingFloatMinor ?? 0,
|
||||||
cashAddedMinor: pl.cashAddedMinor ?? 0,
|
cashAddedMinor: pl.cashAddedMinor ?? 0,
|
||||||
cashRemovedMinor: pl.cashRemovedMinor ?? 0,
|
cashRemovedMinor: pl.cashRemovedMinor ?? 0,
|
||||||
@@ -267,10 +325,10 @@ export class ShiftService {
|
|||||||
return out.reverse();
|
return out.reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Require an open shift for the booth money path; returns it or throws. */
|
/** Require an open shift on a till for its money path; returns it or throws. */
|
||||||
requireOpenShift() {
|
requireOpenShift(till: TillId = BOOTH_TILL) {
|
||||||
const open = this.currentOpenShift();
|
const open = this.currentOpenShift(till);
|
||||||
if (!open) throw new NoShiftOpenError();
|
if (!open) throw new NoShiftOpenError(till);
|
||||||
return open;
|
return open;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,9 +341,10 @@ export class ShiftService {
|
|||||||
* - `cash_out` (Mandat Pagese): − amountMinor (positive magnitude)
|
* - `cash_out` (Mandat Pagese): − amountMinor (positive magnitude)
|
||||||
* - `cash_movement` (legacy, pre-2026-06-20): a SIGNED amountMinor (+ load / −
|
* - `cash_movement` (legacy, pre-2026-06-20): a SIGNED amountMinor (+ load / −
|
||||||
* removal) — historical chain events that still fold in unchanged.
|
* removal) — historical chain events that still fold in unchanged.
|
||||||
* This is what carries across shifts.
|
* This is what carries across shifts. ONE till: every money event is filtered by
|
||||||
|
* `tillOf(payload)` (absent = booth).
|
||||||
*/
|
*/
|
||||||
#drawerBalanceAt(at: string): { balanceMinor: number; currency: string | null } {
|
#drawerBalanceAt(at: string, till: TillId): { balanceMinor: number; currency: string | null } {
|
||||||
const rows = this.#db
|
const rows = this.#db
|
||||||
.select()
|
.select()
|
||||||
.from(ledgerEvents)
|
.from(ledgerEvents)
|
||||||
@@ -295,16 +354,20 @@ export class ShiftService {
|
|||||||
(r) =>
|
(r) =>
|
||||||
r.occurredAt <= at &&
|
r.occurredAt <= at &&
|
||||||
(r.type === "payment" ||
|
(r.type === "payment" ||
|
||||||
|
// Car Wash module: money taken at the bay (cash adds to the drawer, card
|
||||||
|
// never does — same tender rule as a parking payment).
|
||||||
|
r.type === "carwash_payment" ||
|
||||||
r.type === "cash_in" ||
|
r.type === "cash_in" ||
|
||||||
r.type === "cash_out" ||
|
r.type === "cash_out" ||
|
||||||
r.type === "cash_movement"),
|
r.type === "cash_movement") &&
|
||||||
|
tillOf(r.payload as LedgerPayload | null) === till,
|
||||||
);
|
);
|
||||||
let balanceMinor = 0;
|
let balanceMinor = 0;
|
||||||
let currency: string | null = null;
|
let currency: string | null = null;
|
||||||
for (const r of rows) {
|
for (const r of rows) {
|
||||||
const pl = (r.payload ?? {}) as LedgerPayload;
|
const pl = (r.payload ?? {}) as LedgerPayload;
|
||||||
const amt = typeof pl.amountMinor === "number" ? pl.amountMinor : 0;
|
const amt = typeof pl.amountMinor === "number" ? pl.amountMinor : 0;
|
||||||
if (r.type === "payment") {
|
if (r.type === "payment" || r.type === "carwash_payment") {
|
||||||
// Only CASH enters the till; card settles to the bank.
|
// Only CASH enters the till; card settles to the bank.
|
||||||
if (pl.tender !== "card") balanceMinor += amt;
|
if (pl.tender !== "card") balanceMinor += amt;
|
||||||
} else if (r.type === "cash_in") {
|
} else if (r.type === "cash_in") {
|
||||||
@@ -347,8 +410,11 @@ export class ShiftService {
|
|||||||
amountMinor: number;
|
amountMinor: number;
|
||||||
reason: string;
|
reason: string;
|
||||||
currency?: string;
|
currency?: string;
|
||||||
}): Promise<{ type: "cash_in" | "cash_out"; amountMinor: number; voucherNo: string; balanceMinor: number; printed: boolean }> {
|
/** Which drawer the cash moved in/out of (default: the booth). */
|
||||||
|
till?: TillId;
|
||||||
|
}): Promise<{ type: "cash_in" | "cash_out"; till: TillId; amountMinor: number; voucherNo: string; balanceMinor: number; printed: boolean }> {
|
||||||
const { type, operator, reason } = args;
|
const { type, operator, reason } = args;
|
||||||
|
const till = args.till ?? BOOTH_TILL;
|
||||||
if (!Number.isInteger(args.amountMinor) || args.amountMinor <= 0) {
|
if (!Number.isInteger(args.amountMinor) || args.amountMinor <= 0) {
|
||||||
throw new InvalidCashMovementError("amountMinor must be a positive integer (minor units)");
|
throw new InvalidCashMovementError("amountMinor must be a positive integer (minor units)");
|
||||||
}
|
}
|
||||||
@@ -365,15 +431,16 @@ export class ShiftService {
|
|||||||
...(args.currency ? { currency: args.currency } : {}),
|
...(args.currency ? { currency: args.currency } : {}),
|
||||||
operator,
|
operator,
|
||||||
voucherNo,
|
voucherNo,
|
||||||
|
till,
|
||||||
},
|
},
|
||||||
occurredAt: now,
|
occurredAt: now,
|
||||||
});
|
});
|
||||||
const { balanceMinor, currency } = this.#drawerBalanceAt(now);
|
const { balanceMinor, currency } = this.#drawerBalanceAt(now, till);
|
||||||
const printed = await this.#printVoucher({ type, voucherNo, amountMinor, reason, operator, currency, at: now });
|
const printed = await this.#printVoucher({ type, voucherNo, amountMinor, reason, operator, currency, at: now, till });
|
||||||
this.#logger.info(
|
this.#logger.info(
|
||||||
`${type} ${voucherNo} ${amountMinor} by ${operator} (${reason || "no reason"}) → drawer ${balanceMinor}`,
|
`${type} ${voucherNo} ${amountMinor} by ${operator} on ${till} (${reason || "no reason"}) → drawer ${balanceMinor}`,
|
||||||
);
|
);
|
||||||
return { type, amountMinor, voucherNo, balanceMinor, printed };
|
return { type, till, amountMinor, voucherNo, balanceMinor, printed };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -431,7 +498,7 @@ export class ShiftService {
|
|||||||
* review queue. `operator` (optional) scopes to one operator's movements (an operator
|
* review queue. `operator` (optional) scopes to one operator's movements (an operator
|
||||||
* sees only their own; a reviewer sees all). See wiki/concepts/shift.md.
|
* sees only their own; a reviewer sees all). See wiki/concepts/shift.md.
|
||||||
*/
|
*/
|
||||||
movementsWithStatus(filter?: { operator?: string; status?: MovementStatus }): DrawerMovement[] {
|
movementsWithStatus(filter?: { operator?: string; status?: MovementStatus; till?: TillId }): DrawerMovement[] {
|
||||||
const rows = this.#db.select().from(ledgerEvents).orderBy(ledgerEvents.index).all();
|
const rows = this.#db.select().from(ledgerEvents).orderBy(ledgerEvents.index).all();
|
||||||
// Latest review decision per movement id.
|
// Latest review decision per movement id.
|
||||||
const reviewByRef = new Map<string, { decision: "authorize" | "deny"; reviewedBy: string; note?: string; at: string }>();
|
const reviewByRef = new Map<string, { decision: "authorize" | "deny"; reviewedBy: string; note?: string; at: string }>();
|
||||||
@@ -452,12 +519,15 @@ export class ShiftService {
|
|||||||
const pl = (r.payload ?? {}) as LedgerPayload;
|
const pl = (r.payload ?? {}) as LedgerPayload;
|
||||||
const operator = (typeof pl.operator === "string" ? pl.operator : null) ?? r.identity ?? "";
|
const operator = (typeof pl.operator === "string" ? pl.operator : null) ?? r.identity ?? "";
|
||||||
if (filter?.operator && operator !== filter.operator) continue;
|
if (filter?.operator && operator !== filter.operator) continue;
|
||||||
|
const till = tillOf(pl);
|
||||||
|
if (filter?.till && till !== filter.till) continue;
|
||||||
const review = reviewByRef.get(r.id);
|
const review = reviewByRef.get(r.id);
|
||||||
const status: MovementStatus = review ? (review.decision === "authorize" ? "authorized" : "denied") : "pending";
|
const status: MovementStatus = review ? (review.decision === "authorize" ? "authorized" : "denied") : "pending";
|
||||||
if (filter?.status && status !== filter.status) continue;
|
if (filter?.status && status !== filter.status) continue;
|
||||||
out.push({
|
out.push({
|
||||||
id: r.id,
|
id: r.id,
|
||||||
type: r.type,
|
type: r.type,
|
||||||
|
till,
|
||||||
amountMinor: typeof pl.amountMinor === "number" ? Math.abs(pl.amountMinor) : 0,
|
amountMinor: typeof pl.amountMinor === "number" ? Math.abs(pl.amountMinor) : 0,
|
||||||
currency: pl.currency ?? null,
|
currency: pl.currency ?? null,
|
||||||
reason: pl.reason ?? null,
|
reason: pl.reason ?? null,
|
||||||
@@ -474,27 +544,28 @@ export class ShiftService {
|
|||||||
return out.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : 0));
|
return out.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Open a shift for the operator (explicit start). The opening float is auto-
|
/** Open a shift for the operator on a till (explicit start). The opening float is
|
||||||
* inherited from the chain = the drawer balance at the start instant. */
|
* auto-inherited from the chain = that till's drawer balance at the start instant. */
|
||||||
async open(operator: string): Promise<{ startedAt: string; openingFloatMinor: number }> {
|
async open(operator: string, till: TillId = BOOTH_TILL): Promise<{ startedAt: string; till: TillId; openingFloatMinor: number }> {
|
||||||
// Site-wide single-open invariant: refuse if ANY shift is open — whether this
|
// Single-open-per-till invariant: refuse if a shift is open ON THIS TILL — whether
|
||||||
// operator's own (double-open) or another operator's (handover not done). Only
|
// this operator's own (double-open) or another operator's (handover not done).
|
||||||
// one accountability period at a time.
|
// One accountability period per drawer at a time. (Another till's shift is
|
||||||
const current = this.currentOpenShift();
|
// independent: the booth and the wash desk run side by side.)
|
||||||
if (current) throw new ShiftAlreadyOpenError(operator, current.identity ?? operator);
|
const current = this.currentOpenShift(till);
|
||||||
|
if (current) throw new ShiftAlreadyOpenError(operator, current.identity ?? operator, till);
|
||||||
const startedAt = new Date().toISOString();
|
const startedAt = new Date().toISOString();
|
||||||
const { balanceMinor: openingFloatMinor } = this.#drawerBalanceAt(startedAt);
|
const { balanceMinor: openingFloatMinor } = this.#drawerBalanceAt(startedAt, till);
|
||||||
await this.#log.append({
|
await this.#log.append({
|
||||||
type: "shift_open",
|
type: "shift_open",
|
||||||
source: "manual",
|
source: "manual",
|
||||||
identity: operator, // the shift's operator; `identity` keys the shift to them
|
identity: operator, // the shift's operator; `identity` keys the shift to them
|
||||||
// Record the inherited opening float on the shift_open so it's reproducible
|
// Record the inherited opening float on the shift_open so it's reproducible
|
||||||
// and the next operator's handover figure is fixed in the chain.
|
// and the next operator's handover figure is fixed in the chain.
|
||||||
payload: { operator, openingFloatMinor },
|
payload: { operator, openingFloatMinor, till },
|
||||||
occurredAt: startedAt,
|
occurredAt: startedAt,
|
||||||
});
|
});
|
||||||
this.#logger.info(`shift opened for ${operator} (opening float ${openingFloatMinor})`);
|
this.#logger.info(`${till} shift opened for ${operator} (opening float ${openingFloatMinor})`);
|
||||||
return { startedAt, openingFloatMinor };
|
return { startedAt, till, openingFloatMinor };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -510,15 +581,22 @@ export class ShiftService {
|
|||||||
): Omit<ShiftReport, "printed"> {
|
): Omit<ShiftReport, "printed"> {
|
||||||
const operator = open.identity ?? "?";
|
const operator = open.identity ?? "?";
|
||||||
const startedAt = open.occurredAt;
|
const startedAt = open.occurredAt;
|
||||||
|
const till = tillOf(open.payload as LedgerPayload | null);
|
||||||
|
|
||||||
// All payments taken in [startedAt, asOf], summed by tender. Payment time =
|
// All payments taken ON THIS TILL in [startedAt, asOf], summed by tender. Payment
|
||||||
// the operator who handled the money (decision: sum by payment time).
|
// time = the operator who handled the money (decision: sum by payment time).
|
||||||
const payments = this.#db
|
const payments = this.#db
|
||||||
.select()
|
.select()
|
||||||
.from(ledgerEvents)
|
.from(ledgerEvents)
|
||||||
.where(eq(ledgerEvents.type, "payment"))
|
// Parking payments + Car Wash bay payments (a wash paid at the BOOTH is inside the
|
||||||
|
// parking payment's amount already, as chargeLines). Both fold into the cash/card
|
||||||
|
// tender totals so the expected drawer is right; the booth-paid wash is then
|
||||||
|
// broken OUT of the ticket bucket into chargesByModuleMinor (see below).
|
||||||
|
.where(inArray(ledgerEvents.type, ["payment", "carwash_payment"]))
|
||||||
.all()
|
.all()
|
||||||
.filter((r) => r.occurredAt >= startedAt && r.occurredAt <= asOf);
|
.filter(
|
||||||
|
(r) => r.occurredAt >= startedAt && r.occurredAt <= asOf && tillOf(r.payload as LedgerPayload | null) === till,
|
||||||
|
);
|
||||||
|
|
||||||
let cashTotalMinor = 0;
|
let cashTotalMinor = 0;
|
||||||
let cardTotalMinor = 0;
|
let cardTotalMinor = 0;
|
||||||
@@ -532,11 +610,20 @@ export class ShiftService {
|
|||||||
// Merchant-validation leakage: Σ discountMinor across the window's payments. The
|
// Merchant-validation leakage: Σ discountMinor across the window's payments. The
|
||||||
// tender totals are already NET; this is the "given away" figure beside them.
|
// tender totals are already NET; this is the "given away" figure beside them.
|
||||||
let discountTotalMinor = 0;
|
let discountTotalMinor = 0;
|
||||||
|
// Forgiven exits: $0 payments whose `forgoneMinor` is the fee waived. Not money —
|
||||||
|
// the tender sums above add 0 for them; this is the "let out for free" figure.
|
||||||
|
let forgoneTotalMinor = 0;
|
||||||
|
let forgoneCount = 0;
|
||||||
|
// Module charges folded into this till's payments (chargeLines on a booth payment),
|
||||||
|
// summed by owning module. Part of cash/card; NOT ticket money.
|
||||||
|
const chargesByModuleMinor: ChargesByModule = {};
|
||||||
|
let chargesTotalMinor = 0;
|
||||||
let currency: string | null = null;
|
let currency: string | null = null;
|
||||||
for (const p of payments) {
|
for (const p of payments) {
|
||||||
const pl = (p.payload ?? {}) as LedgerPayload & {
|
const pl = (p.payload ?? {}) as LedgerPayload & {
|
||||||
subscriptionSale?: boolean;
|
subscriptionSale?: boolean;
|
||||||
subscriptionWindowCharge?: boolean;
|
subscriptionWindowCharge?: boolean;
|
||||||
|
chargeLines?: ChargeLine[];
|
||||||
};
|
};
|
||||||
const amt = typeof pl.amountMinor === "number" ? pl.amountMinor : 0;
|
const amt = typeof pl.amountMinor === "number" ? pl.amountMinor : 0;
|
||||||
if (pl.tender === "card") cardTotalMinor += amt;
|
if (pl.tender === "card") cardTotalMinor += amt;
|
||||||
@@ -545,10 +632,21 @@ export class ShiftService {
|
|||||||
else if (pl.subscriptionWindowCharge === true) subscriptionWindowMinor += amt;
|
else if (pl.subscriptionWindowCharge === true) subscriptionWindowMinor += amt;
|
||||||
// (else → transient ticket; derived below as total − subscription)
|
// (else → transient ticket; derived below as total − subscription)
|
||||||
if (typeof pl.discountMinor === "number") discountTotalMinor += pl.discountMinor;
|
if (typeof pl.discountMinor === "number") discountTotalMinor += pl.discountMinor;
|
||||||
|
if (typeof pl.forgoneMinor === "number") {
|
||||||
|
forgoneTotalMinor += pl.forgoneMinor;
|
||||||
|
forgoneCount += 1;
|
||||||
|
}
|
||||||
|
for (const l of pl.chargeLines ?? []) {
|
||||||
|
if (typeof l.amountMinor !== "number" || !l.module) continue;
|
||||||
|
chargesByModuleMinor[l.module] = (chargesByModuleMinor[l.module] ?? 0) + l.amountMinor;
|
||||||
|
chargesTotalMinor += l.amountMinor;
|
||||||
|
}
|
||||||
if (pl.currency) currency = pl.currency;
|
if (pl.currency) currency = pl.currency;
|
||||||
}
|
}
|
||||||
const subscriptionTotalMinor = subscriptionSalesMinor + subscriptionWindowMinor;
|
const subscriptionTotalMinor = subscriptionSalesMinor + subscriptionWindowMinor;
|
||||||
const ticketTotalMinor = cashTotalMinor + cardTotalMinor - subscriptionTotalMinor;
|
// Ticket = what is left once subscriber money and module charges are taken out:
|
||||||
|
// ticket + subscriptions + Σcharges = cash + card, always.
|
||||||
|
const ticketTotalMinor = cashTotalMinor + cardTotalMinor - subscriptionTotalMinor - chargesTotalMinor;
|
||||||
|
|
||||||
// --- Drawer figures ---
|
// --- Drawer figures ---
|
||||||
// Opening float was fixed on shift_open (inherited from the chain at start);
|
// Opening float was fixed on shift_open (inherited from the chain at start);
|
||||||
@@ -557,7 +655,7 @@ export class ShiftService {
|
|||||||
const openingFloatMinor =
|
const openingFloatMinor =
|
||||||
typeof openPl.openingFloatMinor === "number"
|
typeof openPl.openingFloatMinor === "number"
|
||||||
? openPl.openingFloatMinor
|
? openPl.openingFloatMinor
|
||||||
: this.#drawerBalanceAt(startedAt).balanceMinor;
|
: this.#drawerBalanceAt(startedAt, till).balanceMinor;
|
||||||
|
|
||||||
// Drawer movements within the window, split into added (+) and removed (−).
|
// Drawer movements within the window, split into added (+) and removed (−).
|
||||||
// Three side-by-side types: cash_in (+), cash_out (−), and the legacy signed-±
|
// Three side-by-side types: cash_in (+), cash_out (−), and the legacy signed-±
|
||||||
@@ -570,7 +668,8 @@ export class ShiftService {
|
|||||||
(r) =>
|
(r) =>
|
||||||
(r.type === "cash_in" || r.type === "cash_out" || r.type === "cash_movement") &&
|
(r.type === "cash_in" || r.type === "cash_out" || r.type === "cash_movement") &&
|
||||||
r.occurredAt >= startedAt &&
|
r.occurredAt >= startedAt &&
|
||||||
r.occurredAt <= asOf,
|
r.occurredAt <= asOf &&
|
||||||
|
tillOf(r.payload as LedgerPayload | null) === till,
|
||||||
);
|
);
|
||||||
let cashAddedMinor = 0;
|
let cashAddedMinor = 0;
|
||||||
let cashRemovedMinor = 0;
|
let cashRemovedMinor = 0;
|
||||||
@@ -589,6 +688,7 @@ export class ShiftService {
|
|||||||
const expectedDrawerMinor = openingFloatMinor + cashTotalMinor + cashAddedMinor - cashRemovedMinor;
|
const expectedDrawerMinor = openingFloatMinor + cashTotalMinor + cashAddedMinor - cashRemovedMinor;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
till,
|
||||||
operator,
|
operator,
|
||||||
startedAt,
|
startedAt,
|
||||||
endedAt: asOf,
|
endedAt: asOf,
|
||||||
@@ -601,6 +701,9 @@ export class ShiftService {
|
|||||||
subscriptionSalesMinor,
|
subscriptionSalesMinor,
|
||||||
subscriptionWindowMinor,
|
subscriptionWindowMinor,
|
||||||
discountTotalMinor,
|
discountTotalMinor,
|
||||||
|
forgoneTotalMinor,
|
||||||
|
forgoneCount,
|
||||||
|
chargesByModuleMinor,
|
||||||
openingFloatMinor,
|
openingFloatMinor,
|
||||||
cashAddedMinor,
|
cashAddedMinor,
|
||||||
cashRemovedMinor,
|
cashRemovedMinor,
|
||||||
@@ -615,17 +718,18 @@ export class ShiftService {
|
|||||||
* projection the Z-report prints, so the operator sees exactly what their close
|
* projection the Z-report prints, so the operator sees exactly what their close
|
||||||
* will show. See wiki/concepts/shift.md.
|
* will show. See wiki/concepts/shift.md.
|
||||||
*/
|
*/
|
||||||
currentReport(): (Omit<ShiftReport, "printed"> & { asOf: string }) | null {
|
currentReport(till: TillId = BOOTH_TILL): (Omit<ShiftReport, "printed"> & { asOf: string }) | null {
|
||||||
const open = this.currentOpenShift();
|
const open = this.currentOpenShift(till);
|
||||||
if (!open) return null;
|
if (!open) return null;
|
||||||
const asOf = new Date().toISOString();
|
const asOf = new Date().toISOString();
|
||||||
return { ...this.#summariseWindow(open, asOf), asOf };
|
return { ...this.#summariseWindow(open, asOf), asOf };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Close the operator's open shift: sum payments in the window, sign + print the Z-report. */
|
/** Close the operator's open shift on a till: sum its payments in the window, sign +
|
||||||
async close(operator: string): Promise<ShiftReport> {
|
* print the Z-report. */
|
||||||
const open = this.openShiftFor(operator);
|
async close(operator: string, till: TillId = BOOTH_TILL): Promise<ShiftReport> {
|
||||||
if (!open) throw new NoOpenShiftError(operator);
|
const open = this.openShiftFor(operator, till);
|
||||||
|
if (!open) throw new NoOpenShiftError(operator, till);
|
||||||
const endedAt = new Date().toISOString();
|
const endedAt = new Date().toISOString();
|
||||||
|
|
||||||
const report = this.#summariseWindow(open, endedAt);
|
const report = this.#summariseWindow(open, endedAt);
|
||||||
@@ -640,6 +744,9 @@ export class ShiftService {
|
|||||||
subscriptionSalesMinor,
|
subscriptionSalesMinor,
|
||||||
subscriptionWindowMinor,
|
subscriptionWindowMinor,
|
||||||
discountTotalMinor,
|
discountTotalMinor,
|
||||||
|
forgoneTotalMinor,
|
||||||
|
forgoneCount,
|
||||||
|
chargesByModuleMinor,
|
||||||
openingFloatMinor,
|
openingFloatMinor,
|
||||||
cashAddedMinor,
|
cashAddedMinor,
|
||||||
cashRemovedMinor,
|
cashRemovedMinor,
|
||||||
@@ -652,6 +759,7 @@ export class ShiftService {
|
|||||||
identity: operator,
|
identity: operator,
|
||||||
payload: {
|
payload: {
|
||||||
operator,
|
operator,
|
||||||
|
till,
|
||||||
startedAt,
|
startedAt,
|
||||||
endedAt,
|
endedAt,
|
||||||
cashTotalMinor,
|
cashTotalMinor,
|
||||||
@@ -663,6 +771,10 @@ export class ShiftService {
|
|||||||
subscriptionSalesMinor,
|
subscriptionSalesMinor,
|
||||||
subscriptionWindowMinor,
|
subscriptionWindowMinor,
|
||||||
discountTotalMinor,
|
discountTotalMinor,
|
||||||
|
// Forgiven exits — only when any happened (older payloads stay identical).
|
||||||
|
...(forgoneCount > 0 ? { forgoneTotalMinor, forgoneCount } : {}),
|
||||||
|
// Only when a module charged in the window (older slips/payloads stay identical).
|
||||||
|
...(Object.keys(chargesByModuleMinor).length ? { chargesByModuleMinor } : {}),
|
||||||
openingFloatMinor,
|
openingFloatMinor,
|
||||||
cashAddedMinor,
|
cashAddedMinor,
|
||||||
cashRemovedMinor,
|
cashRemovedMinor,
|
||||||
@@ -673,25 +785,23 @@ export class ShiftService {
|
|||||||
const printed = await this.#printZReport(report);
|
const printed = await this.#printZReport(report);
|
||||||
|
|
||||||
this.#logger.info(
|
this.#logger.info(
|
||||||
`shift closed for ${operator}: cash ${cashTotalMinor} card ${cardTotalMinor} (${paymentCount} payments); ` +
|
`${till} shift closed for ${operator}: cash ${cashTotalMinor} card ${cardTotalMinor} (${paymentCount} payments); ` +
|
||||||
`drawer open ${openingFloatMinor} +${cashAddedMinor} −${cashRemovedMinor} → expected ${expectedDrawerMinor}`,
|
`drawer open ${openingFloatMinor} +${cashAddedMinor} −${cashRemovedMinor} → expected ${expectedDrawerMinor}`,
|
||||||
);
|
);
|
||||||
return { ...report, printed };
|
return { ...report, printed };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Print the Z-report on a booth-receipt printer (best-effort; the signed event
|
/** Print the Z-report on the till's printer (best-effort; the signed event is the
|
||||||
* is the record — a failed print doesn't undo the close). */
|
* record — a failed print doesn't undo the close). */
|
||||||
async #printZReport(r: Omit<ShiftReport, "printed">): Promise<boolean> {
|
async #printZReport(r: Omit<ShiftReport, "printed">): Promise<boolean> {
|
||||||
const printer = await this.#boothPrinter();
|
|
||||||
if (!printer) {
|
|
||||||
this.#logger.warn(`no booth-receipt printer — Z-report for ${r.operator} not printed (event is recorded)`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const cur = r.currency ?? "";
|
const cur = r.currency ?? "";
|
||||||
const money = (m: number) => (m / 100).toFixed(2);
|
const money = (m: number) => (m / 100).toFixed(2);
|
||||||
// Customer/operator-facing print is Albanian (see i18n.md — printed slips are not
|
// Customer/operator-facing print is Albanian (see i18n.md — printed slips are not
|
||||||
// governed by the UI language), with human dates "19 Qershor 2026 10:48:25".
|
// governed by the UI language), with human dates "19 Qershor 2026 10:48:25".
|
||||||
const lines = [
|
const lines = [
|
||||||
|
// Which drawer this report reconciles — only printed off the booth, so booth
|
||||||
|
// slips stay byte-identical to before tills existed.
|
||||||
|
...(r.till !== BOOTH_TILL ? [`Arka: ${TILL_PRINT_LABEL[r.till]}`] : []),
|
||||||
`Operatori: ${r.operator}`,
|
`Operatori: ${r.operator}`,
|
||||||
`Nga: ${zStamp(r.startedAt)}`,
|
`Nga: ${zStamp(r.startedAt)}`,
|
||||||
`Deri: ${zStamp(r.endedAt)}`,
|
`Deri: ${zStamp(r.endedAt)}`,
|
||||||
@@ -701,14 +811,28 @@ export class ShiftService {
|
|||||||
`Kartë: ${money(r.cardTotalMinor)} ${cur}`,
|
`Kartë: ${money(r.cardTotalMinor)} ${cur}`,
|
||||||
"",
|
"",
|
||||||
"-- Arkëtime sipas burimit --",
|
"-- Arkëtime sipas burimit --",
|
||||||
|
// The booth prints its three classic lines (byte-identical to before tills); a
|
||||||
|
// module's till prints its own takings under its own name — it sells no tickets
|
||||||
|
// and no subscriptions.
|
||||||
|
...(r.till === BOOTH_TILL
|
||||||
|
? [
|
||||||
`Bileta: ${money(r.ticketTotalMinor)} ${cur}`,
|
`Bileta: ${money(r.ticketTotalMinor)} ${cur}`,
|
||||||
// Abonime is the subscription TOTAL; only the out-of-window part is broken out.
|
// Abonime is the subscription TOTAL; only the out-of-window part is broken out.
|
||||||
// (subscriptionSalesMinor stays in the signed payload — it's just not printed.)
|
// (subscriptionSalesMinor stays in the signed payload — it's just not printed.)
|
||||||
`Abonime: ${money(r.subscriptionTotalMinor)} ${cur}`,
|
`Abonime: ${money(r.subscriptionTotalMinor)} ${cur}`,
|
||||||
`Jashtë orarit: ${money(r.subscriptionWindowMinor)} ${cur}`,
|
`Jashtë orarit: ${money(r.subscriptionWindowMinor)} ${cur}`,
|
||||||
|
]
|
||||||
|
: [`${TILL_TAKINGS_LABEL[r.till]}: ${money(r.ticketTotalMinor)} ${cur}`]),
|
||||||
|
// Module money that rode this till's tickets (a booth-paid wash) — its own line,
|
||||||
|
// only when any was taken, so the operator sees parking and wash money apart.
|
||||||
|
...Object.entries(r.chargesByModuleMinor)
|
||||||
|
.filter(([, v]) => (v ?? 0) > 0)
|
||||||
|
.map(([m, v]) => `${MODULE_PRINT_LABEL[m as ModuleId] ?? m} (në biletë): ${money(v ?? 0)} ${cur}`),
|
||||||
// Merchant-validation leakage — printed only when the shift actually gave any
|
// Merchant-validation leakage — printed only when the shift actually gave any
|
||||||
// (older slips stay byte-identical). The takings above are already NET of it.
|
// (older slips stay byte-identical). The takings above are already NET of it.
|
||||||
...(r.discountTotalMinor > 0 ? [`Zbritje (validime): ${money(r.discountTotalMinor)} ${cur}`] : []),
|
...(r.discountTotalMinor > 0 ? [`Zbritje (validime): ${money(r.discountTotalMinor)} ${cur}`] : []),
|
||||||
|
// Forgiven exits — fees waived by an operator; $0 payments, NOT in the takings.
|
||||||
|
...(r.forgoneCount > 0 ? [`Dalje manuale (${r.forgoneCount}): ${money(r.forgoneTotalMinor)} ${cur}`] : []),
|
||||||
"",
|
"",
|
||||||
"-- Arka --",
|
"-- Arka --",
|
||||||
`Gjëndje fillestare: ${money(r.openingFloatMinor)} ${cur}`,
|
`Gjëndje fillestare: ${money(r.openingFloatMinor)} ${cur}`,
|
||||||
@@ -717,13 +841,7 @@ export class ShiftService {
|
|||||||
`Pagesa: ${money(r.cashRemovedMinor)} ${cur}`,
|
`Pagesa: ${money(r.cashRemovedMinor)} ${cur}`,
|
||||||
`Gjëndje aktuale: ${money(r.expectedDrawerMinor)} ${cur}`,
|
`Gjëndje aktuale: ${money(r.expectedDrawerMinor)} ${cur}`,
|
||||||
];
|
];
|
||||||
try {
|
return this.#printOn(r.till, `Z-report for ${r.operator}`, (p) => p.printReport({ title: "RAPORT TURNI", lines }));
|
||||||
await printer.printReport({ title: "RAPORT TURNI", lines });
|
|
||||||
return true;
|
|
||||||
} catch (err) {
|
|
||||||
this.#logger.warn(`Z-report print failed for ${r.operator}: ${(err as Error).message} (event recorded)`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Print a drawer-voucher slip (Mandat Arkëtimi / Mandat Pagese). Best-effort —
|
/** Print a drawer-voucher slip (Mandat Arkëtimi / Mandat Pagese). Best-effort —
|
||||||
@@ -737,17 +855,14 @@ export class ShiftService {
|
|||||||
operator: string;
|
operator: string;
|
||||||
currency: string | null;
|
currency: string | null;
|
||||||
at: string;
|
at: string;
|
||||||
|
till: TillId;
|
||||||
}): Promise<boolean> {
|
}): Promise<boolean> {
|
||||||
const printer = await this.#boothPrinter();
|
|
||||||
if (!printer) {
|
|
||||||
this.#logger.warn(`no booth-receipt printer — ${v.type} ${v.voucherNo} not printed (event recorded)`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const cur = v.currency ?? "";
|
const cur = v.currency ?? "";
|
||||||
const money = (m: number) => (m / 100).toFixed(2);
|
const money = (m: number) => (m / 100).toFixed(2);
|
||||||
const title = v.type === "cash_in" ? "MANDAT ARKËTIMI" : "MANDAT PAGESE";
|
const title = v.type === "cash_in" ? "MANDAT ARKËTIMI" : "MANDAT PAGESE";
|
||||||
const lines = [
|
const lines = [
|
||||||
`Mandat Nr.: ${v.voucherNo}`,
|
`Mandat Nr.: ${v.voucherNo}`,
|
||||||
|
...(v.till !== BOOTH_TILL ? [`Arka: ${TILL_PRINT_LABEL[v.till]}`] : []),
|
||||||
`Data: ${zStamp(v.at)}`,
|
`Data: ${zStamp(v.at)}`,
|
||||||
"",
|
"",
|
||||||
`Shuma: ${money(v.amountMinor)} ${cur}`,
|
`Shuma: ${money(v.amountMinor)} ${cur}`,
|
||||||
@@ -755,27 +870,57 @@ export class ShiftService {
|
|||||||
"",
|
"",
|
||||||
`Regjistroi: ${v.operator}`,
|
`Regjistroi: ${v.operator}`,
|
||||||
];
|
];
|
||||||
try {
|
return this.#printOn(v.till, `${v.type} ${v.voucherNo}`, (p) => p.printReport({ title, lines }));
|
||||||
await printer.printReport({ title, lines });
|
|
||||||
return true;
|
|
||||||
} catch (err) {
|
|
||||||
this.#logger.warn(`${v.type} ${v.voucherNo} print failed: ${(err as Error).message} (event recorded)`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** First enabled booth-receipt printer, or any enabled printer. */
|
/** Print a till's slip on its printer with failover (wash desk → booth printer;
|
||||||
async #boothPrinter(): Promise<PrinterDevice | null> {
|
* see TILL_PRINTER_ROLE / orderForRole). Best-effort: the signed event is the
|
||||||
const rows = await this.#db.select().from(devices).where(eq(devices.category, "printer")).all();
|
* record — every failure is logged and reported as "not printed", never thrown.
|
||||||
const enabled = rows.filter((r) => r.enabled);
|
* Legacy fallback: a site whose only printer carries no booth role (one unit,
|
||||||
const booth = enabled.find((r) => (r.config as { role?: string }).role === "booth-receipt") ?? enabled[0];
|
* configured as the entry dispenser) still prints its slips on it, as before. */
|
||||||
if (!booth) return null;
|
async #printOn(till: TillId, what: string, job: (p: PrinterDevice) => Promise<void>): Promise<boolean> {
|
||||||
const driver = registry.get(booth.driverId);
|
const printers = this.#loadPrinters();
|
||||||
if (!driver) return null;
|
const want = TILL_PRINTER_ROLE[till];
|
||||||
try {
|
let ordered = orderForRole(printers, want);
|
||||||
return driver.create(booth.config as never) as PrinterDevice;
|
if (ordered.length === 0 && till === BOOTH_TILL) ordered = printers.slice(0, 1);
|
||||||
} catch {
|
if (ordered.length === 0) {
|
||||||
return null;
|
this.#logger.warn(`no ${want} printer — ${what} not printed (event is recorded)`);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
const attempts: string[] = [];
|
||||||
|
for (const p of ordered) {
|
||||||
|
try {
|
||||||
|
await job(p.device);
|
||||||
|
if (p.role !== want) this.#logger.info(`${what} printed on ${p.id} (${p.role}; no ${want} printer reachable)`);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
attempts.push(`${p.id} (${(err as Error).message})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.#logger.warn(`${what} print failed on every candidate: ${attempts.join(", ")} (event recorded)`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Every enabled printer as a live instance (role + rank from its saved config). */
|
||||||
|
#loadPrinters(): PrinterInstance[] {
|
||||||
|
const rows = this.#db.select().from(devices).where(eq(devices.category, "printer")).all();
|
||||||
|
const out: PrinterInstance[] = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
if (!row.enabled) continue;
|
||||||
|
const driver = registry.get(row.driverId);
|
||||||
|
if (!driver) continue;
|
||||||
|
const cfg = row.config as Record<string, unknown>;
|
||||||
|
try {
|
||||||
|
out.push({
|
||||||
|
id: row.id,
|
||||||
|
role: printerRoleOf(cfg),
|
||||||
|
failoverRank: typeof cfg.failoverRank === "number" ? cfg.failoverRank : 0,
|
||||||
|
device: driver.create(cfg as never) as PrinterDevice,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// skip a printer whose config won't build
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+68
-10
@@ -2,12 +2,12 @@ import { randomUUID } from "node:crypto";
|
|||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
import { and, eq, gte, sessions, deviceEvents as deviceEventsTable, snapshots, type Db } from "@parking/db";
|
import { and, eq, gte, sessions, deviceEvents as deviceEventsTable, snapshots, type Db } from "@parking/db";
|
||||||
import { registry, type CameraDevice, type Snapshot } from "@parking/devices";
|
import { registry, type CameraDevice, type Snapshot } from "@parking/devices";
|
||||||
import { reasonPayload } from "@parking/shared";
|
import { reasonPayload, type NormBox } from "@parking/shared";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
import { devicesByDirection, type FlowDirection } from "./device-resolve.js";
|
import { devicesByDirection, type FlowDirection } from "./device-resolve.js";
|
||||||
import { deviceEvents } from "./device-events.js";
|
import { deviceEvents } from "./device-events.js";
|
||||||
import type { EventLog } from "./event-log.js";
|
import type { EventLog } from "./event-log.js";
|
||||||
import type { VisionClient } from "./vision-client.js";
|
import type { VisionClient, VisionResult } from "./vision-client.js";
|
||||||
|
|
||||||
// Camera snapshot capture, fired AFTER the barrier opens and never awaited on the
|
// Camera snapshot capture, fired AFTER the barrier opens and never awaited on the
|
||||||
// open path (decision 2026-06-16): a snapshot is EVIDENCE, not a gate. A camera
|
// open path (decision 2026-06-16): a snapshot is EVIDENCE, not a gate. A camera
|
||||||
@@ -167,22 +167,35 @@ async function recognizePlate(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const result = await vision.analyze(shot.bytes, shot.contentType);
|
const result = await vision.analyze(shot.bytes, shot.contentType);
|
||||||
if (!result || !result.plate || result.lowConfidence) return; // nothing trustworthy to record
|
if (!result) return;
|
||||||
const plate = result.plate.text.trim().toUpperCase();
|
// Boxes are kept as FRACTIONS of the analysed frame (the stored snapshot is a
|
||||||
if (!plate) return;
|
// downscaled copy — see reencodeForStorage), so the wash's review crop can cut the
|
||||||
|
// vehicle out of whatever copy survives and blur the plate inside it.
|
||||||
|
const boxes = await normalizedBoxes(shot.bytes, result);
|
||||||
|
// The vehicle's body type (advisory; the wash desk's category suggestion — see
|
||||||
|
// venue-modules.md §Vehicle category). Rides the plate's read row when there is one,
|
||||||
|
// else a row of its own: a car with an unreadable plate is still a car of some class.
|
||||||
|
const vehicleBox = boxes.vehicleBox;
|
||||||
|
const vehicle = result.vehicle
|
||||||
|
? { bodyType: result.vehicle.bodyType, bodyConfidence: result.vehicle.confidence, ...(vehicleBox ? { vehicleBox } : {}) }
|
||||||
|
: {};
|
||||||
|
const plate = !result.plate || result.lowConfidence ? "" : result.plate.text.trim().toUpperCase();
|
||||||
|
const plateBox = plate ? boxes.plateBox : null;
|
||||||
|
if (!plate && !result.vehicle) return; // nothing trustworthy to record
|
||||||
db.insert(deviceEventsTable)
|
db.insert(deviceEventsTable)
|
||||||
.values({
|
.values({
|
||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
deviceId,
|
deviceId,
|
||||||
category: "camera",
|
category: "camera",
|
||||||
kind: "read",
|
kind: "read",
|
||||||
// `identity` ties the plate to the session; `snapshotId` to the evidence image.
|
// `identity` ties the read to the session; `snapshotId` to the evidence image.
|
||||||
detail: {
|
detail: {
|
||||||
identity,
|
identity,
|
||||||
direction,
|
direction,
|
||||||
plate,
|
...(plate
|
||||||
confidence: result.plate.confidence,
|
? { plate, confidence: result.plate!.confidence, region: result.plate!.region ?? null, ...(plateBox ? { plateBox } : {}) }
|
||||||
region: result.plate.region ?? null,
|
: {}),
|
||||||
|
...vehicle,
|
||||||
modelVersion: result.modelVersion,
|
modelVersion: result.modelVersion,
|
||||||
snapshotId,
|
snapshotId,
|
||||||
source: "entry-exit-snapshot",
|
source: "entry-exit-snapshot",
|
||||||
@@ -190,7 +203,19 @@ async function recognizePlate(
|
|||||||
occurredAt: new Date().toISOString(),
|
occurredAt: new Date().toISOString(),
|
||||||
})
|
})
|
||||||
.run();
|
.run();
|
||||||
logger.info(`anpr plate '${plate}' (${result.plate.confidence.toFixed(3)}) for ${identity}`);
|
if (result.vehicle) {
|
||||||
|
logger.info(`vision vehicle '${result.vehicle.bodyType}' (${result.vehicle.confidence.toFixed(3)}) for ${identity}`);
|
||||||
|
if (vehicleBox) {
|
||||||
|
deviceEvents.emitVehicleRead({
|
||||||
|
identity,
|
||||||
|
direction,
|
||||||
|
deviceId,
|
||||||
|
read: { bodyType: result.vehicle.bodyType, confidence: result.vehicle.confidence, snapshotId, box: vehicleBox, plateBox },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!plate) return;
|
||||||
|
logger.info(`anpr plate '${plate}' (${result.plate!.confidence.toFixed(3)}) for ${identity}`);
|
||||||
// The session's entry/exit event already shipped without this (async) plate — tell the
|
// The session's entry/exit event already shipped without this (async) plate — tell the
|
||||||
// booth so it backfills the plate badge in place (no refresh). Advisory; ledger untouched.
|
// booth so it backfills the plate badge in place (no refresh). Advisory; ledger untouched.
|
||||||
deviceEvents.emitPlateRecognized({ identity, plate, direction });
|
deviceEvents.emitPlateRecognized({ identity, plate, direction });
|
||||||
@@ -206,6 +231,39 @@ async function recognizePlate(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clamp01(v: number): number {
|
||||||
|
return Math.max(0, Math.min(1, v));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The vehicle's and the plate's boxes as FRACTIONS of the analysed frame (null when the
|
||||||
|
* stage found none or the frame size is unreadable). Fractions, so the box fits the
|
||||||
|
* downscaled stored copy the wash's review crop is cut from. Shared by the session-tied
|
||||||
|
* recognition above and the ANPR bridge's per-car pass sample (anpr-entry.ts). */
|
||||||
|
export async function normalizedBoxes(
|
||||||
|
bytes: Buffer,
|
||||||
|
result: VisionResult,
|
||||||
|
): Promise<{ vehicleBox: NormBox | null; plateBox: NormBox | null }> {
|
||||||
|
const frame = await frameSize(bytes);
|
||||||
|
const norm = (b: { x1: number; y1: number; x2: number; y2: number } | null | undefined): NormBox | null =>
|
||||||
|
b && frame
|
||||||
|
? {
|
||||||
|
x1: clamp01(b.x1 / frame.w), y1: clamp01(b.y1 / frame.h),
|
||||||
|
x2: clamp01(b.x2 / frame.w), y2: clamp01(b.y2 / frame.h),
|
||||||
|
}
|
||||||
|
: null;
|
||||||
|
return { vehicleBox: norm(result.vehicle?.bbox), plateBox: norm(result.plate?.bbox) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Pixel size of the analysed frame (JPEG header only — cheap). Null when unreadable. */
|
||||||
|
async function frameSize(bytes: Buffer): Promise<{ w: number; h: number } | null> {
|
||||||
|
try {
|
||||||
|
const m = await sharp(bytes, { failOn: "none" }).metadata();
|
||||||
|
return m.width && m.height ? { w: m.width, h: m.height } : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** How far back a recognized entry plate is compared against other OPEN sessions'
|
/** How far back a recognized entry plate is compared against other OPEN sessions'
|
||||||
* entry plates. Short on purpose: the duplicate-ticket scenario is the same car
|
* entry plates. Short on purpose: the duplicate-ticket scenario is the same car
|
||||||
* re-pressing within minutes; a long window would flag legit re-visits. */
|
* re-pressing within minutes; a long window would flag legit re-visits. */
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
import { registry, type AccessControlDevice } from "@parking/devices";
|
import { registry, type AccessControlDevice } from "@parking/devices";
|
||||||
import { reasonPayload, type PlanTimeframes, type ReasonCode } from "@parking/shared";
|
import { reasonPayload, type PlanTimeframes, type ReasonCode } from "@parking/shared";
|
||||||
import type { FastifyBaseLogger } from "fastify";
|
import type { FastifyBaseLogger } from "fastify";
|
||||||
|
import { pulseBarrier } from "./relay-pulse.js";
|
||||||
import { printWindowChargeNotice } from "./booth-print.js";
|
import { printWindowChargeNotice } from "./booth-print.js";
|
||||||
import type { DeviceReadEvent, ReadOutcome } from "./device-events.js";
|
import type { DeviceReadEvent, ReadOutcome } from "./device-events.js";
|
||||||
import type { EventLog } from "./event-log.js";
|
import type { EventLog } from "./event-log.js";
|
||||||
@@ -211,8 +212,9 @@ export class SubscriptionFlow {
|
|||||||
// `permitId` carries the subscription id; `via` records which credential left.
|
// `permitId` carries the subscription id; `via` records which credential left.
|
||||||
payload: { sessionRef: occurrenceId, permitId: m.subscriptionId, via: m.via },
|
payload: { sessionRef: occurrenceId, permitId: m.subscriptionId, via: m.via },
|
||||||
});
|
});
|
||||||
await this.#open(resolved, "exit", occurrenceId, "subscription exit");
|
// Cache close before the pulse, for the same reason as the entry-side insert.
|
||||||
this.#closeCache(occurrenceId);
|
this.#closeCache(occurrenceId);
|
||||||
|
await this.#open(resolved, "exit", occurrenceId, "subscription exit");
|
||||||
return { accepted: true, direction: "exit" };
|
return { accepted: true, direction: "exit" };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +270,9 @@ export class SubscriptionFlow {
|
|||||||
`subscription out-of-window entry marked on ${occurrenceId} (charge priced from parked minutes at exit)`,
|
`subscription out-of-window entry marked on ${occurrenceId} (charge priced from parked minutes at exit)`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await this.#open(resolved, "entry", occurrenceId, "subscription entry");
|
// Session cache BEFORE the pulse: the entry is already signed, so the projection must
|
||||||
|
// reflect it even if the relay call throws (otherwise the ledger and the sessions
|
||||||
|
// table disagree about the same read — seen at park-2, 2026-10-24).
|
||||||
try {
|
try {
|
||||||
this.#db
|
this.#db
|
||||||
.insert(sessions)
|
.insert(sessions)
|
||||||
@@ -284,6 +288,7 @@ export class SubscriptionFlow {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.#logger.error(`session-cache insert failed for ${occurrenceId}: ${(err as Error).message}`);
|
this.#logger.error(`session-cache insert failed for ${occurrenceId}: ${(err as Error).message}`);
|
||||||
}
|
}
|
||||||
|
await this.#open(resolved, "entry", occurrenceId, "subscription entry");
|
||||||
// BEST-EFFORT: print the out-of-window TICKET so the subscriber has the paper the
|
// BEST-EFFORT: print the out-of-window TICKET so the subscriber has the paper the
|
||||||
// operator scans to settle at the booth. It carries the occurrence id as a scannable
|
// operator scans to settle at the booth. It carries the occurrence id as a scannable
|
||||||
// code; the amount is computed at settlement from the minutes actually parked
|
// code; the amount is computed at settlement from the minutes actually parked
|
||||||
@@ -409,12 +414,16 @@ export class SubscriptionFlow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async #open(resolved: ResolvedRelay, dir: FlowDirection, carKey: string, what: string): Promise<void> {
|
async #open(resolved: ResolvedRelay, dir: FlowDirection, carKey: string, what: string): Promise<void> {
|
||||||
const access = this.#buildAccess(resolved.controller);
|
// SNAPSHOT FIRST — fire the directional camera(s), never awaited (evidence, not a
|
||||||
if (access) await access.pulseOpen(resolved.relay);
|
// gate). Before the pulse, so a pulse failure (the pulse helper rethrows) still leaves
|
||||||
else this.#logger.warn(`${what} signed for ${carKey} but the ${dir} relay won't build`);
|
// an image for the signed event: at park-2 four signed entries had no snapshot because
|
||||||
|
// the relay timed out and this method unwound before reaching the camera (2026-10-24).
|
||||||
// SNAPSHOT — fire the directional camera(s), never awaited (evidence, not a gate).
|
|
||||||
this.#fireSnapshot(dir, carKey);
|
this.#fireSnapshot(dir, carKey);
|
||||||
|
|
||||||
|
const access = this.#buildAccess(resolved.controller);
|
||||||
|
if (access) {
|
||||||
|
await pulseBarrier(this.#db, this.#logger, access, resolved, { direction: dir, identity: carKey, source: "subscription" });
|
||||||
|
} else this.#logger.warn(`${what} signed for ${carKey} but the ${dir} relay won't build`);
|
||||||
}
|
}
|
||||||
|
|
||||||
#closeCache(carKey: string): void {
|
#closeCache(carKey: string): void {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { eq, ledgerEvents, type Db } from "@parking/db";
|
import { eq, ledgerEvents, type Db, and, isNull, validationPrograms } from "@parking/db";
|
||||||
|
import type { EventLog } from "./event-log.js";
|
||||||
import type { SessionValidation, ValidationMode } from "@parking/shared";
|
import type { SessionValidation, ValidationMode } from "@parking/shared";
|
||||||
|
|
||||||
// Merchant-validation ledger folds. A validation is a SIGNED, appended event on the
|
// Merchant-validation ledger folds. A validation is a SIGNED, appended event on the
|
||||||
@@ -20,6 +21,16 @@ export interface AppliedValidation extends SessionValidation {
|
|||||||
readonly consumedBy: string | null;
|
readonly consumedBy: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The ledger columns the validation fold reads. Any identity-scoped, index-ordered
|
||||||
|
* slice of `ledger_events` satisfies it (a direct query, or the per-identity group of a
|
||||||
|
* whole-ledger scan — see PayStation.activeSessions). */
|
||||||
|
export interface LedgerRowLike {
|
||||||
|
readonly id: string;
|
||||||
|
readonly type: string;
|
||||||
|
readonly occurredAt: string;
|
||||||
|
readonly payload: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
/** All validations ever applied to a session (newest last), with voided/consumed
|
/** All validations ever applied to a session (newest last), with voided/consumed
|
||||||
* state folded from the chain. One identity-scoped ledger scan. */
|
* state folded from the chain. One identity-scoped ledger scan. */
|
||||||
export function sessionValidations(db: Db, identity: string): AppliedValidation[] {
|
export function sessionValidations(db: Db, identity: string): AppliedValidation[] {
|
||||||
@@ -34,7 +45,13 @@ export function sessionValidations(db: Db, identity: string): AppliedValidation[
|
|||||||
.where(eq(ledgerEvents.identity, identity))
|
.where(eq(ledgerEvents.identity, identity))
|
||||||
.orderBy(ledgerEvents.index)
|
.orderBy(ledgerEvents.index)
|
||||||
.all();
|
.all();
|
||||||
|
return sessionValidationsFromRows(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Same fold over rows the caller already holds (identity-scoped, oldest first). Lets a
|
||||||
|
* list view price hundreds of sessions from ONE ledger scan instead of one per row —
|
||||||
|
* the per-row scans were what froze the booth server (park-2, 2026-10-24). */
|
||||||
|
export function sessionValidationsFromRows(rows: readonly LedgerRowLike[]): AppliedValidation[] {
|
||||||
const voided = new Set<string>();
|
const voided = new Set<string>();
|
||||||
const consumedBy = new Map<string, string>();
|
const consumedBy = new Map<string, string>();
|
||||||
const applies: AppliedValidation[] = [];
|
const applies: AppliedValidation[] = [];
|
||||||
@@ -86,3 +103,175 @@ export function sessionValidations(db: Db, identity: string): AppliedValidation[
|
|||||||
export function liveValidations(db: Db, identity: string): AppliedValidation[] {
|
export function liveValidations(db: Db, identity: string): AppliedValidation[] {
|
||||||
return sessionValidations(db, identity).filter((v) => !v.voided && v.consumedBy == null);
|
return sessionValidations(db, identity).filter((v) => !v.voided && v.consumedBy == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** `liveValidations` over rows the caller already holds (see sessionValidationsFromRows). */
|
||||||
|
export function liveValidationsFromRows(rows: readonly LedgerRowLike[]): AppliedValidation[] {
|
||||||
|
return sessionValidationsFromRows(rows).filter((v) => !v.voided && v.consumedBy == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Apply (shared by the merchant route and the Car Wash module) ----------------
|
||||||
|
|
||||||
|
export interface ApplyValidationInput {
|
||||||
|
programId: string;
|
||||||
|
identity: string;
|
||||||
|
/** Username recorded as the applying operator. */
|
||||||
|
actor: string;
|
||||||
|
/** fixed mode only: the amount the operator grants (minor units, ≤ maxAmountMinor). */
|
||||||
|
amountMinor?: number;
|
||||||
|
/** Car Wash context — required by the wash-only modes (doneTolerance / washPrice), which
|
||||||
|
* are RESOLVED here into a plain timeCredit / fixed event the pricing fold already
|
||||||
|
* understands: `washMinutes` = the wash window (order intake → done), NOT the whole
|
||||||
|
* stay; `priceMinor` = the wash price. */
|
||||||
|
wash?: { washMinutes: number; priceMinor: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ApplyValidationResult =
|
||||||
|
| {
|
||||||
|
ok: true;
|
||||||
|
eventId: string;
|
||||||
|
programId: string;
|
||||||
|
label: string;
|
||||||
|
mode: string;
|
||||||
|
minutes?: number | null;
|
||||||
|
percent?: number | null;
|
||||||
|
amountMinor?: number;
|
||||||
|
}
|
||||||
|
| { ok: false; status: 400 | 404 | 409; error: string };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply a validation program to an open transient session and append the signed
|
||||||
|
* `validation` event with the RESOLVED values. The decision chain, in order: program
|
||||||
|
* live + active → open TRANSIENT session → not already carrying a live application of
|
||||||
|
* this program → per-day cap → fixed-amount bounds. The merchant route adds its own
|
||||||
|
* program↔user BINDING check before calling this; a module applying its own program
|
||||||
|
* (Car Wash sponsorship) has no binding — the actor is attributed on the event instead.
|
||||||
|
* Returns a result object rather than throwing so each caller maps to its own HTTP
|
||||||
|
* shape. See wiki/concepts/validation-discounts.md.
|
||||||
|
*/
|
||||||
|
export async function applyValidation(
|
||||||
|
db: Db,
|
||||||
|
eventLog: EventLog,
|
||||||
|
input: ApplyValidationInput,
|
||||||
|
): Promise<ApplyValidationResult> {
|
||||||
|
const { programId, identity, actor } = input;
|
||||||
|
const program = db
|
||||||
|
.select()
|
||||||
|
.from(validationPrograms)
|
||||||
|
.where(and(eq(validationPrograms.id, programId), isNull(validationPrograms.deletedAt)))
|
||||||
|
.get();
|
||||||
|
if (!program || !program.active) return { ok: false, status: 404, error: "program not found or inactive" };
|
||||||
|
|
||||||
|
const rows = db
|
||||||
|
.select({ type: ledgerEvents.type, payload: ledgerEvents.payload })
|
||||||
|
.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, status: 404, error: "no session for ticket" };
|
||||||
|
const entryPl = (entry.payload ?? {}) as { permit?: boolean; permitId?: string };
|
||||||
|
if (entryPl.permit === true || entryPl.permitId != null) {
|
||||||
|
return { ok: false, status: 409, error: "subscription sessions cannot be validated" };
|
||||||
|
}
|
||||||
|
if (rows.some((r) => r.type === "vehicle_exit" || r.type === "void")) {
|
||||||
|
return { ok: false, status: 409, error: "session is closed" };
|
||||||
|
}
|
||||||
|
if (liveValidations(db, identity).some((v) => v.programId === programId)) {
|
||||||
|
return { ok: false, status: 409, error: "this program is already applied to the ticket" };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Per-day cap: unvoided applications of this program since LOCAL midnight (the
|
||||||
|
// appliance runs in site time).
|
||||||
|
if (program.maxPerDay != null) {
|
||||||
|
const midnight = new Date();
|
||||||
|
midnight.setHours(0, 0, 0, 0);
|
||||||
|
const todays = db
|
||||||
|
.select({ id: ledgerEvents.id, occurredAt: ledgerEvents.occurredAt, payload: ledgerEvents.payload, type: ledgerEvents.type })
|
||||||
|
.from(ledgerEvents)
|
||||||
|
.where(eq(ledgerEvents.type, "validation"))
|
||||||
|
.all()
|
||||||
|
.filter((r) => Date.parse(r.occurredAt) >= midnight.getTime());
|
||||||
|
const voidedIds = new Set(
|
||||||
|
todays.map((r) => (r.payload as { refId?: string } | null)?.refId).filter(Boolean) as string[],
|
||||||
|
);
|
||||||
|
const count = todays.filter((r) => {
|
||||||
|
const p = (r.payload ?? {}) as { programId?: string; refId?: string };
|
||||||
|
return p.programId === programId && !p.refId && !voidedIds.has(r.id);
|
||||||
|
}).length;
|
||||||
|
if (count >= program.maxPerDay) return { ok: false, status: 409, error: "daily cap reached for this program" };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve the program into the event's (mode, minutes/percent/amount). The wash-only
|
||||||
|
// modes become the plain modes the pricing fold knows; `programMode` keeps the original
|
||||||
|
// on the signed event for audit.
|
||||||
|
let mode: "comp" | "timeCredit" | "fixed" | "percent";
|
||||||
|
let minutes: number | undefined;
|
||||||
|
let percent: number | undefined;
|
||||||
|
let amountMinor: number | undefined;
|
||||||
|
switch (program.mode) {
|
||||||
|
case "comp":
|
||||||
|
mode = "comp";
|
||||||
|
break;
|
||||||
|
case "timeCredit":
|
||||||
|
mode = "timeCredit";
|
||||||
|
minutes = program.minutes ?? undefined;
|
||||||
|
break;
|
||||||
|
case "percent":
|
||||||
|
mode = "percent";
|
||||||
|
percent = program.percent ?? undefined;
|
||||||
|
break;
|
||||||
|
case "fixed": {
|
||||||
|
const a = input.amountMinor;
|
||||||
|
if (a == null || !Number.isInteger(a) || a <= 0) {
|
||||||
|
return { ok: false, status: 400, error: "amountMinor (positive integer) required for this program" };
|
||||||
|
}
|
||||||
|
if (program.maxAmountMinor != null && a > program.maxAmountMinor) {
|
||||||
|
return { ok: false, status: 400, error: `amount exceeds the program cap (${program.maxAmountMinor})` };
|
||||||
|
}
|
||||||
|
mode = "fixed";
|
||||||
|
amountMinor = a;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "doneTolerance": {
|
||||||
|
if (!input.wash) return { ok: false, status: 400, error: "this program needs a car wash order (done time)" };
|
||||||
|
mode = "timeCredit";
|
||||||
|
minutes = Math.max(0, input.wash.washMinutes) + Math.max(0, program.minutes ?? 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "washPrice": {
|
||||||
|
if (!input.wash) return { ok: false, status: 400, error: "this program needs a car wash order (price)" };
|
||||||
|
mode = "fixed";
|
||||||
|
amountMinor = Math.max(0, input.wash.priceMinor);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return { ok: false, status: 400, error: `unknown program mode ${String(program.mode)}` };
|
||||||
|
}
|
||||||
|
|
||||||
|
const ev = await eventLog.append({
|
||||||
|
type: "validation",
|
||||||
|
source: "manual",
|
||||||
|
identity,
|
||||||
|
payload: {
|
||||||
|
sessionRef: identity,
|
||||||
|
programId,
|
||||||
|
programLabel: program.name,
|
||||||
|
mode,
|
||||||
|
...(program.mode !== mode ? { programMode: program.mode } : {}),
|
||||||
|
...(minutes != null ? { minutes } : {}),
|
||||||
|
...(percent != null ? { percent } : {}),
|
||||||
|
...(amountMinor != null ? { amountMinor } : {}),
|
||||||
|
operator: actor,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
eventId: ev.id,
|
||||||
|
programId,
|
||||||
|
label: program.name,
|
||||||
|
mode,
|
||||||
|
minutes,
|
||||||
|
percent,
|
||||||
|
amountMinor,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ import type { FastifyBaseLogger } from "fastify";
|
|||||||
// transport + contract adapter only.
|
// transport + contract adapter only.
|
||||||
|
|
||||||
/** Plate bounding box (pixels, top-left origin) — mirrors the service schema. */
|
/** Plate bounding box (pixels, top-left origin) — mirrors the service schema. */
|
||||||
|
import { isVehicleClass, type VehicleClass } from "@parking/shared";
|
||||||
|
|
||||||
export interface PlateBBox {
|
export interface PlateBBox {
|
||||||
readonly x1: number;
|
readonly x1: number;
|
||||||
readonly y1: number;
|
readonly y1: number;
|
||||||
@@ -40,12 +42,21 @@ export interface VisionPlate {
|
|||||||
readonly region?: string | null;
|
readonly region?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The raw /analyze response shape (the Python contract). `vehicle` is reserved for
|
/** The vehicle attributes stage of /analyze (advisory). `body_type` is one of the shared
|
||||||
* Job 2 (vehicle verification) — not yet produced. */
|
* VEHICLE_CLASSES vocabulary (the service's raw label is normalised there); a stub or a
|
||||||
|
* plate-only recognizer sends null. */
|
||||||
|
export interface VisionVehicle {
|
||||||
|
readonly bodyType: VehicleClass;
|
||||||
|
readonly confidence: number;
|
||||||
|
/** The vehicle's box in frame pixels, when the stage found one. */
|
||||||
|
readonly bbox?: PlateBBox | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The raw /analyze response shape (the Python contract). */
|
||||||
interface AnalyzeResponse {
|
interface AnalyzeResponse {
|
||||||
readonly plate: VisionPlate | null;
|
readonly plate: VisionPlate | null;
|
||||||
readonly plates: VisionPlate[];
|
readonly plates: VisionPlate[];
|
||||||
readonly vehicle: unknown | null;
|
readonly vehicle: { body_type?: string | null; confidence?: number | null; bbox?: PlateBBox | null } | null;
|
||||||
readonly low_confidence: boolean;
|
readonly low_confidence: boolean;
|
||||||
readonly model_version: string;
|
readonly model_version: string;
|
||||||
readonly took_ms: number;
|
readonly took_ms: number;
|
||||||
@@ -61,6 +72,8 @@ export interface VisionResult {
|
|||||||
/** True when the best plate is below the confidence floor — treat as advisory only
|
/** True when the best plate is below the confidence floor — treat as advisory only
|
||||||
* and fall back to the ticket/manual path. */
|
* and fall back to the ticket/manual path. */
|
||||||
readonly lowConfidence: boolean;
|
readonly lowConfidence: boolean;
|
||||||
|
/** The vehicle's body type, when the service ran that stage and named a known class. */
|
||||||
|
readonly vehicle: VisionVehicle | null;
|
||||||
readonly modelVersion: string;
|
readonly modelVersion: string;
|
||||||
readonly tookMs: number;
|
readonly tookMs: number;
|
||||||
}
|
}
|
||||||
@@ -124,10 +137,16 @@ export class VisionClient {
|
|||||||
const best = res.plate ?? null;
|
const best = res.plate ?? null;
|
||||||
const lowConfidence =
|
const lowConfidence =
|
||||||
res.low_confidence || (best != null && best.confidence < this.#minConfidence);
|
res.low_confidence || (best != null && best.confidence < this.#minConfidence);
|
||||||
|
const v = res.vehicle;
|
||||||
|
const vehicle: VisionVehicle | null =
|
||||||
|
v && isVehicleClass(v.body_type) && typeof v.confidence === "number"
|
||||||
|
? { bodyType: v.body_type, confidence: Math.max(0, Math.min(1, v.confidence)), bbox: v.bbox ?? null }
|
||||||
|
: null;
|
||||||
return {
|
return {
|
||||||
plate: best,
|
plate: best,
|
||||||
plates: Array.isArray(res.plates) ? res.plates : [],
|
plates: Array.isArray(res.plates) ? res.plates : [],
|
||||||
lowConfidence,
|
lowConfidence,
|
||||||
|
vehicle,
|
||||||
modelVersion: res.model_version ?? "unknown",
|
modelVersion: res.model_version ?? "unknown",
|
||||||
tookMs: typeof res.took_ms === "number" ? res.took_ms : 0,
|
tookMs: typeof res.took_ms === "number" ? res.took_ms : 0,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
.venv/
|
||||||
|
**/__pycache__/
|
||||||
|
.pytest_cache/
|
||||||
|
.mypy_cache/
|
||||||
|
.ruff_cache/
|
||||||
|
out/
|
||||||
|
.env
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# Python
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
.venv/
|
||||||
|
.mypy_cache/
|
||||||
|
.pytest_cache/
|
||||||
|
.ruff_cache/
|
||||||
|
|
||||||
|
# Model weights (fetched at deploy / first run, never committed — can be large + license-scoped)
|
||||||
|
out/
|
||||||
|
|
||||||
|
*.onnx
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
3.12
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
# syntax=docker/dockerfile:1.7
|
||||||
|
# Parking TRAINER image: the phase-B body-type classifier. Build CONTEXT is apps/trainer
|
||||||
|
# (self-contained Python package). Runs on the reviewer's host (art-docker-station) beside
|
||||||
|
# the collector, never on a booth: by default it SERVES the job API the collector's Training
|
||||||
|
# section drives (`serve`); the same image runs the CLI one-off (`train`, `inspect`, …). It
|
||||||
|
# reads the wash collector's volume (collector.sqlite + crops/) and writes versioned model
|
||||||
|
# folders. CPU-only PyTorch — the host has no usable GPU and a few thousand crops train in
|
||||||
|
# minutes/an hour on four Xeon cores.
|
||||||
|
# See wiki/decisions/bodytype-classifier-training.md.
|
||||||
|
|
||||||
|
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS base
|
||||||
|
WORKDIR /app
|
||||||
|
ENV UV_LINK_MODE=copy \
|
||||||
|
UV_COMPILE_BYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends libgl1 libglib2.0-0 \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY pyproject.toml uv.lock .python-version ./
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
||||||
|
uv sync --frozen --no-install-project --no-dev --extra train
|
||||||
|
|
||||||
|
COPY trainer/ ./trainer/
|
||||||
|
COPY README.md ./
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
||||||
|
uv sync --frozen --no-dev --extra train
|
||||||
|
|
||||||
|
# Pre-warm the ImageNet backbone weights INTO the image so a run needs no network (the
|
||||||
|
# host has one, but a job that fetches at run time is a job that fails at 2 am). Best-effort:
|
||||||
|
# without network at build time torchvision fetches lazily on the first run.
|
||||||
|
ENV TORCH_HOME=/app/torch-home
|
||||||
|
RUN uv run python -c "import torchvision.models as m; m.resnet18(weights=m.ResNet18_Weights.IMAGENET1K_V1); m.mobilenet_v3_small(weights=m.MobileNet_V3_Small_Weights.IMAGENET1K_V1)" \
|
||||||
|
|| echo "[build] backbone weights not pre-warmed (no network) — fetched on first run"
|
||||||
|
|
||||||
|
RUN useradd --system --create-home --uid 999 trainer \
|
||||||
|
&& mkdir -p /data /out && chown -R trainer:trainer /app /out
|
||||||
|
USER trainer
|
||||||
|
|
||||||
|
ENV TRAINER_DATA_DIR=/data \
|
||||||
|
TRAINER_OUT_DIR=/out \
|
||||||
|
TRAINER_PORT=8091
|
||||||
|
VOLUME ["/out"]
|
||||||
|
EXPOSE 8091
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
||||||
|
CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://localhost:8091/health').status==200 else 1)" || exit 1
|
||||||
|
ENTRYPOINT ["uv", "run", "--no-sync", "parking-trainer"]
|
||||||
|
CMD ["serve"]
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# parking-trainer
|
||||||
|
|
||||||
|
The phase-B **body-type classifier** job. Reads the wash collector's volume
|
||||||
|
(`collector.sqlite` + `crops/`), trains a classifier on the reviewer's labels, and writes a
|
||||||
|
versioned model folder the vision image bakes in — or refuses when validation is below the
|
||||||
|
floor. Design and decisions: `wiki/decisions/bodytype-classifier-training.md`.
|
||||||
|
|
||||||
|
```
|
||||||
|
parking-trainer inspect --data /data # what a run would train on
|
||||||
|
parking-trainer train --data /data --out /out # features mode (minutes)
|
||||||
|
parking-trainer train --mode finetune --epochs 12 ... # full fine-tune (about an hour on 4 cores)
|
||||||
|
parking-trainer evaluate --model /out/<version>/bodytype.onnx --data /data
|
||||||
|
parking-trainer publish /out/<version> --url https://git.infra.msai.al/api/packages/mca/generic/parking-bodytype
|
||||||
|
```
|
||||||
|
|
||||||
|
Exit codes: `0` model written · `2` not enough labels · `3` below the floor (report written,
|
||||||
|
no model) · `1` other.
|
||||||
|
|
||||||
|
A passing run writes `<out>/<version>/`:
|
||||||
|
|
||||||
|
| file | what |
|
||||||
|
| --- | --- |
|
||||||
|
| `bodytype.onnx` | the classifier; input `image` = RGB float32 0–255 `[N,3,S,S]`, output `logits` `[N,K]`; normalisation is inside the graph |
|
||||||
|
| `bodytype.json` | sidecar: version, class list (in vocabulary order), input size, crop margin, backbone, mode, label counts, validation metrics |
|
||||||
|
| `report.md` | the human report: accuracy, per-class recall/precision, confusion matrix, dropped classes, loss weights |
|
||||||
|
| `metrics.json` | the same numbers, machine-readable |
|
||||||
|
|
||||||
|
On the reviewer's host the image runs `serve` as the `trainer` service of the
|
||||||
|
`wash-collector` stack: a job API (`/health`, `/readiness`, `/versions`, `/jobs`) on the compose
|
||||||
|
network that the collector's **Training section** (`/review`) drives — readiness, Train /
|
||||||
|
Evaluate / Publish, reports and logs. Jobs run as subprocesses of the CLI, one at a time; state
|
||||||
|
and logs persist under `/out/jobs/`. The CLI stays for debugging:
|
||||||
|
`docker compose -f docker-compose.collector.yml exec trainer parking-trainer inspect`.
|
||||||
|
|
||||||
|
Local dev: `uv sync --extra train` (CPU torch, ~200 MB), `uv run pytest -q`. The test suite
|
||||||
|
runs without the extra (torch tests skip), matching CI.
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "@parking/trainer",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"//": "Thin shim so this Python job is a node in the Turbo task graph (NOT a JS package — deps are managed by uv/pyproject.toml). It is a one-off job image, never a booth service: see wiki/decisions/bodytype-classifier-training.md.",
|
||||||
|
"scripts": {
|
||||||
|
"lint": "uv run ruff check .",
|
||||||
|
"format": "uv run ruff format .",
|
||||||
|
"typecheck": "uv run mypy trainer",
|
||||||
|
"test": "uv run pytest -q",
|
||||||
|
"build": "echo 'no build step (Python job; see Dockerfile)'"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
[project]
|
||||||
|
name = "parking-trainer"
|
||||||
|
version = "0.0.0"
|
||||||
|
description = "Phase-B body-type classifier trainer: reviewer labels + crops off the wash collector's volume → an ONNX classifier the vision image bakes in."
|
||||||
|
requires-python = ">=3.10,<4.0"
|
||||||
|
# Core deps are LIGHT on purpose (same rule as the vision service): `inspect`, `evaluate`
|
||||||
|
# and the data/report code run with only these, so `uv sync` and the test suite work
|
||||||
|
# in CI without the PyTorch stack. Training itself needs the `train` extra.
|
||||||
|
# See wiki/decisions/bodytype-classifier-training.md.
|
||||||
|
dependencies = [
|
||||||
|
"numpy>=1.26",
|
||||||
|
# OpenCV does the decode + resize on BOTH sides (trainer and vision service): same
|
||||||
|
# library, same interpolation, same pixels — the preprocessing contract (preprocess.py).
|
||||||
|
"opencv-python-headless>=4.10",
|
||||||
|
"onnxruntime>=1.19",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.scripts]
|
||||||
|
parking-trainer = "trainer.cli:main"
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
# The training stack. CPU-only PyTorch (the reviewer's host has no usable GPU — the
|
||||||
|
# decision is recorded in the wiki page above): resolved from PyTorch's CPU wheel index,
|
||||||
|
# ~200 MB instead of the ~5 GB CUDA build. Install with: uv sync --extra train
|
||||||
|
# torch / torchvision are BSD-3; the ImageNet backbone weights ship under the same
|
||||||
|
# licence (the licence rule applies to weights as much as code).
|
||||||
|
train = [
|
||||||
|
"torch>=2.4",
|
||||||
|
"torchvision>=0.19",
|
||||||
|
"onnx>=1.16",
|
||||||
|
"onnxscript>=0.3", # the torch.export-based ONNX exporter (MIT)
|
||||||
|
]
|
||||||
|
|
||||||
|
[dependency-groups]
|
||||||
|
dev = [
|
||||||
|
"ruff>=0.8",
|
||||||
|
"pytest>=8.3",
|
||||||
|
"mypy>=1.13",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.uv]
|
||||||
|
# Pick the CPU wheels for torch/torchvision from PyTorch's own index; everything else
|
||||||
|
# from PyPI. `explicit = true` keeps the index from shadowing PyPI for other packages.
|
||||||
|
[[tool.uv.index]]
|
||||||
|
name = "pytorch-cpu"
|
||||||
|
url = "https://download.pytorch.org/whl/cpu"
|
||||||
|
explicit = true
|
||||||
|
|
||||||
|
[tool.uv.sources]
|
||||||
|
torch = [{ index = "pytorch-cpu" }]
|
||||||
|
torchvision = [{ index = "pytorch-cpu" }]
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
line-length = 110
|
||||||
|
target-version = "py310"
|
||||||
|
|
||||||
|
[tool.ruff.lint]
|
||||||
|
select = ["E", "F", "I", "B", "UP"]
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
testpaths = ["tests"]
|
||||||
|
|
||||||
|
[tool.mypy]
|
||||||
|
python_version = "3.12"
|
||||||
|
strict = true
|
||||||
|
ignore_missing_imports = true
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["hatchling"]
|
||||||
|
build-backend = "hatchling.build"
|
||||||
|
|
||||||
|
[tool.hatch.build.targets.wheel]
|
||||||
|
packages = ["trainer"]
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
"""A synthetic collector volume: the collector's `items` table (same DDL as apps/collector
|
||||||
|
src/db.ts) + JPEG crops. Classes are told apart by COLOUR so even a random-init backbone's
|
||||||
|
features separate them — the tests check the plumbing (split, floor, export, sidecar),
|
||||||
|
not accuracy on real cars."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
DDL = """
|
||||||
|
CREATE TABLE IF NOT EXISTS items (
|
||||||
|
id TEXT PRIMARY KEY, booth TEXT NOT NULL, kind TEXT NOT NULL DEFAULT 'wash',
|
||||||
|
order_ref TEXT NOT NULL, at TEXT NOT NULL, operator_ref TEXT NOT NULL DEFAULT '',
|
||||||
|
operator_category_id TEXT NOT NULL DEFAULT '', operator_category_name TEXT NOT NULL DEFAULT '',
|
||||||
|
operator_classes TEXT NOT NULL DEFAULT '[]', service TEXT NOT NULL, vision_class TEXT NOT NULL,
|
||||||
|
vision_confidence REAL NOT NULL, vision_category_id TEXT, downgraded INTEGER NOT NULL DEFAULT 0,
|
||||||
|
image_width INTEGER NOT NULL, image_height INTEGER NOT NULL, plate_blurred INTEGER NOT NULL,
|
||||||
|
image_path TEXT NOT NULL, received_at TEXT NOT NULL, review_label TEXT, reviewed_at TEXT, reviewer TEXT
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
COLOURS = {"sedan": (200, 40, 40), "suv": (40, 200, 40), "van": (40, 40, 200), "truck": (200, 200, 40)}
|
||||||
|
|
||||||
|
|
||||||
|
def write_jpeg(path: Path, colour: tuple[int, int, int], rng: np.random.Generator) -> None:
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
h, w = int(rng.integers(120, 200)), int(rng.integers(160, 260))
|
||||||
|
img = np.empty((h, w, 3), np.uint8)
|
||||||
|
img[:] = colour[::-1] # BGR
|
||||||
|
noise = rng.integers(-20, 20, size=img.shape, dtype=np.int16)
|
||||||
|
img = np.clip(img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
|
||||||
|
cv2.imwrite(str(path), img, [cv2.IMWRITE_JPEG_QUALITY, 85])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def collector_dir(tmp_path: Path) -> Path:
|
||||||
|
"""40 labelled crops per class for sedan/suv/van, 5 for truck (below the minimum), a few
|
||||||
|
unusable, a few pending, one labelled row whose file is missing."""
|
||||||
|
rng = np.random.default_rng(1)
|
||||||
|
con = sqlite3.connect(tmp_path / "collector.sqlite")
|
||||||
|
con.executescript(DDL)
|
||||||
|
t0 = datetime(2026, 9, 1, tzinfo=timezone.utc)
|
||||||
|
n = 0
|
||||||
|
|
||||||
|
def add(label: str | None, reviewed: bool, kind: str = "wash", missing: bool = False) -> None:
|
||||||
|
nonlocal n
|
||||||
|
n += 1
|
||||||
|
item = f"item-{n:04d}"
|
||||||
|
rel = f"crops/booth-2/{item}.jpg"
|
||||||
|
colour = COLOURS.get(label or "sedan", (128, 128, 128))
|
||||||
|
if not missing:
|
||||||
|
write_jpeg(tmp_path / rel, colour, rng)
|
||||||
|
at = (t0 + timedelta(minutes=10 * n)).isoformat().replace("+00:00", "Z")
|
||||||
|
reviewed_at = (
|
||||||
|
(t0 + timedelta(days=1, minutes=n)).isoformat().replace("+00:00", "Z") if reviewed else None
|
||||||
|
)
|
||||||
|
con.execute(
|
||||||
|
"INSERT INTO items (id, booth, kind, order_ref, at, service, vision_class, vision_confidence, "
|
||||||
|
"image_width, image_height, plate_blurred, image_path, received_at, review_label, "
|
||||||
|
"reviewed_at, reviewer) "
|
||||||
|
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||||
|
(
|
||||||
|
item,
|
||||||
|
"booth-2",
|
||||||
|
kind,
|
||||||
|
"o",
|
||||||
|
at,
|
||||||
|
"wash",
|
||||||
|
"car" if label != "truck" else "truck",
|
||||||
|
0.9,
|
||||||
|
200,
|
||||||
|
150,
|
||||||
|
1,
|
||||||
|
rel,
|
||||||
|
at,
|
||||||
|
label if reviewed else None,
|
||||||
|
reviewed_at,
|
||||||
|
"reviewer" if reviewed else None,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Interleaved in time so every class exists on both sides of the time split.
|
||||||
|
for i in range(40):
|
||||||
|
for label in ("sedan", "suv", "van"):
|
||||||
|
add(label, True)
|
||||||
|
if i % 8 == 0:
|
||||||
|
add("truck", True)
|
||||||
|
add("unusable", True)
|
||||||
|
add("unusable", True)
|
||||||
|
add("sedan", True, missing=True)
|
||||||
|
for _ in range(6):
|
||||||
|
add(None, False, kind="entry")
|
||||||
|
con.commit()
|
||||||
|
con.close()
|
||||||
|
return tmp_path
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
"""Data rules, torch-free: labels, the time split, thin classes, weights, the report."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from trainer.cli import main
|
||||||
|
from trainer.data import (
|
||||||
|
class_weights,
|
||||||
|
load_labelled,
|
||||||
|
load_reviewed_since,
|
||||||
|
load_unlabelled,
|
||||||
|
make_split,
|
||||||
|
summarise,
|
||||||
|
)
|
||||||
|
from trainer.preprocess import CROP_MARGIN, Sidecar, load_input
|
||||||
|
from trainer.report import compute_metrics, render_report
|
||||||
|
|
||||||
|
|
||||||
|
def test_loads_only_reviewed_usable_rows_with_a_crop_on_disk(collector_dir: Path) -> None:
|
||||||
|
samples, missing = load_labelled(collector_dir)
|
||||||
|
assert missing == 1 # the labelled row whose file is gone
|
||||||
|
assert len(samples) == 125 # 3×40 + 5 trucks; unusable and pending excluded
|
||||||
|
assert all(s.path.is_file() for s in samples)
|
||||||
|
assert {s.label for s in samples} == {"sedan", "suv", "van", "truck"}
|
||||||
|
assert summarise(samples)["byClass"] == {"sedan": 40, "suv": 40, "van": 40, "truck": 5}
|
||||||
|
assert len(load_unlabelled(collector_dir)) == 6
|
||||||
|
assert len(load_reviewed_since(collector_dir, "2026-09-02T00:00:00Z")) == 125
|
||||||
|
assert load_reviewed_since(collector_dir, "2030-01-01T00:00:00Z") == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_split_is_by_time_and_drops_thin_classes(collector_dir: Path) -> None:
|
||||||
|
samples, _ = load_labelled(collector_dir)
|
||||||
|
split = make_split(samples, val_fraction=0.2, min_per_class=20)
|
||||||
|
assert split.classes == ("sedan", "suv", "van") # canonical order, truck dropped
|
||||||
|
assert split.dropped == {"truck": 5}
|
||||||
|
assert len(split.train) + len(split.val) == 120
|
||||||
|
assert len(split.val) == 24
|
||||||
|
assert max(s.at for s in split.train) < min(s.at for s in split.val) # newest = validation
|
||||||
|
assert all(v > 0 for v in split.counts("val").values())
|
||||||
|
|
||||||
|
|
||||||
|
def test_class_weights_lean_against_imbalance_but_gently(collector_dir: Path) -> None:
|
||||||
|
samples, _ = load_labelled(collector_dir)
|
||||||
|
vans = [s for s in samples if s.label == "van"]
|
||||||
|
keep = set(vans[::10]) # 4 of 40 vans survive
|
||||||
|
split = make_split([s for s in samples if s.label != "van" or s in keep], 0.2, 3)
|
||||||
|
w = dict(zip(split.classes, class_weights(split), strict=True))
|
||||||
|
assert w["van"] > w["sedan"] > 0 # the rare class weighs more
|
||||||
|
assert w["van"] / w["sedan"] < 4 # but not the full inverse ratio (damped)
|
||||||
|
assert abs(sum(w.values()) / len(w) - 1.0) < 1e-9
|
||||||
|
|
||||||
|
|
||||||
|
def test_metrics_and_report() -> None:
|
||||||
|
classes = ("sedan", "suv")
|
||||||
|
m = compute_metrics(classes, [0, 0, 1, 1], [0, 1, 1, 1], camera=["car"] * 4)
|
||||||
|
assert m.accuracy == 0.75
|
||||||
|
assert m.per_class["sedan"].recall == 0.5 and m.per_class["suv"].precision == 2 / 3
|
||||||
|
assert m.confusion == [[1, 1], [0, 2]]
|
||||||
|
assert m.camera_agreement == 0.0
|
||||||
|
text = render_report(
|
||||||
|
version="v1",
|
||||||
|
trained_at="t",
|
||||||
|
mode="features",
|
||||||
|
backbone="resnet18",
|
||||||
|
epochs=3,
|
||||||
|
classes=classes,
|
||||||
|
train_counts={"sedan": 10, "suv": 8},
|
||||||
|
val_counts={"sedan": 2, "suv": 2},
|
||||||
|
dropped={"truck": 2},
|
||||||
|
missing_files=1,
|
||||||
|
weights=[0.9, 1.1],
|
||||||
|
metrics=m,
|
||||||
|
min_accuracy=0.85,
|
||||||
|
written=False,
|
||||||
|
)
|
||||||
|
assert "MODEL NOT WRITTEN" in text and "| **sedan** | 1 | 1 |" in text and "truck (2)" in text
|
||||||
|
|
||||||
|
|
||||||
|
def test_preprocess_contract(tmp_path: Path, collector_dir: Path) -> None:
|
||||||
|
samples, _ = load_labelled(collector_dir)
|
||||||
|
x = load_input(samples[0].path, 32)
|
||||||
|
assert x.shape == (3, 32, 32) and x.dtype.name == "float32" and 0 <= x.min() and x.max() <= 255
|
||||||
|
assert x[0].mean() > x[2].mean() # a sedan crop is red: RGB order, not BGR
|
||||||
|
assert load_input(tmp_path / "nope.jpg", 32) is None
|
||||||
|
side = Sidecar(version="v1", classes=["sedan", "suv"])
|
||||||
|
side.write(tmp_path / "s.json")
|
||||||
|
back = Sidecar.read(tmp_path / "s.json")
|
||||||
|
assert back == side and back.crop_margin == CROP_MARGIN == 0.08 and back.normalization == "in-graph"
|
||||||
|
|
||||||
|
|
||||||
|
def test_inspect_prints_the_run_shape(collector_dir: Path, capsys) -> None: # type: ignore[no-untyped-def]
|
||||||
|
assert main(["inspect", "--data", str(collector_dir)]) == 0
|
||||||
|
out = json.loads(capsys.readouterr().out)
|
||||||
|
assert out["ready"] is True and out["run"]["classes"] == ["sedan", "suv", "van"]
|
||||||
|
assert out["run"]["dropped"] == {"truck": 5} and out["missingCrops"] == 1
|
||||||
|
assert main(["inspect", "--data", str(collector_dir), "--min-per-class", "100"]) == 2
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
"""The job API: readiness, one job at a time, subprocess jobs with persisted logs, versions."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
import threading
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
from http.server import ThreadingHTTPServer
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from trainer.server import Handler, Jobs, readiness, versions, wait_idle
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def api(collector_dir: Path, tmp_path: Path): # type: ignore[no-untyped-def]
|
||||||
|
out = tmp_path / "out"
|
||||||
|
Handler.jobs = Jobs(collector_dir, out, "https://example.invalid/pkg", "tok")
|
||||||
|
httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||||||
|
t = threading.Thread(target=httpd.serve_forever, daemon=True)
|
||||||
|
t.start()
|
||||||
|
base = f"http://127.0.0.1:{httpd.server_address[1]}"
|
||||||
|
|
||||||
|
def call(method: str, path: str, body: dict | None = None): # type: ignore[no-untyped-def]
|
||||||
|
req = urllib.request.Request(base + path, method=method)
|
||||||
|
data = None
|
||||||
|
if body is not None:
|
||||||
|
data = json.dumps(body).encode()
|
||||||
|
req.add_header("Content-Type", "application/json")
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req, data=data, timeout=10) as r:
|
||||||
|
raw = r.read()
|
||||||
|
return r.status, (
|
||||||
|
json.loads(raw) if r.headers.get_content_type() == "application/json" else raw.decode()
|
||||||
|
)
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
return e.code, json.loads(e.read() or b"{}")
|
||||||
|
|
||||||
|
yield call, out
|
||||||
|
httpd.shutdown()
|
||||||
|
httpd.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_readiness_and_empty_versions(api) -> None: # type: ignore[no-untyped-def]
|
||||||
|
call, _ = api
|
||||||
|
code, r = call("GET", "/readiness")
|
||||||
|
assert code == 200 and r["ready"] is True and r["run"]["classes"] == ["sedan", "suv", "van"]
|
||||||
|
assert r["defaults"]["minAccuracy"] == 0.85 and "finetune" in r["modes"]
|
||||||
|
assert call("GET", "/versions") == (200, {"versions": []})
|
||||||
|
assert call("GET", "/health")[1]["busy"] is False
|
||||||
|
assert readiness(Path("/nonexistent"))["ready"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_evaluate_job_runs_as_a_subprocess_and_is_recorded(api) -> None: # type: ignore[no-untyped-def]
|
||||||
|
call, out = api
|
||||||
|
code, job = call("POST", "/jobs", {"kind": "evaluate", "version": "nope"})
|
||||||
|
assert code == 202 and job["status"] == "running" and job["kind"] == "evaluate"
|
||||||
|
wait_idle(Handler.jobs)
|
||||||
|
code, j = call("GET", f"/jobs/{job['id']}")
|
||||||
|
assert code == 200 and j["status"] == "failed" and j["exitCode"] == 1
|
||||||
|
assert "evaluate --data" in j["log"] and "nope" in j["log"]
|
||||||
|
assert (out / "jobs" / f"{job['id']}.json").is_file() and (out / "jobs" / f"{job['id']}.log").is_file()
|
||||||
|
code, lst = call("GET", "/jobs")
|
||||||
|
assert code == 200 and lst["jobs"][0]["id"] == job["id"] and lst["current"] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_requests(api) -> None: # type: ignore[no-untyped-def]
|
||||||
|
call, _ = api
|
||||||
|
assert call("POST", "/jobs", {"kind": "nuke"})[0] == 400
|
||||||
|
assert call("POST", "/jobs", {"kind": "train", "mode": "magic"})[0] == 400
|
||||||
|
assert call("POST", "/jobs", {"kind": "evaluate", "version": "../etc"})[0] == 400
|
||||||
|
assert call("POST", "/jobs", {"kind": "publish", "version": "v1", "url": "ftp://x"})[0] == 400
|
||||||
|
assert call("GET", "/versions/../x/report")[0] == 400
|
||||||
|
assert call("GET", "/versions/v9/report")[0] == 404
|
||||||
|
assert call("GET", "/jobs/nope")[0] == 404
|
||||||
|
assert call("GET", "/nothing")[0] == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_train_job_then_versions_and_report(api) -> None: # type: ignore[no-untyped-def]
|
||||||
|
pytest.importorskip("torch")
|
||||||
|
call, out = api
|
||||||
|
body = {"kind": "train", "mode": "features", "minAccuracy": 0.0, "epochs": 100, "version": "vapi"}
|
||||||
|
# The test-only flags are not offered by the API; inject them via the CLI args the runner builds.
|
||||||
|
orig = Jobs._argv
|
||||||
|
|
||||||
|
def patched(self, kind, a): # type: ignore[no-untyped-def]
|
||||||
|
argv = orig(self, kind, a)
|
||||||
|
return argv + ["--no-pretrained", "--input-size", "64", "--no-cache"] if kind == "train" else argv
|
||||||
|
|
||||||
|
Jobs._argv = patched # type: ignore[method-assign]
|
||||||
|
try:
|
||||||
|
code, job = call("POST", "/jobs", body)
|
||||||
|
assert code == 202
|
||||||
|
assert call("POST", "/jobs", {"kind": "evaluate", "version": "vapi"})[0] == 409 # one at a time
|
||||||
|
wait_idle(Handler.jobs, 120)
|
||||||
|
finally:
|
||||||
|
Jobs._argv = orig # type: ignore[method-assign]
|
||||||
|
code, j = call("GET", f"/jobs/{job['id']}")
|
||||||
|
assert j["status"] == "done" and "MODEL WRITTEN" in j["log"]
|
||||||
|
code, v = call("GET", "/versions")
|
||||||
|
assert code == 200 and v["versions"][0]["version"] == "vapi" and v["versions"][0]["written"] is True
|
||||||
|
assert v["versions"][0]["classes"] == ["sedan", "suv", "van"] and v["versions"][0]["accuracy"] >= 0.9
|
||||||
|
code, report = call("GET", "/versions/vapi/report")
|
||||||
|
assert code == 200 and report.startswith("# Body-type classifier vapi")
|
||||||
|
assert versions(out)[0]["floor"] == 0.0
|
||||||
|
# evaluate on the written model now succeeds
|
||||||
|
code, job2 = call("POST", "/jobs", {"kind": "evaluate", "version": "vapi"})
|
||||||
|
wait_idle(Handler.jobs)
|
||||||
|
assert call("GET", f"/jobs/{job2['id']}")[1]["status"] == "done"
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_handler_error_is_a_500_json_reply_not_a_dropped_connection(tmp_path: Path) -> None:
|
||||||
|
"""A collector DB from before the `kind` column (art-docker-station, 2026-09-16): readiness
|
||||||
|
raised sqlite3.OperationalError, the stdlib server printed the traceback and closed the
|
||||||
|
socket, and the collector could only say "trainer not reachable: fetch failed". The
|
||||||
|
handler must answer 500 JSON naming the error instead."""
|
||||||
|
old = tmp_path / "old"
|
||||||
|
old.mkdir()
|
||||||
|
con = sqlite3.connect(old / "collector.sqlite")
|
||||||
|
con.executescript(
|
||||||
|
"CREATE TABLE items (id TEXT PRIMARY KEY, booth TEXT NOT NULL, order_ref TEXT NOT NULL,"
|
||||||
|
" at TEXT NOT NULL, service TEXT NOT NULL, vision_class TEXT NOT NULL,"
|
||||||
|
" vision_confidence REAL NOT NULL, image_width INTEGER NOT NULL, image_height INTEGER NOT NULL,"
|
||||||
|
" plate_blurred INTEGER NOT NULL, image_path TEXT NOT NULL, received_at TEXT NOT NULL,"
|
||||||
|
" review_label TEXT, reviewed_at TEXT, reviewer TEXT)"
|
||||||
|
)
|
||||||
|
con.commit()
|
||||||
|
con.close()
|
||||||
|
Handler.jobs = Jobs(old, tmp_path / "out", "https://example.invalid/pkg", "tok")
|
||||||
|
httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||||||
|
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||||
|
try:
|
||||||
|
req = urllib.request.Request(f"http://127.0.0.1:{httpd.server_address[1]}/readiness")
|
||||||
|
with pytest.raises(urllib.error.HTTPError) as ei:
|
||||||
|
urllib.request.urlopen(req, timeout=10)
|
||||||
|
assert ei.value.code == 500
|
||||||
|
body = json.loads(ei.value.read())
|
||||||
|
assert "no such column: kind" in body["error"]
|
||||||
|
# /health does not touch the DB and still answers.
|
||||||
|
with urllib.request.urlopen(f"http://127.0.0.1:{httpd.server_address[1]}/health", timeout=10) as r:
|
||||||
|
assert r.status == 200
|
||||||
|
finally:
|
||||||
|
httpd.shutdown()
|
||||||
|
httpd.server_close()
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
"""The training job end to end on the synthetic volume — needs the `train` extra (torch);
|
||||||
|
skipped where it is not installed (CI syncs without it, like the vision service)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
torch = pytest.importorskip("torch")
|
||||||
|
|
||||||
|
from trainer.cli import main # noqa: E402
|
||||||
|
from trainer.infer import OnnxClassifier # noqa: E402
|
||||||
|
from trainer.preprocess import Sidecar # noqa: E402
|
||||||
|
|
||||||
|
COMMON = ["--no-pretrained", "--input-size", "64", "--no-cache", "--seed", "3"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_features_run_writes_model_sidecar_report_and_evaluates(
|
||||||
|
collector_dir: Path, tmp_path: Path, capsys
|
||||||
|
) -> None: # type: ignore[no-untyped-def]
|
||||||
|
out = tmp_path / "out"
|
||||||
|
rc = main(
|
||||||
|
[
|
||||||
|
"train",
|
||||||
|
"--data",
|
||||||
|
str(collector_dir),
|
||||||
|
"--out",
|
||||||
|
str(out),
|
||||||
|
"--version",
|
||||||
|
"vtest",
|
||||||
|
"--mode",
|
||||||
|
"features",
|
||||||
|
"--epochs",
|
||||||
|
"150",
|
||||||
|
"--min-accuracy",
|
||||||
|
"0.0",
|
||||||
|
*COMMON,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
assert rc == 0
|
||||||
|
d = out / "vtest"
|
||||||
|
assert {p.name for p in d.iterdir()} == {"bodytype.onnx", "bodytype.json", "report.md", "metrics.json"}
|
||||||
|
side = Sidecar.read(d / "bodytype.json")
|
||||||
|
assert side.classes == ["sedan", "suv", "van"] and side.input_size == 64 and side.mode == "features"
|
||||||
|
assert side.labels == {"train": 96, "val": 24} and side.metrics["floor"] == 0.0
|
||||||
|
metrics = json.loads((d / "metrics.json").read_text())
|
||||||
|
assert metrics["n"] == 24 and metrics["onnx_agreement"] == 1.0
|
||||||
|
# Colour-coded classes: even a random backbone's pooled features separate them.
|
||||||
|
assert metrics["accuracy"] >= 0.9
|
||||||
|
report = (d / "report.md").read_text()
|
||||||
|
assert (
|
||||||
|
"MODEL WRITTEN" in report
|
||||||
|
and "truck (5)" in report
|
||||||
|
and "crop is missing on disk (skipped): 1" in report
|
||||||
|
)
|
||||||
|
|
||||||
|
# The exported graph takes raw 0–255 RGB and answers by itself.
|
||||||
|
clf = OnnxClassifier(d / "bodytype.onnx")
|
||||||
|
probs, kept = clf.predict_files(
|
||||||
|
[s for s in sorted((collector_dir / "crops" / "booth-2").glob("*.jpg"))][:6]
|
||||||
|
)
|
||||||
|
assert probs.shape == (6, 3) and kept == [0, 1, 2, 3, 4, 5]
|
||||||
|
assert np.allclose(probs.sum(axis=1), 1.0, atol=1e-4)
|
||||||
|
|
||||||
|
# evaluate: labels reviewed after training (none — the fixture's reviews predate it) and the
|
||||||
|
# unlabelled pile (6 entry samples).
|
||||||
|
capsys.readouterr()
|
||||||
|
assert main(["evaluate", "--data", str(collector_dir), "--model", str(d / "bodytype.onnx")]) == 0
|
||||||
|
res = json.loads(capsys.readouterr().out)
|
||||||
|
assert res["model"] == "vtest" and res["reviewedSince"] is None
|
||||||
|
assert res["unlabelled"]["n"] == 6 and sum(res["unlabelled"]["predicted"].values()) == 6
|
||||||
|
assert (
|
||||||
|
main(
|
||||||
|
[
|
||||||
|
"evaluate",
|
||||||
|
"--data",
|
||||||
|
str(collector_dir),
|
||||||
|
"--model",
|
||||||
|
str(d / "bodytype.onnx"),
|
||||||
|
"--since",
|
||||||
|
"2026-09-01T00:00:00Z",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
== 0
|
||||||
|
)
|
||||||
|
res2 = json.loads(capsys.readouterr().out)
|
||||||
|
assert res2["reviewedSince"]["n"] == 125 - 5 # trucks are not a class the model knows
|
||||||
|
|
||||||
|
|
||||||
|
def test_below_the_floor_writes_the_report_but_no_model(collector_dir: Path, tmp_path: Path) -> None:
|
||||||
|
out = tmp_path / "out"
|
||||||
|
rc = main(
|
||||||
|
[
|
||||||
|
"train",
|
||||||
|
"--data",
|
||||||
|
str(collector_dir),
|
||||||
|
"--out",
|
||||||
|
str(out),
|
||||||
|
"--version",
|
||||||
|
"vlow",
|
||||||
|
"--mode",
|
||||||
|
"features",
|
||||||
|
"--epochs",
|
||||||
|
"5",
|
||||||
|
"--min-accuracy",
|
||||||
|
"1.01",
|
||||||
|
*COMMON,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
assert rc == 3
|
||||||
|
d = out / "vlow"
|
||||||
|
assert {p.name for p in d.iterdir()} == {"report.md", "metrics.json"}
|
||||||
|
assert "MODEL NOT WRITTEN" in (d / "report.md").read_text()
|
||||||
|
|
||||||
|
|
||||||
|
def test_not_enough_labels_is_exit_2(collector_dir: Path, tmp_path: Path) -> None:
|
||||||
|
out = tmp_path / "out"
|
||||||
|
rc = main(["train", "--data", str(collector_dir), "--out", str(out), "--min-per-class", "100", *COMMON])
|
||||||
|
assert rc == 2
|
||||||
|
assert not out.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_finetune_runs_and_uses_the_feature_cache(collector_dir: Path, tmp_path: Path) -> None:
|
||||||
|
out = tmp_path / "out"
|
||||||
|
args = [
|
||||||
|
"train",
|
||||||
|
"--data",
|
||||||
|
str(collector_dir),
|
||||||
|
"--out",
|
||||||
|
str(out),
|
||||||
|
"--mode",
|
||||||
|
"finetune",
|
||||||
|
"--backbone",
|
||||||
|
"mobilenet_v3_small",
|
||||||
|
"--epochs",
|
||||||
|
"1",
|
||||||
|
"--batch",
|
||||||
|
"16",
|
||||||
|
"--min-accuracy",
|
||||||
|
"0.0",
|
||||||
|
"--no-pretrained",
|
||||||
|
"--input-size",
|
||||||
|
"64",
|
||||||
|
"--seed",
|
||||||
|
"3",
|
||||||
|
]
|
||||||
|
assert main([*args, "--version", "vft"]) == 0
|
||||||
|
cache = out / "cache" / "features-mobilenet_v3_small-64.npz"
|
||||||
|
assert cache.exists()
|
||||||
|
z = np.load(cache)
|
||||||
|
assert len(z["ids"]) == 96 and z["feats"].shape == (96, 576)
|
||||||
|
assert Sidecar.read(out / "vft" / "bodytype.json").mode == "finetune"
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
"""parking-trainer — the phase-B body-type classifier job.
|
||||||
|
|
||||||
|
Reads the wash collector's SQLite + crops straight off its volume, splits by TIME, trains a
|
||||||
|
small classifier on a pretrained backbone, and writes the ONNX model + sidecar + report —
|
||||||
|
or refuses to write the model when validation is below the owner's floor.
|
||||||
|
See wiki/decisions/bodytype-classifier-training.md.
|
||||||
|
"""
|
||||||
@@ -0,0 +1,414 @@
|
|||||||
|
"""parking-trainer — inspect / train / evaluate / publish.
|
||||||
|
|
||||||
|
parking-trainer inspect --data /data
|
||||||
|
parking-trainer train --data /data --out /out [--mode features|finetune] [--min-accuracy 0.85]
|
||||||
|
parking-trainer evaluate --model /out/<version>/bodytype.onnx --data /data
|
||||||
|
parking-trainer publish /out/<version> --url https://<gitea>/api/packages/<owner>/generic/parking-bodytype
|
||||||
|
|
||||||
|
Exit codes: 0 ok · 2 not enough labels · 3 trained but below the floor (report written, model
|
||||||
|
NOT written) · 1 anything else.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from .data import (
|
||||||
|
VEHICLE_CLASSES,
|
||||||
|
class_weights,
|
||||||
|
load_labelled,
|
||||||
|
load_reviewed_since,
|
||||||
|
load_unlabelled,
|
||||||
|
make_split,
|
||||||
|
suggested_epochs,
|
||||||
|
summarise,
|
||||||
|
)
|
||||||
|
from .preprocess import CROP_MARGIN, Sidecar
|
||||||
|
from .report import compute_metrics, render_report
|
||||||
|
|
||||||
|
MODEL_FILE = "bodytype.onnx"
|
||||||
|
SIDECAR_FILE = "bodytype.json"
|
||||||
|
REPORT_FILE = "report.md"
|
||||||
|
METRICS_FILE = "metrics.json"
|
||||||
|
|
||||||
|
|
||||||
|
def _log(msg: str) -> None:
|
||||||
|
print(f"[trainer] {msg}", file=sys.stderr, flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _now() -> str:
|
||||||
|
return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# inspect
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_inspect(a: argparse.Namespace) -> int:
|
||||||
|
samples, missing = load_labelled(a.data)
|
||||||
|
split = make_split(samples, a.val_fraction, a.min_per_class)
|
||||||
|
out = {
|
||||||
|
"labelled": summarise(samples),
|
||||||
|
"missingCrops": missing,
|
||||||
|
"run": {
|
||||||
|
"classes": list(split.classes),
|
||||||
|
"train": split.counts("train"),
|
||||||
|
"val": split.counts("val"),
|
||||||
|
"dropped": split.dropped,
|
||||||
|
"minPerClass": a.min_per_class,
|
||||||
|
"valFraction": a.val_fraction,
|
||||||
|
},
|
||||||
|
"ready": len(split.classes) >= 2,
|
||||||
|
}
|
||||||
|
print(json.dumps(out, indent=2))
|
||||||
|
return 0 if out["ready"] else 2
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# train
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_train(a: argparse.Namespace) -> int:
|
||||||
|
try:
|
||||||
|
from . import model as M
|
||||||
|
except ImportError as exc: # torch missing
|
||||||
|
_log(f"the training stack is not installed ({exc}); install with: uv sync --extra train")
|
||||||
|
return 1
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
samples, missing = load_labelled(a.data)
|
||||||
|
split = make_split(samples, a.val_fraction, a.min_per_class)
|
||||||
|
if len(split.classes) < 2:
|
||||||
|
_log(
|
||||||
|
f"not enough labels: {len(samples)} usable, classes with >= {a.min_per_class}: "
|
||||||
|
f"{list(split.classes)} (dropped {split.dropped}); nothing to train"
|
||||||
|
)
|
||||||
|
return 2
|
||||||
|
version = a.version or datetime.now(timezone.utc).strftime("v%Y%m%d-%H%M")
|
||||||
|
out_dir = a.out / version
|
||||||
|
epochs = a.epochs or suggested_epochs(len(split.train), a.mode)
|
||||||
|
weights = class_weights(split)
|
||||||
|
idx = split.class_index
|
||||||
|
_log(
|
||||||
|
f"{version}: {a.mode} on {a.backbone}, classes {list(split.classes)}, "
|
||||||
|
f"{len(split.train)} train / {len(split.val)} val, {epochs} epochs"
|
||||||
|
)
|
||||||
|
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
x_train, train = M.load_images(split.train, a.input_size)
|
||||||
|
x_val, val = M.load_images(split.val, a.input_size)
|
||||||
|
y_train = [idx[s.label] for s in train]
|
||||||
|
y_val = [idx[s.label] for s in val]
|
||||||
|
_log(f"decoded {len(train)} + {len(val)} crops in {time.perf_counter() - t0:.1f}s")
|
||||||
|
if len(val) == 0 or len(set(y_train)) < 2:
|
||||||
|
_log("not enough decodable crops on both sides of the split")
|
||||||
|
return 2
|
||||||
|
|
||||||
|
backbone = M.build_backbone(a.backbone, pretrained=not a.no_pretrained)
|
||||||
|
cache = (
|
||||||
|
None if a.no_cache else M.FeatureCache(a.out / "cache" / f"features-{a.backbone}-{a.input_size}.npz")
|
||||||
|
)
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
f_train = M.features_for(backbone, train, x_train, cache)
|
||||||
|
_log(
|
||||||
|
f"features for {len(train)} train crops in {time.perf_counter() - t0:.1f}s "
|
||||||
|
f"(cache: {cache.path if cache else 'off'})"
|
||||||
|
)
|
||||||
|
head_epochs = epochs if a.mode == "features" else max(30, epochs * 5)
|
||||||
|
head = M.train_head(f_train, y_train, len(split.classes), weights, head_epochs, seed=a.seed)
|
||||||
|
net = M.Classifier.make(backbone, head)
|
||||||
|
|
||||||
|
if a.mode == "finetune":
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
net = M.finetune(
|
||||||
|
net, x_train, y_train, weights, epochs, batch=a.batch, lr=a.lr, seed=a.seed, log=_log
|
||||||
|
)
|
||||||
|
_log(f"fine-tuned in {(time.perf_counter() - t0) / 60:.1f} min")
|
||||||
|
|
||||||
|
logits = M.predict_logits(net, x_val)
|
||||||
|
y_pred = logits.argmax(axis=1).tolist()
|
||||||
|
metrics = compute_metrics(split.classes, y_val, y_pred, camera=[s.vision_class for s in val])
|
||||||
|
|
||||||
|
# Export and check the graph gives the same answers as the torch model.
|
||||||
|
out_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
tmp_model = out_dir / (MODEL_FILE + ".tmp")
|
||||||
|
M.export_onnx(net, a.input_size, tmp_model)
|
||||||
|
from .infer import OnnxClassifier
|
||||||
|
|
||||||
|
sidecar = Sidecar(
|
||||||
|
version=version,
|
||||||
|
classes=list(split.classes),
|
||||||
|
input_size=a.input_size,
|
||||||
|
backbone=a.backbone,
|
||||||
|
mode=a.mode,
|
||||||
|
trained_at=_now(),
|
||||||
|
labels={"train": len(train), "val": len(val)},
|
||||||
|
)
|
||||||
|
tmp_side = out_dir / (SIDECAR_FILE + ".tmp")
|
||||||
|
sidecar.write(tmp_side)
|
||||||
|
onnx_pred = OnnxClassifier(tmp_model, tmp_side).predict_inputs(x_val.astype(np.float32)).argmax(axis=1)
|
||||||
|
metrics.onnx_agreement = float((onnx_pred == np.array(y_pred)).mean()) if len(y_pred) else None
|
||||||
|
sidecar.metrics = {
|
||||||
|
"accuracy": metrics.accuracy,
|
||||||
|
"macroRecall": metrics.macro_recall,
|
||||||
|
"perClass": {c: m.__dict__ for c, m in metrics.per_class.items()},
|
||||||
|
"floor": a.min_accuracy,
|
||||||
|
}
|
||||||
|
|
||||||
|
written = metrics.accuracy >= a.min_accuracy and (metrics.onnx_agreement or 0.0) >= 0.99
|
||||||
|
notes = []
|
||||||
|
if metrics.onnx_agreement is not None and metrics.onnx_agreement < 0.99:
|
||||||
|
notes.append(
|
||||||
|
f"ONNX export disagrees with the torch model ({metrics.onnx_agreement:.3f}); model withheld"
|
||||||
|
)
|
||||||
|
report = render_report(
|
||||||
|
version=version,
|
||||||
|
trained_at=sidecar.trained_at,
|
||||||
|
mode=a.mode,
|
||||||
|
backbone=a.backbone,
|
||||||
|
epochs=epochs,
|
||||||
|
classes=split.classes,
|
||||||
|
train_counts=split.counts("train"),
|
||||||
|
val_counts=split.counts("val"),
|
||||||
|
dropped=split.dropped,
|
||||||
|
missing_files=missing,
|
||||||
|
weights=weights,
|
||||||
|
metrics=metrics,
|
||||||
|
min_accuracy=a.min_accuracy,
|
||||||
|
written=written,
|
||||||
|
notes=notes,
|
||||||
|
)
|
||||||
|
(out_dir / REPORT_FILE).write_text(report)
|
||||||
|
(out_dir / METRICS_FILE).write_text(json.dumps(metrics.to_dict(), indent=2) + "\n")
|
||||||
|
if written:
|
||||||
|
sidecar.write(out_dir / SIDECAR_FILE)
|
||||||
|
tmp_model.replace(out_dir / MODEL_FILE)
|
||||||
|
tmp_side.unlink(missing_ok=True)
|
||||||
|
_log(
|
||||||
|
f"MODEL WRITTEN: {out_dir / MODEL_FILE} "
|
||||||
|
f"(accuracy {metrics.accuracy:.3f} >= floor {a.min_accuracy})"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
tmp_model.unlink(missing_ok=True)
|
||||||
|
tmp_side.unlink(missing_ok=True)
|
||||||
|
_log(
|
||||||
|
f"MODEL NOT WRITTEN: accuracy {metrics.accuracy:.3f} < floor {a.min_accuracy}; "
|
||||||
|
f"see {out_dir / REPORT_FILE}"
|
||||||
|
)
|
||||||
|
print(report)
|
||||||
|
return 0 if written else 3
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# evaluate — an existing model against labels that arrived AFTER it was trained, and its
|
||||||
|
# view of the unlabelled pile (the ongoing accuracy check without labelling everything)
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_evaluate(a: argparse.Namespace) -> int:
|
||||||
|
from .infer import OnnxClassifier
|
||||||
|
|
||||||
|
clf = OnnxClassifier(a.model)
|
||||||
|
since = a.since or clf.sidecar.trained_at
|
||||||
|
classes = clf.classes
|
||||||
|
result: dict[str, object] = {"model": clf.sidecar.version, "classes": classes, "since": since}
|
||||||
|
|
||||||
|
reviewed = [s for s in load_reviewed_since(a.data, since) if s.label in classes]
|
||||||
|
if reviewed:
|
||||||
|
probs, kept = clf.predict_files([s.path for s in reviewed])
|
||||||
|
rows = [reviewed[i] for i in kept]
|
||||||
|
y_true = [classes.index(s.label) for s in rows]
|
||||||
|
y_pred = probs.argmax(axis=1).tolist()
|
||||||
|
m = compute_metrics(classes, y_true, y_pred, camera=[s.vision_class for s in rows])
|
||||||
|
result["reviewedSince"] = m.to_dict()
|
||||||
|
else:
|
||||||
|
result["reviewedSince"] = None
|
||||||
|
|
||||||
|
pending = load_unlabelled(a.data, a.limit)
|
||||||
|
if pending:
|
||||||
|
probs, kept = clf.predict_files([s.path for s in pending])
|
||||||
|
rows = [pending[i] for i in kept]
|
||||||
|
pred = probs.argmax(axis=1)
|
||||||
|
conf = probs.max(axis=1)
|
||||||
|
hist = {c: int((pred == i).sum()) for i, c in enumerate(classes)}
|
||||||
|
result["unlabelled"] = {
|
||||||
|
"n": len(rows),
|
||||||
|
"predicted": hist,
|
||||||
|
"meanConfidence": float(conf.mean()) if len(rows) else None,
|
||||||
|
"belowHalf": int((conf < 0.5).sum()),
|
||||||
|
"agreesWithDetector": float(
|
||||||
|
sum(1 for p, s in zip(pred, rows, strict=True) if classes[int(p)] == s.vision_class)
|
||||||
|
/ len(rows)
|
||||||
|
)
|
||||||
|
if rows
|
||||||
|
else None,
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
result["unlabelled"] = None
|
||||||
|
print(json.dumps(result, indent=2))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# publish — the versioned files to a Gitea generic package (weights are not code, they
|
||||||
|
# do not live in git; the vision image fetches them by URL at build)
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_publish(a: argparse.Namespace) -> int:
|
||||||
|
d: Path = a.dir
|
||||||
|
files = [d / MODEL_FILE, d / SIDECAR_FILE, d / REPORT_FILE, d / METRICS_FILE]
|
||||||
|
for f in files[:2]:
|
||||||
|
if not f.exists():
|
||||||
|
_log(f"{f} missing — nothing to publish (a run below the floor writes no model)")
|
||||||
|
return 1
|
||||||
|
version = Sidecar.read(d / SIDECAR_FILE).version
|
||||||
|
if not a.url:
|
||||||
|
_log("no publish url: pass --url or set TRAINER_PUBLISH_URL")
|
||||||
|
return 1
|
||||||
|
token = a.token or os.environ.get("TRAINER_PUBLISH_TOKEN", "")
|
||||||
|
if not token:
|
||||||
|
_log("no token: pass --token or set TRAINER_PUBLISH_TOKEN")
|
||||||
|
return 1
|
||||||
|
base = a.url.rstrip("/") + "/" + version
|
||||||
|
for f in files:
|
||||||
|
if not f.exists():
|
||||||
|
continue
|
||||||
|
req = urllib.request.Request(f"{base}/{f.name}", data=f.read_bytes(), method="PUT")
|
||||||
|
req.add_header("Authorization", f"token {token}")
|
||||||
|
req.add_header("Content-Type", "application/octet-stream")
|
||||||
|
with urllib.request.urlopen(req, timeout=120) as r:
|
||||||
|
_log(f"PUT {base}/{f.name} → {r.status}")
|
||||||
|
_log(f"published {version}; pin it in apps/vision/models/bodytype.version and rebuild the vision image")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_serve(a: argparse.Namespace) -> int:
|
||||||
|
from .server import serve
|
||||||
|
|
||||||
|
serve(
|
||||||
|
a.data,
|
||||||
|
a.out,
|
||||||
|
a.host,
|
||||||
|
a.port,
|
||||||
|
os.environ.get("TRAINER_PUBLISH_URL", ""),
|
||||||
|
os.environ.get("TRAINER_PUBLISH_TOKEN", ""),
|
||||||
|
)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def build_parser() -> argparse.ArgumentParser:
|
||||||
|
p = argparse.ArgumentParser(
|
||||||
|
prog="parking-trainer", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
|
||||||
|
)
|
||||||
|
sub = p.add_subparsers(dest="cmd", required=True)
|
||||||
|
|
||||||
|
def data_args(sp: argparse.ArgumentParser) -> None:
|
||||||
|
sp.add_argument(
|
||||||
|
"--data",
|
||||||
|
type=Path,
|
||||||
|
default=Path(os.environ.get("TRAINER_DATA_DIR", "/data")),
|
||||||
|
help="collector volume (collector.sqlite + crops/)",
|
||||||
|
)
|
||||||
|
|
||||||
|
def split_args(sp: argparse.ArgumentParser) -> None:
|
||||||
|
sp.add_argument(
|
||||||
|
"--min-per-class",
|
||||||
|
type=int,
|
||||||
|
default=20,
|
||||||
|
help="classes with fewer reviewed crops are dropped from the run",
|
||||||
|
)
|
||||||
|
sp.add_argument(
|
||||||
|
"--val-fraction", type=float, default=0.2, help="newest fraction held out for validation"
|
||||||
|
)
|
||||||
|
|
||||||
|
i = sub.add_parser("inspect", help="what a run would train on")
|
||||||
|
data_args(i)
|
||||||
|
split_args(i)
|
||||||
|
i.set_defaults(fn=cmd_inspect)
|
||||||
|
|
||||||
|
t = sub.add_parser("train", help="train, evaluate, export (or refuse)")
|
||||||
|
data_args(t)
|
||||||
|
split_args(t)
|
||||||
|
t.add_argument(
|
||||||
|
"--out",
|
||||||
|
type=Path,
|
||||||
|
default=Path(os.environ.get("TRAINER_OUT_DIR", "/out")),
|
||||||
|
help="output root; a <version>/ folder is created under it",
|
||||||
|
)
|
||||||
|
t.add_argument("--mode", choices=["features", "finetune"], default="features")
|
||||||
|
t.add_argument(
|
||||||
|
"--backbone", choices=["resnet18", "mobilenet_v3_small", "efficientnet_b0"], default="resnet18"
|
||||||
|
)
|
||||||
|
t.add_argument("--epochs", type=int, default=0, help="0 = pick from the data size")
|
||||||
|
t.add_argument("--batch", type=int, default=32)
|
||||||
|
t.add_argument("--lr", type=float, default=1e-4, help="fine-tune learning rate")
|
||||||
|
t.add_argument("--input-size", type=int, default=224)
|
||||||
|
t.add_argument(
|
||||||
|
"--min-accuracy", type=float, default=0.85, help="validation floor below which NO model is written"
|
||||||
|
)
|
||||||
|
t.add_argument("--version", default="", help="model version (default v<date>-<time>)")
|
||||||
|
t.add_argument("--seed", type=int, default=7)
|
||||||
|
t.add_argument(
|
||||||
|
"--no-pretrained", action="store_true", help="random init (tests only — never for a real run)"
|
||||||
|
)
|
||||||
|
t.add_argument("--no-cache", action="store_true", help="do not read/write the feature cache")
|
||||||
|
t.set_defaults(fn=cmd_train)
|
||||||
|
|
||||||
|
e = sub.add_parser(
|
||||||
|
"evaluate", help="an existing model vs labels reviewed after it was trained + the unlabelled pile"
|
||||||
|
)
|
||||||
|
data_args(e)
|
||||||
|
e.add_argument(
|
||||||
|
"--model", type=Path, required=True, help="path to bodytype.onnx (sidecar .json beside it)"
|
||||||
|
)
|
||||||
|
e.add_argument("--since", default="", help="ISO time; default = the model's trained_at")
|
||||||
|
e.add_argument(
|
||||||
|
"--limit", type=int, default=2000, help="how many unlabelled crops to score (newest first)"
|
||||||
|
)
|
||||||
|
e.set_defaults(fn=cmd_evaluate)
|
||||||
|
|
||||||
|
u = sub.add_parser("publish", help="PUT a version folder to a Gitea generic package")
|
||||||
|
u.add_argument("dir", type=Path, help="the <version>/ folder a passing run wrote")
|
||||||
|
u.add_argument(
|
||||||
|
"--url",
|
||||||
|
default=os.environ.get("TRAINER_PUBLISH_URL", ""),
|
||||||
|
help="https://<gitea>/api/packages/<owner>/generic/parking-bodytype (or TRAINER_PUBLISH_URL)",
|
||||||
|
)
|
||||||
|
u.add_argument("--token", default="", help="Gitea token with package:write (or TRAINER_PUBLISH_TOKEN)")
|
||||||
|
u.set_defaults(fn=cmd_publish)
|
||||||
|
|
||||||
|
s = sub.add_parser("serve", help="the job API the collector's Training section talks to")
|
||||||
|
data_args(s)
|
||||||
|
s.add_argument("--out", type=Path, default=Path(os.environ.get("TRAINER_OUT_DIR", "/out")))
|
||||||
|
s.add_argument("--host", default=os.environ.get("TRAINER_HOST", "0.0.0.0"))
|
||||||
|
s.add_argument("--port", type=int, default=int(os.environ.get("TRAINER_PORT", "8091")))
|
||||||
|
s.set_defaults(fn=cmd_serve)
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str] | None = None) -> int:
|
||||||
|
a = build_parser().parse_args(argv)
|
||||||
|
try:
|
||||||
|
return int(a.fn(a))
|
||||||
|
except FileNotFoundError as exc:
|
||||||
|
_log(str(exc))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["main", "build_parser", "VEHICLE_CLASSES", "CROP_MARGIN"]
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
"""The training set: reviewed, usable rows off the collector's SQLite, and how they are
|
||||||
|
split and weighed. Pure Python + sqlite3 — no torch, so `inspect` and the tests run light.
|
||||||
|
|
||||||
|
Rules (wiki/decisions/bodytype-classifier-training.md):
|
||||||
|
- Only rows a REVIEWER labelled count; the operator's pick and the camera's class are
|
||||||
|
never labels. `unusable` rows are dropped.
|
||||||
|
- Split by TIME (`at` = when the vehicle was seen): validation = the newest slice, so
|
||||||
|
the number reflects tomorrow's traffic rather than a random shuffle of the same days.
|
||||||
|
- Classes with too few labels are dropped from the run (and reported), never trained
|
||||||
|
on a handful of examples that would make the softmax confidently wrong.
|
||||||
|
- Class imbalance is weighed in the loss (inverse frequency, damped) and reported.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import math
|
||||||
|
import sqlite3
|
||||||
|
from collections import Counter
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# The shared vocabulary (packages/shared VEHICLE_CLASSES) — the only labels a reviewer can
|
||||||
|
# give, and the only classes a model may emit. Order is the canonical one; the model's own
|
||||||
|
# class list (sidecar) is the subset it trained on, in this order.
|
||||||
|
VEHICLE_CLASSES: tuple[str, ...] = (
|
||||||
|
"car",
|
||||||
|
"sedan",
|
||||||
|
"hatchback",
|
||||||
|
"suv",
|
||||||
|
"minivan",
|
||||||
|
"pickup",
|
||||||
|
"van",
|
||||||
|
"truck",
|
||||||
|
"bus",
|
||||||
|
"motorcycle",
|
||||||
|
)
|
||||||
|
|
||||||
|
DB_FILE = "collector.sqlite"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Sample:
|
||||||
|
item: str
|
||||||
|
booth: str
|
||||||
|
kind: str # wash | entry
|
||||||
|
at: str # ISO-8601, when the vehicle was seen (the split key)
|
||||||
|
label: str # the reviewer's class
|
||||||
|
vision_class: str # what the detector said (for the report, never a label)
|
||||||
|
path: Path # the crop on disk
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Split:
|
||||||
|
classes: tuple[str, ...]
|
||||||
|
train: list[Sample]
|
||||||
|
val: list[Sample]
|
||||||
|
dropped: dict[str, int] # class → count, below the per-class minimum
|
||||||
|
missing_files: int # labelled rows whose crop is not on disk
|
||||||
|
|
||||||
|
@property
|
||||||
|
def class_index(self) -> dict[str, int]:
|
||||||
|
return {c: i for i, c in enumerate(self.classes)}
|
||||||
|
|
||||||
|
def counts(self, part: str) -> dict[str, int]:
|
||||||
|
rows = self.train if part == "train" else self.val
|
||||||
|
c = Counter(s.label for s in rows)
|
||||||
|
return {k: c.get(k, 0) for k in self.classes}
|
||||||
|
|
||||||
|
|
||||||
|
_SELECT = "SELECT id, booth, kind, at, review_label, vision_class, image_path FROM items "
|
||||||
|
|
||||||
|
|
||||||
|
def _rows(
|
||||||
|
data_dir: Path, where: str, params: tuple[object, ...], db_file: Path | None = None
|
||||||
|
) -> list[Sample]:
|
||||||
|
db = db_file or data_dir / DB_FILE
|
||||||
|
if not db.exists():
|
||||||
|
raise FileNotFoundError(f"collector database not found: {db}")
|
||||||
|
con = sqlite3.connect(f"file:{db}?mode=ro", uri=True)
|
||||||
|
try:
|
||||||
|
rows = con.execute(_SELECT + where, params).fetchall()
|
||||||
|
finally:
|
||||||
|
con.close()
|
||||||
|
out: list[Sample] = []
|
||||||
|
for item, booth, kind, at, label, vision_class, image_path in rows:
|
||||||
|
out.append(Sample(item, booth, kind, at, label or "", vision_class, data_dir / image_path))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def load_labelled(data_dir: Path, db_file: Path | None = None) -> tuple[list[Sample], int]:
|
||||||
|
"""Every reviewed, usable row with its crop path resolved. Returns (samples, missing)
|
||||||
|
where `missing` counts rows whose crop file is gone (pruned/moved) — skipped."""
|
||||||
|
rows = _rows(
|
||||||
|
data_dir,
|
||||||
|
"WHERE reviewed_at IS NOT NULL AND review_label != 'unusable' ORDER BY at, id",
|
||||||
|
(),
|
||||||
|
db_file,
|
||||||
|
)
|
||||||
|
out: list[Sample] = []
|
||||||
|
missing = 0
|
||||||
|
for s in rows:
|
||||||
|
if s.label not in VEHICLE_CLASSES:
|
||||||
|
continue # a label outside the vocabulary can only be a future/foreign row
|
||||||
|
if not s.path.is_file():
|
||||||
|
missing += 1
|
||||||
|
continue
|
||||||
|
out.append(s)
|
||||||
|
return out, missing
|
||||||
|
|
||||||
|
|
||||||
|
def load_reviewed_since(data_dir: Path, since: str, db_file: Path | None = None) -> list[Sample]:
|
||||||
|
"""Usable labels whose REVIEW happened after `since` (ISO) — a clean held-out check for a
|
||||||
|
model trained before then. Rows whose crop is gone are skipped."""
|
||||||
|
rows = _rows(
|
||||||
|
data_dir,
|
||||||
|
"WHERE reviewed_at IS NOT NULL AND review_label != 'unusable' AND reviewed_at > ? "
|
||||||
|
"ORDER BY reviewed_at, id",
|
||||||
|
(since,),
|
||||||
|
db_file,
|
||||||
|
)
|
||||||
|
return [s for s in rows if s.label in VEHICLE_CLASSES and s.path.is_file()]
|
||||||
|
|
||||||
|
|
||||||
|
def load_unlabelled(data_dir: Path, limit: int = 2000, db_file: Path | None = None) -> list[Sample]:
|
||||||
|
"""The pending pile, newest first — what the model would say about traffic nobody has
|
||||||
|
labelled (its class histogram and detector agreement are the cheap drift check)."""
|
||||||
|
rows = _rows(data_dir, "WHERE reviewed_at IS NULL ORDER BY received_at DESC LIMIT ?", (limit,), db_file)
|
||||||
|
return [s for s in rows if s.path.is_file()]
|
||||||
|
|
||||||
|
|
||||||
|
def make_split(samples: list[Sample], val_fraction: float = 0.2, min_per_class: int = 20) -> Split:
|
||||||
|
"""Drop thin classes, then cut by time: the newest `val_fraction` is validation."""
|
||||||
|
if not 0.0 < val_fraction < 1.0:
|
||||||
|
raise ValueError("val_fraction must be in (0, 1)")
|
||||||
|
counts = Counter(s.label for s in samples)
|
||||||
|
kept = tuple(c for c in VEHICLE_CLASSES if counts.get(c, 0) >= min_per_class)
|
||||||
|
dropped = {c: n for c, n in counts.items() if c not in kept}
|
||||||
|
rows = sorted((s for s in samples if s.label in kept), key=lambda s: (s.at, s.item))
|
||||||
|
n_val = int(round(len(rows) * val_fraction))
|
||||||
|
if rows and n_val == 0:
|
||||||
|
n_val = 1
|
||||||
|
cut = len(rows) - n_val
|
||||||
|
return Split(classes=kept, train=rows[:cut], val=rows[cut:], dropped=dropped, missing_files=0)
|
||||||
|
|
||||||
|
|
||||||
|
def class_weights(split: Split, damping: float = 0.5) -> list[float]:
|
||||||
|
"""Inverse-frequency weights for the loss, damped by `damping` (0.5 = square root, so a
|
||||||
|
1:9 imbalance becomes 1:3 rather than 1:9 — full inverse weights over-correct on small
|
||||||
|
sets). Normalised to mean 1 so the learning rate keeps its meaning."""
|
||||||
|
counts = split.counts("train")
|
||||||
|
total = sum(counts.values())
|
||||||
|
k = len(split.classes)
|
||||||
|
raw = [(total / (k * max(1, counts[c]))) ** damping for c in split.classes]
|
||||||
|
mean = sum(raw) / max(1, len(raw))
|
||||||
|
return [w / mean for w in raw]
|
||||||
|
|
||||||
|
|
||||||
|
def summarise(samples: list[Sample]) -> dict[str, object]:
|
||||||
|
"""What `inspect` prints: per-class counts, per-booth counts, time range."""
|
||||||
|
by_class = Counter(s.label for s in samples)
|
||||||
|
by_booth = Counter(s.booth for s in samples)
|
||||||
|
by_kind = Counter(s.kind for s in samples)
|
||||||
|
ats = sorted(s.at for s in samples)
|
||||||
|
return {
|
||||||
|
"total": len(samples),
|
||||||
|
"byClass": {c: by_class.get(c, 0) for c in VEHICLE_CLASSES if by_class.get(c, 0)},
|
||||||
|
"byBooth": dict(sorted(by_booth.items())),
|
||||||
|
"byKind": dict(sorted(by_kind.items())),
|
||||||
|
"from": ats[0] if ats else None,
|
||||||
|
"to": ats[-1] if ats else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def suggested_epochs(n_train: int, mode: str) -> int:
|
||||||
|
"""A sane default when the owner gives none: enough passes for a small set, fewer as
|
||||||
|
it grows. Feature-extraction heads converge fast; fine-tunes need more but cost more."""
|
||||||
|
if mode == "features":
|
||||||
|
return int(min(200, max(30, 4000 / max(1, n_train) * 10)))
|
||||||
|
return int(min(30, max(8, math.ceil(3000 / max(1, n_train)) * 4)))
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
"""Run an exported classifier (ONNX + sidecar) — torch-free. Used by `evaluate` and by
|
||||||
|
the tests; the vision service carries its own, equivalent, reader (vehicle.py)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from .preprocess import Sidecar, load_input, softmax
|
||||||
|
|
||||||
|
|
||||||
|
class OnnxClassifier:
|
||||||
|
def __init__(self, model_path: Path, sidecar_path: Path | None = None) -> None:
|
||||||
|
import onnxruntime as ort
|
||||||
|
|
||||||
|
self.model_path = Path(model_path)
|
||||||
|
self.sidecar = Sidecar.read(sidecar_path or self.model_path.with_suffix(".json"))
|
||||||
|
opts = ort.SessionOptions()
|
||||||
|
opts.intra_op_num_threads = 2
|
||||||
|
self._session = ort.InferenceSession(
|
||||||
|
str(self.model_path), sess_options=opts, providers=["CPUExecutionProvider"]
|
||||||
|
)
|
||||||
|
self._input = self._session.get_inputs()[0].name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def classes(self) -> list[str]:
|
||||||
|
return list(self.sidecar.classes)
|
||||||
|
|
||||||
|
def predict_inputs(self, x: Any, batch: int = 64) -> Any:
|
||||||
|
"""[N,3,S,S] float32 → probabilities [N,K]."""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
outs = []
|
||||||
|
for i in range(0, len(x), batch):
|
||||||
|
logits = self._session.run(None, {self._input: x[i : i + batch]})[0]
|
||||||
|
outs.append(softmax(logits))
|
||||||
|
return np.concatenate(outs, axis=0) if outs else np.zeros((0, len(self.classes)), np.float32)
|
||||||
|
|
||||||
|
def predict_files(self, paths: list[Path], batch: int = 64) -> tuple[Any, list[int]]:
|
||||||
|
"""Decode + classify crop files. Returns (probs, indices of paths that decoded)."""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
xs, kept = [], []
|
||||||
|
for i, p in enumerate(paths):
|
||||||
|
x = load_input(p, self.sidecar.input_size)
|
||||||
|
if x is not None:
|
||||||
|
xs.append(x)
|
||||||
|
kept.append(i)
|
||||||
|
if not xs:
|
||||||
|
return np.zeros((0, len(self.classes)), np.float32), []
|
||||||
|
return self.predict_inputs(np.stack(xs), batch), kept
|
||||||
@@ -0,0 +1,305 @@
|
|||||||
|
"""The network and the two ways of training it. Imports torch — only `train` reaches here;
|
||||||
|
everything else in the package stays torch-free (the `train` extra is heavy).
|
||||||
|
|
||||||
|
Backbone: a small ImageNet-pretrained torchvision model (BSD-3, weights included), used as
|
||||||
|
a feature extractor; head: one linear layer over its pooled features.
|
||||||
|
|
||||||
|
- "features" mode: the backbone is FROZEN. Every crop goes through it once, the vectors
|
||||||
|
are cached on disk (keyed by item id), and only the head is trained — minutes for a few
|
||||||
|
thousand crops, seconds to retrain when new labels arrive. Expected to carry most of
|
||||||
|
the accuracy on frontal gate views.
|
||||||
|
- "finetune" mode: warm-starts the head the same way, then unfreezes everything and trains
|
||||||
|
end to end with light augmentation. Roughly an hour on four Xeon cores for a few
|
||||||
|
thousand crops with a mobile-sized backbone; the step when the cheap mode plateaus.
|
||||||
|
|
||||||
|
The exported ONNX graph takes raw RGB 0–255 float pixels and normalises INSIDE
|
||||||
|
(see preprocess.py), so the vision service cannot get the constants wrong.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from .data import Sample
|
||||||
|
from .preprocess import IMAGENET_MEAN, IMAGENET_STD, load_input
|
||||||
|
|
||||||
|
BACKBONES: dict[str, int] = {"resnet18": 512, "mobilenet_v3_small": 576, "efficientnet_b0": 1280}
|
||||||
|
|
||||||
|
|
||||||
|
def _torch() -> Any:
|
||||||
|
import torch
|
||||||
|
|
||||||
|
torch.set_num_threads(max(1, os.cpu_count() or 1))
|
||||||
|
return torch
|
||||||
|
|
||||||
|
|
||||||
|
def build_backbone(name: str, pretrained: bool = True) -> Any:
|
||||||
|
"""torchvision model with its classifier removed → pooled feature vector."""
|
||||||
|
torch = _torch()
|
||||||
|
import torchvision.models as tvm
|
||||||
|
|
||||||
|
if name not in BACKBONES:
|
||||||
|
raise ValueError(f"unknown backbone {name!r} (choose from {', '.join(BACKBONES)})")
|
||||||
|
if name == "resnet18":
|
||||||
|
m = tvm.resnet18(weights=tvm.ResNet18_Weights.IMAGENET1K_V1 if pretrained else None)
|
||||||
|
m.fc = torch.nn.Identity()
|
||||||
|
elif name == "mobilenet_v3_small":
|
||||||
|
m = tvm.mobilenet_v3_small(
|
||||||
|
weights=tvm.MobileNet_V3_Small_Weights.IMAGENET1K_V1 if pretrained else None
|
||||||
|
)
|
||||||
|
m.classifier = torch.nn.Identity()
|
||||||
|
else:
|
||||||
|
m = tvm.efficientnet_b0(weights=tvm.EfficientNet_B0_Weights.IMAGENET1K_V1 if pretrained else None)
|
||||||
|
m.classifier = torch.nn.Identity()
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
class Classifier: # a factory, not a Module subclass at import time (torch is lazy)
|
||||||
|
@staticmethod
|
||||||
|
def make(backbone: Any, head: Any) -> Any:
|
||||||
|
torch = _torch()
|
||||||
|
|
||||||
|
class _Net(torch.nn.Module): # type: ignore[misc,name-defined]
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.backbone = backbone
|
||||||
|
self.head = head
|
||||||
|
self.register_buffer("mean", torch.tensor(IMAGENET_MEAN).view(1, 3, 1, 1))
|
||||||
|
self.register_buffer("std", torch.tensor(IMAGENET_STD).view(1, 3, 1, 1))
|
||||||
|
|
||||||
|
def forward(self, x: Any) -> Any:
|
||||||
|
x = (x / 255.0 - self.mean) / self.std
|
||||||
|
return self.head(self.backbone(x))
|
||||||
|
|
||||||
|
return _Net()
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# Images in memory (uint8 — a few thousand 224² crops is a few hundred MB; float32 would
|
||||||
|
# be four times that)
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def load_images(samples: list[Sample], input_size: int) -> tuple[Any, list[Sample]]:
|
||||||
|
"""Decode + resize every crop once. Returns (uint8 [N,3,S,S], the samples that decoded)."""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
xs, kept = [], []
|
||||||
|
for s in samples:
|
||||||
|
x = load_input(s.path, input_size)
|
||||||
|
if x is None:
|
||||||
|
continue
|
||||||
|
xs.append(x.astype(np.uint8))
|
||||||
|
kept.append(s)
|
||||||
|
if not xs:
|
||||||
|
return np.zeros((0, 3, input_size, input_size), np.uint8), []
|
||||||
|
return np.stack(xs), kept
|
||||||
|
|
||||||
|
|
||||||
|
def _batches(n: int, batch: int) -> list[slice]:
|
||||||
|
return [slice(i, min(n, i + batch)) for i in range(0, n, batch)]
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# Feature extraction (+ on-disk cache) and the head
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FeatureCache:
|
||||||
|
"""`<cache_dir>/features-<backbone>-<size>.npz`: item ids + vectors. Retraining the head
|
||||||
|
after new labels arrive only runs the backbone on the NEW crops."""
|
||||||
|
|
||||||
|
path: Path
|
||||||
|
|
||||||
|
def load(self) -> dict[str, Any]:
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if not self.path.exists():
|
||||||
|
return {}
|
||||||
|
z = np.load(self.path, allow_pickle=False)
|
||||||
|
return dict(zip(z["ids"].tolist(), z["feats"], strict=True))
|
||||||
|
|
||||||
|
def save(self, table: dict[str, Any]) -> None:
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
ids = np.array(list(table), dtype=str)
|
||||||
|
feats = np.stack(list(table.values())) if table else np.zeros((0, 0), np.float32)
|
||||||
|
np.savez(self.path, ids=ids, feats=feats)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_features(backbone: Any, x_u8: Any, batch: int = 64) -> Any:
|
||||||
|
"""Frozen forward pass → [N,D] float32 (normalisation applied here, as in the graph)."""
|
||||||
|
torch = _torch()
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
net = Classifier.make(backbone, torch.nn.Identity()).eval()
|
||||||
|
out = []
|
||||||
|
with torch.no_grad():
|
||||||
|
for sl in _batches(len(x_u8), batch):
|
||||||
|
xb = torch.from_numpy(x_u8[sl]).float()
|
||||||
|
out.append(net(xb).numpy())
|
||||||
|
return np.concatenate(out, axis=0) if out else np.zeros((0, 0), np.float32)
|
||||||
|
|
||||||
|
|
||||||
|
def features_for(
|
||||||
|
backbone: Any, samples: list[Sample], x_u8: Any, cache: FeatureCache | None, batch: int = 64
|
||||||
|
) -> Any:
|
||||||
|
"""Feature vectors for `samples` (aligned with x_u8), from the cache where present."""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
table = cache.load() if cache else {}
|
||||||
|
todo = [i for i, s in enumerate(samples) if s.item not in table]
|
||||||
|
if todo:
|
||||||
|
fresh = extract_features(backbone, x_u8[todo], batch)
|
||||||
|
for i, f in zip(todo, fresh, strict=True):
|
||||||
|
table[samples[i].item] = f.astype(np.float32)
|
||||||
|
if cache:
|
||||||
|
cache.save(table)
|
||||||
|
return np.stack([table[s.item] for s in samples]) if samples else np.zeros((0, 0), np.float32)
|
||||||
|
|
||||||
|
|
||||||
|
def train_head(
|
||||||
|
feats: Any,
|
||||||
|
y: list[int],
|
||||||
|
n_classes: int,
|
||||||
|
weights: list[float],
|
||||||
|
epochs: int,
|
||||||
|
lr: float = 1e-3,
|
||||||
|
seed: int = 7,
|
||||||
|
) -> Any:
|
||||||
|
"""Multinomial logistic regression on cached features (full batch, Adam, weighted CE)."""
|
||||||
|
torch = _torch()
|
||||||
|
torch.manual_seed(seed)
|
||||||
|
f = torch.from_numpy(feats).float()
|
||||||
|
t = torch.tensor(y, dtype=torch.long)
|
||||||
|
head = torch.nn.Linear(f.shape[1], n_classes)
|
||||||
|
opt = torch.optim.Adam(head.parameters(), lr=lr, weight_decay=1e-4)
|
||||||
|
loss_fn = torch.nn.CrossEntropyLoss(weight=torch.tensor(weights, dtype=torch.float32))
|
||||||
|
head.train()
|
||||||
|
for _ in range(epochs):
|
||||||
|
opt.zero_grad()
|
||||||
|
loss = loss_fn(head(f), t)
|
||||||
|
loss.backward()
|
||||||
|
opt.step()
|
||||||
|
return head.eval()
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
# Full fine-tune
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def _augment(xb: Any) -> Any:
|
||||||
|
"""Light, label-preserving augmentation on a float batch [B,3,S,S] (0–255): horizontal
|
||||||
|
flip (a gate view mirrored is still the same body type), a mild random zoom, and
|
||||||
|
brightness/contrast jitter (dusk, headlights, wet tarmac)."""
|
||||||
|
torch = _torch()
|
||||||
|
b, _, s, _ = xb.shape
|
||||||
|
flip = torch.rand(b) < 0.5
|
||||||
|
xb = torch.where(flip.view(b, 1, 1, 1), xb.flip(-1), xb)
|
||||||
|
# zoom: crop a random 85–100 % window and resize back
|
||||||
|
out = torch.empty_like(xb)
|
||||||
|
for i in range(b):
|
||||||
|
frac = float(torch.empty(1).uniform_(0.85, 1.0))
|
||||||
|
w = max(8, int(s * frac))
|
||||||
|
x0 = int(torch.randint(0, s - w + 1, (1,)))
|
||||||
|
y0 = int(torch.randint(0, s - w + 1, (1,)))
|
||||||
|
crop = xb[i : i + 1, :, y0 : y0 + w, x0 : x0 + w]
|
||||||
|
out[i : i + 1] = torch.nn.functional.interpolate(
|
||||||
|
crop, size=(s, s), mode="bilinear", align_corners=False
|
||||||
|
)
|
||||||
|
bright = torch.empty(b, 1, 1, 1).uniform_(-25, 25)
|
||||||
|
contrast = torch.empty(b, 1, 1, 1).uniform_(0.8, 1.2)
|
||||||
|
mean = out.mean(dim=(1, 2, 3), keepdim=True)
|
||||||
|
out = (out - mean) * contrast + mean + bright
|
||||||
|
return out.clamp_(0, 255)
|
||||||
|
|
||||||
|
|
||||||
|
def finetune(
|
||||||
|
model: Any,
|
||||||
|
x_u8: Any,
|
||||||
|
y: list[int],
|
||||||
|
weights: list[float],
|
||||||
|
epochs: int,
|
||||||
|
batch: int = 32,
|
||||||
|
lr: float = 1e-4,
|
||||||
|
seed: int = 7,
|
||||||
|
log: Any = None,
|
||||||
|
) -> Any:
|
||||||
|
torch = _torch()
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
torch.manual_seed(seed)
|
||||||
|
rng = np.random.default_rng(seed)
|
||||||
|
t = torch.tensor(y, dtype=torch.long)
|
||||||
|
opt = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-2)
|
||||||
|
steps = epochs * max(1, (len(y) + batch - 1) // batch)
|
||||||
|
sched = torch.optim.lr_scheduler.OneCycleLR(opt, max_lr=lr, total_steps=max(1, steps), pct_start=0.15)
|
||||||
|
loss_fn = torch.nn.CrossEntropyLoss(weight=torch.tensor(weights, dtype=torch.float32))
|
||||||
|
for epoch in range(epochs):
|
||||||
|
model.train()
|
||||||
|
order = rng.permutation(len(y))
|
||||||
|
total = 0.0
|
||||||
|
for sl in _batches(len(y), batch):
|
||||||
|
idx = order[sl]
|
||||||
|
xb = _augment(torch.from_numpy(x_u8[idx]).float())
|
||||||
|
opt.zero_grad()
|
||||||
|
loss = loss_fn(model(xb), t[idx])
|
||||||
|
loss.backward()
|
||||||
|
opt.step()
|
||||||
|
sched.step()
|
||||||
|
total += float(loss.detach()) * len(idx)
|
||||||
|
if log:
|
||||||
|
log(f"epoch {epoch + 1}/{epochs} loss {total / max(1, len(y)):.4f}")
|
||||||
|
return model.eval()
|
||||||
|
|
||||||
|
|
||||||
|
def predict_logits(model: Any, x_u8: Any, batch: int = 64) -> Any:
|
||||||
|
torch = _torch()
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
model.eval()
|
||||||
|
out = []
|
||||||
|
with torch.no_grad():
|
||||||
|
for sl in _batches(len(x_u8), batch):
|
||||||
|
out.append(model(torch.from_numpy(x_u8[sl]).float()).numpy())
|
||||||
|
return np.concatenate(out, axis=0) if out else np.zeros((0, 0), np.float32)
|
||||||
|
|
||||||
|
|
||||||
|
def export_onnx(model: Any, input_size: int, path: Path) -> None:
|
||||||
|
"""Export with the current (torch.export-based) exporter; fall back to the legacy
|
||||||
|
TorchScript one where the new path is unavailable or trips over an op. Whichever wrote
|
||||||
|
the graph, the job then checks it against the torch model (onnx_agreement) before the
|
||||||
|
file is kept."""
|
||||||
|
torch = _torch()
|
||||||
|
|
||||||
|
model.eval()
|
||||||
|
dummy = torch.zeros(1, 3, input_size, input_size)
|
||||||
|
names: dict[str, Any] = dict(input_names=["image"], output_names=["logits"])
|
||||||
|
try:
|
||||||
|
torch.onnx.export(
|
||||||
|
model,
|
||||||
|
(dummy,),
|
||||||
|
str(path),
|
||||||
|
dynamo=True,
|
||||||
|
dynamic_shapes={"x": {0: "batch"}},
|
||||||
|
opset_version=18,
|
||||||
|
external_data=False, # ONE file: the vision image bakes bodytype.onnx + .json, nothing else
|
||||||
|
verbose=False,
|
||||||
|
**names,
|
||||||
|
)
|
||||||
|
except Exception: # noqa: BLE001 - any failure → the legacy exporter
|
||||||
|
torch.onnx.export(
|
||||||
|
model,
|
||||||
|
dummy,
|
||||||
|
str(path),
|
||||||
|
dynamo=False,
|
||||||
|
dynamic_axes={"image": {0: "batch"}, "logits": {0: "batch"}},
|
||||||
|
opset_version=17,
|
||||||
|
**names,
|
||||||
|
)
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
"""Crop → model input. THE CONTRACT between the trainer and the vision service's classifier
|
||||||
|
stage: what the network sees at training time must be exactly what it sees on the booth.
|
||||||
|
|
||||||
|
The trainer does not share code with the vision service (different packages, different
|
||||||
|
images), so the contract is DATA: every constant here is written into the model's sidecar
|
||||||
|
(`bodytype.json`) and the vision side reads and applies them from there — nothing is
|
||||||
|
assumed on either side. Both use OpenCV with the same interpolation so the pixels match.
|
||||||
|
|
||||||
|
- input: the collector's crop (the detector's vehicle box + margin, plate blurred), or on
|
||||||
|
the booth the same cut made live from the frame (vehicle.py mirrors `makeReviewCrop`).
|
||||||
|
- resize: squash to input_size × input_size with INTER_AREA (the crop IS the vehicle; no
|
||||||
|
centre-crop that would lose a bumper or a roofline — the shape is the signal).
|
||||||
|
- colour: RGB, float32, 0–255. Normalisation (/255, ImageNet mean/std) lives INSIDE the
|
||||||
|
ONNX graph, so a consumer feeds raw pixels and cannot get the constants wrong.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from dataclasses import asdict, dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
SIDECAR_FORMAT = "parking-bodytype/1"
|
||||||
|
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
||||||
|
IMAGENET_STD = (0.229, 0.224, 0.225)
|
||||||
|
CROP_MARGIN = 0.08 # must equal CROP_MARGIN in apps/server review-outbox.ts
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Sidecar:
|
||||||
|
"""`bodytype.json` beside `bodytype.onnx`."""
|
||||||
|
|
||||||
|
version: str
|
||||||
|
classes: list[str]
|
||||||
|
input_size: int = 224
|
||||||
|
color: str = "rgb"
|
||||||
|
resize: str = "area"
|
||||||
|
crop_margin: float = CROP_MARGIN
|
||||||
|
normalization: str = "in-graph" # the ONNX divides by 255 and applies mean/std itself
|
||||||
|
mean: list[float] = field(default_factory=lambda: list(IMAGENET_MEAN))
|
||||||
|
std: list[float] = field(default_factory=lambda: list(IMAGENET_STD))
|
||||||
|
backbone: str = ""
|
||||||
|
mode: str = ""
|
||||||
|
trained_at: str = ""
|
||||||
|
labels: dict[str, int] = field(default_factory=dict) # train / val counts
|
||||||
|
metrics: dict[str, Any] = field(default_factory=dict) # accuracy, macro, per-class
|
||||||
|
format: str = SIDECAR_FORMAT
|
||||||
|
|
||||||
|
def write(self, path: Path) -> None:
|
||||||
|
path.write_text(json.dumps(asdict(self), indent=2) + "\n")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def read(cls, path: Path) -> Sidecar:
|
||||||
|
d = json.loads(path.read_text())
|
||||||
|
if d.get("format") != SIDECAR_FORMAT:
|
||||||
|
raise ValueError(f"{path}: unknown sidecar format {d.get('format')!r}")
|
||||||
|
known = {f for f in cls.__dataclass_fields__}
|
||||||
|
return cls(**{k: v for k, v in d.items() if k in known})
|
||||||
|
|
||||||
|
|
||||||
|
def load_input(path: Path, input_size: int) -> Any:
|
||||||
|
"""Decode a crop and produce the network input: RGB float32 CHW, 0–255, squashed to
|
||||||
|
input_size. Returns None when the file cannot be decoded."""
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
img = cv2.imread(str(path), cv2.IMREAD_COLOR)
|
||||||
|
if img is None:
|
||||||
|
return None
|
||||||
|
return array_to_input(img, input_size)
|
||||||
|
|
||||||
|
|
||||||
|
def array_to_input(bgr: Any, input_size: int) -> Any:
|
||||||
|
"""BGR uint8 HWC (OpenCV's native) → RGB float32 CHW 0–255 at input_size."""
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
resized = cv2.resize(bgr, (input_size, input_size), interpolation=cv2.INTER_AREA)
|
||||||
|
rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
|
||||||
|
return np.ascontiguousarray(rgb.transpose(2, 0, 1).astype(np.float32))
|
||||||
|
|
||||||
|
|
||||||
|
def softmax(logits: Any) -> Any:
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
z = logits - logits.max(axis=-1, keepdims=True)
|
||||||
|
e = np.exp(z)
|
||||||
|
return e / e.sum(axis=-1, keepdims=True)
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
"""Metrics + the human-readable report. The owner reads this BEFORE anything ships; the
|
||||||
|
floor decision is made on these numbers. Pure Python, no torch."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import asdict, dataclass, field
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ClassMetrics:
|
||||||
|
support: int
|
||||||
|
recall: float # of the true members, how many the model caught
|
||||||
|
precision: float # of the model's picks, how many were right
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Metrics:
|
||||||
|
n: int
|
||||||
|
accuracy: float
|
||||||
|
macro_recall: float
|
||||||
|
per_class: dict[str, ClassMetrics]
|
||||||
|
confusion: list[list[int]] # rows = true class, cols = predicted, in `classes` order
|
||||||
|
camera_agreement: float | None = None # how often the model equals the detector's class
|
||||||
|
onnx_agreement: float | None = None # exported graph vs the torch model, argmax
|
||||||
|
extra: dict[str, Any] = field(default_factory=dict)
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return asdict(self)
|
||||||
|
|
||||||
|
|
||||||
|
def compute_metrics(
|
||||||
|
classes: tuple[str, ...] | list[str],
|
||||||
|
y_true: list[int],
|
||||||
|
y_pred: list[int],
|
||||||
|
camera: list[str] | None = None,
|
||||||
|
) -> Metrics:
|
||||||
|
k = len(classes)
|
||||||
|
conf = [[0] * k for _ in range(k)]
|
||||||
|
for t, p in zip(y_true, y_pred, strict=True):
|
||||||
|
conf[t][p] += 1
|
||||||
|
per: dict[str, ClassMetrics] = {}
|
||||||
|
recalls: list[float] = []
|
||||||
|
for i, c in enumerate(classes):
|
||||||
|
support = sum(conf[i])
|
||||||
|
tp = conf[i][i]
|
||||||
|
picked = sum(conf[r][i] for r in range(k))
|
||||||
|
recall = tp / support if support else 0.0
|
||||||
|
precision = tp / picked if picked else 0.0
|
||||||
|
per[c] = ClassMetrics(support=support, recall=recall, precision=precision)
|
||||||
|
if support:
|
||||||
|
recalls.append(recall)
|
||||||
|
n = len(y_true)
|
||||||
|
acc = sum(1 for t, p in zip(y_true, y_pred, strict=True) if t == p) / n if n else 0.0
|
||||||
|
agree = None
|
||||||
|
if camera is not None and n:
|
||||||
|
agree = sum(1 for p, cam in zip(y_pred, camera, strict=True) if classes[p] == cam) / n
|
||||||
|
return Metrics(
|
||||||
|
n=n,
|
||||||
|
accuracy=acc,
|
||||||
|
macro_recall=sum(recalls) / len(recalls) if recalls else 0.0,
|
||||||
|
per_class=per,
|
||||||
|
confusion=conf,
|
||||||
|
camera_agreement=agree,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _pct(x: float | None) -> str:
|
||||||
|
return "—" if x is None else f"{x * 100:.1f} %"
|
||||||
|
|
||||||
|
|
||||||
|
def render_report(
|
||||||
|
*,
|
||||||
|
version: str,
|
||||||
|
trained_at: str,
|
||||||
|
mode: str,
|
||||||
|
backbone: str,
|
||||||
|
epochs: int,
|
||||||
|
classes: tuple[str, ...] | list[str],
|
||||||
|
train_counts: dict[str, int],
|
||||||
|
val_counts: dict[str, int],
|
||||||
|
dropped: dict[str, int],
|
||||||
|
missing_files: int,
|
||||||
|
weights: list[float],
|
||||||
|
metrics: Metrics,
|
||||||
|
min_accuracy: float,
|
||||||
|
written: bool,
|
||||||
|
notes: list[str] | None = None,
|
||||||
|
) -> str:
|
||||||
|
lines: list[str] = []
|
||||||
|
verdict = "MODEL WRITTEN" if written else "MODEL NOT WRITTEN — below the floor"
|
||||||
|
lines.append(f"# Body-type classifier {version}")
|
||||||
|
lines.append("")
|
||||||
|
lines.append(
|
||||||
|
f"**{verdict}** · validation accuracy {_pct(metrics.accuracy)} vs floor {_pct(min_accuracy)}"
|
||||||
|
)
|
||||||
|
lines.append("")
|
||||||
|
lines.append(f"- trained: {trained_at}")
|
||||||
|
lines.append(f"- mode: {mode} · backbone: {backbone} · epochs: {epochs}")
|
||||||
|
lines.append(f"- classes ({len(classes)}): {', '.join(classes)}")
|
||||||
|
lines.append(
|
||||||
|
f"- labels: {sum(train_counts.values())} train · {sum(val_counts.values())} validation "
|
||||||
|
"(validation = the NEWEST slice, by time seen)"
|
||||||
|
)
|
||||||
|
if dropped:
|
||||||
|
lines.append(
|
||||||
|
"- dropped (too few labels this run): "
|
||||||
|
+ ", ".join(f"{c} ({n})" for c, n in sorted(dropped.items()))
|
||||||
|
)
|
||||||
|
if missing_files:
|
||||||
|
lines.append(f"- labelled rows whose crop is missing on disk (skipped): {missing_files}")
|
||||||
|
lines.append("")
|
||||||
|
lines.append("## Validation")
|
||||||
|
lines.append("")
|
||||||
|
lines.append(f"- accuracy: {_pct(metrics.accuracy)} on {metrics.n} crops")
|
||||||
|
lines.append(f"- macro recall (each class counted equally): {_pct(metrics.macro_recall)}")
|
||||||
|
if metrics.camera_agreement is not None:
|
||||||
|
lines.append(
|
||||||
|
f"- agrees with the detector's coarse class: {_pct(metrics.camera_agreement)} "
|
||||||
|
"(informational — the detector only knows car/truck/bus/motorcycle)"
|
||||||
|
)
|
||||||
|
if metrics.onnx_agreement is not None:
|
||||||
|
lines.append(
|
||||||
|
f"- exported ONNX matches the trained model on validation: {_pct(metrics.onnx_agreement)}"
|
||||||
|
)
|
||||||
|
lines.append("")
|
||||||
|
lines.append("| class | train | val | recall | precision | loss weight |")
|
||||||
|
lines.append("|---|---:|---:|---:|---:|---:|")
|
||||||
|
for c, w in zip(classes, weights, strict=True):
|
||||||
|
m = metrics.per_class[c]
|
||||||
|
lines.append(
|
||||||
|
f"| {c} | {train_counts.get(c, 0)} | {m.support} | {_pct(m.recall) if m.support else '—'} | "
|
||||||
|
f"{_pct(m.precision) if m.support else '—'} | {w:.2f} |"
|
||||||
|
)
|
||||||
|
lines.append("")
|
||||||
|
lines.append("## Confusion (rows = reviewer's label, columns = model)")
|
||||||
|
lines.append("")
|
||||||
|
lines.append("| | " + " | ".join(classes) + " |")
|
||||||
|
lines.append("|---|" + "---:|" * len(classes))
|
||||||
|
for c, row in zip(classes, metrics.confusion, strict=True):
|
||||||
|
lines.append(f"| **{c}** | " + " | ".join(str(n) for n in row) + " |")
|
||||||
|
lines.append("")
|
||||||
|
if notes:
|
||||||
|
lines.append("## Notes")
|
||||||
|
lines.append("")
|
||||||
|
lines.extend(f"- {n}" for n in notes)
|
||||||
|
lines.append("")
|
||||||
|
lines.append(
|
||||||
|
"The flag on the booth records, it never bills: even a model that passes the floor is "
|
||||||
|
"advisory (the site threshold gates the flag)."
|
||||||
|
)
|
||||||
|
return "\n".join(lines) + "\n"
|
||||||
@@ -0,0 +1,402 @@
|
|||||||
|
"""`parking-trainer serve` — the job API behind the collector's Training section.
|
||||||
|
|
||||||
|
A tiny stdlib HTTP server (no framework, no extra deps) on the compose-internal network,
|
||||||
|
never published: the collector proxies to it behind the reviewer's login. One job at a
|
||||||
|
time; each job is the CLI run as a SUBPROCESS (`python -m trainer.cli …`) with its output
|
||||||
|
captured to a log file — torch's memory goes away with the process, and a crashing job
|
||||||
|
cannot take the service down. Job state + logs persist under `<out>/jobs/` so a restart
|
||||||
|
still shows history.
|
||||||
|
|
||||||
|
GET /health {ok, busy, version}
|
||||||
|
GET /readiness what `inspect` prints (+ the defaults the UI offers)
|
||||||
|
GET /versions every <out>/<version>/ folder: written?, metrics, sidecar
|
||||||
|
GET /versions/<v>/report report.md (text/markdown)
|
||||||
|
GET /jobs recent jobs, newest first
|
||||||
|
GET /jobs/<id> one job incl. the log tail
|
||||||
|
POST /jobs {kind: train|evaluate|publish, …args} → 202 {id} | 409 busy
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from .cli import METRICS_FILE, MODEL_FILE, REPORT_FILE, SIDECAR_FILE
|
||||||
|
from .data import load_labelled, make_split, summarise
|
||||||
|
from .preprocess import Sidecar
|
||||||
|
|
||||||
|
_VERSION_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
|
||||||
|
BACKBONES = ("resnet18", "mobilenet_v3_small", "efficientnet_b0")
|
||||||
|
MODES = ("features", "finetune")
|
||||||
|
LOG_TAIL_BYTES = 16_000
|
||||||
|
|
||||||
|
|
||||||
|
def _now() -> str:
|
||||||
|
return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
|
||||||
|
|
||||||
|
|
||||||
|
class Jobs:
|
||||||
|
"""The single-slot job runner. `start` refuses while one runs."""
|
||||||
|
|
||||||
|
def __init__(self, data_dir: Path, out_dir: Path, publish_url: str, publish_token: str) -> None:
|
||||||
|
self.data_dir = data_dir
|
||||||
|
self.out_dir = out_dir
|
||||||
|
self.publish_url = publish_url
|
||||||
|
self.publish_token = publish_token
|
||||||
|
self.jobs_dir = out_dir / "jobs"
|
||||||
|
self.jobs_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
self._current: dict[str, Any] | None = None
|
||||||
|
self._proc: subprocess.Popen[bytes] | None = None
|
||||||
|
|
||||||
|
# ---- state -----------------------------------------------------------------------
|
||||||
|
def _write(self, job: dict[str, Any]) -> None:
|
||||||
|
(self.jobs_dir / f"{job['id']}.json").write_text(json.dumps(job, indent=2))
|
||||||
|
|
||||||
|
def _read(self, job_id: str) -> dict[str, Any] | None:
|
||||||
|
p = self.jobs_dir / f"{job_id}.json"
|
||||||
|
if not p.is_file():
|
||||||
|
return None
|
||||||
|
return json.loads(p.read_text()) # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
def log_tail(self, job_id: str) -> str:
|
||||||
|
p = self.jobs_dir / f"{job_id}.log"
|
||||||
|
if not p.is_file():
|
||||||
|
return ""
|
||||||
|
size = p.stat().st_size
|
||||||
|
with p.open("rb") as f:
|
||||||
|
if size > LOG_TAIL_BYTES:
|
||||||
|
f.seek(size - LOG_TAIL_BYTES)
|
||||||
|
return f.read().decode("utf-8", "replace")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def busy(self) -> bool:
|
||||||
|
return self._current is not None
|
||||||
|
|
||||||
|
def current(self) -> dict[str, Any] | None:
|
||||||
|
return dict(self._current) if self._current else None
|
||||||
|
|
||||||
|
def get(self, job_id: str) -> dict[str, Any] | None:
|
||||||
|
if self._current and self._current["id"] == job_id:
|
||||||
|
job = dict(self._current)
|
||||||
|
else:
|
||||||
|
job = self._read(job_id) or {}
|
||||||
|
if not job:
|
||||||
|
return None
|
||||||
|
job["log"] = self.log_tail(job_id)
|
||||||
|
return job
|
||||||
|
|
||||||
|
def recent(self, limit: int = 20) -> list[dict[str, Any]]:
|
||||||
|
files = sorted(self.jobs_dir.glob("*.json"), key=lambda p: p.stat().st_mtime, reverse=True)
|
||||||
|
out = []
|
||||||
|
for p in files[:limit]:
|
||||||
|
try:
|
||||||
|
out.append(json.loads(p.read_text()))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if self._current and all(j["id"] != self._current["id"] for j in out):
|
||||||
|
out.insert(0, dict(self._current))
|
||||||
|
out.sort(key=lambda j: j.get("startedAt", ""), reverse=True)
|
||||||
|
return out
|
||||||
|
|
||||||
|
# ---- args → CLI ------------------------------------------------------------------
|
||||||
|
def _argv(self, kind: str, a: dict[str, Any]) -> list[str]:
|
||||||
|
base = [sys.executable, "-m", "trainer.cli"]
|
||||||
|
if kind == "train":
|
||||||
|
mode = a.get("mode", "features")
|
||||||
|
backbone = a.get("backbone", "resnet18")
|
||||||
|
if mode not in MODES or backbone not in BACKBONES:
|
||||||
|
raise ValueError("bad mode/backbone")
|
||||||
|
floor = float(a.get("minAccuracy", 0.85))
|
||||||
|
min_per = int(a.get("minPerClass", 20))
|
||||||
|
epochs = int(a.get("epochs", 0))
|
||||||
|
if not 0.0 <= floor <= 1.0 or min_per < 1 or epochs < 0:
|
||||||
|
raise ValueError("bad numbers")
|
||||||
|
argv = base + [
|
||||||
|
"train",
|
||||||
|
"--data",
|
||||||
|
str(self.data_dir),
|
||||||
|
"--out",
|
||||||
|
str(self.out_dir),
|
||||||
|
"--mode",
|
||||||
|
mode,
|
||||||
|
"--backbone",
|
||||||
|
backbone,
|
||||||
|
"--min-accuracy",
|
||||||
|
str(floor),
|
||||||
|
"--min-per-class",
|
||||||
|
str(min_per),
|
||||||
|
"--epochs",
|
||||||
|
str(epochs),
|
||||||
|
]
|
||||||
|
if a.get("version"):
|
||||||
|
argv += ["--version", self._version(a["version"])]
|
||||||
|
return argv
|
||||||
|
if kind == "evaluate":
|
||||||
|
v = self._version(a.get("version", ""))
|
||||||
|
return base + [
|
||||||
|
"evaluate",
|
||||||
|
"--data",
|
||||||
|
str(self.data_dir),
|
||||||
|
"--model",
|
||||||
|
str(self.out_dir / v / MODEL_FILE),
|
||||||
|
]
|
||||||
|
if kind == "publish":
|
||||||
|
v = self._version(a.get("version", ""))
|
||||||
|
url = str(a.get("url") or self.publish_url)
|
||||||
|
if not url.startswith("https://") and not url.startswith("http://"):
|
||||||
|
raise ValueError("bad publish url")
|
||||||
|
return base + ["publish", str(self.out_dir / v), "--url", url]
|
||||||
|
raise ValueError("kind must be train, evaluate or publish")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _version(v: Any) -> str:
|
||||||
|
if not isinstance(v, str) or not _VERSION_RE.match(v) or v in ("cache", "jobs"):
|
||||||
|
raise ValueError("bad version")
|
||||||
|
return v
|
||||||
|
|
||||||
|
# ---- run -------------------------------------------------------------------------
|
||||||
|
def start(self, kind: str, args: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
argv = self._argv(kind, args)
|
||||||
|
with self._lock:
|
||||||
|
if self._current is not None:
|
||||||
|
raise RuntimeError("busy")
|
||||||
|
job_id = f"{datetime.now(timezone.utc).strftime('%Y%m%d-%H%M%S')}-{uuid.uuid4().hex[:6]}"
|
||||||
|
job: dict[str, Any] = {
|
||||||
|
"id": job_id,
|
||||||
|
"kind": kind,
|
||||||
|
"args": {k: v for k, v in args.items() if k != "token"},
|
||||||
|
"status": "running",
|
||||||
|
"startedAt": _now(),
|
||||||
|
"finishedAt": None,
|
||||||
|
"exitCode": None,
|
||||||
|
}
|
||||||
|
env = dict(os.environ)
|
||||||
|
if kind == "publish":
|
||||||
|
env["TRAINER_PUBLISH_TOKEN"] = self.publish_token
|
||||||
|
log = (self.jobs_dir / f"{job_id}.log").open("wb")
|
||||||
|
log.write(f"$ {' '.join(argv[3:])}\n".encode())
|
||||||
|
log.flush()
|
||||||
|
self._proc = subprocess.Popen(argv, stdout=log, stderr=subprocess.STDOUT, env=env)
|
||||||
|
self._current = job
|
||||||
|
self._write(job)
|
||||||
|
threading.Thread(target=self._wait, args=(job, log), daemon=True).start()
|
||||||
|
return dict(job)
|
||||||
|
|
||||||
|
def _wait(self, job: dict[str, Any], log: Any) -> None:
|
||||||
|
assert self._proc is not None
|
||||||
|
code = self._proc.wait()
|
||||||
|
log.close()
|
||||||
|
with self._lock:
|
||||||
|
job["exitCode"] = code
|
||||||
|
job["finishedAt"] = _now()
|
||||||
|
job["status"] = "done" if code == 0 else ("refused" if code in (2, 3) else "failed")
|
||||||
|
self._write(job)
|
||||||
|
self._current = None
|
||||||
|
self._proc = None
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def readiness(data_dir: Path, min_per_class: int = 20, val_fraction: float = 0.2) -> dict[str, Any]:
|
||||||
|
try:
|
||||||
|
samples, missing = load_labelled(data_dir)
|
||||||
|
except FileNotFoundError:
|
||||||
|
return {
|
||||||
|
"ready": False,
|
||||||
|
"labelled": {"total": 0, "byClass": {}},
|
||||||
|
"missingCrops": 0,
|
||||||
|
"error": "no collector database yet",
|
||||||
|
}
|
||||||
|
split = make_split(samples, val_fraction, min_per_class)
|
||||||
|
return {
|
||||||
|
"ready": len(split.classes) >= 2,
|
||||||
|
"labelled": summarise(samples),
|
||||||
|
"missingCrops": missing,
|
||||||
|
"run": {
|
||||||
|
"classes": list(split.classes),
|
||||||
|
"train": split.counts("train"),
|
||||||
|
"val": split.counts("val"),
|
||||||
|
"dropped": split.dropped,
|
||||||
|
"minPerClass": min_per_class,
|
||||||
|
"valFraction": val_fraction,
|
||||||
|
},
|
||||||
|
"defaults": {
|
||||||
|
"mode": "features",
|
||||||
|
"backbone": "resnet18",
|
||||||
|
"minAccuracy": 0.85,
|
||||||
|
"minPerClass": min_per_class,
|
||||||
|
},
|
||||||
|
"modes": list(MODES),
|
||||||
|
"backbones": list(BACKBONES),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def versions(out_dir: Path) -> list[dict[str, Any]]:
|
||||||
|
out: list[dict[str, Any]] = []
|
||||||
|
if not out_dir.is_dir():
|
||||||
|
return out
|
||||||
|
for d in sorted(out_dir.iterdir(), key=lambda p: p.name, reverse=True):
|
||||||
|
if not d.is_dir() or d.name in ("cache", "jobs"):
|
||||||
|
continue
|
||||||
|
if not (d / REPORT_FILE).is_file() and not (d / METRICS_FILE).is_file():
|
||||||
|
continue
|
||||||
|
entry: dict[str, Any] = {
|
||||||
|
"version": d.name,
|
||||||
|
"written": (d / MODEL_FILE).is_file() and (d / SIDECAR_FILE).is_file(),
|
||||||
|
"hasReport": (d / REPORT_FILE).is_file(),
|
||||||
|
"modifiedAt": datetime.fromtimestamp(d.stat().st_mtime, tz=timezone.utc)
|
||||||
|
.replace(microsecond=0)
|
||||||
|
.isoformat(),
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
m = json.loads((d / METRICS_FILE).read_text())
|
||||||
|
entry["accuracy"] = m.get("accuracy")
|
||||||
|
entry["macroRecall"] = m.get("macro_recall")
|
||||||
|
entry["n"] = m.get("n")
|
||||||
|
except (OSError, ValueError):
|
||||||
|
pass
|
||||||
|
if entry["written"]:
|
||||||
|
try:
|
||||||
|
side = Sidecar.read(d / SIDECAR_FILE)
|
||||||
|
entry["classes"] = side.classes
|
||||||
|
entry["mode"] = side.mode
|
||||||
|
entry["backbone"] = side.backbone
|
||||||
|
entry["trainedAt"] = side.trained_at
|
||||||
|
entry["labels"] = side.labels
|
||||||
|
entry["floor"] = side.metrics.get("floor")
|
||||||
|
except (OSError, ValueError):
|
||||||
|
pass
|
||||||
|
out.append(entry)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class Handler(BaseHTTPRequestHandler):
|
||||||
|
jobs: Jobs # set on the class by serve()
|
||||||
|
server_version = "parking-trainer"
|
||||||
|
|
||||||
|
def log_message(self, fmt: str, *args: Any) -> None: # quieter than the default
|
||||||
|
sys.stderr.write(f"[trainer.serve] {self.address_string()} {fmt % args}\n")
|
||||||
|
|
||||||
|
def _json(self, code: int, body: Any) -> None:
|
||||||
|
raw = json.dumps(body).encode()
|
||||||
|
self.send_response(code)
|
||||||
|
self.send_header("Content-Type", "application/json")
|
||||||
|
self.send_header("Content-Length", str(len(raw)))
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(raw)
|
||||||
|
|
||||||
|
def _text(self, code: int, body: str, ctype: str = "text/markdown; charset=utf-8") -> None:
|
||||||
|
raw = body.encode()
|
||||||
|
self.send_response(code)
|
||||||
|
self.send_header("Content-Type", ctype)
|
||||||
|
self.send_header("Content-Length", str(len(raw)))
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(raw)
|
||||||
|
|
||||||
|
def do_GET(self) -> None: # noqa: N802
|
||||||
|
self._guarded(self._get)
|
||||||
|
|
||||||
|
def do_POST(self) -> None: # noqa: N802
|
||||||
|
self._guarded(self._post)
|
||||||
|
|
||||||
|
def _guarded(self, handler: Any) -> None:
|
||||||
|
"""Run a handler; an unexpected exception becomes a 500 JSON reply instead of a
|
||||||
|
dropped connection (the stdlib server would print the traceback and close the
|
||||||
|
socket, which the collector could only report as "fetch failed" — 2026-09-16,
|
||||||
|
a collector DB from before the `kind` column)."""
|
||||||
|
try:
|
||||||
|
handler()
|
||||||
|
except Exception as exc: # noqa: BLE001 — anything: the reply must be a reply
|
||||||
|
sys.stderr.write(f"[trainer.serve] {self.command} {self.path}: {type(exc).__name__}: {exc}\n")
|
||||||
|
try:
|
||||||
|
self._json(500, {"error": f"{type(exc).__name__}: {exc}"})
|
||||||
|
except Exception: # noqa: BLE001 — headers already sent; nothing left to do
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _get(self) -> None:
|
||||||
|
path = self.path.split("?", 1)[0]
|
||||||
|
j = self.jobs
|
||||||
|
if path == "/health":
|
||||||
|
self._json(200, {"ok": True, "busy": j.busy, "version": "parking-trainer"})
|
||||||
|
elif path == "/readiness":
|
||||||
|
self._json(200, readiness(j.data_dir))
|
||||||
|
elif path == "/versions":
|
||||||
|
self._json(200, {"versions": versions(j.out_dir)})
|
||||||
|
elif path.startswith("/versions/") and path.endswith("/report"):
|
||||||
|
v = path[len("/versions/") : -len("/report")]
|
||||||
|
try:
|
||||||
|
p = j.out_dir / Jobs._version(v) / REPORT_FILE
|
||||||
|
except ValueError:
|
||||||
|
self._json(400, {"error": "bad version"})
|
||||||
|
return
|
||||||
|
if not p.is_file():
|
||||||
|
self._json(404, {"error": "no report"})
|
||||||
|
else:
|
||||||
|
self._text(200, p.read_text())
|
||||||
|
elif path == "/jobs":
|
||||||
|
self._json(200, {"jobs": j.recent(), "current": j.current()})
|
||||||
|
elif path.startswith("/jobs/"):
|
||||||
|
job = j.get(path[len("/jobs/") :])
|
||||||
|
self._json(200, job) if job else self._json(404, {"error": "no such job"})
|
||||||
|
else:
|
||||||
|
self._json(404, {"error": "not found"})
|
||||||
|
|
||||||
|
def _post(self) -> None:
|
||||||
|
if self.path.split("?", 1)[0] != "/jobs":
|
||||||
|
self._json(404, {"error": "not found"})
|
||||||
|
return
|
||||||
|
n = int(self.headers.get("Content-Length") or 0)
|
||||||
|
if n > 64_000:
|
||||||
|
self._json(413, {"error": "too large"})
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
body = json.loads(self.rfile.read(n) or b"{}")
|
||||||
|
if not isinstance(body, dict):
|
||||||
|
raise ValueError("object expected")
|
||||||
|
except ValueError as exc:
|
||||||
|
self._json(400, {"error": f"bad json: {exc}"})
|
||||||
|
return
|
||||||
|
kind = str(body.pop("kind", ""))
|
||||||
|
try:
|
||||||
|
job = self.jobs.start(kind, body)
|
||||||
|
except ValueError as exc:
|
||||||
|
self._json(400, {"error": str(exc)})
|
||||||
|
return
|
||||||
|
except RuntimeError:
|
||||||
|
self._json(409, {"error": "a job is already running", "current": self.jobs.current()})
|
||||||
|
return
|
||||||
|
self._json(202, job)
|
||||||
|
|
||||||
|
|
||||||
|
def serve(data_dir: Path, out_dir: Path, host: str, port: int, publish_url: str, publish_token: str) -> None:
|
||||||
|
Handler.jobs = Jobs(data_dir, out_dir, publish_url, publish_token)
|
||||||
|
httpd = ThreadingHTTPServer((host, port), Handler)
|
||||||
|
sys.stderr.write(f"[trainer.serve] listening on {host}:{port}, data {data_dir}, out {out_dir}\n")
|
||||||
|
try:
|
||||||
|
httpd.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
httpd.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def wait_idle(jobs: Jobs, timeout: float = 60.0) -> None:
|
||||||
|
"""Test helper: block until no job runs."""
|
||||||
|
t0 = time.monotonic()
|
||||||
|
while jobs.busy and time.monotonic() - t0 < timeout:
|
||||||
|
time.sleep(0.1)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://turbo.build/schema.json",
|
||||||
|
"extends": ["//"],
|
||||||
|
"tasks": {
|
||||||
|
"build": {
|
||||||
|
"outputs": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+1444
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user