Compare commits
2 commits
main
...
feat/admin
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a6143d54d | |||
| cf8ef26f63 |
141 changed files with 1200 additions and 8996 deletions
11
.env.example
11
.env.example
|
|
@ -15,17 +15,20 @@ JWT_SECRET=changeme-generate-a-real-random-secret-at-least-32-chars
|
||||||
# (docker-compose.yml), serving both the API and the built frontend.
|
# (docker-compose.yml), serving both the API and the built frontend.
|
||||||
# APP_PORT=3000
|
# APP_PORT=3000
|
||||||
|
|
||||||
# --- Admin surface (apps/web's /admin/* routes + the /admin/* API) --------
|
# --- Admin application (apps/admin-web + the /admin/* API surface) ---------
|
||||||
# All optional: an instance that doesn't run the admin app needs none of
|
# All optional: an instance that doesn't run the admin app needs none of
|
||||||
# these. `requireAdmin` fails closed when ADMIN_JWT_SECRET is unset, so
|
# these. `requireAdmin` fails closed when ADMIN_JWT_SECRET is unset, so
|
||||||
# leaving it out simply disables every /admin/* route. The admin UI is
|
# leaving it out simply disables every /admin/* route.
|
||||||
# served by the same app/container as the rest of the frontend — no
|
|
||||||
# separate origin, so no CORS entry of its own.
|
|
||||||
#
|
#
|
||||||
# Secret for the admin session JWT — MUST be different from JWT_SECRET so an
|
# Secret for the admin session JWT — MUST be different from JWT_SECRET so an
|
||||||
# end-user token can never be replayed against /admin/*. Generate your own
|
# end-user token can never be replayed against /admin/*. Generate your own
|
||||||
# the same way as JWT_SECRET above.
|
# the same way as JWT_SECRET above.
|
||||||
# ADMIN_JWT_SECRET=changeme-generate-a-real-random-secret-at-least-32-chars
|
# ADMIN_JWT_SECRET=changeme-generate-a-real-random-secret-at-least-32-chars
|
||||||
|
# Origin apps/admin-web is served from, added to the CORS allow-list.
|
||||||
|
# ADMIN_CORS_ORIGIN=http://localhost:5174
|
||||||
|
# Host port for the Docker `admin-web` service (static nginx serving the
|
||||||
|
# built admin frontend).
|
||||||
|
# ADMIN_WEB_PORT=3001
|
||||||
# Optional — read only by `src/scripts/create-admin.ts` when its --email /
|
# Optional — read only by `src/scripts/create-admin.ts` when its --email /
|
||||||
# --password / --name flags are omitted (e.g. to bootstrap the first admin
|
# --password / --name flags are omitted (e.g. to bootstrap the first admin
|
||||||
# from inside the container). Never read by the running server.
|
# from inside the container). Never read by the running server.
|
||||||
|
|
|
||||||
13
.github/workflows/ci.yml
vendored
13
.github/workflows/ci.yml
vendored
|
|
@ -58,16 +58,14 @@ jobs:
|
||||||
POSTGRES_USER: ci
|
POSTGRES_USER: ci
|
||||||
POSTGRES_PASSWORD: ci
|
POSTGRES_PASSWORD: ci
|
||||||
POSTGRES_DB: batchcooking_ci
|
POSTGRES_DB: batchcooking_ci
|
||||||
|
ports:
|
||||||
|
- 5433:5432
|
||||||
options: >-
|
options: >-
|
||||||
--health-cmd pg_isready
|
--health-cmd pg_isready
|
||||||
--health-interval 5s
|
--health-interval 5s
|
||||||
--health-timeout 5s
|
--health-timeout 5s
|
||||||
--health-retries 10
|
--health-retries 10
|
||||||
steps:
|
steps:
|
||||||
- name: Install curl and dependencies
|
|
||||||
run: |
|
|
||||||
apt-get update && apt-get install -y curl
|
|
||||||
|
|
||||||
- uses: https://github.com/actions/checkout@v4
|
- uses: https://github.com/actions/checkout@v4
|
||||||
|
|
||||||
- uses: pnpm/action-setup@v4
|
- uses: pnpm/action-setup@v4
|
||||||
|
|
@ -179,3 +177,10 @@ jobs:
|
||||||
# `component.devServer`), unlike `e2e` above which needs the real app
|
# `component.devServer`), unlike `e2e` above which needs the real app
|
||||||
# running first.
|
# running first.
|
||||||
- run: pnpm --filter web cy:run:component
|
- run: pnpm --filter web cy:run:component
|
||||||
|
# The admin app's own Cypress suite (`apps/admin-web`) — its own dev
|
||||||
|
# server on :5174, all `/admin/*` calls mocked via `cy.intercept`
|
||||||
|
# (no live backend needed), same as the `web` e2e run above.
|
||||||
|
- name: Run admin-web E2E tests
|
||||||
|
env:
|
||||||
|
HOST: "0.0.0.0"
|
||||||
|
run: pnpm --filter admin-web e2e
|
||||||
|
|
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -163,6 +163,3 @@ tmp-mockups/
|
||||||
apps/web/cypress/screenshots/
|
apps/web/cypress/screenshots/
|
||||||
apps/web/cypress/videos/
|
apps/web/cypress/videos/
|
||||||
apps/web/cypress/downloads/
|
apps/web/cypress/downloads/
|
||||||
apps/admin-web/cypress/screenshots/
|
|
||||||
apps/admin-web/cypress/videos/
|
|
||||||
apps/admin-web/cypress/downloads/
|
|
||||||
|
|
|
||||||
6
apps/admin-web/.env.example
Normal file
6
apps/admin-web/.env.example
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
# Vite only exposes vars prefixed with VITE_ to client code.
|
||||||
|
# Base URL of the API's /admin/* surface. Empty string = same origin as the
|
||||||
|
# page (correct behind a shared reverse proxy). Native dev overrides it in
|
||||||
|
# apps/admin-web/.env since the Vite dev server (5174) and the API (3000)
|
||||||
|
# are different origins.
|
||||||
|
VITE_ADMIN_API_URL=http://localhost:3000
|
||||||
25
apps/admin-web/Dockerfile
Normal file
25
apps/admin-web/Dockerfile
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Its own image (not built into apps/api's) — the admin app is deployed
|
||||||
|
# independently of the main app. Build stage compiles the Vite bundle from
|
||||||
|
# the monorepo; runtime is a plain static nginx serving that bundle.
|
||||||
|
#
|
||||||
|
# Build context is the repo root (like apps/api/Dockerfile) — the workspace
|
||||||
|
# packages (@batch-cooking/shared, @batch-cooking/date-tools) must resolve.
|
||||||
|
FROM node:22-slim AS build
|
||||||
|
RUN corepack enable
|
||||||
|
WORKDIR /repo
|
||||||
|
# Skip Cypress's Electron binary download — this image never runs it.
|
||||||
|
ENV CYPRESS_INSTALL_BINARY=0
|
||||||
|
COPY . .
|
||||||
|
RUN pnpm install --frozen-lockfile
|
||||||
|
# The admin bundle bakes in VITE_ADMIN_API_URL at build time. Default ""
|
||||||
|
# (same-origin — correct behind a shared reverse proxy); override with
|
||||||
|
# `--build-arg VITE_ADMIN_API_URL=https://api.example.com` when the admin
|
||||||
|
# app is served from a different origin than the API.
|
||||||
|
ARG VITE_ADMIN_API_URL=""
|
||||||
|
ENV VITE_ADMIN_API_URL=$VITE_ADMIN_API_URL
|
||||||
|
RUN pnpm --filter admin-web build
|
||||||
|
|
||||||
|
FROM nginx:alpine AS runtime
|
||||||
|
COPY apps/admin-web/nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
|
COPY --from=build /repo/apps/admin-web/dist /usr/share/nginx/html
|
||||||
|
EXPOSE 80
|
||||||
28
apps/admin-web/cypress.config.ts
Normal file
28
apps/admin-web/cypress.config.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";
|
||||||
|
import { createEsbuildPlugin } from "@badeball/cypress-cucumber-preprocessor/esbuild";
|
||||||
|
import createBundler from "@bahmutov/cypress-esbuild-preprocessor";
|
||||||
|
import { defineConfig } from "cypress";
|
||||||
|
|
||||||
|
// Disable GPU for headless/sandboxed environments where no GPU device is
|
||||||
|
// available — same helper as apps/web's cypress.config.ts.
|
||||||
|
function disableGpu(on: Cypress.PluginEvents) {
|
||||||
|
on("before:browser:launch", (browser, launchOptions) => {
|
||||||
|
if (browser.family === "chromium") {
|
||||||
|
launchOptions.args.push("--disable-gpu", "--no-sandbox");
|
||||||
|
}
|
||||||
|
return launchOptions;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
e2e: {
|
||||||
|
baseUrl: "http://localhost:5174",
|
||||||
|
specPattern: ["cypress/e2e/**/*.cy.ts", "cypress/e2e/**/*.feature"],
|
||||||
|
async setupNodeEvents(on, config) {
|
||||||
|
disableGpu(on);
|
||||||
|
await addCucumberPreprocessorPlugin(on, config);
|
||||||
|
on("file:preprocessor", createBundler({ plugins: [createEsbuildPlugin(config)] }));
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
@ -15,40 +15,35 @@ describe("Admin layout", () => {
|
||||||
statusCode: 401,
|
statusCode: 401,
|
||||||
body: { code: 4011, message: "no" },
|
body: { code: 4011, message: "no" },
|
||||||
});
|
});
|
||||||
cy.visit("/admin/monitoring");
|
cy.visit("/monitoring");
|
||||||
cy.url().should("include", "/admin/login");
|
cy.url().should("include", "/login");
|
||||||
cy.contains("h1", "Administration").should("be.visible");
|
cy.contains("h1", "Administration").should("be.visible");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows the sidebar and navigates between the sections", () => {
|
it("shows the sidebar and navigates between the three sections", () => {
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
||||||
cy.visit("/admin");
|
cy.visit("/");
|
||||||
|
|
||||||
cy.contains("h1", "Tableau de bord").should("be.visible");
|
cy.contains("h1", "Tableau de bord").should("be.visible");
|
||||||
cy.contains(".admin-sidebar__who", "Ops").should("be.visible");
|
cy.contains(".admin-sidebar__who", "Ops").should("be.visible");
|
||||||
|
|
||||||
cy.contains("nav a", "Monitoring").click();
|
cy.contains("nav a", "Monitoring").click();
|
||||||
cy.url().should("include", "/admin/monitoring");
|
cy.url().should("include", "/monitoring");
|
||||||
cy.contains("h1", "Monitoring").should("be.visible");
|
cy.contains("h1", "Monitoring").should("be.visible");
|
||||||
cy.contains("nav a", "Monitoring").should("have.class", "active");
|
cy.contains("nav a", "Monitoring").should("have.class", "active");
|
||||||
|
|
||||||
cy.contains("nav a", "Corrections").click();
|
cy.contains("nav a", "Corrections").click();
|
||||||
cy.url().should("include", "/admin/corrections");
|
cy.url().should("include", "/corrections");
|
||||||
cy.contains("h1", "Corrections").should("be.visible");
|
cy.contains("h1", "Corrections").should("be.visible");
|
||||||
|
|
||||||
cy.intercept("GET", "**/admin/catalog/placeholders*", { statusCode: 200, body: [] });
|
|
||||||
cy.contains("nav a", "Catalogue").click();
|
|
||||||
cy.url().should("include", "/admin/catalogue");
|
|
||||||
cy.contains("h1", "Ingrédients hors-catalogue").should("be.visible");
|
|
||||||
|
|
||||||
cy.contains("nav a", "Tableau de bord").click();
|
cy.contains("nav a", "Tableau de bord").click();
|
||||||
cy.url().should("eq", `${Cypress.config().baseUrl}/admin`);
|
cy.url().should("eq", `${Cypress.config().baseUrl}/`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("logs out back to /login", () => {
|
it("logs out back to /login", () => {
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
||||||
cy.intercept("POST", "**/admin/auth/logout", { statusCode: 204 });
|
cy.intercept("POST", "**/admin/auth/logout", { statusCode: 204 });
|
||||||
cy.visit("/admin");
|
cy.visit("/");
|
||||||
|
|
||||||
// Wait until the guarded layout has actually mounted before acting.
|
// Wait until the guarded layout has actually mounted before acting.
|
||||||
cy.contains("h1", "Tableau de bord").should("be.visible");
|
cy.contains("h1", "Tableau de bord").should("be.visible");
|
||||||
|
|
@ -57,6 +52,6 @@ describe("Admin layout", () => {
|
||||||
// guard to /login — no fresh `me` round-trip involved, so nothing to
|
// guard to /login — no fresh `me` round-trip involved, so nothing to
|
||||||
// re-stub here.
|
// re-stub here.
|
||||||
cy.contains("button", "Se déconnecter").click();
|
cy.contains("button", "Se déconnecter").click();
|
||||||
cy.url().should("include", "/admin/login");
|
cy.url().should("include", "/login");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -6,19 +6,19 @@ Feature: Admin login
|
||||||
Scenario: A wrong password shows a translated error, no redirect
|
Scenario: A wrong password shows a translated error, no redirect
|
||||||
Given the admin session check returns unauthenticated
|
Given the admin session check returns unauthenticated
|
||||||
And admin login fails with invalid credentials
|
And admin login fails with invalid credentials
|
||||||
When I visit "/admin/login"
|
When I visit "/login"
|
||||||
And I fill in the "email" field with "ops@example.com"
|
And I fill in the "email" field with "ops@example.com"
|
||||||
And I fill in the "password" field with "wrong"
|
And I fill in the "password" field with "wrong"
|
||||||
And I click the button "Se connecter"
|
And I click the button "Se connecter"
|
||||||
Then I should see "Email ou mot de passe incorrect"
|
Then I should see "Email ou mot de passe incorrect"
|
||||||
And the URL should include "/admin/login"
|
And the URL should include "/login"
|
||||||
|
|
||||||
Scenario: A correct login lands on the dashboard
|
Scenario: A correct login lands on the dashboard
|
||||||
Given the admin session check returns unauthenticated
|
Given the admin session check returns unauthenticated
|
||||||
And admin login succeeds as "Ops"
|
And admin login succeeds as "Ops"
|
||||||
When I visit "/admin/login"
|
When I visit "/login"
|
||||||
And I fill in the "email" field with "ops@example.com"
|
And I fill in the "email" field with "ops@example.com"
|
||||||
And I fill in the "password" field with "correct-horse"
|
And I fill in the "password" field with "correct-horse"
|
||||||
And I click the button "Se connecter"
|
And I click the button "Se connecter"
|
||||||
Then the URL should not include "/admin/login"
|
Then the URL should not include "/login"
|
||||||
And I should see the heading "Tableau de bord"
|
And I should see the heading "Tableau de bord"
|
||||||
24
apps/admin-web/cypress/e2e/login.ts
Normal file
24
apps/admin-web/cypress/e2e/login.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { Given } from "@badeball/cypress-cucumber-preprocessor";
|
||||||
|
|
||||||
|
const adminBody = {
|
||||||
|
id: 1,
|
||||||
|
email: "ops@example.com",
|
||||||
|
name: "Ops",
|
||||||
|
createdAt: "2026-08-01T00:00:00.000Z",
|
||||||
|
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
||||||
|
};
|
||||||
|
|
||||||
|
Given("admin login fails with invalid credentials", () => {
|
||||||
|
cy.intercept("POST", "**/admin/auth/login", {
|
||||||
|
statusCode: 401,
|
||||||
|
body: { code: 4010, message: "Invalid email or password" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Given("admin login succeeds as {string}", (name: string) => {
|
||||||
|
const body = { ...adminBody, name, email: `${name.toLowerCase()}@example.com` };
|
||||||
|
cy.intercept("POST", "**/admin/auth/login", { statusCode: 200, body });
|
||||||
|
// After navigate("/"), RequireAdmin re-checks the session — from now on it
|
||||||
|
// must report authenticated (last matching intercept wins).
|
||||||
|
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body });
|
||||||
|
});
|
||||||
3
apps/admin-web/cypress/support/e2e.ts
Normal file
3
apps/admin-web/cypress/support/e2e.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
// Cypress support file — global config and custom commands go here as the
|
||||||
|
// admin app grows. Same minimal starting point as apps/web's e2e.ts.
|
||||||
|
export {};
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
import { Given, Then, When } from "@badeball/cypress-cucumber-preprocessor";
|
||||||
|
|
||||||
|
// Steps shared across admin feature specs — navigation and generic UI
|
||||||
|
// assertions. Anything specific to one feature (its own API mocks, its own
|
||||||
|
// DOM structure) lives in that feature's own `<name>.ts` step file.
|
||||||
|
//
|
||||||
|
// Every admin API call is mocked via `cy.intercept` — the Cypress suite
|
||||||
|
// never runs a live backend; apps/api's own Mocha suite covers real
|
||||||
|
// `/admin/*` behaviour.
|
||||||
|
|
||||||
|
Given("the admin session check returns unauthenticated", () => {
|
||||||
|
cy.intercept("GET", "**/admin/auth/me", { statusCode: 401, body: { code: 4011, message: "no" } });
|
||||||
|
});
|
||||||
|
|
||||||
|
Given("I am signed in as admin {string}", (name: string) => {
|
||||||
|
cy.intercept("GET", "**/admin/auth/me", {
|
||||||
|
statusCode: 200,
|
||||||
|
body: {
|
||||||
|
id: 1,
|
||||||
|
email: `${name.toLowerCase()}@example.com`,
|
||||||
|
name,
|
||||||
|
createdAt: "2026-08-01T00:00:00.000Z",
|
||||||
|
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
When("I visit {string}", (path: string) => {
|
||||||
|
cy.visit(path);
|
||||||
|
});
|
||||||
|
|
||||||
|
When("I fill in the {string} field with {string}", (fieldId: string, value: string) => {
|
||||||
|
cy.get(`#${fieldId}`).clear().type(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
When("I click the button {string}", (text: string) => {
|
||||||
|
cy.contains("button", text).click();
|
||||||
|
});
|
||||||
|
|
||||||
|
Then("the URL should include {string}", (fragment: string) => {
|
||||||
|
cy.url().should("include", fragment);
|
||||||
|
});
|
||||||
|
|
||||||
|
Then("the URL should not include {string}", (fragment: string) => {
|
||||||
|
cy.url().should("not.include", fragment);
|
||||||
|
});
|
||||||
|
|
||||||
|
Then("I should see {string}", (text: string) => {
|
||||||
|
cy.contains(text).should("be.visible");
|
||||||
|
});
|
||||||
|
|
||||||
|
Then("I should see the heading {string}", (text: string) => {
|
||||||
|
cy.contains("h1", text).should("be.visible");
|
||||||
|
});
|
||||||
12
apps/admin-web/index.html
Normal file
12
apps/admin-web/index.html
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="fr">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>batchCooking — Admin</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
20
apps/admin-web/nginx.conf
Normal file
20
apps/admin-web/nginx.conf
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Static host for the built admin SPA. Client-side routing (react-router)
|
||||||
|
# means any unknown path must fall back to index.html rather than 404 —
|
||||||
|
# same reason apps/api serves its own SPA that way.
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ /index.html;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Long-cache the fingerprinted assets Vite emits; never cache the HTML
|
||||||
|
# entry point so a new deploy is picked up immediately.
|
||||||
|
location /assets/ {
|
||||||
|
expires 1y;
|
||||||
|
add_header Cache-Control "public, immutable";
|
||||||
|
}
|
||||||
|
}
|
||||||
42
apps/admin-web/package.json
Normal file
42
apps/admin-web/package.json
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"name": "admin-web",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite --host 0.0.0.0 --port 5174",
|
||||||
|
"build": "tsc -b && vite build",
|
||||||
|
"preview": "vite preview --port 5174",
|
||||||
|
"test": "echo \"no unit tests yet\" && exit 0",
|
||||||
|
"cy:open": "cypress open",
|
||||||
|
"cy:run": "cypress run",
|
||||||
|
"e2e": "start-server-and-test dev http://localhost:5174 cy:run"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@batch-cooking/date-tools": "workspace:*",
|
||||||
|
"@batch-cooking/shared": "workspace:*",
|
||||||
|
"i18next": "^26.3.6",
|
||||||
|
"lucide-react": "^1.32.0",
|
||||||
|
"react": "^18.3.1",
|
||||||
|
"react-dom": "^18.3.1",
|
||||||
|
"react-i18next": "^17.0.11",
|
||||||
|
"react-router-dom": "^7.18.2",
|
||||||
|
"recharts": "^2.15.0",
|
||||||
|
"zod": "^3.25.76"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@badeball/cypress-cucumber-preprocessor": "22.2.0",
|
||||||
|
"@bahmutov/cypress-esbuild-preprocessor": "2.2.8",
|
||||||
|
"@cypress/vite-dev-server": "5.2.1",
|
||||||
|
"@types/node": "^22.9.0",
|
||||||
|
"@types/react": "^18.3.12",
|
||||||
|
"@types/react-dom": "^18.3.1",
|
||||||
|
"@vitejs/plugin-react": "^4.3.3",
|
||||||
|
"cypress": "13.17.0",
|
||||||
|
"esbuild": "0.21.5",
|
||||||
|
"sass": "^1.102.0",
|
||||||
|
"start-server-and-test": "^2.0.8",
|
||||||
|
"typescript": "^5.7.2",
|
||||||
|
"vite": "^5.4.11"
|
||||||
|
}
|
||||||
|
}
|
||||||
34
apps/admin-web/src/App.tsx
Normal file
34
apps/admin-web/src/App.tsx
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { Navigate, Route, Routes } from "react-router-dom";
|
||||||
|
import { RequireAdmin } from "./features/auth/RequireAdmin";
|
||||||
|
import { AdminLayout } from "./layouts/AdminLayout";
|
||||||
|
import { CorrectionsPage } from "./pages/corrections/CorrectionsPage";
|
||||||
|
import { DashboardPage } from "./pages/dashboard/DashboardPage";
|
||||||
|
import { LoginPage } from "./pages/login/LoginPage";
|
||||||
|
import { MonitoringPage } from "./pages/monitoring/MonitoringPage";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin app route table. `/login` is the only unauthenticated route;
|
||||||
|
* everything else is nested under one `RequireAdmin` + `AdminLayout` parent
|
||||||
|
* (the guard + sidebar chrome applied once), same shape as apps/web's
|
||||||
|
* `App.tsx`. Unknown paths fall back to `/`, which redirects to `/login`
|
||||||
|
* when there's no admin session.
|
||||||
|
*/
|
||||||
|
export function App() {
|
||||||
|
return (
|
||||||
|
<Routes>
|
||||||
|
<Route path="/login" element={<LoginPage />} />
|
||||||
|
<Route
|
||||||
|
element={
|
||||||
|
<RequireAdmin>
|
||||||
|
<AdminLayout />
|
||||||
|
</RequireAdmin>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Route path="/" element={<DashboardPage />} />
|
||||||
|
<Route path="/monitoring" element={<MonitoringPage />} />
|
||||||
|
<Route path="/corrections" element={<CorrectionsPage />} />
|
||||||
|
</Route>
|
||||||
|
<Route path="*" element={<Navigate to="/" replace />} />
|
||||||
|
</Routes>
|
||||||
|
);
|
||||||
|
}
|
||||||
96
apps/admin-web/src/api/client.ts
Normal file
96
apps/admin-web/src/api/client.ts
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
import {
|
||||||
|
type AdminLoginInput,
|
||||||
|
type AdminUserView,
|
||||||
|
type ApiErrorResponse,
|
||||||
|
ErrorCode,
|
||||||
|
} from "@batch-cooking/shared";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base URL of the admin API surface, configurable via `VITE_ADMIN_API_URL`
|
||||||
|
* (see `.env.example`). Defaults to `""` (same origin) — correct behind a
|
||||||
|
* shared reverse proxy; native dev overrides it to `http://localhost:3000`
|
||||||
|
* in `apps/admin-web/.env` since the Vite dev server (5174) and the API
|
||||||
|
* (3000) are different origins.
|
||||||
|
*/
|
||||||
|
const ADMIN_API_BASE_URL: string = import.meta.env.VITE_ADMIN_API_URL ?? "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown by {@link AdminApiClient} on any non-2xx response — carries the
|
||||||
|
* same {@link ErrorCode} the API returned. Same shape as apps/web's
|
||||||
|
* `ApiError`; kept separate rather than shared so the two apps' transport
|
||||||
|
* layers stay independent.
|
||||||
|
*/
|
||||||
|
export class ApiError extends Error {
|
||||||
|
public readonly status: number;
|
||||||
|
public readonly code: ErrorCode;
|
||||||
|
public readonly fieldErrors?: Record<string, string[] | undefined>;
|
||||||
|
|
||||||
|
public constructor(status: number, body: ApiErrorResponse) {
|
||||||
|
super(body.message);
|
||||||
|
this.name = "ApiError";
|
||||||
|
this.status = status;
|
||||||
|
this.code = body.code;
|
||||||
|
this.fieldErrors = body.details;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thin fetch wrapper around the `/admin/*` endpoints — same design as
|
||||||
|
* apps/web's `ApiClient` (a class for cohesion/extensibility, one shared
|
||||||
|
* stateless instance). Every request sends credentials so the
|
||||||
|
* `admin_session` httpOnly cookie round-trips.
|
||||||
|
*/
|
||||||
|
export class AdminApiClient {
|
||||||
|
/**
|
||||||
|
* Performs a JSON request against the admin API and returns the parsed body.
|
||||||
|
*
|
||||||
|
* @throws {ApiError} if the response status is not in the 2xx range.
|
||||||
|
*/
|
||||||
|
private async _request<TResponseBody>(
|
||||||
|
path: string,
|
||||||
|
options: RequestInit = {},
|
||||||
|
): Promise<TResponseBody> {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${ADMIN_API_BASE_URL}${path}`, {
|
||||||
|
...options,
|
||||||
|
credentials: "include",
|
||||||
|
headers: { "Content-Type": "application/json", ...options.headers },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const body = (await response.json().catch(() => null)) as ApiErrorResponse | null;
|
||||||
|
throw new ApiError(
|
||||||
|
response.status,
|
||||||
|
body ?? { code: ErrorCode.INTERNAL_ERROR, message: "Something went wrong" },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.status === 204) {
|
||||||
|
return undefined as TResponseBody;
|
||||||
|
}
|
||||||
|
return (await response.json()) as TResponseBody;
|
||||||
|
} catch (err) {
|
||||||
|
// Rethrown as-is — callers surface it their own way; this is just the
|
||||||
|
// one place the fetch/`await` sits in a try/catch per the repo's rule.
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Verifies admin credentials and starts an admin session. */
|
||||||
|
public login(input: AdminLoginInput): Promise<AdminUserView> {
|
||||||
|
return this._request("/admin/auth/login", { method: "POST", body: JSON.stringify(input) });
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ends the current admin session. */
|
||||||
|
public logout(): Promise<void> {
|
||||||
|
return this._request("/admin/auth/logout", { method: "POST" });
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fetches the currently authenticated admin — rejects with `NOT_AUTHENTICATED` if there's no session. */
|
||||||
|
public me(): Promise<AdminUserView> {
|
||||||
|
return this._request("/admin/auth/me");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Single shared instance — stateless, same reasoning as apps/web's `apiClient`. */
|
||||||
|
export const adminApiClient = new AdminApiClient();
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import type { AdminLoginInput, AdminUserView } from "@batch-cooking/shared";
|
import type { AdminLoginInput, AdminUserView } from "@batch-cooking/shared";
|
||||||
import { createContext, type ReactNode, useCallback, useContext, useEffect, useState } from "react";
|
import { createContext, type ReactNode, useCallback, useContext, useEffect, useState } from "react";
|
||||||
import { adminApiClient } from "../../api/admin-client";
|
import { adminApiClient } from "../../api/client";
|
||||||
|
|
||||||
/** Auth state/actions exposed via {@link useAdminAuth} — the admin-app counterpart of apps/web's `AuthContext`. */
|
/** Auth state/actions exposed via {@link useAdminAuth} — the admin-app counterpart of apps/web's `AuthContext`. */
|
||||||
interface AdminAuthContextValue {
|
interface AdminAuthContextValue {
|
||||||
|
|
@ -5,9 +5,8 @@ import { useAdminAuth } from "./AdminAuthContext";
|
||||||
/**
|
/**
|
||||||
* Route guard for every admin page. Renders nothing while the initial
|
* Route guard for every admin page. Renders nothing while the initial
|
||||||
* `GET /admin/auth/me` check is pending (avoids a flash-then-redirect);
|
* `GET /admin/auth/me` check is pending (avoids a flash-then-redirect);
|
||||||
* once resolved, renders `children` or redirects to `/admin/login`. Mirror
|
* once resolved, renders `children` or redirects to `/login`. Mirror of
|
||||||
* of `RequireAuth` (`features/auth/`), but keyed to the separate admin
|
* apps/web's `RequireAuth`.
|
||||||
* session (`admin_session` cookie), not the user one.
|
|
||||||
*/
|
*/
|
||||||
export function RequireAdmin({ children }: { children: ReactNode }) {
|
export function RequireAdmin({ children }: { children: ReactNode }) {
|
||||||
const { admin, isLoading } = useAdminAuth();
|
const { admin, isLoading } = useAdminAuth();
|
||||||
|
|
@ -16,7 +15,7 @@ export function RequireAdmin({ children }: { children: ReactNode }) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!admin) {
|
if (!admin) {
|
||||||
return <Navigate to="/admin/login" replace />;
|
return <Navigate to="/login" replace />;
|
||||||
}
|
}
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
}
|
}
|
||||||
22
apps/admin-web/src/i18n/i18n.ts
Normal file
22
apps/admin-web/src/i18n/i18n.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import i18next from "i18next";
|
||||||
|
import { initReactI18next } from "react-i18next";
|
||||||
|
import fr from "../locales/fr/translation.json";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* i18next instance for the admin app, imported once for its side effect
|
||||||
|
* (`main.tsx`) before anything renders. Only French exists today — same
|
||||||
|
* setup as apps/web's `i18n/i18n.ts`, its own separate locale file so the
|
||||||
|
* two apps' copy never has to be kept identical. `packages/shared`'s
|
||||||
|
* `ErrorCode` member names double as keys under the `errors` namespace
|
||||||
|
* (see `services/error-message.service.ts`).
|
||||||
|
*/
|
||||||
|
void i18next.use(initReactI18next).init({
|
||||||
|
resources: {
|
||||||
|
fr: { translation: fr },
|
||||||
|
},
|
||||||
|
lng: "fr",
|
||||||
|
fallbackLng: "fr",
|
||||||
|
interpolation: { escapeValue: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
export default i18next;
|
||||||
|
|
@ -1,21 +1,18 @@
|
||||||
import { Activity, LayoutDashboard, ListChecks, PackageSearch } from "lucide-react";
|
import { Activity, LayoutDashboard, ListChecks } from "lucide-react";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { NavLink, Outlet, useNavigate } from "react-router-dom";
|
import { NavLink, Outlet, useNavigate } from "react-router-dom";
|
||||||
import { useAdminAuth } from "../features/admin/AdminAuthContext";
|
import { useAdminAuth } from "../features/auth/AdminAuthContext";
|
||||||
import "./AdminLayout.scss";
|
import "./AdminLayout.scss";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One entry in the admin sidebar's nav. `key` maps to `admin.nav.<key>` in
|
* One entry in the admin sidebar's nav. `key` maps to `admin.nav.<key>` in
|
||||||
* the locale file — adding a section is one array entry plus one locale key.
|
* the locale file — adding a section is one array entry plus one locale key.
|
||||||
* Paths are absolute under `/admin` (the admin route group lives inside
|
|
||||||
* `apps/web`'s `App.tsx`, mounted at `/admin`).
|
|
||||||
*/
|
*/
|
||||||
const NAV_ITEMS = [
|
const NAV_ITEMS = [
|
||||||
{ to: "/admin", key: "dashboard", Icon: LayoutDashboard, end: true },
|
{ to: "/", key: "dashboard", Icon: LayoutDashboard, end: true },
|
||||||
{ to: "/admin/monitoring", key: "monitoring", Icon: Activity, end: false },
|
{ to: "/monitoring", key: "monitoring", Icon: Activity, end: false },
|
||||||
{ to: "/admin/corrections", key: "corrections", Icon: ListChecks, end: false },
|
{ to: "/corrections", key: "corrections", Icon: ListChecks, end: false },
|
||||||
{ to: "/admin/catalogue", key: "catalog", Icon: PackageSearch, end: false },
|
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -35,12 +32,12 @@ export function AdminLayout() {
|
||||||
setIsLoggingOut(true);
|
setIsLoggingOut(true);
|
||||||
try {
|
try {
|
||||||
await logout();
|
await logout();
|
||||||
void navigate("/admin/login");
|
void navigate("/login");
|
||||||
} catch {
|
} catch {
|
||||||
// Even if the network call failed, the local session state was
|
// Even if the network call failed, the local session state was
|
||||||
// cleared optimistically enough for the guard to bounce to /login;
|
// cleared optimistically enough for the guard to bounce to /login;
|
||||||
// nothing useful to show the operator here.
|
// nothing useful to show the operator here.
|
||||||
void navigate("/admin/login");
|
void navigate("/login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
18
apps/admin-web/src/lib/zod-errors.ts
Normal file
18
apps/admin-web/src/lib/zod-errors.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import type { ZodError } from "zod";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flattens a zod validation error into `{ fieldName: firstMessage }` for
|
||||||
|
* inline display under each form field — verbatim copy of apps/web's
|
||||||
|
* `lib/zod-errors.ts` (only the first message per field, enough for the
|
||||||
|
* single-rule-per-field schemas used here).
|
||||||
|
*/
|
||||||
|
export function fieldErrorsFrom(error: ZodError): Record<string, string> {
|
||||||
|
const fieldErrors = error.flatten().fieldErrors;
|
||||||
|
const firstMessagePerField: Record<string, string> = {};
|
||||||
|
for (const [field, messages] of Object.entries(fieldErrors)) {
|
||||||
|
if (messages?.[0]) {
|
||||||
|
firstMessagePerField[field] = messages[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return firstMessagePerField;
|
||||||
|
}
|
||||||
42
apps/admin-web/src/locales/fr/translation.json
Normal file
42
apps/admin-web/src/locales/fr/translation.json
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"errors": {
|
||||||
|
"VALIDATION_ERROR": "Erreur de validation",
|
||||||
|
"INVALID_CREDENTIALS": "Email ou mot de passe incorrect",
|
||||||
|
"NOT_AUTHENTICATED": "Vous devez être connecté",
|
||||||
|
"NOT_FOUND": "Ressource introuvable",
|
||||||
|
"TECH_STEP_NOT_FOUND": "Cette technique n'existe pas",
|
||||||
|
"INTERNAL_ERROR": "Une erreur est survenue, réessayez plus tard"
|
||||||
|
},
|
||||||
|
"admin": {
|
||||||
|
"common": {
|
||||||
|
"comingSoon": "Section à venir."
|
||||||
|
},
|
||||||
|
"login": {
|
||||||
|
"title": "Administration",
|
||||||
|
"emailLabel": "Email",
|
||||||
|
"passwordLabel": "Mot de passe",
|
||||||
|
"submit": "Se connecter",
|
||||||
|
"submitting": "Connexion…"
|
||||||
|
},
|
||||||
|
"nav": {
|
||||||
|
"dashboard": "Tableau de bord",
|
||||||
|
"monitoring": "Monitoring",
|
||||||
|
"corrections": "Corrections"
|
||||||
|
},
|
||||||
|
"layout": {
|
||||||
|
"logout": "Se déconnecter"
|
||||||
|
},
|
||||||
|
"dashboard": {
|
||||||
|
"title": "Tableau de bord",
|
||||||
|
"lead": "Métriques d'utilisation de l'application."
|
||||||
|
},
|
||||||
|
"monitoring": {
|
||||||
|
"title": "Monitoring",
|
||||||
|
"lead": "Santé des microservices et de la base de données."
|
||||||
|
},
|
||||||
|
"corrections": {
|
||||||
|
"title": "Corrections",
|
||||||
|
"lead": "Tri des corrections utilisateur pour le ré-entraînement NLP."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
apps/admin-web/src/main.tsx
Normal file
24
apps/admin-web/src/main.tsx
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { StrictMode } from "react";
|
||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import { App } from "./App";
|
||||||
|
import { AdminAuthProvider } from "./features/auth/AdminAuthContext";
|
||||||
|
// Side-effect import: initializes i18next before anything renders.
|
||||||
|
import "./i18n/i18n";
|
||||||
|
// Global stylesheet (theme tokens + minimal reset) — the only non-colocated .scss import.
|
||||||
|
import "./styles/global.scss";
|
||||||
|
|
||||||
|
const rootElement = document.getElementById("root");
|
||||||
|
if (!rootElement) {
|
||||||
|
throw new Error("Root element not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
createRoot(rootElement).render(
|
||||||
|
<StrictMode>
|
||||||
|
<BrowserRouter>
|
||||||
|
<AdminAuthProvider>
|
||||||
|
<App />
|
||||||
|
</AdminAuthProvider>
|
||||||
|
</BrowserRouter>
|
||||||
|
</StrictMode>,
|
||||||
|
);
|
||||||
19
apps/admin-web/src/pages/corrections/CorrectionsPage.tsx
Normal file
19
apps/admin-web/src/pages/corrections/CorrectionsPage.tsx
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import "../admin-page.scss";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tech-step correction triage — review `TechStepTrainingSuggestion` /
|
||||||
|
* `StepTechStepCorrection`, mark applied/rejected, generate the
|
||||||
|
* `training_data.py` snippet, and trigger the F1 gate + backfill. Placeholder
|
||||||
|
* until PR 5 (correction triage + retrain).
|
||||||
|
*/
|
||||||
|
export function CorrectionsPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
return (
|
||||||
|
<div className="admin-page">
|
||||||
|
<h1 className="admin-page__title">{t("admin.corrections.title")}</h1>
|
||||||
|
<p className="admin-page__lead">{t("admin.corrections.lead")}</p>
|
||||||
|
<p className="admin-page__placeholder">{t("admin.common.comingSoon")}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
17
apps/admin-web/src/pages/dashboard/DashboardPage.tsx
Normal file
17
apps/admin-web/src/pages/dashboard/DashboardPage.tsx
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import "../admin-page.scss";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Usage-metrics dashboard — KPI tiles + trend charts fed by
|
||||||
|
* `GET /admin/metrics`. Placeholder until PR 3 (metrics) fills it in.
|
||||||
|
*/
|
||||||
|
export function DashboardPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
return (
|
||||||
|
<div className="admin-page">
|
||||||
|
<h1 className="admin-page__title">{t("admin.dashboard.title")}</h1>
|
||||||
|
<p className="admin-page__lead">{t("admin.dashboard.lead")}</p>
|
||||||
|
<p className="admin-page__placeholder">{t("admin.common.comingSoon")}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -2,20 +2,20 @@ import { adminLoginSchema, ErrorCode } from "@batch-cooking/shared";
|
||||||
import { type FormEvent, useState } from "react";
|
import { type FormEvent, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { AdminApiError } from "../../../api/admin-client";
|
import { ApiError } from "../../api/client";
|
||||||
import { useAdminAuth } from "../../../features/admin/AdminAuthContext";
|
import { useAdminAuth } from "../../features/auth/AdminAuthContext";
|
||||||
import "../../../features/admin/admin-auth.scss";
|
import "../../features/auth/admin-auth.scss";
|
||||||
import { fieldErrorsFrom } from "../../../lib/zod-errors";
|
import { fieldErrorsFrom } from "../../lib/zod-errors";
|
||||||
import { errorMessageService } from "../../../services/error-message.service";
|
import { errorMessageService } from "../../services/error-message.service";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The admin login screen — the only unauthenticated route under `/admin`.
|
* The admin login screen — the only unauthenticated route. Client-side
|
||||||
* Client-side validation via the shared `adminLoginSchema` (same rules the
|
* validation via the shared `adminLoginSchema` (same rules the API
|
||||||
* API enforces), then `POST /admin/auth/login`; any API failure is
|
* enforces), then `POST /admin/auth/login`; any API failure is translated
|
||||||
* translated to a localized label via {@link ErrorMessageService}. Same
|
* to a localized label via {@link ErrorMessageService}. Same structure as
|
||||||
* structure as the user-facing `LoginPage` (`pages/auth/`).
|
* apps/web's `LoginPage`.
|
||||||
*/
|
*/
|
||||||
export function AdminLoginPage() {
|
export function LoginPage() {
|
||||||
const { login } = useAdminAuth();
|
const { login } = useAdminAuth();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
@ -40,9 +40,9 @@ export function AdminLoginPage() {
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
try {
|
try {
|
||||||
await login(result.data);
|
await login(result.data);
|
||||||
void navigate("/admin");
|
void navigate("/");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const code = err instanceof AdminApiError ? err.code : ErrorCode.INTERNAL_ERROR;
|
const code = err instanceof ApiError ? err.code : ErrorCode.INTERNAL_ERROR;
|
||||||
setFormError(errorMessageService.getLabel(code));
|
setFormError(errorMessageService.getLabel(code));
|
||||||
} finally {
|
} finally {
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
18
apps/admin-web/src/pages/monitoring/MonitoringPage.tsx
Normal file
18
apps/admin-web/src/pages/monitoring/MonitoringPage.tsx
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import "../admin-page.scss";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Microservice health board — active probes of Postgres, the API, the
|
||||||
|
* intent-service and the LLM worker's heartbeat, fed by
|
||||||
|
* `GET /admin/monitoring`. Placeholder until PR 4 (monitoring).
|
||||||
|
*/
|
||||||
|
export function MonitoringPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
return (
|
||||||
|
<div className="admin-page">
|
||||||
|
<h1 className="admin-page__title">{t("admin.monitoring.title")}</h1>
|
||||||
|
<p className="admin-page__lead">{t("admin.monitoring.lead")}</p>
|
||||||
|
<p className="admin-page__placeholder">{t("admin.common.comingSoon")}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
19
apps/admin-web/src/services/error-message.service.ts
Normal file
19
apps/admin-web/src/services/error-message.service.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { ErrorCode } from "@batch-cooking/shared";
|
||||||
|
import i18n from "../i18n/i18n";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Localized label for an {@link ErrorCode} returned by the admin API —
|
||||||
|
* verbatim behaviour of apps/web's `ErrorMessageService`: reverse-maps the
|
||||||
|
* numeric enum value to its member name (`4010` → `"INVALID_CREDENTIALS"`)
|
||||||
|
* and looks it up under the `errors` namespace, falling back to
|
||||||
|
* `INTERNAL_ERROR` for a code this client doesn't recognise.
|
||||||
|
*/
|
||||||
|
export class ErrorMessageService {
|
||||||
|
public getLabel(code: ErrorCode): string {
|
||||||
|
const memberName = ErrorCode[code] ?? ErrorCode[ErrorCode.INTERNAL_ERROR];
|
||||||
|
return i18n.t(`errors.${memberName}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Single shared instance — stateless. */
|
||||||
|
export const errorMessageService = new ErrorMessageService();
|
||||||
171
apps/admin-web/src/styles/_theme.scss
Normal file
171
apps/admin-web/src/styles/_theme.scss
Normal file
|
|
@ -0,0 +1,171 @@
|
||||||
|
// NOTE: verbatim copy of apps/web/src/styles/_theme.scss. Extracting these
|
||||||
|
// tokens into a shared package (consumed by both apps) is tracked separately
|
||||||
|
// — keep the two files in sync by hand until then.
|
||||||
|
// =============================================================================
|
||||||
|
// Design tokens — the single source of truth for colors, spacing, typography
|
||||||
|
// and other reusable values across the whole app.
|
||||||
|
//
|
||||||
|
// Exposed as CSS custom properties on :root (not plain SCSS variables) so
|
||||||
|
// they're available at *runtime*, not just compile time — this is what lets
|
||||||
|
// dark mode work below by simply redefining these variables instead of
|
||||||
|
// rebuilding the stylesheet. Every other .scss file should reference
|
||||||
|
// `var(--token-name)`, never a hardcoded color/size.
|
||||||
|
//
|
||||||
|
// Palette name: "Mise en Place" — a kitchen-operations identity (batch
|
||||||
|
// cooking as logistics: everything labeled and in its place before you
|
||||||
|
// start) rather than a food-blog one. See the design proposal for the full
|
||||||
|
// rationale: https://claude.ai/code/artifact/1db63af0-cfd1-4f77-9369-71ca6accd06f
|
||||||
|
//
|
||||||
|
// Import this partial once, globally (see global.scss) — never re-import it
|
||||||
|
// from a component-level .scss file, `:root` only needs to be declared once.
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
:root {
|
||||||
|
// --- Surfaces & ink ---------------------------------------------------
|
||||||
|
// Neutral surface: page background ("porcelaine") vs. the card surface
|
||||||
|
// content sits on, plus a recessed variant for panels/table headers.
|
||||||
|
--color-background: #eef2ed;
|
||||||
|
--color-surface: #ffffff;
|
||||||
|
--color-surface-alt: #e2e8e0;
|
||||||
|
// Text.
|
||||||
|
--color-text: #1f2a22;
|
||||||
|
--color-text-muted: #57685a;
|
||||||
|
--color-border: #c7d0c4;
|
||||||
|
|
||||||
|
// --- Brand accents, each with one job --------------------------------
|
||||||
|
// Basil — primary actions, links, brand presence.
|
||||||
|
--color-primary: #2e6b4a;
|
||||||
|
--color-primary-hover: #244f38;
|
||||||
|
// Vermillion — secondary accent for urgency/strong calls to action
|
||||||
|
// (e.g. a timer, "start session"). Never reused for allergen alerts
|
||||||
|
// below — those need their own, unambiguous color.
|
||||||
|
--color-accent: #cc4b26;
|
||||||
|
--color-accent-hover: #a83c1c;
|
||||||
|
// Turmeric — category/classification tags.
|
||||||
|
--color-tag: #c98a1b;
|
||||||
|
--color-tag-ink: #3a2c05; // pairs with a solid --color-tag fill only.
|
||||||
|
|
||||||
|
// --- Feedback -----------------------------------------------------------
|
||||||
|
--color-success: #2e6b4a;
|
||||||
|
--color-warning: #c98a1b;
|
||||||
|
--color-error: #b3271e;
|
||||||
|
|
||||||
|
// --- Allergens / intolerances --------------------------------------------
|
||||||
|
// A 3-tier food-safety scale, kept distinct from --color-error so an
|
||||||
|
// allergen warning is never confused with a form validation error:
|
||||||
|
// - critical (declared allergen): its own color, solid/inverted fill
|
||||||
|
// - moderate (intolerance): reuses --color-warning, tinted fill
|
||||||
|
// - trace ("may contain traces of…"): neutral, dashed outline
|
||||||
|
// The severity is carried by the FILL TREATMENT, not the hue alone, so
|
||||||
|
// it stays legible for color-blind users. See the design proposal's
|
||||||
|
// "Alertes & allergènes" section for the full component set.
|
||||||
|
--color-allergen: #a8123f;
|
||||||
|
--color-allergen-ink: #ffe9ef; // pairs with a solid --color-allergen fill only.
|
||||||
|
|
||||||
|
// --- Spacing scale ---------------------------------------------------------
|
||||||
|
// Multiples of a 4px base unit — use these instead of ad hoc px values so
|
||||||
|
// spacing stays visually consistent as the app grows.
|
||||||
|
--space-xs: 0.25rem; // 4px
|
||||||
|
--space-sm: 0.5rem; // 8px
|
||||||
|
--space-md: 1rem; // 16px
|
||||||
|
--space-lg: 1.5rem; // 24px
|
||||||
|
--space-xl: 2rem; // 32px
|
||||||
|
--space-2xl: 3rem; // 48px — inter-section spacing
|
||||||
|
|
||||||
|
// --- Typography --------------------------------------------------------
|
||||||
|
// System font stacks only — no remote webfont, so there's zero loading
|
||||||
|
// latency and no flash of unstyled text, which fits an app meant to be
|
||||||
|
// used quickly under time pressure. Three roles: a condensed "label"
|
||||||
|
// face for headings/eyebrows, a humanist face for body copy, and a
|
||||||
|
// monospace for anything that lines up in columns (times, quantities).
|
||||||
|
--font-display: "Bahnschrift", "Arial Narrow", "Segoe UI", sans-serif;
|
||||||
|
--font-body: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
|
||||||
|
--font-mono: "Cascadia Mono", Consolas, "SF Mono", "Liberation Mono", monospace;
|
||||||
|
|
||||||
|
--font-size-xs: 0.75rem; // 12px — captions, meta
|
||||||
|
--font-size-sm: 0.875rem; // 14px — labels, secondary text
|
||||||
|
--font-size-base: 1rem; // 16px — body
|
||||||
|
--font-size-md: 1.125rem; // 18px — lead paragraph
|
||||||
|
--font-size-lg: 1.375rem; // 22px — H3 / card titles
|
||||||
|
--font-size-xl: 1.75rem; // 28px — H2 / section titles
|
||||||
|
--font-size-2xl: 2.25rem; // 36px — H1 / page titles
|
||||||
|
--font-size-3xl: 3rem; // 48px — display, exceptional use only
|
||||||
|
|
||||||
|
// --- Shape / elevation --------------------------------------------------
|
||||||
|
--radius-base: 4px; // controls (inputs, buttons) — deliberately flat
|
||||||
|
--radius-md: 10px; // cards
|
||||||
|
--radius-lg: 18px; // panels, modals
|
||||||
|
--radius-pill: 999px; // tags, badges
|
||||||
|
--max-width-form: 22rem;
|
||||||
|
|
||||||
|
--shadow-sm: 0 1px 2px rgba(31, 42, 34, 0.08), 0 1px 1px rgba(31, 42, 34, 0.06);
|
||||||
|
--shadow-md: 0 6px 16px rgba(31, 42, 34, 0.12), 0 2px 6px rgba(31, 42, 34, 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lets the browser pick sensible default colors (form controls, scrollbars)
|
||||||
|
// for whichever mode the user ends up in.
|
||||||
|
:root {
|
||||||
|
color-scheme: light dark;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Dark mode ---------------------------------------------------------
|
||||||
|
// Follows the OS/browser preference by default. Guarded with
|
||||||
|
// `:root:not([data-theme="light"])` so an explicit "light" choice (see
|
||||||
|
// apps/web's `ThemeContext`, `SYSTEM` = no `data-theme` attribute at all —
|
||||||
|
// this block then decides) can override a dark OS setting.
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root:not([data-theme="light"]) {
|
||||||
|
--color-background: #14181a;
|
||||||
|
--color-surface: #1c221e;
|
||||||
|
--color-surface-alt: #262e27;
|
||||||
|
--color-text: #edf1ea;
|
||||||
|
--color-text-muted: #a9b5a6;
|
||||||
|
--color-border: #384038;
|
||||||
|
|
||||||
|
--color-primary: #5fae7e;
|
||||||
|
--color-primary-hover: #7cc496;
|
||||||
|
--color-accent: #ea7a48;
|
||||||
|
--color-accent-hover: #f0946c;
|
||||||
|
--color-tag: #e8b84b;
|
||||||
|
--color-tag-ink: #2a2005;
|
||||||
|
|
||||||
|
--color-success: #5fae7e;
|
||||||
|
--color-warning: #e8b84b;
|
||||||
|
--color-error: #e5675a;
|
||||||
|
|
||||||
|
--color-allergen: #e2547b;
|
||||||
|
--color-allergen-ink: #3a0416;
|
||||||
|
|
||||||
|
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
|
||||||
|
--shadow-md: 0 8px 20px rgba(0, 0, 0, 0.45);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mirrors the block above for an explicit "dark" choice (`ThemeContext`
|
||||||
|
// sets `data-theme="dark"` on `<html>`), so it wins over the OS setting in
|
||||||
|
// both directions.
|
||||||
|
:root[data-theme="dark"] {
|
||||||
|
--color-background: #14181a;
|
||||||
|
--color-surface: #1c221e;
|
||||||
|
--color-surface-alt: #262e27;
|
||||||
|
--color-text: #edf1ea;
|
||||||
|
--color-text-muted: #a9b5a6;
|
||||||
|
--color-border: #384038;
|
||||||
|
|
||||||
|
--color-primary: #5fae7e;
|
||||||
|
--color-primary-hover: #7cc496;
|
||||||
|
--color-accent: #ea7a48;
|
||||||
|
--color-accent-hover: #f0946c;
|
||||||
|
--color-tag: #e8b84b;
|
||||||
|
--color-tag-ink: #2a2005;
|
||||||
|
|
||||||
|
--color-success: #5fae7e;
|
||||||
|
--color-warning: #e8b84b;
|
||||||
|
--color-error: #e5675a;
|
||||||
|
|
||||||
|
--color-allergen: #e2547b;
|
||||||
|
--color-allergen-ink: #3a0416;
|
||||||
|
|
||||||
|
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
|
||||||
|
--shadow-md: 0 8px 20px rgba(0, 0, 0, 0.45);
|
||||||
|
}
|
||||||
149
apps/admin-web/src/styles/global.scss
Normal file
149
apps/admin-web/src/styles/global.scss
Normal file
|
|
@ -0,0 +1,149 @@
|
||||||
|
// =============================================================================
|
||||||
|
// Global stylesheet — imported exactly once, in main.tsx. Contains only
|
||||||
|
// truly app-wide rules: the theme tokens and a minimal reset/base styling
|
||||||
|
// that every page inherits. Anything specific to one component or page
|
||||||
|
// belongs in a .scss file colocated next to that component/page instead.
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
@use "./theme";
|
||||||
|
|
||||||
|
// Include borders/padding in an element's declared width/height everywhere,
|
||||||
|
// rather than the browser default of adding them on top.
|
||||||
|
*,
|
||||||
|
*::before,
|
||||||
|
*::after {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minimal reset: remove the default body margin so pages can control their
|
||||||
|
// own layout without fighting the browser's default 8px margin.
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
font-family: var(--font-body);
|
||||||
|
font-size: var(--font-size-base);
|
||||||
|
line-height: 1.55;
|
||||||
|
color: var(--color-text);
|
||||||
|
background: var(--color-background);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headings use the condensed "label" face app-wide — see _theme.scss for
|
||||||
|
// the rationale. `text-wrap: balance` avoids a lone short word wrapping
|
||||||
|
// onto its own line in multi-line titles.
|
||||||
|
h1,
|
||||||
|
h2,
|
||||||
|
h3,
|
||||||
|
h4,
|
||||||
|
h5,
|
||||||
|
h6 {
|
||||||
|
margin: 0;
|
||||||
|
font-family: var(--font-display);
|
||||||
|
font-weight: 700;
|
||||||
|
text-wrap: balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to the page-title size; a heading used as a smaller component
|
||||||
|
// title (e.g. the auth card's <h1>) overrides this in its own stylesheet.
|
||||||
|
h1 {
|
||||||
|
font-size: var(--font-size-2xl);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A visible, consistent focus ring for keyboard navigation — the browser
|
||||||
|
// default varies a lot between elements and browsers.
|
||||||
|
:focus-visible {
|
||||||
|
outline: 2px solid var(--color-accent);
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checkbox/radio appearance, app-wide — "selectable card" style: the native
|
||||||
|
// control itself is visually hidden (still real, focusable and
|
||||||
|
// screen-reader-visible — see the `input[type=...]` rule below, not
|
||||||
|
// `display: none`) and the whole label row it lives in becomes the
|
||||||
|
// interactive surface instead: a flat bordered box that fills in with a
|
||||||
|
// tinted background + primary border once selected, with a checkmark
|
||||||
|
// fading in on the leading edge.
|
||||||
|
//
|
||||||
|
// The base (unselected) look below is detected structurally with `:has()`
|
||||||
|
// — safe, since "does this label contain a checkbox/radio" never changes
|
||||||
|
// after mount. The *selected* look is instead driven by the `is-selected`
|
||||||
|
// class {@link CheckboxOption}/{@link RadioOption} (components/ui/) toggle
|
||||||
|
// in JS from the same boolean their caller already passes to `checked` —
|
||||||
|
// chaining a second `:has(:checked)` to react to that live state turned
|
||||||
|
// out to be unreliable across browsers, so this only needs one
|
||||||
|
// always-true `:has()`.
|
||||||
|
//
|
||||||
|
// Every checkbox/radio in the app goes through this one place (the allergy
|
||||||
|
// grid, the theme picker, anywhere future) rather than each feature styling
|
||||||
|
// its own — see profile-forms.scss / settings-pages.scss, which only
|
||||||
|
// arrange these within their own layout (grid vs. stacked list) and
|
||||||
|
// intentionally don't re-style the control/label look itself.
|
||||||
|
label:has(> input[type="checkbox"]),
|
||||||
|
label:has(> input[type="radio"]) {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--space-xs);
|
||||||
|
padding: var(--space-xs) var(--space-sm);
|
||||||
|
// Overrides the generic `label { font-weight: 600 }` base rule
|
||||||
|
// (profile-forms.scss) — without this, an *unselected* row reads just as
|
||||||
|
// bold as a selected one (only `.allergy-select__option` happened to set
|
||||||
|
// its own 400 already; `.theme-select__option` didn't, so its rows were
|
||||||
|
// all permanently bold until this was centralized here).
|
||||||
|
font-weight: 400;
|
||||||
|
border: 1.5px solid var(--color-border);
|
||||||
|
border-radius: var(--radius-base);
|
||||||
|
background: var(--color-surface);
|
||||||
|
cursor: pointer;
|
||||||
|
transition:
|
||||||
|
background-color 0.15s ease,
|
||||||
|
border-color 0.15s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
border-color: var(--color-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
&.is-selected {
|
||||||
|
border-color: var(--color-primary);
|
||||||
|
background: color-mix(in srgb, var(--color-primary) 12%, var(--color-surface));
|
||||||
|
color: var(--color-primary);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:has(:focus-visible) {
|
||||||
|
outline: 2px solid var(--color-accent);
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The control itself is removed from the visual flow — hidden the
|
||||||
|
// "sr-only" way (not `display: none`) so it stays focusable/tabbable and
|
||||||
|
// announced correctly by screen readers; the label above carries the
|
||||||
|
// entire visible selected/unchecked look.
|
||||||
|
input[type="checkbox"],
|
||||||
|
input[type="radio"] {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
margin: 0;
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The checkmark — a real element (see components/ui/Checkbox.tsx /
|
||||||
|
// Radio.tsx) shown via the same `is-selected` class as the label's own
|
||||||
|
// look above, not a separate CSS-only trigger. Scaled in from nothing so
|
||||||
|
// toggling has a bit of motion. Same mark for both checkbox and radio: one
|
||||||
|
// consistent "selected" language app-wide rather than a checkmark here and
|
||||||
|
// a dot there. Sits first in the row (before the label text, per DOM
|
||||||
|
// order) — a classic "control on the left" layout rather than trailing.
|
||||||
|
.check-mark {
|
||||||
|
flex: none;
|
||||||
|
width: 0.9rem;
|
||||||
|
height: 0.9rem;
|
||||||
|
background: var(--color-primary);
|
||||||
|
clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
|
||||||
|
transform: scale(0);
|
||||||
|
transition: transform 0.1s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.is-selected .check-mark {
|
||||||
|
transform: scale(1);
|
||||||
|
}
|
||||||
5
apps/admin-web/src/vite-env.d.ts
vendored
Normal file
5
apps/admin-web/src/vite-env.d.ts
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
/// <reference types="vite/client" />
|
||||||
|
|
||||||
|
// Injected by `define` in vite.config.ts, sourced from package.json's
|
||||||
|
// version field — rendered in AdminLayout.tsx.
|
||||||
|
declare const __APP_VERSION__: string;
|
||||||
14
apps/admin-web/tsconfig.app.json
Normal file
14
apps/admin-web/tsconfig.app.json
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "Bundler",
|
||||||
|
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"types": ["vite/client"],
|
||||||
|
"noEmit": true,
|
||||||
|
"composite": true,
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo"
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
8
apps/admin-web/tsconfig.json
Normal file
8
apps/admin-web/tsconfig.json
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "Bundler"
|
||||||
|
},
|
||||||
|
"files": [],
|
||||||
|
"references": [{ "path": "./tsconfig.app.json" }, { "path": "./tsconfig.node.json" }]
|
||||||
|
}
|
||||||
12
apps/admin-web/tsconfig.node.json
Normal file
12
apps/admin-web/tsconfig.node.json
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "Bundler",
|
||||||
|
"composite": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["vite.config.ts"]
|
||||||
|
}
|
||||||
19
apps/admin-web/vite.config.ts
Normal file
19
apps/admin-web/vite.config.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import react from "@vitejs/plugin-react";
|
||||||
|
import { defineConfig } from "vite";
|
||||||
|
|
||||||
|
// Read once at config-eval time — same trick as apps/web's vite.config.ts.
|
||||||
|
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf-8"));
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [react()],
|
||||||
|
server: { port: 5174 },
|
||||||
|
css: {
|
||||||
|
preprocessorOptions: {
|
||||||
|
scss: { api: "modern-compiler" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
define: {
|
||||||
|
__APP_VERSION__: JSON.stringify(pkg.version),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
@ -25,13 +25,3 @@ INTENT_SERVICE_SECRET=changeme-generate-a-real-random-secret-at-least-32-chars
|
||||||
# Generate your own the same way as JWT_SECRET above; must match the
|
# Generate your own the same way as JWT_SECRET above; must match the
|
||||||
# worker's own INTERNAL_WORKER_SECRET.
|
# worker's own INTERNAL_WORKER_SECRET.
|
||||||
# INTERNAL_WORKER_SECRET=changeme-generate-a-real-random-secret-at-least-32-chars
|
# INTERNAL_WORKER_SECRET=changeme-generate-a-real-random-secret-at-least-32-chars
|
||||||
|
|
||||||
# Only needed to use the admin surface (apps/web's /admin/* routes) — every
|
|
||||||
# /admin/* request 401s while unset (`requireAdmin` fails closed). MUST be a
|
|
||||||
# different value than JWT_SECRET. Generate your own the same way.
|
|
||||||
# ADMIN_JWT_SECRET=changeme-a-different-random-secret-at-least-32-chars
|
|
||||||
# Read only by `src/scripts/create-admin.ts` when its --email/--password/
|
|
||||||
# --name flags are omitted — never by the running server.
|
|
||||||
# ADMIN_INITIAL_EMAIL=ops@example.com
|
|
||||||
# ADMIN_INITIAL_PASSWORD=changeme-at-least-8-chars
|
|
||||||
# ADMIN_INITIAL_NAME=Ops
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
-- AlterTable: usage-metrics timestamps. Existing rows adopt the migration's
|
|
||||||
-- own timestamp (acceptable one-off skew for trend charts — same posture as
|
|
||||||
-- the ingredient_unit_catalog migration).
|
|
||||||
ALTER TABLE "user_profiles" ADD COLUMN "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
|
||||||
ALTER TABLE "recipe" ADD COLUMN "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
|
||||||
ALTER TABLE "planning" ADD COLUMN "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
|
||||||
ALTER TABLE "planning_item" ADD COLUMN "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "analytics_events" (
|
|
||||||
"id" SERIAL NOT NULL,
|
|
||||||
"type" TEXT NOT NULL,
|
|
||||||
"actor_type" TEXT NOT NULL,
|
|
||||||
"actor_id" INTEGER,
|
|
||||||
"context" JSONB,
|
|
||||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
|
|
||||||
CONSTRAINT "analytics_events_pkey" PRIMARY KEY ("id")
|
|
||||||
);
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE INDEX "analytics_events_type_created_at_idx" ON "analytics_events"("type", "created_at");
|
|
||||||
|
|
||||||
-- CreateTable
|
|
||||||
CREATE TABLE "worker_heartbeats" (
|
|
||||||
"worker_key" TEXT NOT NULL,
|
|
||||||
"last_seen_at" TIMESTAMP(3) NOT NULL,
|
|
||||||
"last_run_at" TIMESTAMP(3),
|
|
||||||
"last_result" JSONB,
|
|
||||||
|
|
||||||
CONSTRAINT "worker_heartbeats_pkey" PRIMARY KEY ("worker_key")
|
|
||||||
);
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
-- AlterTable: off-catalog ingredient "placeholder" rows. Every existing row
|
|
||||||
-- is a real seeded catalog entry, so the flag defaults to false and the four
|
|
||||||
-- new nullable columns stay NULL for them — no backfill needed.
|
|
||||||
ALTER TABLE "ingredients" ADD COLUMN "is_placeholder" BOOLEAN NOT NULL DEFAULT false;
|
|
||||||
ALTER TABLE "ingredients" ADD COLUMN "display_name" TEXT;
|
|
||||||
ALTER TABLE "ingredients" ADD COLUMN "created_by_id" INTEGER;
|
|
||||||
ALTER TABLE "ingredients" ADD COLUMN "created_at" TIMESTAMP(3);
|
|
||||||
ALTER TABLE "ingredients" ADD COLUMN "reviewed_at" TIMESTAMP(3);
|
|
||||||
|
|
||||||
-- CreateIndex
|
|
||||||
CREATE INDEX "ingredients_is_placeholder_idx" ON "ingredients"("is_placeholder");
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "ingredients" ADD CONSTRAINT "ingredients_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "user_profiles"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
||||||
|
|
@ -103,10 +103,6 @@ model UserProfile {
|
||||||
tokenVersion Int @default(0) @map("token_version")
|
tokenVersion Int @default(0) @map("token_version")
|
||||||
houseId Int? @map("house_id")
|
houseId Int? @map("house_id")
|
||||||
dietId Int? @map("diet_id")
|
dietId Int? @map("diet_id")
|
||||||
/// See `Planning.createdAt` — same admin-metrics-only timestamp, added by
|
|
||||||
/// the `admin_metrics` migration for the dashboard's signup curve. No
|
|
||||||
/// application code reads it (auth doesn't need it).
|
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
|
||||||
|
|
||||||
house House? @relation("HouseMember", fields: [houseId], references: [id], onDelete: SetNull)
|
house House? @relation("HouseMember", fields: [houseId], references: [id], onDelete: SetNull)
|
||||||
diet Diet? @relation(fields: [dietId], references: [id], onDelete: SetNull)
|
diet Diet? @relation(fields: [dietId], references: [id], onDelete: SetNull)
|
||||||
|
|
@ -129,9 +125,6 @@ model UserProfile {
|
||||||
/// view a recipe may correct its tech-step matches, not just its author —
|
/// view a recipe may correct its tech-step matches, not just its author —
|
||||||
/// see `StepTechStepCorrection.correctorId`).
|
/// see `StepTechStepCorrection.correctorId`).
|
||||||
techStepCorrections StepTechStepCorrection[]
|
techStepCorrections StepTechStepCorrection[]
|
||||||
/// Placeholder `Ingredient` rows this profile created by typing a free-text
|
|
||||||
/// ingredient the catalog didn't cover — see `Ingredient.isPlaceholder`.
|
|
||||||
createdIngredientPlaceholders Ingredient[] @relation("PlaceholderCreator")
|
|
||||||
|
|
||||||
@@map("user_profiles")
|
@@map("user_profiles")
|
||||||
}
|
}
|
||||||
|
|
@ -199,12 +192,6 @@ model Planning {
|
||||||
startDate DateTime @map("start_date") @db.Date
|
startDate DateTime @map("start_date") @db.Date
|
||||||
finishDate DateTime @map("finish_date") @db.Date
|
finishDate DateTime @map("finish_date") @db.Date
|
||||||
houseId Int @map("house_id")
|
houseId Int @map("house_id")
|
||||||
/// When this planning row was first created. Added by the `admin_metrics`
|
|
||||||
/// migration purely for the admin dashboard's activity curves — no
|
|
||||||
/// application code reads it. Rows that predate the migration all get the
|
|
||||||
/// migration's own timestamp (same acceptable one-off skew as the
|
|
||||||
/// `ingredient_unit_catalog` migration), which is fine for a trend chart.
|
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
|
||||||
|
|
||||||
house House @relation(fields: [houseId], references: [id], onDelete: Cascade)
|
house House @relation(fields: [houseId], references: [id], onDelete: Cascade)
|
||||||
items PlanningItem[]
|
items PlanningItem[]
|
||||||
|
|
@ -219,8 +206,6 @@ model PlanningItem {
|
||||||
meal String
|
meal String
|
||||||
recipeId Int @map("recipe_id")
|
recipeId Int @map("recipe_id")
|
||||||
portions Int
|
portions Int
|
||||||
/// See `Planning.createdAt` — same admin-metrics-only timestamp.
|
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
|
||||||
|
|
||||||
planning Planning @relation(fields: [planningId], references: [id], onDelete: Cascade)
|
planning Planning @relation(fields: [planningId], references: [id], onDelete: Cascade)
|
||||||
recipe Recipe @relation(fields: [recipeId], references: [id])
|
recipe Recipe @relation(fields: [recipeId], references: [id])
|
||||||
|
|
@ -334,10 +319,6 @@ model Recipe {
|
||||||
/// the author had no household yet.
|
/// the author had no household yet.
|
||||||
authorHouseId Int? @map("author_house_id")
|
authorHouseId Int? @map("author_house_id")
|
||||||
visibility RecipeVisibility @default(PERSONAL)
|
visibility RecipeVisibility @default(PERSONAL)
|
||||||
/// See `Planning.createdAt` — same admin-metrics-only timestamp, added by
|
|
||||||
/// the `admin_metrics` migration for the dashboard's "recipes created"
|
|
||||||
/// curve. No application code reads it.
|
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
|
||||||
|
|
||||||
author UserProfile @relation(fields: [authorId], references: [id])
|
author UserProfile @relation(fields: [authorId], references: [id])
|
||||||
authorHouse House? @relation(fields: [authorHouseId], references: [id], onDelete: SetNull)
|
authorHouse House? @relation(fields: [authorHouseId], references: [id], onDelete: SetNull)
|
||||||
|
|
@ -533,38 +514,6 @@ model Ingredient {
|
||||||
/// ingredient↔recipe linking in the database, the UI only pre-fills the
|
/// ingredient↔recipe linking in the database, the UI only pre-fills the
|
||||||
/// catalog's own search with this ingredient's name).
|
/// catalog's own search with this ingredient's name).
|
||||||
reproducible Boolean @default(false)
|
reproducible Boolean @default(false)
|
||||||
/// `true` = a "placeholder" row: a free-text ingredient a user typed on a
|
|
||||||
/// recipe line because the seeded catalog had nothing matching (see
|
|
||||||
/// specs/batch-cooking-modele.md's "ingrédients hors-catalogue"). Such a
|
|
||||||
/// row has `displayName` non-null, a generated `key` (`placeholder:<uuid>`,
|
|
||||||
/// never an i18n label), and the default metadata (`JAR`/`dryGoods`/`other`,
|
|
||||||
/// no allergen/diet links). It is referenced by `RecipeIngredient` like any
|
|
||||||
/// other `Ingredient`, but `GET /reference/ingredients` and
|
|
||||||
/// `ingredient-matcher.ts`'s `loadIngredientCatalog` both exclude it — it is
|
|
||||||
/// never a browsable/matchable target, only a per-line stand-in a
|
|
||||||
/// maintainer later promotes into a real catalog entry by hand. The
|
|
||||||
/// `/admin/catalog/placeholders` view groups these by normalized name so
|
|
||||||
/// the maintainer sees which ingredients the catalog is missing.
|
|
||||||
isPlaceholder Boolean @default(false) @map("is_placeholder")
|
|
||||||
/// Display name of a placeholder ingredient — the exact text the user
|
|
||||||
/// typed. `null` for a real catalog row (whose label lives in i18n under
|
|
||||||
/// `catalog.ingredients.<key>`). Invariant "non-null iff `isPlaceholder`"
|
|
||||||
/// is enforced service-side, not by the schema (same posture as other
|
|
||||||
/// cross-field invariants here).
|
|
||||||
displayName String? @map("display_name")
|
|
||||||
/// Profile that first created this placeholder — context for the admin
|
|
||||||
/// catalog-gap review. `onDelete: SetNull` so deleting an account never
|
|
||||||
/// blocks on, or cascades into, the recipes that still use its placeholder.
|
|
||||||
/// `null` for a real catalog row.
|
|
||||||
createdById Int? @map("created_by_id")
|
|
||||||
/// When this placeholder was created. `null` for a real catalog row (the
|
|
||||||
/// seed carries no timestamp).
|
|
||||||
createdAt DateTime? @map("created_at")
|
|
||||||
/// Stamped when an admin has triaged this catalog gap
|
|
||||||
/// (`PATCH /admin/catalog/placeholders/mark-reviewed`) — the group then
|
|
||||||
/// drops out of the default "à traiter" list. `null` while pending / for a
|
|
||||||
/// real catalog row.
|
|
||||||
reviewedAt DateTime? @map("reviewed_at")
|
|
||||||
|
|
||||||
recipes RecipeIngredient[]
|
recipes RecipeIngredient[]
|
||||||
allergies IngredientAllergy[]
|
allergies IngredientAllergy[]
|
||||||
|
|
@ -575,10 +524,7 @@ model Ingredient {
|
||||||
/// Mentions of this ingredient detected in a step's free text alongside a
|
/// Mentions of this ingredient detected in a step's free text alongside a
|
||||||
/// technique — see `StepTechStepIngredient`.
|
/// technique — see `StepTechStepIngredient`.
|
||||||
stepTechSteps StepTechStepIngredient[]
|
stepTechSteps StepTechStepIngredient[]
|
||||||
/// The profile that created this row when it is a placeholder — see `createdById`.
|
|
||||||
createdBy UserProfile? @relation("PlaceholderCreator", fields: [createdById], references: [id], onDelete: SetNull)
|
|
||||||
|
|
||||||
@@index([isPlaceholder])
|
|
||||||
@@map("ingredients")
|
@@map("ingredients")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -984,48 +930,3 @@ model AdminUser {
|
||||||
|
|
||||||
@@map("admin_users")
|
@@map("admin_users")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// One recorded product event, for the admin dashboard's usage metrics.
|
|
||||||
/// Written fire-and-forget by `lib/analytics.service.ts`'s `recordEvent`
|
|
||||||
/// from a handful of key service methods (signup, recipe import/create,
|
|
||||||
/// planning add, cooking-session open, tech-step correction, shopping-list
|
|
||||||
/// view) — never on the request's critical path, so a failed insert is
|
|
||||||
/// logged and swallowed, never surfaced to the user.
|
|
||||||
///
|
|
||||||
/// `type` is a free `String` (`"user.signup"`, `"recipe.imported"`…), not
|
|
||||||
/// an enum: adding a new event to instrument is a one-line call site
|
|
||||||
/// change with **no migration**. `actorId` is a `UserProfile.id` when
|
|
||||||
/// `actorType == "user"` but carries **no FK** — an event is an immutable
|
|
||||||
/// historical fact that must outlive the account it describes (a deleted
|
|
||||||
/// user's signup still counts on the curve). `context` is a small free
|
|
||||||
/// JSON blob (`{ sourceKey, recipeId, … }`) for slicing later; nothing
|
|
||||||
/// queries into it today.
|
|
||||||
model AnalyticsEvent {
|
|
||||||
id Int @id @default(autoincrement())
|
|
||||||
type String
|
|
||||||
actorType String @map("actor_type")
|
|
||||||
actorId Int? @map("actor_id")
|
|
||||||
context Json?
|
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
|
||||||
|
|
||||||
@@index([type, createdAt])
|
|
||||||
@@map("analytics_events")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Liveness/last-run record for a background worker that has no inbound
|
|
||||||
/// HTTP surface of its own — one row per worker (`workerKey`, today only
|
|
||||||
/// `"tech-step-llm-worker"`). The worker POSTs `/internal/tech-steps/heartbeat`
|
|
||||||
/// (`requireInternalWorker`) on boot, on every scheduler tick, and after
|
|
||||||
/// each job; the admin monitoring board reads this to show the worker as
|
|
||||||
/// up / stale / down and to surface its last job result. Upserted, never
|
|
||||||
/// accumulated — only the latest state matters.
|
|
||||||
model WorkerHeartbeat {
|
|
||||||
workerKey String @id @map("worker_key")
|
|
||||||
lastSeenAt DateTime @map("last_seen_at")
|
|
||||||
/// Set only by a `"job"` heartbeat — the last time the worker actually ran a job (vs. just a tick proving it's alive).
|
|
||||||
lastRunAt DateTime? @map("last_run_at")
|
|
||||||
/// Small JSON summary of that last job (`{ job, ok, counts }`).
|
|
||||||
lastResult Json? @map("last_result")
|
|
||||||
|
|
||||||
@@map("worker_heartbeats")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import { errorLogger } from "./middlewares/error-logger.js";
|
||||||
import { requestLogger } from "./middlewares/request-logger.js";
|
import { requestLogger } from "./middlewares/request-logger.js";
|
||||||
import { adminRouter } from "./modules/admin/admin.routes.js";
|
import { adminRouter } from "./modules/admin/admin.routes.js";
|
||||||
import { authRouter } from "./modules/auth/auth.routes.js";
|
import { authRouter } from "./modules/auth/auth.routes.js";
|
||||||
import { cookingSessionRouter } from "./modules/cooking-session/cooking-session.routes.js";
|
|
||||||
import { houseRouter } from "./modules/house/house.routes.js";
|
import { houseRouter } from "./modules/house/house.routes.js";
|
||||||
import { techStepWorkerRouter } from "./modules/internal/tech-step-worker.routes.js";
|
import { techStepWorkerRouter } from "./modules/internal/tech-step-worker.routes.js";
|
||||||
import { planningRouter } from "./modules/planning/planning.routes.js";
|
import { planningRouter } from "./modules/planning/planning.routes.js";
|
||||||
|
|
@ -34,20 +33,18 @@ export function createServer(): ExpressServer {
|
||||||
// pipeline (its "finish" listener still fires for a request that never
|
// pipeline (its "finish" listener still fires for a request that never
|
||||||
// makes it past CORS/body-parsing, not just ones that reach a route).
|
// makes it past CORS/body-parsing, not just ones that reach a route).
|
||||||
server.addMiddleware(requestLogger);
|
server.addMiddleware(requestLogger);
|
||||||
// One allowed origin: the app (`CORS_ORIGIN`). The admin surface
|
// Two allowed origins: the main app (`CORS_ORIGIN`) and the separate
|
||||||
// (`/admin/*`) is served by this same API and consumed by `apps/web`'s
|
// admin app (`ADMIN_CORS_ORIGIN`). The `cors` package matches an incoming
|
||||||
// own `/admin/*` routes — same origin as the rest of the app, so no
|
// `Origin` against any entry of the list.
|
||||||
// extra CORS entry.
|
server.setupCore({ corsOrigin: [env.CORS_ORIGIN, env.ADMIN_CORS_ORIGIN] });
|
||||||
server.setupCore({ corsOrigin: env.CORS_ORIGIN });
|
|
||||||
|
|
||||||
server.addRoute("get", "/health", (_req: Request, res: Response) => {
|
server.addRoute("get", "/health", (_req: Request, res: Response) => {
|
||||||
res.status(200).json({ status: "ok" });
|
res.status(200).json({ status: "ok" });
|
||||||
});
|
});
|
||||||
|
|
||||||
server.mountRouter("/auth", authRouter);
|
server.mountRouter("/auth", authRouter);
|
||||||
// Admin application surface (`apps/web`'s `/admin/*` routes) — its own
|
// Admin application surface (`apps/admin-web`) — its own auth
|
||||||
// auth (`requireAdmin`, distinct cookie/secret), never the end-user
|
// (`requireAdmin`, distinct cookie/secret), never the end-user session.
|
||||||
// session.
|
|
||||||
server.mountRouter("/admin", adminRouter);
|
server.mountRouter("/admin", adminRouter);
|
||||||
server.mountRouter("/house", houseRouter);
|
server.mountRouter("/house", houseRouter);
|
||||||
// Not user-facing — `services/tech-step-llm-worker` only, guarded by
|
// Not user-facing — `services/tech-step-llm-worker` only, guarded by
|
||||||
|
|
@ -62,7 +59,6 @@ export function createServer(): ExpressServer {
|
||||||
server.mountRouter("/recipes", recipeRouter);
|
server.mountRouter("/recipes", recipeRouter);
|
||||||
server.mountRouter("/reference", referenceRouter);
|
server.mountRouter("/reference", referenceRouter);
|
||||||
server.mountRouter("/shopping-list", shoppingListRouter);
|
server.mountRouter("/shopping-list", shoppingListRouter);
|
||||||
server.mountRouter("/cooking-session", cookingSessionRouter);
|
|
||||||
server.mountRouter("/sources", sourcesRouter);
|
server.mountRouter("/sources", sourcesRouter);
|
||||||
|
|
||||||
// Serves the built frontend (production Docker image only — see
|
// Serves the built frontend (production Docker image only — see
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,8 @@ const envSchema = z.object({
|
||||||
.optional(),
|
.optional(),
|
||||||
/** Name of the httpOnly cookie carrying the admin session JWT — must differ from `AUTH_COOKIE_NAME` so the two sessions coexist in one browser. */
|
/** Name of the httpOnly cookie carrying the admin session JWT — must differ from `AUTH_COOKIE_NAME` so the two sessions coexist in one browser. */
|
||||||
ADMIN_COOKIE_NAME: z.string().default("admin_session"),
|
ADMIN_COOKIE_NAME: z.string().default("admin_session"),
|
||||||
|
/** Origin `apps/admin-web` is served from — added to the CORS allow-list alongside `CORS_ORIGIN`. */
|
||||||
|
ADMIN_CORS_ORIGIN: z.string().default("http://localhost:5174"),
|
||||||
/** Optional seed values read by `src/scripts/create-admin.ts` when its `--email`/`--password`/`--name` flags are omitted — never used by the running server. */
|
/** Optional seed values read by `src/scripts/create-admin.ts` when its `--email`/`--password`/`--name` flags are omitted — never used by the running server. */
|
||||||
ADMIN_INITIAL_EMAIL: z.string().optional(),
|
ADMIN_INITIAL_EMAIL: z.string().optional(),
|
||||||
ADMIN_INITIAL_PASSWORD: z.string().optional(),
|
ADMIN_INITIAL_PASSWORD: z.string().optional(),
|
||||||
|
|
|
||||||
|
|
@ -1,65 +0,0 @@
|
||||||
import type { Prisma } from "@prisma/client";
|
|
||||||
import { prisma } from "../db/prisma.js";
|
|
||||||
import { logger } from "./logger.service.js";
|
|
||||||
|
|
||||||
/** Who caused an {@link AnalyticsEvent}. `"user"` pairs with an `actorId` (`UserProfile.id`); `"system"` is a background job; `"anon"` is an unauthenticated request. */
|
|
||||||
export type AnalyticsActorType = "user" | "system" | "anon";
|
|
||||||
|
|
||||||
/** Optional context for {@link AnalyticsService.recordEvent}. */
|
|
||||||
export interface RecordEventOptions {
|
|
||||||
/** `UserProfile.id` — set together with `actorType: "user"` (the default when this is present). */
|
|
||||||
actorId?: number;
|
|
||||||
/** Overrides the inferred actor type (`"user"` when `actorId` is set, else `"anon"`). */
|
|
||||||
actorType?: AnalyticsActorType;
|
|
||||||
/** Small free-form blob for later slicing (`{ sourceKey, recipeId, … }`) — nothing queries into it today. */
|
|
||||||
context?: Prisma.InputJsonValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Records product usage events for the admin dashboard's metrics (see the
|
|
||||||
* `AnalyticsEvent` model doc comment). A class rather than a bare function
|
|
||||||
* — same convention as `LoggerService`/`ErrorHandlerService`: `public`
|
|
||||||
* `recordEvent` is the API, `_insert` is the internal it fans out to.
|
|
||||||
*
|
|
||||||
* **Fire-and-forget by contract**: `recordEvent` returns `void`, not a
|
|
||||||
* promise. The insert runs detached, and a failure is logged at `warn` and
|
|
||||||
* swallowed — analytics must never add latency to, or fail, the request
|
|
||||||
* that triggered it. Call sites therefore never `await` it.
|
|
||||||
*/
|
|
||||||
export class AnalyticsService {
|
|
||||||
public recordEvent(type: string, options: RecordEventOptions = {}): void {
|
|
||||||
const actorType: AnalyticsActorType =
|
|
||||||
options.actorType ?? (options.actorId !== undefined ? "user" : "anon");
|
|
||||||
|
|
||||||
void this._insert(type, actorType, options).catch((err: unknown) => {
|
|
||||||
logger.warn("Analytics event insert failed", {
|
|
||||||
eventType: type,
|
|
||||||
error: err instanceof Error ? err.message : String(err),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private async _insert(
|
|
||||||
type: string,
|
|
||||||
actorType: AnalyticsActorType,
|
|
||||||
options: RecordEventOptions,
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
await prisma.analyticsEvent.create({
|
|
||||||
data: {
|
|
||||||
type,
|
|
||||||
actorType,
|
|
||||||
actorId: options.actorId ?? null,
|
|
||||||
context: options.context,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
// Rethrown so `recordEvent`'s `.catch` above logs it — this layer
|
|
||||||
// just isn't allowed a bare `await` per the repo's convention.
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Single shared instance — stateless, same reasoning as `logger`. */
|
|
||||||
export const analytics = new AnalyticsService();
|
|
||||||
|
|
@ -1,511 +0,0 @@
|
||||||
import type {
|
|
||||||
CookingBackgroundTaskView,
|
|
||||||
CookingPhaseKind,
|
|
||||||
CookingPhaseView,
|
|
||||||
CookingSessionRecipeRef,
|
|
||||||
CookingTaskIngredientView,
|
|
||||||
CookingTaskView,
|
|
||||||
TechStepView,
|
|
||||||
UtensilView,
|
|
||||||
} from "@batch-cooking/shared";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The pure core of the "Calcul batch-cooking" module (`specs/batch-cooking-architecture.md`):
|
|
||||||
* takes the week's planned recipes — already resolved to reference views by
|
|
||||||
* `cooking-session.service.ts` — and reorganizes their steps into an ordered
|
|
||||||
* sequence of {@link CookingPhaseView}s that pools shared preparation and
|
|
||||||
* interleaves the recipes so passive cooks (simmer, braise, bake…) run in
|
|
||||||
* the background while the cook does active work from another recipe.
|
|
||||||
*
|
|
||||||
* Pure and synchronous, no database access — same `matchXxx()` pure /
|
|
||||||
* `loadXxx()` DB-backed split as `ingredient-matcher.ts` /
|
|
||||||
* `tech-step-matcher.ts` / `shopping-list.service.ts`'s
|
|
||||||
* `aggregateShoppingList`, so the whole optimization is unit-testable
|
|
||||||
* without a Postgres round-trip.
|
|
||||||
*
|
|
||||||
* v1 scope (see the plan / spec): preparation is the only thing *merged*
|
|
||||||
* across recipes — a `chop`/`peel`/… technique applied to the same
|
|
||||||
* ingredient by two or more recipes, in a step that does nothing but prep,
|
|
||||||
* collapses into a single {@link CookingTaskView} of `kind: "merged-prep"`.
|
|
||||||
* Cooking steps themselves are never merged (no "same oven, same
|
|
||||||
* temperature" reasoning yet); they're only *reordered* for parallelism.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Technique keys (`TechStep.key`, see `reference-seed-data.ts`'s
|
|
||||||
* `TECH_STEPS`) that are pure knife/prep work on an ingredient — the only
|
|
||||||
* techniques v1 pools across recipes. A *step* counts as prep only when
|
|
||||||
* **every** technique it mentions is in here (see {@link isPurePrepStep}):
|
|
||||||
* "émincer les oignons" merges, "faire revenir les oignons émincés" does
|
|
||||||
* not (its `panFry` keeps it a cooking step).
|
|
||||||
*/
|
|
||||||
const PREP_TECHNIQUES: ReadonlySet<string> = new Set([
|
|
||||||
"chop",
|
|
||||||
"peel",
|
|
||||||
"mince",
|
|
||||||
"julienne",
|
|
||||||
"brunoise",
|
|
||||||
"concasse",
|
|
||||||
"paysanne",
|
|
||||||
"mirepoix",
|
|
||||||
"zest",
|
|
||||||
"score",
|
|
||||||
"pod",
|
|
||||||
"shellEgg",
|
|
||||||
"hollowOut",
|
|
||||||
"filet",
|
|
||||||
"disgorge",
|
|
||||||
"sift",
|
|
||||||
"dustWithFlour",
|
|
||||||
"peelBlanch",
|
|
||||||
]);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How much of the cook's attention a technique needs once it's under way —
|
|
||||||
* the axis that makes parallelism possible.
|
|
||||||
*
|
|
||||||
* - `"SETUP"` — a short active trigger, then it looks after itself: preheat
|
|
||||||
* the oven, bring a pot of water to the boil. Pooled into the first
|
|
||||||
* ("mise en place") phase so it's running before it's needed.
|
|
||||||
* - `"PASSIVE"` — unattended once started (simmer, braise, bake, marinate,
|
|
||||||
* rest…). Scheduled, then floated into every following phase's
|
|
||||||
* `background` until the step that consumes it comes up.
|
|
||||||
* - anything not listed here, or a step with no detected technique at all,
|
|
||||||
* is treated as `"ACTIVE"` — hands-on, occupies the cook.
|
|
||||||
*/
|
|
||||||
const SETUP_TECHNIQUES: ReadonlySet<string> = new Set(["preheat", "boil", "bainMarie"]);
|
|
||||||
|
|
||||||
/** See {@link SETUP_TECHNIQUES}. */
|
|
||||||
const PASSIVE_TECHNIQUES: ReadonlySet<string> = new Set([
|
|
||||||
"simmer",
|
|
||||||
"bake",
|
|
||||||
"roast",
|
|
||||||
"braise",
|
|
||||||
"marinate",
|
|
||||||
"rest",
|
|
||||||
"proof",
|
|
||||||
"confit",
|
|
||||||
"reduce",
|
|
||||||
"blindBake",
|
|
||||||
"compote",
|
|
||||||
"smother",
|
|
||||||
"setGel",
|
|
||||||
"pasteurize",
|
|
||||||
"appertize",
|
|
||||||
"poach",
|
|
||||||
"sweat",
|
|
||||||
"glaze",
|
|
||||||
]);
|
|
||||||
|
|
||||||
/** Attention class of a single step — see {@link SETUP_TECHNIQUES}. */
|
|
||||||
type Attention = "SETUP" | "PASSIVE" | "ACTIVE";
|
|
||||||
|
|
||||||
/** One technique occurrence within a step, already scaled to the planned portions. */
|
|
||||||
interface OptimizerTechStepInput {
|
|
||||||
techStep: TechStepView;
|
|
||||||
order: number;
|
|
||||||
ingredients: CookingTaskIngredientView[];
|
|
||||||
utensils: UtensilView[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** One recipe step, as handed to {@link optimizeCookingPlan}. */
|
|
||||||
interface OptimizerStepInput {
|
|
||||||
stepId: number;
|
|
||||||
order: number;
|
|
||||||
description: string;
|
|
||||||
techSteps: OptimizerTechStepInput[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* One planned recipe, as handed to {@link optimizeCookingPlan}. `portions`
|
|
||||||
* is the planning slot's own count and `recipePortions` the recipe's
|
|
||||||
* as-written yield — quantities are scaled by `portions / recipePortions`
|
|
||||||
* (see {@link scaleOf}). The same recipe planned twice at different portion
|
|
||||||
* counts arrives as two entries with the same `recipeId`; that's
|
|
||||||
* intentional (two real cooking jobs), and merged-prep still pools their
|
|
||||||
* knife work back together.
|
|
||||||
*/
|
|
||||||
interface OptimizerRecipeInput {
|
|
||||||
recipeId: number;
|
|
||||||
name: string;
|
|
||||||
portions: number;
|
|
||||||
recipePortions: number;
|
|
||||||
steps: OptimizerStepInput[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@link optimizeCookingPlan}'s result — the date-range/legend wrapper is added by the service. */
|
|
||||||
interface OptimizeCookingPlanResult {
|
|
||||||
recipes: CookingSessionRecipeRef[];
|
|
||||||
phases: CookingPhaseView[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export type {
|
|
||||||
OptimizeCookingPlanResult,
|
|
||||||
OptimizerRecipeInput,
|
|
||||||
OptimizerStepInput,
|
|
||||||
OptimizerTechStepInput,
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Portion scale factor for a recipe — guards a missing/zero as-written yield (bad data) by falling back to 1× rather than dividing by zero. */
|
|
||||||
function scaleOf(recipe: OptimizerRecipeInput): number {
|
|
||||||
if (!recipe.recipePortions || recipe.recipePortions <= 0) return 1;
|
|
||||||
return recipe.portions / recipe.recipePortions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A step normalized for scheduling — techniques scaled, attention resolved, ingredients/utensils unioned across its technique clauses. */
|
|
||||||
interface NormalizedStep {
|
|
||||||
/** Stable within one response: `step:<recipeIndex>:<stepId>` (the index disambiguates the same recipe planned twice). */
|
|
||||||
taskId: string;
|
|
||||||
recipeIndex: number;
|
|
||||||
recipe: CookingSessionRecipeRef;
|
|
||||||
stepId: number;
|
|
||||||
order: number;
|
|
||||||
description: string;
|
|
||||||
techSteps: OptimizerTechStepInput[];
|
|
||||||
attention: Attention;
|
|
||||||
isPurePrep: boolean;
|
|
||||||
dominantTechnique: TechStepView | null;
|
|
||||||
ingredients: CookingTaskIngredientView[];
|
|
||||||
utensils: UtensilView[];
|
|
||||||
/** Set once merged-prep extraction absorbs this step wholesale (all its prep pooled elsewhere) — it then produces no standalone task. */
|
|
||||||
absorbed: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sums two ingredient lines only when it's unambiguous — same unit id and both quantities known; otherwise the pooled line carries no number (see `ShoppingListItemView`'s "don't guess a conversion" rule). */
|
|
||||||
function poolIngredient(lines: CookingTaskIngredientView[]): {
|
|
||||||
quantity: number | null;
|
|
||||||
unit: CookingTaskIngredientView["unit"];
|
|
||||||
} {
|
|
||||||
const first = lines[0];
|
|
||||||
if (!first) return { quantity: null, unit: null };
|
|
||||||
const unitId = first.unit?.id ?? null;
|
|
||||||
let total = 0;
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.quantity === null || (line.unit?.id ?? null) !== unitId) {
|
|
||||||
return { quantity: null, unit: null };
|
|
||||||
}
|
|
||||||
total += line.quantity;
|
|
||||||
}
|
|
||||||
return { quantity: total, unit: first.unit };
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Unions ingredient lines by `(ingredientId, unitId)`, summing quantities within a group the same careful way as {@link poolIngredient}. */
|
|
||||||
function unionIngredients(lines: CookingTaskIngredientView[]): CookingTaskIngredientView[] {
|
|
||||||
const groups = new Map<string, CookingTaskIngredientView[]>();
|
|
||||||
for (const line of lines) {
|
|
||||||
const key = `${line.ingredient.id}:${line.unit?.id ?? "x"}`;
|
|
||||||
const group = groups.get(key);
|
|
||||||
if (group) group.push(line);
|
|
||||||
else groups.set(key, [line]);
|
|
||||||
}
|
|
||||||
const out: CookingTaskIngredientView[] = [];
|
|
||||||
for (const group of groups.values()) {
|
|
||||||
const head = group[0];
|
|
||||||
if (!head) continue;
|
|
||||||
const pooled = poolIngredient(group);
|
|
||||||
out.push({ ingredient: head.ingredient, quantity: pooled.quantity, unit: pooled.unit });
|
|
||||||
}
|
|
||||||
return out.sort((a, b) => a.ingredient.key.localeCompare(b.ingredient.key));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Unions utensils by id, keeping a stable order by key. */
|
|
||||||
function unionUtensils(utensils: UtensilView[]): UtensilView[] {
|
|
||||||
const byId = new Map<number, UtensilView>();
|
|
||||||
for (const utensil of utensils) byId.set(utensil.id, utensil);
|
|
||||||
return [...byId.values()].sort((a, b) => a.key.localeCompare(b.key));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A step is pure prep only if it has techniques and every one of them is in {@link PREP_TECHNIQUES}. */
|
|
||||||
function isPurePrepStep(techSteps: OptimizerTechStepInput[]): boolean {
|
|
||||||
return techSteps.length > 0 && techSteps.every((ts) => PREP_TECHNIQUES.has(ts.techStep.key));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Resolves a step's {@link Attention} — SETUP wins, then a *trailing* passive technique, else ACTIVE (see {@link SETUP_TECHNIQUES}). */
|
|
||||||
function attentionOf(techSteps: OptimizerTechStepInput[]): Attention {
|
|
||||||
if (techSteps.some((ts) => SETUP_TECHNIQUES.has(ts.techStep.key))) return "SETUP";
|
|
||||||
const last = techSteps[techSteps.length - 1];
|
|
||||||
if (last && PASSIVE_TECHNIQUES.has(last.techStep.key)) return "PASSIVE";
|
|
||||||
return "ACTIVE";
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Turns one recipe's raw steps into {@link NormalizedStep}s — scales quantities, resolves attention, unions per-clause ingredients/utensils up to the step. */
|
|
||||||
function normalizeRecipe(recipe: OptimizerRecipeInput, recipeIndex: number): NormalizedStep[] {
|
|
||||||
const scale = scaleOf(recipe);
|
|
||||||
const recipeRef: CookingSessionRecipeRef = {
|
|
||||||
recipeId: recipe.recipeId,
|
|
||||||
name: recipe.name,
|
|
||||||
portions: recipe.portions,
|
|
||||||
};
|
|
||||||
|
|
||||||
return [...recipe.steps]
|
|
||||||
.sort((a, b) => a.order - b.order)
|
|
||||||
.map((step) => {
|
|
||||||
const techSteps: OptimizerTechStepInput[] = [...step.techSteps]
|
|
||||||
.sort((a, b) => a.order - b.order)
|
|
||||||
.map((ts) => ({
|
|
||||||
techStep: ts.techStep,
|
|
||||||
order: ts.order,
|
|
||||||
ingredients: ts.ingredients.map((line) => ({
|
|
||||||
ingredient: line.ingredient,
|
|
||||||
quantity: line.quantity === null ? null : line.quantity * scale,
|
|
||||||
unit: line.unit,
|
|
||||||
})),
|
|
||||||
utensils: ts.utensils,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const lastTech = techSteps[techSteps.length - 1];
|
|
||||||
return {
|
|
||||||
taskId: `step:${recipeIndex}:${step.stepId}`,
|
|
||||||
recipeIndex,
|
|
||||||
recipe: recipeRef,
|
|
||||||
stepId: step.stepId,
|
|
||||||
order: step.order,
|
|
||||||
description: step.description,
|
|
||||||
techSteps,
|
|
||||||
attention: attentionOf(techSteps),
|
|
||||||
isPurePrep: isPurePrepStep(techSteps),
|
|
||||||
dominantTechnique: lastTech ? lastTech.techStep : null,
|
|
||||||
ingredients: unionIngredients(techSteps.flatMap((ts) => ts.ingredients)),
|
|
||||||
utensils: unionUtensils(techSteps.flatMap((ts) => ts.utensils)),
|
|
||||||
absorbed: false,
|
|
||||||
} satisfies NormalizedStep;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The prep signature of a pure-prep step — sorted `<techniqueKey>:<ingredientId>` pairs; two steps with the same signature do identical knife work and can be pooled. */
|
|
||||||
function prepSignature(step: NormalizedStep): string {
|
|
||||||
const pairs: string[] = [];
|
|
||||||
for (const ts of step.techSteps) {
|
|
||||||
for (const line of ts.ingredients) {
|
|
||||||
pairs.push(`${ts.techStep.key}:${line.ingredient.id}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [...new Set(pairs)].sort().join("+");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Builds one {@link CookingTaskView} from a normalized step run as written. */
|
|
||||||
function stepToTask(step: NormalizedStep): CookingTaskView {
|
|
||||||
return {
|
|
||||||
id: step.taskId,
|
|
||||||
kind: "step",
|
|
||||||
technique: step.dominantTechnique,
|
|
||||||
description: step.description,
|
|
||||||
ingredients: step.ingredients,
|
|
||||||
utensils: step.utensils,
|
|
||||||
sourceRecipes: [step.recipe],
|
|
||||||
originalSteps: [
|
|
||||||
{
|
|
||||||
recipeId: step.recipe.recipeId,
|
|
||||||
recipeName: step.recipe.name,
|
|
||||||
description: step.description,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Builds the running-in-the-background status line for a passive step already scheduled in an earlier phase. */
|
|
||||||
function stepToBackground(step: NormalizedStep): CookingBackgroundTaskView {
|
|
||||||
return {
|
|
||||||
id: `bg:${step.taskId}`,
|
|
||||||
technique: step.dominantTechnique,
|
|
||||||
description: step.description,
|
|
||||||
recipeId: step.recipe.recipeId,
|
|
||||||
recipeName: step.recipe.name,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pools pure-prep steps that do the *exact same* knife work (same
|
|
||||||
* {@link prepSignature}) in two or more distinct recipes into one
|
|
||||||
* `merged-prep` {@link CookingTaskView}, and marks every contributing step
|
|
||||||
* `absorbed` so it produces no standalone task. A pure-prep step whose
|
|
||||||
* signature is unique (only one recipe needs it) is left untouched — it
|
|
||||||
* still lands in the mise-en-place phase, just as its own step task.
|
|
||||||
*
|
|
||||||
* Returns the merged tasks in a stable order (by id).
|
|
||||||
*/
|
|
||||||
function extractMergedPrep(steps: NormalizedStep[]): CookingTaskView[] {
|
|
||||||
const bySignature = new Map<string, NormalizedStep[]>();
|
|
||||||
for (const step of steps) {
|
|
||||||
if (!step.isPurePrep) continue;
|
|
||||||
const signature = prepSignature(step);
|
|
||||||
if (signature === "") continue;
|
|
||||||
const group = bySignature.get(signature);
|
|
||||||
if (group) group.push(step);
|
|
||||||
else bySignature.set(signature, [step]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const merged: CookingTaskView[] = [];
|
|
||||||
for (const [signature, group] of bySignature) {
|
|
||||||
const recipeIndexes = new Set(group.map((s) => s.recipeIndex));
|
|
||||||
if (recipeIndexes.size < 2) continue;
|
|
||||||
|
|
||||||
for (const step of group) step.absorbed = true;
|
|
||||||
|
|
||||||
// Every contributing clause's ingredient lines, pooled per ingredient.
|
|
||||||
const allLines = group.flatMap((s) => s.techSteps.flatMap((ts) => ts.ingredients));
|
|
||||||
const ingredients = unionIngredients(allLines);
|
|
||||||
const utensils = unionUtensils(group.flatMap((s) => s.utensils));
|
|
||||||
|
|
||||||
// Dominant technique of the pool = the first pair's technique (v1
|
|
||||||
// signatures are almost always a single `<technique>:<ingredient>`
|
|
||||||
// pair; a multi-pair signature just takes the earliest).
|
|
||||||
const firstTech = group[0]?.techSteps[0]?.techStep ?? null;
|
|
||||||
const firstIngredientKey = ingredients[0]?.ingredient.key ?? signature;
|
|
||||||
|
|
||||||
// Distinct source recipes / original step texts, in input order.
|
|
||||||
const sourceRecipes: CookingSessionRecipeRef[] = [];
|
|
||||||
const seenRecipe = new Set<number>();
|
|
||||||
const originalSteps: CookingTaskView["originalSteps"] = [];
|
|
||||||
for (const step of [...group].sort((a, b) => a.recipeIndex - b.recipeIndex)) {
|
|
||||||
if (!seenRecipe.has(step.recipeIndex)) {
|
|
||||||
seenRecipe.add(step.recipeIndex);
|
|
||||||
sourceRecipes.push(step.recipe);
|
|
||||||
}
|
|
||||||
originalSteps.push({
|
|
||||||
recipeId: step.recipe.recipeId,
|
|
||||||
recipeName: step.recipe.name,
|
|
||||||
description: step.description,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
merged.push({
|
|
||||||
id: `prep:${firstTech ? firstTech.key : "prep"}:${firstIngredientKey}`,
|
|
||||||
kind: "merged-prep",
|
|
||||||
technique: firstTech,
|
|
||||||
description: null,
|
|
||||||
ingredients,
|
|
||||||
utensils,
|
|
||||||
sourceRecipes,
|
|
||||||
originalSteps,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return merged.sort((a, b) => a.id.localeCompare(b.id));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Maps the input recipe list to its display legend, de-duplicating an exact `(recipeId, portions)` repeat. */
|
|
||||||
function toRecipeLegend(recipes: OptimizerRecipeInput[]): CookingSessionRecipeRef[] {
|
|
||||||
const seen = new Set<string>();
|
|
||||||
const out: CookingSessionRecipeRef[] = [];
|
|
||||||
for (const recipe of recipes) {
|
|
||||||
const key = `${recipe.recipeId}:${recipe.portions}`;
|
|
||||||
if (seen.has(key)) continue;
|
|
||||||
seen.add(key);
|
|
||||||
out.push({ recipeId: recipe.recipeId, name: recipe.name, portions: recipe.portions });
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A phase is `"finishing"` when everything left in it is plating; otherwise it's a normal `"cooking"` phase. */
|
|
||||||
function cookingPhaseKind(tasks: CookingTaskView[]): CookingPhaseKind {
|
|
||||||
return tasks.every((task) => task.technique?.key === "plate") ? "finishing" : "cooking";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* See the file header. Given the week's planned recipes (already resolved
|
|
||||||
* to reference views), returns the display legend plus the ordered phases:
|
|
||||||
*
|
|
||||||
* 1. **Mise en place** (`"mise-en-place"`) — every `merged-prep` task, then
|
|
||||||
* every leftover pure-prep step, then every `SETUP` step. Omitted
|
|
||||||
* entirely if it would be empty.
|
|
||||||
* 2. **Cooking** (`"cooking"` / `"finishing"`) — the recipes interleaved:
|
|
||||||
* each phase pops the next remaining step of every recipe that still has
|
|
||||||
* one (passive-cook steps first, so long cooks start early). A passive
|
|
||||||
* step scheduled in one phase is echoed in every later phase's
|
|
||||||
* `background` until that recipe's next step is popped.
|
|
||||||
*/
|
|
||||||
export function optimizeCookingPlan(recipes: OptimizerRecipeInput[]): OptimizeCookingPlanResult {
|
|
||||||
const legend = toRecipeLegend(recipes);
|
|
||||||
const normalized = recipes.map((recipe, index) => normalizeRecipe(recipe, index));
|
|
||||||
const allSteps = normalized.flat();
|
|
||||||
|
|
||||||
const mergedPrep = extractMergedPrep(allSteps);
|
|
||||||
|
|
||||||
const phases: CookingPhaseView[] = [];
|
|
||||||
|
|
||||||
// Phase 0 — mise en place.
|
|
||||||
const miseTasks: CookingTaskView[] = [...mergedPrep];
|
|
||||||
for (const step of allSteps) {
|
|
||||||
if (step.absorbed) continue;
|
|
||||||
if (step.isPurePrep || step.attention === "SETUP") {
|
|
||||||
miseTasks.push(stepToTask(step));
|
|
||||||
step.absorbed = true; // consumed here, not again in the cooking loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (miseTasks.length > 0) {
|
|
||||||
phases.push({ index: 0, kind: "mise-en-place", tasks: miseTasks, background: [] });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cooking phases — one "next step of each recipe" per phase. `hold` keeps
|
|
||||||
// a recipe out of the *next* phase right after it starts a passive cook,
|
|
||||||
// so another recipe's active work fills that phase and the passive cook
|
|
||||||
// shows up as `background` there instead of being immediately followed by
|
|
||||||
// its own next step.
|
|
||||||
const queues = normalized.map((steps) => ({
|
|
||||||
remaining: steps.filter((s) => !s.absorbed),
|
|
||||||
cursor: 0,
|
|
||||||
hold: 0,
|
|
||||||
}));
|
|
||||||
/** Passive steps started in an earlier phase, keyed by recipe index, still "cooking". */
|
|
||||||
const runningPassive = new Map<number, NormalizedStep>();
|
|
||||||
|
|
||||||
while (queues.some((queue) => queue.cursor < queue.remaining.length)) {
|
|
||||||
const phaseSteps: NormalizedStep[] = [];
|
|
||||||
queues.forEach((queue, recipeIndex) => {
|
|
||||||
const next = queue.remaining[queue.cursor];
|
|
||||||
if (!next) return;
|
|
||||||
if (queue.hold > 0) {
|
|
||||||
// Still tending its passive cook this phase — leave it in
|
|
||||||
// `runningPassive` so it renders as background, don't advance.
|
|
||||||
queue.hold--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// This recipe is advancing — whatever passive cook it had going is
|
|
||||||
// now being tended to, so it stops showing as background.
|
|
||||||
runningPassive.delete(recipeIndex);
|
|
||||||
phaseSteps.push(next);
|
|
||||||
queue.cursor++;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Every recipe with steps left is holding on a passive cook — break the
|
|
||||||
// stall by releasing all holds and letting the next iteration advance.
|
|
||||||
if (phaseSteps.length === 0) {
|
|
||||||
for (const queue of queues) queue.hold = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Background = passive cooks from earlier phases not yet resolved above.
|
|
||||||
const background = [...runningPassive.values()].map(stepToBackground);
|
|
||||||
|
|
||||||
// Start the long cooks first within the phase.
|
|
||||||
phaseSteps.sort((a, b) => {
|
|
||||||
const rank = (s: NormalizedStep) => (s.attention === "PASSIVE" ? 0 : 1);
|
|
||||||
return rank(a) - rank(b) || a.recipeIndex - b.recipeIndex;
|
|
||||||
});
|
|
||||||
|
|
||||||
const tasks = phaseSteps.map(stepToTask);
|
|
||||||
phases.push({
|
|
||||||
index: phases.length,
|
|
||||||
kind: cookingPhaseKind(tasks),
|
|
||||||
tasks,
|
|
||||||
background,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const step of phaseSteps) {
|
|
||||||
if (step.attention === "PASSIVE") {
|
|
||||||
runningPassive.set(step.recipeIndex, step);
|
|
||||||
const queue = queues[step.recipeIndex];
|
|
||||||
if (queue) queue.hold = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// `index` was set from `phases.length` as we went; re-stamp so it always
|
|
||||||
// matches the final array position even if phase 0 was skipped.
|
|
||||||
phases.forEach((phase, index) => {
|
|
||||||
phase.index = index;
|
|
||||||
});
|
|
||||||
|
|
||||||
return { recipes: legend, phases };
|
|
||||||
}
|
|
||||||
|
|
@ -427,11 +427,6 @@ export async function loadIngredientCatalog(locale = "en"): Promise<IngredientMa
|
||||||
const labels = INGREDIENT_LABELS_BY_LOCALE[locale] ?? {};
|
const labels = INGREDIENT_LABELS_BY_LOCALE[locale] ?? {};
|
||||||
const synonyms = INGREDIENT_LABEL_SYNONYMS_BY_LOCALE[locale] ?? {};
|
const synonyms = INGREDIENT_LABEL_SYNONYMS_BY_LOCALE[locale] ?? {};
|
||||||
const ingredients = await prisma.ingredient.findMany({
|
const ingredients = await prisma.ingredient.findMany({
|
||||||
// Placeholder rows (user free-text, `placeholder:<uuid>` key) have no
|
|
||||||
// authored label so they'd be skipped by the `label === undefined`
|
|
||||||
// check below anyway — filtered here too so an import never even
|
|
||||||
// considers resolving one raw line to another line's placeholder.
|
|
||||||
where: { isPlaceholder: false },
|
|
||||||
select: { id: true, key: true },
|
select: { id: true, key: true },
|
||||||
});
|
});
|
||||||
const catalog: IngredientMatchEntry[] = [];
|
const catalog: IngredientMatchEntry[] = [];
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@ export interface AdminLocals {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Express middleware guarding every `/admin/*` route — the operations UI
|
* Express middleware guarding every `/admin/*` route — the operations app
|
||||||
* (`apps/web`'s `/admin/*` routes) authenticating as an `AdminUser`. Reads the admin
|
* (`apps/admin-web`) authenticating as an `AdminUser`. Reads the admin
|
||||||
* session cookie (`ADMIN_COOKIE_NAME`, deliberately **not** the same cookie
|
* session cookie (`ADMIN_COOKIE_NAME`, deliberately **not** the same cookie
|
||||||
* as end-user sessions), verifies the JWT against `ADMIN_JWT_SECRET`
|
* as end-user sessions), verifies the JWT against `ADMIN_JWT_SECRET`
|
||||||
* (a different secret than `JWT_SECRET`), and re-checks `tokenVersion`
|
* (a different secret than `JWT_SECRET`), and re-checks `tokenVersion`
|
||||||
|
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
|
||||||
import { listPlaceholdersQuerySchema, markPlaceholdersReviewedSchema } from "@batch-cooking/shared";
|
|
||||||
import { Router } from "express";
|
|
||||||
import { requireAdmin } from "../../middlewares/require-admin.js";
|
|
||||||
import {
|
|
||||||
listPlaceholderGroups,
|
|
||||||
markPlaceholdersReviewed,
|
|
||||||
pruneOrphanPlaceholders,
|
|
||||||
} from "./admin-catalog.service.js";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router mounted at `/admin/catalog` (via `admin.routes.ts`) — every route
|
|
||||||
* behind {@link requireAdmin}. Surfaces the off-catalog ingredient
|
|
||||||
* "placeholders" users typed when the seeded catalog fell short, so a
|
|
||||||
* maintainer can see what's missing and mark gaps as handled.
|
|
||||||
*/
|
|
||||||
export const adminCatalogRouter = Router();
|
|
||||||
|
|
||||||
adminCatalogRouter.get(
|
|
||||||
"/placeholders",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
res.status(200).json(await listPlaceholderGroups(listPlaceholdersQuerySchema.parse(req.query)));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
adminCatalogRouter.patch(
|
|
||||||
"/placeholders/mark-reviewed",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
res
|
|
||||||
.status(200)
|
|
||||||
.json(await markPlaceholdersReviewed(markPlaceholdersReviewedSchema.parse(req.body)));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
/** Deletes placeholder rows no recipe references any more — see {@link pruneOrphanPlaceholders}. */
|
|
||||||
adminCatalogRouter.post(
|
|
||||||
"/placeholders/prune-orphans",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (_req, res) => {
|
|
||||||
res.status(200).json(await pruneOrphanPlaceholders());
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
@ -1,156 +0,0 @@
|
||||||
import type {
|
|
||||||
CatalogPlaceholderGroupView,
|
|
||||||
ListPlaceholdersQuery,
|
|
||||||
MarkPlaceholdersReviewedInput,
|
|
||||||
PruneOrphansResultView,
|
|
||||||
} from "@batch-cooking/shared";
|
|
||||||
import { prisma } from "../../db/prisma.js";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Grouping key for two placeholder spellings that mean the same missing
|
|
||||||
* ingredient — lower-cased, accent-stripped, punctuation-neutralised,
|
|
||||||
* whitespace-collapsed. "Piment d'Espelette", "piment d espelette" and
|
|
||||||
* "PIMENT D'ESPELETTE" all normalise to `"piment d espelette"`, so the
|
|
||||||
* admin view shows one gap, not three. Pure (no DB) — unit-tested on its
|
|
||||||
* own, same split convention as `matchXxx()` vs `loadXxx()` elsewhere.
|
|
||||||
*/
|
|
||||||
export function normalizePlaceholderName(raw: string): string {
|
|
||||||
return raw
|
|
||||||
.normalize("NFD")
|
|
||||||
.replace(/\p{Diacritic}/gu, "")
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/[^\p{Letter}\p{Number}]+/gu, " ")
|
|
||||||
.trim()
|
|
||||||
.replace(/\s+/g, " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prisma `include` for the placeholder query — up to a few `RecipeIngredient` links per row, each with just enough of its recipe for the "seen in…" preview and the distinct-recipe count. */
|
|
||||||
const placeholderInclude = {
|
|
||||||
recipes: {
|
|
||||||
take: 5,
|
|
||||||
include: { recipe: { select: { id: true, name: true } } },
|
|
||||||
},
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Every placeholder `Ingredient` (see `Ingredient.isPlaceholder` in
|
|
||||||
* schema.prisma), grouped by {@link normalizePlaceholderName} so a
|
|
||||||
* maintainer reviews one row per *missing ingredient* rather than one per
|
|
||||||
* recipe line. Ordered by recipe impact (most-requested gap first), then
|
|
||||||
* name.
|
|
||||||
*
|
|
||||||
* `query.reviewed` selects which side to show: omitted / `"false"` drops
|
|
||||||
* groups whose every row has already been triaged (`reviewedAt` set) — the
|
|
||||||
* default working list; `"true"` keeps only those fully-triaged groups.
|
|
||||||
*/
|
|
||||||
export async function listPlaceholderGroups(
|
|
||||||
query: ListPlaceholdersQuery,
|
|
||||||
): Promise<CatalogPlaceholderGroupView[]> {
|
|
||||||
try {
|
|
||||||
const rows = await prisma.ingredient.findMany({
|
|
||||||
where: { isPlaceholder: true },
|
|
||||||
include: placeholderInclude,
|
|
||||||
orderBy: { createdAt: "asc" },
|
|
||||||
});
|
|
||||||
|
|
||||||
/** Accumulator per normalized name — mutated in the loop, shaped into the view after. */
|
|
||||||
interface GroupAccumulator {
|
|
||||||
normalizedName: string;
|
|
||||||
displayNames: Set<string>;
|
|
||||||
ingredientIds: number[];
|
|
||||||
recipeIds: Set<number>;
|
|
||||||
sampleRecipes: Map<number, string>;
|
|
||||||
firstSeenAt: Date | null;
|
|
||||||
allReviewed: boolean;
|
|
||||||
}
|
|
||||||
const groups = new Map<string, GroupAccumulator>();
|
|
||||||
|
|
||||||
for (const row of rows) {
|
|
||||||
const name = row.displayName ?? "";
|
|
||||||
const normalizedName = normalizePlaceholderName(name);
|
|
||||||
let group = groups.get(normalizedName);
|
|
||||||
if (!group) {
|
|
||||||
group = {
|
|
||||||
normalizedName,
|
|
||||||
displayNames: new Set(),
|
|
||||||
ingredientIds: [],
|
|
||||||
recipeIds: new Set(),
|
|
||||||
sampleRecipes: new Map(),
|
|
||||||
firstSeenAt: null,
|
|
||||||
allReviewed: true,
|
|
||||||
};
|
|
||||||
groups.set(normalizedName, group);
|
|
||||||
}
|
|
||||||
if (name.length > 0) group.displayNames.add(name);
|
|
||||||
group.ingredientIds.push(row.id);
|
|
||||||
for (const link of row.recipes) {
|
|
||||||
group.recipeIds.add(link.recipe.id);
|
|
||||||
if (group.sampleRecipes.size < 5) group.sampleRecipes.set(link.recipe.id, link.recipe.name);
|
|
||||||
}
|
|
||||||
if (row.createdAt && (group.firstSeenAt === null || row.createdAt < group.firstSeenAt)) {
|
|
||||||
group.firstSeenAt = row.createdAt;
|
|
||||||
}
|
|
||||||
if (row.reviewedAt === null) group.allReviewed = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// `reviewed=true` → the archive of handled gaps; anything else → the
|
|
||||||
// working list of gaps still to look at.
|
|
||||||
const wantReviewed = query.reviewed === "true";
|
|
||||||
return [...groups.values()]
|
|
||||||
.filter((group) => group.allReviewed === wantReviewed)
|
|
||||||
.map((group) => ({
|
|
||||||
normalizedName: group.normalizedName,
|
|
||||||
displayNames: [...group.displayNames].sort((a, b) => a.localeCompare(b, "fr")),
|
|
||||||
ingredientIds: group.ingredientIds,
|
|
||||||
recipeCount: group.recipeIds.size,
|
|
||||||
sampleRecipes: [...group.sampleRecipes.entries()].map(([id, name]) => ({ id, name })),
|
|
||||||
firstSeenAt: group.firstSeenAt?.toISOString() ?? null,
|
|
||||||
allReviewed: group.allReviewed,
|
|
||||||
}))
|
|
||||||
.sort(
|
|
||||||
(a, b) => b.recipeCount - a.recipeCount || a.normalizedName.localeCompare(b.normalizedName),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stamps `reviewedAt` on the given placeholder ids — a maintainer has seen
|
|
||||||
* this gap (and, if it warranted it, added the real catalog entry by hand;
|
|
||||||
* this endpoint never touches the catalog itself). Scoped to
|
|
||||||
* `isPlaceholder: true` so a stray real id is a silent no-op, not a
|
|
||||||
* mislabel. Returns how many rows were actually stamped.
|
|
||||||
*/
|
|
||||||
export async function markPlaceholdersReviewed(
|
|
||||||
input: MarkPlaceholdersReviewedInput,
|
|
||||||
): Promise<{ reviewed: number }> {
|
|
||||||
try {
|
|
||||||
const { count } = await prisma.ingredient.updateMany({
|
|
||||||
where: { id: { in: input.ingredientIds }, isPlaceholder: true },
|
|
||||||
data: { reviewedAt: new Date() },
|
|
||||||
});
|
|
||||||
return { reviewed: count };
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes placeholder rows no recipe references any more — the debris left
|
|
||||||
* when a recipe edit drops a placeholder line (the `RecipeIngredient` row
|
|
||||||
* goes, the `Ingredient` row doesn't). A manual GC (button in the admin
|
|
||||||
* catalog view, or `scripts/prune-orphan-placeholders.ts`) rather than a
|
|
||||||
* cascade: a placeholder is still evidence of a catalog gap even with no
|
|
||||||
* live recipe, so dropping it is a deliberate call, not automatic.
|
|
||||||
*/
|
|
||||||
export async function pruneOrphanPlaceholders(): Promise<PruneOrphansResultView> {
|
|
||||||
try {
|
|
||||||
const { count } = await prisma.ingredient.deleteMany({
|
|
||||||
where: { isPlaceholder: true, recipes: { none: {} } },
|
|
||||||
});
|
|
||||||
return { deleted: count };
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
|
||||||
import { getMetricsSchema } from "@batch-cooking/shared";
|
|
||||||
import { Router } from "express";
|
|
||||||
import { requireAdmin } from "../../middlewares/require-admin.js";
|
|
||||||
import { getMetrics } from "./admin-metrics.service.js";
|
|
||||||
|
|
||||||
/** Router mounted at `/admin/metrics` (via `admin.routes.ts`) — every route behind {@link requireAdmin}. */
|
|
||||||
export const adminMetricsRouter = Router();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the admin dashboard's usage metrics — a `snapshot` of current
|
|
||||||
* totals plus `?days=` (7–365, default 30) days of daily time series (see
|
|
||||||
* {@link getMetrics}). Read-only; no side effects.
|
|
||||||
*/
|
|
||||||
adminMetricsRouter.get(
|
|
||||||
"/",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
const { days } = getMetricsSchema.parse(req.query);
|
|
||||||
res.status(200).json(await getMetrics(days));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
@ -1,242 +0,0 @@
|
||||||
import type {
|
|
||||||
MetricsBreakdownRow,
|
|
||||||
MetricsEventSeries,
|
|
||||||
MetricsSnapshotView,
|
|
||||||
MetricsTimeBucket,
|
|
||||||
MetricsView,
|
|
||||||
} from "@batch-cooking/shared";
|
|
||||||
import { prisma } from "../../db/prisma.js";
|
|
||||||
|
|
||||||
/** UTC `YYYY-MM-DD` for a `Date` — the bucket key used by {@link bucketByDay}. */
|
|
||||||
function utcDayKey(date: Date): string {
|
|
||||||
return date.toISOString().slice(0, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Start-of-day (UTC) `Date` that is `daysAgo` days before `from`. */
|
|
||||||
function startOfUtcDay(from: Date, daysAgo: number): Date {
|
|
||||||
return new Date(Date.UTC(from.getUTCFullYear(), from.getUTCMonth(), from.getUTCDate() - daysAgo));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Buckets `dates` into `days` consecutive daily counts starting at `since`
|
|
||||||
* (a start-of-UTC-day `Date`). Every day in the window is present, days
|
|
||||||
* with no matching date carry `count: 0`. Pure/synchronous — factored out
|
|
||||||
* so the bucketing is unit-testable without a database, same split as
|
|
||||||
* `aggregateShoppingList`.
|
|
||||||
*/
|
|
||||||
export function bucketByDay(dates: Date[], since: Date, days: number): MetricsTimeBucket[] {
|
|
||||||
const counts = new Map<string, number>();
|
|
||||||
for (let i = 0; i < days; i++) {
|
|
||||||
const day = new Date(since.getTime() + i * 86_400_000);
|
|
||||||
counts.set(utcDayKey(day), 0);
|
|
||||||
}
|
|
||||||
for (const date of dates) {
|
|
||||||
const key = utcDayKey(date);
|
|
||||||
const current = counts.get(key);
|
|
||||||
if (current !== undefined) counts.set(key, current + 1);
|
|
||||||
}
|
|
||||||
return [...counts.entries()]
|
|
||||||
.sort(([a], [b]) => a.localeCompare(b))
|
|
||||||
.map(([date, count]) => ({ date, count }));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Shapes a Prisma `groupBy ... _count` result into the frontend's `{ key, label, count }` rows, sorted by count desc. */
|
|
||||||
function toBreakdown(
|
|
||||||
rows: { key: string | null; count: number }[],
|
|
||||||
labelFor: (key: string) => string = (key) => key,
|
|
||||||
): MetricsBreakdownRow[] {
|
|
||||||
return rows
|
|
||||||
.map(({ key, count }) => {
|
|
||||||
const resolved = key ?? "unknown";
|
|
||||||
return { key: resolved, label: labelFor(resolved), count };
|
|
||||||
})
|
|
||||||
.sort((a, b) => b.count - a.count);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Every point-in-time `COUNT` for the KPI tiles — see {@link MetricsSnapshotView}. */
|
|
||||||
async function getSnapshot(): Promise<MetricsSnapshotView> {
|
|
||||||
try {
|
|
||||||
const [
|
|
||||||
admins,
|
|
||||||
users,
|
|
||||||
households,
|
|
||||||
activeHouseholdGroups,
|
|
||||||
recipes,
|
|
||||||
recipesManual,
|
|
||||||
recipesImported,
|
|
||||||
recipeBySourceGroups,
|
|
||||||
sources,
|
|
||||||
plannings,
|
|
||||||
planningItems,
|
|
||||||
steps,
|
|
||||||
detectedTechniques,
|
|
||||||
favorites,
|
|
||||||
corrections,
|
|
||||||
correctionsUnconsumed,
|
|
||||||
correctionsRemoval,
|
|
||||||
trainingSuggestions,
|
|
||||||
suggestionStatusGroups,
|
|
||||||
suggestionSourceTypeGroups,
|
|
||||||
] = await Promise.all([
|
|
||||||
prisma.adminUser.count(),
|
|
||||||
prisma.userProfile.count(),
|
|
||||||
prisma.house.count(),
|
|
||||||
prisma.planning.groupBy({ by: ["houseId"] }),
|
|
||||||
prisma.recipe.count(),
|
|
||||||
prisma.recipe.count({ where: { sourceId: null } }),
|
|
||||||
prisma.recipe.count({ where: { sourceId: { not: null } } }),
|
|
||||||
prisma.recipe.groupBy({
|
|
||||||
by: ["sourceId"],
|
|
||||||
where: { sourceId: { not: null } },
|
|
||||||
_count: { _all: true },
|
|
||||||
}),
|
|
||||||
prisma.source.findMany({ select: { id: true, key: true, name: true } }),
|
|
||||||
prisma.planning.count(),
|
|
||||||
prisma.planningItem.count(),
|
|
||||||
prisma.step.count(),
|
|
||||||
prisma.stepTechStep.count(),
|
|
||||||
prisma.recipeFavorite.count(),
|
|
||||||
prisma.stepTechStepCorrection.count(),
|
|
||||||
prisma.stepTechStepCorrection.count({ where: { consumedAt: null } }),
|
|
||||||
prisma.stepTechStepCorrection.count({ where: { correctedTechStepId: null } }),
|
|
||||||
prisma.techStepTrainingSuggestion.count(),
|
|
||||||
prisma.techStepTrainingSuggestion.groupBy({ by: ["status"], _count: { _all: true } }),
|
|
||||||
prisma.techStepTrainingSuggestion.groupBy({ by: ["sourceType"], _count: { _all: true } }),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const sourceById = new Map(sources.map((source) => [source.id, source]));
|
|
||||||
|
|
||||||
return {
|
|
||||||
admins,
|
|
||||||
users,
|
|
||||||
households,
|
|
||||||
activeHouseholds: activeHouseholdGroups.length,
|
|
||||||
recipes,
|
|
||||||
recipesManual,
|
|
||||||
recipesImported,
|
|
||||||
recipesBySource: toBreakdown(
|
|
||||||
recipeBySourceGroups.map((group) => ({
|
|
||||||
key: group.sourceId === null ? null : (sourceById.get(group.sourceId)?.key ?? null),
|
|
||||||
count: group._count._all,
|
|
||||||
})),
|
|
||||||
(key) => {
|
|
||||||
const source = sources.find((s) => s.key === key);
|
|
||||||
return source ? source.name : key;
|
|
||||||
},
|
|
||||||
),
|
|
||||||
plannings,
|
|
||||||
planningItems,
|
|
||||||
steps,
|
|
||||||
detectedTechniques,
|
|
||||||
favorites,
|
|
||||||
corrections,
|
|
||||||
correctionsUnconsumed,
|
|
||||||
correctionsRemoval,
|
|
||||||
trainingSuggestions,
|
|
||||||
trainingSuggestionsByStatus: toBreakdown(
|
|
||||||
suggestionStatusGroups.map((group) => ({ key: group.status, count: group._count._all })),
|
|
||||||
),
|
|
||||||
trainingSuggestionsBySourceType: toBreakdown(
|
|
||||||
suggestionSourceTypeGroups.map((group) => ({
|
|
||||||
key: group.sourceType,
|
|
||||||
count: group._count._all,
|
|
||||||
})),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Builds the admin dashboard's full metrics payload — a `snapshot` of
|
|
||||||
* current totals plus `rangeDays` days of daily time series, derived from
|
|
||||||
* the `createdAt` columns the `admin_metrics` migration added and from the
|
|
||||||
* `AnalyticsEvent` table. `days` is the caller-validated `?days=` value
|
|
||||||
* (see `getMetricsSchema`, 7–365).
|
|
||||||
*/
|
|
||||||
export async function getMetrics(days: number): Promise<MetricsView> {
|
|
||||||
try {
|
|
||||||
const now = new Date();
|
|
||||||
const since = startOfUtcDay(now, days - 1);
|
|
||||||
|
|
||||||
const [
|
|
||||||
snapshot,
|
|
||||||
signups,
|
|
||||||
recipesCreated,
|
|
||||||
planningItemsAdded,
|
|
||||||
correctionsSubmitted,
|
|
||||||
trainingSuggestions,
|
|
||||||
eventRows,
|
|
||||||
] = await Promise.all([
|
|
||||||
getSnapshot(),
|
|
||||||
prisma.userProfile.findMany({
|
|
||||||
where: { createdAt: { gte: since } },
|
|
||||||
select: { createdAt: true },
|
|
||||||
}),
|
|
||||||
prisma.recipe.findMany({ where: { createdAt: { gte: since } }, select: { createdAt: true } }),
|
|
||||||
prisma.planningItem.findMany({
|
|
||||||
where: { createdAt: { gte: since } },
|
|
||||||
select: { createdAt: true },
|
|
||||||
}),
|
|
||||||
prisma.stepTechStepCorrection.findMany({
|
|
||||||
where: { createdAt: { gte: since } },
|
|
||||||
select: { createdAt: true },
|
|
||||||
}),
|
|
||||||
prisma.techStepTrainingSuggestion.findMany({
|
|
||||||
where: { createdAt: { gte: since } },
|
|
||||||
select: { createdAt: true },
|
|
||||||
}),
|
|
||||||
prisma.analyticsEvent.findMany({
|
|
||||||
where: { createdAt: { gte: since } },
|
|
||||||
select: { type: true, createdAt: true },
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const eventsByType = new Map<string, Date[]>();
|
|
||||||
for (const row of eventRows) {
|
|
||||||
const list = eventsByType.get(row.type);
|
|
||||||
if (list) list.push(row.createdAt);
|
|
||||||
else eventsByType.set(row.type, [row.createdAt]);
|
|
||||||
}
|
|
||||||
const events: MetricsEventSeries[] = [...eventsByType.entries()]
|
|
||||||
.sort(([a], [b]) => a.localeCompare(b))
|
|
||||||
.map(([type, dates]) => ({ type, buckets: bucketByDay(dates, since, days) }));
|
|
||||||
|
|
||||||
return {
|
|
||||||
generatedAt: now.toISOString(),
|
|
||||||
rangeDays: days,
|
|
||||||
snapshot,
|
|
||||||
series: {
|
|
||||||
signups: bucketByDay(
|
|
||||||
signups.map((r) => r.createdAt),
|
|
||||||
since,
|
|
||||||
days,
|
|
||||||
),
|
|
||||||
recipesCreated: bucketByDay(
|
|
||||||
recipesCreated.map((r) => r.createdAt),
|
|
||||||
since,
|
|
||||||
days,
|
|
||||||
),
|
|
||||||
planningItemsAdded: bucketByDay(
|
|
||||||
planningItemsAdded.map((r) => r.createdAt),
|
|
||||||
since,
|
|
||||||
days,
|
|
||||||
),
|
|
||||||
correctionsSubmitted: bucketByDay(
|
|
||||||
correctionsSubmitted.map((r) => r.createdAt),
|
|
||||||
since,
|
|
||||||
days,
|
|
||||||
),
|
|
||||||
trainingSuggestions: bucketByDay(
|
|
||||||
trainingSuggestions.map((r) => r.createdAt),
|
|
||||||
since,
|
|
||||||
days,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
events,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
|
||||||
import { Router } from "express";
|
|
||||||
import { requireAdmin } from "../../middlewares/require-admin.js";
|
|
||||||
import { getMonitoring } from "./admin-monitoring.service.js";
|
|
||||||
|
|
||||||
/** Router mounted at `/admin/monitoring` (via `admin.routes.ts`) — behind {@link requireAdmin}. */
|
|
||||||
export const adminMonitoringRouter = Router();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Actively probes Postgres, the API, `tech-step-intent-service` and the LLM
|
|
||||||
* worker's heartbeat, returning a {@link MonitoringView} status board (see
|
|
||||||
* {@link getMonitoring}). No params; the admin UI polls it on an interval.
|
|
||||||
*/
|
|
||||||
adminMonitoringRouter.get(
|
|
||||||
"/",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (_req, res) => {
|
|
||||||
res.status(200).json(await getMonitoring());
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
@ -1,174 +0,0 @@
|
||||||
import type { MonitoringView, ServiceHealthView, ServiceStatus } from "@batch-cooking/shared";
|
|
||||||
import { env } from "../../config/env.js";
|
|
||||||
import { prisma } from "../../db/prisma.js";
|
|
||||||
|
|
||||||
/** How long each outbound probe (Postgres query, intent-service HTTP) is allowed to take before it counts as `down`. */
|
|
||||||
const PROBE_TIMEOUT_MS = 2000;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Heartbeat-age thresholds for the LLM worker. Its default cron is weekly
|
|
||||||
* (`TECH_STEP_WORKER_CRON`, `0 3 * * 0`), and it also pings on boot/tick —
|
|
||||||
* so no ping for **8 days** means it likely missed its last scheduled fire
|
|
||||||
* (`degraded`), and none for **3 weeks** means it's almost certainly not
|
|
||||||
* running at all (`down`).
|
|
||||||
*/
|
|
||||||
const WORKER_STALE_AFTER_MS = 8 * 24 * 60 * 60 * 1000;
|
|
||||||
const WORKER_DOWN_AFTER_MS = 21 * 24 * 60 * 60 * 1000;
|
|
||||||
|
|
||||||
const WORKER_KEY = "tech-step-llm-worker";
|
|
||||||
|
|
||||||
function nowIso(): string {
|
|
||||||
return new Date().toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
function roundMs(value: number): number {
|
|
||||||
return Math.round(value * 10) / 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
function errMessage(err: unknown): string {
|
|
||||||
return err instanceof Error ? err.message : String(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** `12500` → `"il y a 12 s"`, `90000` → `"il y a 1 min"`, `172800000` → `"il y a 2 j"`. */
|
|
||||||
function formatAgo(ms: number): string {
|
|
||||||
const s = Math.round(ms / 1000);
|
|
||||||
if (s < 60) return `il y a ${s} s`;
|
|
||||||
const m = Math.round(s / 60);
|
|
||||||
if (m < 60) return `il y a ${m} min`;
|
|
||||||
const h = Math.round(m / 60);
|
|
||||||
if (h < 48) return `il y a ${h} h`;
|
|
||||||
return `il y a ${Math.round(h / 24)} j`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** `process.uptime()` seconds → `"3 h 12 min"` / `"5 min"` / `"42 s"`. */
|
|
||||||
function formatUptime(seconds: number): string {
|
|
||||||
const s = Math.floor(seconds);
|
|
||||||
if (s < 60) return `${s} s`;
|
|
||||||
const m = Math.floor(s / 60);
|
|
||||||
if (m < 60) return `${m} min`;
|
|
||||||
const h = Math.floor(m / 60);
|
|
||||||
return `${h} h ${m % 60} min`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** `prisma.$queryRaw\`SELECT 1\`` with a bounded timeout — the DB connectivity probe. */
|
|
||||||
async function probePostgres(): Promise<ServiceHealthView> {
|
|
||||||
const start = performance.now();
|
|
||||||
try {
|
|
||||||
// `$queryRaw` doesn't take an AbortSignal — bound it with a race instead.
|
|
||||||
await Promise.race([
|
|
||||||
prisma.$queryRaw`SELECT 1`,
|
|
||||||
new Promise((_resolve, reject) =>
|
|
||||||
setTimeout(() => reject(new Error("timeout")), PROBE_TIMEOUT_MS),
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
return {
|
|
||||||
key: "postgres",
|
|
||||||
status: "up",
|
|
||||||
latencyMs: roundMs(performance.now() - start),
|
|
||||||
detail: null,
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
return {
|
|
||||||
key: "postgres",
|
|
||||||
status: "down",
|
|
||||||
latencyMs: null,
|
|
||||||
detail: errMessage(err),
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The API itself — trivially "up" (it's answering), reported with its process uptime/memory. */
|
|
||||||
function probeApi(): ServiceHealthView {
|
|
||||||
const mem = process.memoryUsage();
|
|
||||||
return {
|
|
||||||
key: "api",
|
|
||||||
status: "up",
|
|
||||||
latencyMs: 0,
|
|
||||||
detail: `uptime ${formatUptime(process.uptime())} · RSS ${Math.round(mem.rss / 1_000_000)} Mo`,
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** `GET {INTENT_SERVICE_BASE_URL}/health` — no secret needed on that route (see the service's `routes/health.py`). */
|
|
||||||
async function probeIntentService(): Promise<ServiceHealthView> {
|
|
||||||
const start = performance.now();
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${env.INTENT_SERVICE_BASE_URL}/health`, {
|
|
||||||
signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
|
|
||||||
});
|
|
||||||
const latencyMs = roundMs(performance.now() - start);
|
|
||||||
return {
|
|
||||||
key: "intent-service",
|
|
||||||
status: res.ok ? "up" : "degraded",
|
|
||||||
latencyMs,
|
|
||||||
detail: `HTTP ${res.status}`,
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
return {
|
|
||||||
key: "intent-service",
|
|
||||||
status: "down",
|
|
||||||
latencyMs: null,
|
|
||||||
detail: errMessage(err),
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Reads the LLM worker's stored `WorkerHeartbeat` (it has no HTTP surface to probe directly) and grades it by age + last job outcome. */
|
|
||||||
async function probeWorker(): Promise<ServiceHealthView> {
|
|
||||||
const heartbeat = await prisma.workerHeartbeat.findUnique({ where: { workerKey: WORKER_KEY } });
|
|
||||||
if (!heartbeat) {
|
|
||||||
return {
|
|
||||||
key: WORKER_KEY,
|
|
||||||
status: "unknown",
|
|
||||||
latencyMs: null,
|
|
||||||
detail: "aucun battement reçu",
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
lastRunAt: null,
|
|
||||||
lastResult: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const ageMs = Date.now() - heartbeat.lastSeenAt.getTime();
|
|
||||||
const lastResult = (heartbeat.lastResult ?? null) as ServiceHealthView["lastResult"];
|
|
||||||
|
|
||||||
let status: ServiceStatus = "up";
|
|
||||||
if (ageMs > WORKER_DOWN_AFTER_MS) status = "down";
|
|
||||||
else if (ageMs > WORKER_STALE_AFTER_MS || lastResult?.ok === false) status = "degraded";
|
|
||||||
|
|
||||||
return {
|
|
||||||
key: WORKER_KEY,
|
|
||||||
status,
|
|
||||||
latencyMs: null,
|
|
||||||
detail: `dernier battement ${formatAgo(ageMs)}`,
|
|
||||||
checkedAt: nowIso(),
|
|
||||||
lastRunAt: heartbeat.lastRunAt?.toISOString() ?? null,
|
|
||||||
lastResult,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Actively probes every dependency the admin monitoring board watches —
|
|
||||||
* Postgres, the API itself, `tech-step-intent-service` (`/health`), and the
|
|
||||||
* LLM worker (via its stored heartbeat). Each probe is independent and
|
|
||||||
* bounded ({@link PROBE_TIMEOUT_MS}); one being `down` never fails the
|
|
||||||
* others or the endpoint.
|
|
||||||
*/
|
|
||||||
export async function getMonitoring(): Promise<MonitoringView> {
|
|
||||||
try {
|
|
||||||
const [postgres, intentService, worker] = await Promise.all([
|
|
||||||
probePostgres(),
|
|
||||||
probeIntentService(),
|
|
||||||
probeWorker(),
|
|
||||||
]);
|
|
||||||
return {
|
|
||||||
generatedAt: nowIso(),
|
|
||||||
services: [postgres, probeApi(), intentService, worker],
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,80 +0,0 @@
|
||||||
import { HttpError } from "@batch-cooking/error-tools";
|
|
||||||
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
|
||||||
import {
|
|
||||||
ErrorCode,
|
|
||||||
listCorrectionsQuerySchema,
|
|
||||||
listSuggestionsQuerySchema,
|
|
||||||
retrainRequestSchema,
|
|
||||||
trainingDataSnippetQuerySchema,
|
|
||||||
updateTrainingSuggestionSchema,
|
|
||||||
} from "@batch-cooking/shared";
|
|
||||||
import { Router } from "express";
|
|
||||||
import { requireAdmin } from "../../middlewares/require-admin.js";
|
|
||||||
import {
|
|
||||||
getTrainingDataSnippet,
|
|
||||||
listCorrections,
|
|
||||||
listSuggestions,
|
|
||||||
runRetrain,
|
|
||||||
updateSuggestion,
|
|
||||||
} from "./admin-tech-steps.service.js";
|
|
||||||
|
|
||||||
/** Router mounted at `/admin/tech-steps` (via `admin.routes.ts`) — every route behind {@link requireAdmin}. Correction/suggestion triage + the retrain trigger. */
|
|
||||||
export const adminTechStepsRouter = Router();
|
|
||||||
|
|
||||||
adminTechStepsRouter.get(
|
|
||||||
"/suggestions",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
res.status(200).json(await listSuggestions(listSuggestionsQuerySchema.parse(req.query)));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
adminTechStepsRouter.get(
|
|
||||||
"/corrections",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
res.status(200).json(await listCorrections(listCorrectionsQuerySchema.parse(req.query)));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
adminTechStepsRouter.get(
|
|
||||||
"/training-data-snippet",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
res
|
|
||||||
.status(200)
|
|
||||||
.json(await getTrainingDataSnippet(trainingDataSnippetQuerySchema.parse(req.query)));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
adminTechStepsRouter.patch(
|
|
||||||
"/suggestions/:id",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
const id = Number(req.params.id);
|
|
||||||
if (!Number.isInteger(id) || id <= 0) {
|
|
||||||
throw new HttpError(
|
|
||||||
400,
|
|
||||||
ErrorCode.VALIDATION_ERROR,
|
|
||||||
`Not a valid suggestion id: ${req.params.id}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const input = updateTrainingSuggestionSchema.parse(req.body);
|
|
||||||
res.status(200).json(await updateSuggestion(id, input));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the F1 regression gate then (if it passes) the full step backfill,
|
|
||||||
* and marks the given suggestion ids — see {@link runRetrain}. Long-running
|
|
||||||
* and process-locked: `409 RETRAIN_ALREADY_RUNNING` if one is already
|
|
||||||
* underway.
|
|
||||||
*/
|
|
||||||
adminTechStepsRouter.post(
|
|
||||||
"/retrain",
|
|
||||||
requireAdmin,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
const input = retrainRequestSchema.parse(req.body);
|
|
||||||
res.status(200).json(await runRetrain(input));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
@ -1,325 +0,0 @@
|
||||||
import { HttpError } from "@batch-cooking/error-tools";
|
|
||||||
import {
|
|
||||||
type CorrectionAdminView,
|
|
||||||
ErrorCode,
|
|
||||||
type ListCorrectionsQuery,
|
|
||||||
type ListSuggestionsQuery,
|
|
||||||
type RetrainRequestInput,
|
|
||||||
type RetrainResultView,
|
|
||||||
type TrainingDataSnippetQuery,
|
|
||||||
type TrainingDataSnippetView,
|
|
||||||
type TrainingSuggestionAdminView,
|
|
||||||
type TrainingSuggestionGroupView,
|
|
||||||
type UpdateTrainingSuggestionInput,
|
|
||||||
} from "@batch-cooking/shared";
|
|
||||||
import { prisma } from "../../db/prisma.js";
|
|
||||||
import {
|
|
||||||
MIN_OVERALL_F1,
|
|
||||||
runTechStepEvalSuite,
|
|
||||||
} from "../../lib/recipe-matching/tech-step-eval-runner.js";
|
|
||||||
import { backfillTechSteps } from "../../scripts/backfill-tech-steps.js";
|
|
||||||
|
|
||||||
/** How many raw corrections `listCorrections` returns per call — the browser is a triage view, not an export. */
|
|
||||||
const CORRECTIONS_PAGE_SIZE = 200;
|
|
||||||
|
|
||||||
const suggestionInclude = {
|
|
||||||
techStep: { select: { key: true } },
|
|
||||||
sourceCorrection: {
|
|
||||||
include: {
|
|
||||||
step: { select: { id: true, recipeId: true, description: true } },
|
|
||||||
previousTechStep: { select: { key: true } },
|
|
||||||
correctedTechStep: { select: { key: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
type SuggestionRow = Awaited<
|
|
||||||
ReturnType<
|
|
||||||
typeof prisma.techStepTrainingSuggestion.findFirstOrThrow<{ include: typeof suggestionInclude }>
|
|
||||||
>
|
|
||||||
>;
|
|
||||||
|
|
||||||
/** Shapes one Prisma suggestion row (with {@link suggestionInclude}) into its admin view. */
|
|
||||||
function toSuggestionView(row: SuggestionRow): TrainingSuggestionAdminView {
|
|
||||||
const correction = row.sourceCorrection;
|
|
||||||
return {
|
|
||||||
id: row.id,
|
|
||||||
techStepKey: row.techStep.key,
|
|
||||||
locale: row.locale,
|
|
||||||
suggestedSynonyms: row.suggestedSynonyms,
|
|
||||||
suggestedUtterances: row.suggestedUtterances,
|
|
||||||
sourceType: row.sourceType,
|
|
||||||
status: row.status,
|
|
||||||
createdAt: row.createdAt.toISOString(),
|
|
||||||
sourceCorrection: correction
|
|
||||||
? {
|
|
||||||
id: correction.id,
|
|
||||||
recipeId: correction.step.recipeId,
|
|
||||||
stepId: correction.step.id,
|
|
||||||
clauseText: correction.step.description.slice(correction.start, correction.end),
|
|
||||||
previousTechStepKey: correction.previousTechStep?.key ?? null,
|
|
||||||
correctedTechStepKey: correction.correctedTechStep?.key ?? null,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Every `TechStepTrainingSuggestion` matching the (all-optional) filters,
|
|
||||||
* grouped by technique key — same "one block per technique" organisation
|
|
||||||
* as `list-pending-training-suggestions.ts`'s CLI report, which this UI
|
|
||||||
* replaces.
|
|
||||||
*/
|
|
||||||
export async function listSuggestions(
|
|
||||||
query: ListSuggestionsQuery,
|
|
||||||
): Promise<TrainingSuggestionGroupView[]> {
|
|
||||||
try {
|
|
||||||
const rows = await prisma.techStepTrainingSuggestion.findMany({
|
|
||||||
where: {
|
|
||||||
...(query.status ? { status: query.status } : {}),
|
|
||||||
...(query.sourceType ? { sourceType: query.sourceType } : {}),
|
|
||||||
...(query.locale ? { locale: query.locale } : {}),
|
|
||||||
...(query.techStepKey ? { techStep: { key: query.techStepKey } } : {}),
|
|
||||||
},
|
|
||||||
orderBy: [{ techStepId: "asc" }, { createdAt: "asc" }],
|
|
||||||
include: suggestionInclude,
|
|
||||||
});
|
|
||||||
|
|
||||||
const byKey = new Map<string, TrainingSuggestionAdminView[]>();
|
|
||||||
for (const row of rows) {
|
|
||||||
const view = toSuggestionView(row);
|
|
||||||
const group = byKey.get(view.techStepKey);
|
|
||||||
if (group) group.push(view);
|
|
||||||
else byKey.set(view.techStepKey, [view]);
|
|
||||||
}
|
|
||||||
return [...byKey.entries()]
|
|
||||||
.sort(([a], [b]) => a.localeCompare(b))
|
|
||||||
.map(([techStepKey, suggestions]) => ({ techStepKey, suggestions }));
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Raw `StepTechStepCorrection`s for the admin browser — newest first,
|
|
||||||
* capped at {@link CORRECTIONS_PAGE_SIZE}. Unlike the worker's own
|
|
||||||
* `getPendingCorrections`, this **includes** the `correctedTechStepId IS NULL`
|
|
||||||
* removals ("no technique here") that never become suggestions and are
|
|
||||||
* otherwise invisible.
|
|
||||||
*/
|
|
||||||
export async function listCorrections(query: ListCorrectionsQuery): Promise<CorrectionAdminView[]> {
|
|
||||||
try {
|
|
||||||
const consumedFilter =
|
|
||||||
query.consumed === "true"
|
|
||||||
? { consumedAt: { not: null } }
|
|
||||||
: query.consumed === "false"
|
|
||||||
? { consumedAt: null }
|
|
||||||
: {};
|
|
||||||
const correctedFilter =
|
|
||||||
query.hasCorrectedTechStep === "true"
|
|
||||||
? { correctedTechStepId: { not: null } }
|
|
||||||
: query.hasCorrectedTechStep === "false"
|
|
||||||
? { correctedTechStepId: null }
|
|
||||||
: {};
|
|
||||||
|
|
||||||
const rows = await prisma.stepTechStepCorrection.findMany({
|
|
||||||
where: { ...consumedFilter, ...correctedFilter },
|
|
||||||
orderBy: { createdAt: "desc" },
|
|
||||||
take: CORRECTIONS_PAGE_SIZE,
|
|
||||||
include: {
|
|
||||||
step: { select: { id: true, recipeId: true, description: true } },
|
|
||||||
previousTechStep: { select: { key: true } },
|
|
||||||
correctedTechStep: { select: { key: true } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return rows.map((row) => ({
|
|
||||||
id: row.id,
|
|
||||||
recipeId: row.step.recipeId,
|
|
||||||
stepId: row.step.id,
|
|
||||||
stepDescription: row.step.description,
|
|
||||||
clauseText: row.step.description.slice(row.start, row.end),
|
|
||||||
start: row.start,
|
|
||||||
end: row.end,
|
|
||||||
previousTechStepKey: row.previousTechStep?.key ?? null,
|
|
||||||
correctedTechStepKey: row.correctedTechStep?.key ?? null,
|
|
||||||
createdAt: row.createdAt.toISOString(),
|
|
||||||
consumedAt: row.consumedAt?.toISOString() ?? null,
|
|
||||||
}));
|
|
||||||
} catch (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Curates one suggestion — edit its proposed synonyms/utterances and/or
|
|
||||||
* flip its `status`. At least one field must be present.
|
|
||||||
*
|
|
||||||
* @throws {HttpError} `400 VALIDATION_ERROR` if the body is empty.
|
|
||||||
* @throws {HttpError} `404 NOT_FOUND` if `id` matches no suggestion.
|
|
||||||
*/
|
|
||||||
export async function updateSuggestion(
|
|
||||||
id: number,
|
|
||||||
input: UpdateTrainingSuggestionInput,
|
|
||||||
): Promise<TrainingSuggestionAdminView> {
|
|
||||||
try {
|
|
||||||
if (
|
|
||||||
input.status === undefined &&
|
|
||||||
input.suggestedSynonyms === undefined &&
|
|
||||||
input.suggestedUtterances === undefined
|
|
||||||
) {
|
|
||||||
throw new HttpError(400, ErrorCode.VALIDATION_ERROR, "Nothing to update");
|
|
||||||
}
|
|
||||||
|
|
||||||
const existing = await prisma.techStepTrainingSuggestion.findUnique({ where: { id } });
|
|
||||||
if (!existing) {
|
|
||||||
throw new HttpError(404, ErrorCode.NOT_FOUND, `Training suggestion ${id} not found`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const updated = await prisma.techStepTrainingSuggestion.update({
|
|
||||||
where: { id },
|
|
||||||
data: {
|
|
||||||
...(input.status !== undefined ? { status: input.status } : {}),
|
|
||||||
...(input.suggestedSynonyms !== undefined
|
|
||||||
? { suggestedSynonyms: input.suggestedSynonyms }
|
|
||||||
: {}),
|
|
||||||
...(input.suggestedUtterances !== undefined
|
|
||||||
? { suggestedUtterances: input.suggestedUtterances }
|
|
||||||
: {}),
|
|
||||||
},
|
|
||||||
include: suggestionInclude,
|
|
||||||
});
|
|
||||||
return toSuggestionView(updated);
|
|
||||||
} catch (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Deduplicates while preserving first-seen order — for pooling synonyms/utterances across suggestions. */
|
|
||||||
function dedupe(values: string[]): string[] {
|
|
||||||
return [...new Set(values.map((value) => value.trim()).filter((value) => value.length > 0))];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Indents each entry as a Python list literal line (4-space, trailing comma) — the shape `training_data.py`'s blocks use. */
|
|
||||||
function pythonListBody(entries: string[]): string {
|
|
||||||
return entries.map((entry) => ` ${JSON.stringify(entry)},`).join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aggregates the synonyms/utterances of every suggestion matching
|
|
||||||
* `techStepKey` + `locale` + `status` into a ready-to-paste
|
|
||||||
* `training_data.py` block. **Read-only** — editing that Python file and
|
|
||||||
* restarting the intent-service stay a manual maintainer step.
|
|
||||||
*/
|
|
||||||
export async function getTrainingDataSnippet(
|
|
||||||
query: TrainingDataSnippetQuery,
|
|
||||||
): Promise<TrainingDataSnippetView> {
|
|
||||||
try {
|
|
||||||
const rows = await prisma.techStepTrainingSuggestion.findMany({
|
|
||||||
where: {
|
|
||||||
locale: query.locale,
|
|
||||||
status: query.status,
|
|
||||||
techStep: { key: query.techStepKey },
|
|
||||||
},
|
|
||||||
select: { suggestedSynonyms: true, suggestedUtterances: true },
|
|
||||||
});
|
|
||||||
|
|
||||||
const synonyms = dedupe(rows.flatMap((row) => row.suggestedSynonyms));
|
|
||||||
const utterances = dedupe(rows.flatMap((row) => row.suggestedUtterances));
|
|
||||||
|
|
||||||
const snippet = [
|
|
||||||
`# ${query.techStepKey} (${query.locale}) — ${rows.length} suggestion(s) "${query.status}"`,
|
|
||||||
`"synonyms": [`,
|
|
||||||
pythonListBody(synonyms),
|
|
||||||
`],`,
|
|
||||||
`"utterances": [`,
|
|
||||||
pythonListBody(utterances),
|
|
||||||
`],`,
|
|
||||||
]
|
|
||||||
.filter((line) => line.length > 0)
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
return {
|
|
||||||
techStepKey: query.techStepKey,
|
|
||||||
locale: query.locale,
|
|
||||||
status: query.status,
|
|
||||||
suggestionCount: rows.length,
|
|
||||||
synonyms,
|
|
||||||
utterances,
|
|
||||||
snippet,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Process-wide lock — the F1 gate + full backfill is heavy and must never run twice concurrently. */
|
|
||||||
let retrainInProgress = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the training-corpus regression gate then, if it passes, backfills
|
|
||||||
* every step and marks the given suggestion ids — the same three steps as
|
|
||||||
* `scripts/retrain-tech-steps.ts`, callable from the admin UI.
|
|
||||||
*
|
|
||||||
* **Only meaningful after** a maintainer has hand-edited
|
|
||||||
* `services/tech-step-intent-service/intent_service/training_data.py` **and
|
|
||||||
* restarted that service** (it trains once at boot) — this endpoint can do
|
|
||||||
* neither, and the admin UI states that prominently.
|
|
||||||
*
|
|
||||||
* A failed gate returns `gatePassed: false` with no backfill / no marking
|
|
||||||
* (HTTP 200 — it's an expected outcome to show the operator, not an error).
|
|
||||||
*
|
|
||||||
* @throws {HttpError} `409 RETRAIN_ALREADY_RUNNING` if a retrain is already in flight.
|
|
||||||
*/
|
|
||||||
export async function runRetrain(input: RetrainRequestInput): Promise<RetrainResultView> {
|
|
||||||
if (retrainInProgress) {
|
|
||||||
throw new HttpError(
|
|
||||||
409,
|
|
||||||
ErrorCode.RETRAIN_ALREADY_RUNNING,
|
|
||||||
"A retrain (F1 gate + backfill) is already running",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
retrainInProgress = true;
|
|
||||||
try {
|
|
||||||
const { overall } = await runTechStepEvalSuite();
|
|
||||||
const gatePassed = overall.f1 >= MIN_OVERALL_F1;
|
|
||||||
|
|
||||||
let backfilled: RetrainResultView["backfilled"] = null;
|
|
||||||
const marked = { applied: 0, rejected: 0 };
|
|
||||||
|
|
||||||
if (gatePassed) {
|
|
||||||
backfilled = await backfillTechSteps();
|
|
||||||
|
|
||||||
const appliedIds = input.appliedIds ?? [];
|
|
||||||
const rejectedIds = input.rejectedIds ?? [];
|
|
||||||
if (appliedIds.length > 0) {
|
|
||||||
const { count } = await prisma.techStepTrainingSuggestion.updateMany({
|
|
||||||
where: { id: { in: appliedIds } },
|
|
||||||
data: { status: "applied" },
|
|
||||||
});
|
|
||||||
marked.applied = count;
|
|
||||||
}
|
|
||||||
if (rejectedIds.length > 0) {
|
|
||||||
const { count } = await prisma.techStepTrainingSuggestion.updateMany({
|
|
||||||
where: { id: { in: rejectedIds } },
|
|
||||||
data: { status: "rejected" },
|
|
||||||
});
|
|
||||||
marked.rejected = count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
f1: overall.f1,
|
|
||||||
precision: overall.precision,
|
|
||||||
recall: overall.recall,
|
|
||||||
minF1: MIN_OVERALL_F1,
|
|
||||||
gatePassed,
|
|
||||||
backfilled,
|
|
||||||
marked,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
} finally {
|
|
||||||
retrainInProgress = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +1,13 @@
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { adminAuthRouter } from "./admin-auth.routes.js";
|
import { adminAuthRouter } from "./admin-auth.routes.js";
|
||||||
import { adminCatalogRouter } from "./admin-catalog.routes.js";
|
|
||||||
import { adminMetricsRouter } from "./admin-metrics.routes.js";
|
|
||||||
import { adminMonitoringRouter } from "./admin-monitoring.routes.js";
|
|
||||||
import { adminTechStepsRouter } from "./admin-tech-steps.routes.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aggregator for the admin application's API surface, mounted at `/admin`
|
* Aggregator for the admin application's API surface, mounted at `/admin`
|
||||||
* in `app.ts`. Every sub-router here backs `apps/web`'s `/admin/*` routes
|
* in `app.ts`. Every sub-router here is for `apps/admin-web` only —
|
||||||
* only — `/admin/auth` is public (login), everything added later
|
* `/admin/auth` is public (login), everything added later
|
||||||
* (`/admin/metrics`, `/admin/monitoring`, `/admin/tech-steps/*`,
|
* (`/admin/metrics`, `/admin/monitoring`, `/admin/tech-steps/*`) sits
|
||||||
* `/admin/catalog/*`) sits behind `requireAdmin`
|
* behind `requireAdmin` (`middlewares/require-admin.ts`).
|
||||||
* (`middlewares/require-admin.ts`).
|
|
||||||
*/
|
*/
|
||||||
export const adminRouter = Router();
|
export const adminRouter = Router();
|
||||||
|
|
||||||
adminRouter.use("/auth", adminAuthRouter);
|
adminRouter.use("/auth", adminAuthRouter);
|
||||||
adminRouter.use("/metrics", adminMetricsRouter);
|
|
||||||
adminRouter.use("/monitoring", adminMonitoringRouter);
|
|
||||||
adminRouter.use("/tech-steps", adminTechStepsRouter);
|
|
||||||
adminRouter.use("/catalog", adminCatalogRouter);
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import {
|
||||||
import argon2 from "argon2";
|
import argon2 from "argon2";
|
||||||
import { env } from "../../config/env.js";
|
import { env } from "../../config/env.js";
|
||||||
import { prisma } from "../../db/prisma.js";
|
import { prisma } from "../../db/prisma.js";
|
||||||
import { analytics } from "../../lib/analytics.service.js";
|
|
||||||
import { signAuthToken } from "../../lib/jwt.js";
|
import { signAuthToken } from "../../lib/jwt.js";
|
||||||
import { toSafeProfile } from "../../lib/safe-profile.js";
|
import { toSafeProfile } from "../../lib/safe-profile.js";
|
||||||
import { leaveCurrentHouse } from "../house/house.service.js";
|
import { leaveCurrentHouse } from "../house/house.service.js";
|
||||||
|
|
@ -59,8 +58,6 @@ export async function signup(input: SignupInput): Promise<AuthResult> {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
analytics.recordEvent("user.signup", { actorId: profile.id });
|
|
||||||
|
|
||||||
const token = signAuthToken({
|
const token = signAuthToken({
|
||||||
userProfileId: profile.id,
|
userProfileId: profile.id,
|
||||||
tokenVersion: profile.tokenVersion,
|
tokenVersion: profile.tokenVersion,
|
||||||
|
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
import { parseDateOnly } from "@batch-cooking/date-tools";
|
|
||||||
import { HttpError } from "@batch-cooking/error-tools";
|
|
||||||
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
|
||||||
import { ErrorCode, getCookingSessionSchema } from "@batch-cooking/shared";
|
|
||||||
import { Router } from "express";
|
|
||||||
import { type AuthLocals, requireAuth } from "../../middlewares/require-auth.js";
|
|
||||||
import { getCookingPlanForDate } from "./cooking-session.service.js";
|
|
||||||
|
|
||||||
/** Router mounted at `/cooking-session` in app.ts. */
|
|
||||||
export const cookingSessionRouter = Router();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the authenticated user's household's optimized cooking plan for
|
|
||||||
* the week covering `?date=` (`YYYY-MM-DD`) — every recipe planned that
|
|
||||||
* week reorganized into ordered phases (see {@link getCookingPlanForDate}).
|
|
||||||
* Always `200`, never `null` — no household or nothing planned that week
|
|
||||||
* both come back as a normal `OptimizedCookingPlanView` with empty
|
|
||||||
* `recipes`/`phases`. Same request contract as `GET /shopping-list`.
|
|
||||||
*/
|
|
||||||
cookingSessionRouter.get(
|
|
||||||
"/",
|
|
||||||
requireAuth,
|
|
||||||
wrapAsyncHandler<unknown, AuthLocals>(async (req, res) => {
|
|
||||||
const input = getCookingSessionSchema.parse(req.query);
|
|
||||||
const date = parseDateOnly(input.date);
|
|
||||||
if (date === null) {
|
|
||||||
throw new HttpError(
|
|
||||||
400,
|
|
||||||
ErrorCode.VALIDATION_ERROR,
|
|
||||||
`Not a real calendar date: ${input.date}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const plan = await getCookingPlanForDate(res.locals.userProfile.houseId, date);
|
|
||||||
res.status(200).json(plan);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
@ -1,168 +0,0 @@
|
||||||
import { type DateTime, getWeekStart, toDateOnly } from "@batch-cooking/date-tools";
|
|
||||||
import type { CookingTaskIngredientView, OptimizedCookingPlanView } from "@batch-cooking/shared";
|
|
||||||
import type { Prisma } from "@prisma/client";
|
|
||||||
import { prisma } from "../../db/prisma.js";
|
|
||||||
import {
|
|
||||||
type OptimizerRecipeInput,
|
|
||||||
type OptimizerStepInput,
|
|
||||||
optimizeCookingPlan,
|
|
||||||
} from "../../lib/recipe-matching/cooking-optimizer.js";
|
|
||||||
import { toIngredientView, toUnitView } from "../recipe/recipe.service.js";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prisma `include` for a `Planning` query that needs, for every item, its
|
|
||||||
* recipe's ordered steps with the full detected-technique tree — the raw
|
|
||||||
* material the optimizer works on (see `cooking-optimizer.ts`). It's the
|
|
||||||
* `steps` sub-tree of `recipe.service.ts`'s own `recipeInclude`, resolved
|
|
||||||
* the same way so {@link toIngredientView}/{@link toUnitView} can be reused
|
|
||||||
* as-is; deliberately narrower than a full `RecipeView` fetch (no
|
|
||||||
* diets/favorites/recipe-level ingredient list — the optimizer reads
|
|
||||||
* quantities off the technique clauses, not the recipe header).
|
|
||||||
*/
|
|
||||||
function cookingSessionPlanningInclude() {
|
|
||||||
return {
|
|
||||||
items: {
|
|
||||||
include: {
|
|
||||||
recipe: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
name: true,
|
|
||||||
portions: true,
|
|
||||||
steps: {
|
|
||||||
orderBy: { order: "asc" },
|
|
||||||
include: {
|
|
||||||
techSteps: {
|
|
||||||
orderBy: { order: "asc" },
|
|
||||||
include: {
|
|
||||||
techStep: true,
|
|
||||||
ingredients: {
|
|
||||||
include: {
|
|
||||||
ingredient: {
|
|
||||||
include: {
|
|
||||||
allergies: { include: { allergy: { include: { category: true } } } },
|
|
||||||
diets: { include: { diet: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
unit: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
utensils: { include: { utensil: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} satisfies Prisma.PlanningInclude;
|
|
||||||
}
|
|
||||||
|
|
||||||
type PlanningWithSteps = Prisma.PlanningGetPayload<{
|
|
||||||
include: ReturnType<typeof cookingSessionPlanningInclude>;
|
|
||||||
}>;
|
|
||||||
type PlanningItemWithSteps = PlanningWithSteps["items"][number];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Maps one planning item's recipe (with {@link cookingSessionPlanningInclude})
|
|
||||||
* to the optimizer's pure input shape — ingredient/unit/technique/utensil
|
|
||||||
* rows resolved to their reference views here so the optimizer itself never
|
|
||||||
* touches Prisma. `Decimal` quantities become plain numbers (same
|
|
||||||
* `Number(...)` conversion as `recipe.service.ts`'s own view mappers); an
|
|
||||||
* unresolved-unit line keeps `unit: null`.
|
|
||||||
*/
|
|
||||||
function toOptimizerRecipe(item: PlanningItemWithSteps): OptimizerRecipeInput {
|
|
||||||
const steps: OptimizerStepInput[] = item.recipe.steps.map((step) => ({
|
|
||||||
stepId: step.id,
|
|
||||||
order: step.order,
|
|
||||||
description: step.description,
|
|
||||||
techSteps: step.techSteps.map((techStep) => {
|
|
||||||
const ingredients: CookingTaskIngredientView[] = techStep.ingredients.map((line) => ({
|
|
||||||
ingredient: toIngredientView(line.ingredient),
|
|
||||||
quantity: line.quantity === null ? null : Number(line.quantity),
|
|
||||||
unit: line.unit === null ? null : toUnitView(line.unit),
|
|
||||||
}));
|
|
||||||
return {
|
|
||||||
techStep: { id: techStep.techStep.id, key: techStep.techStep.key },
|
|
||||||
order: techStep.order,
|
|
||||||
ingredients,
|
|
||||||
utensils: techStep.utensils.map(({ utensil }) => ({ id: utensil.id, key: utensil.key })),
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
return {
|
|
||||||
recipeId: item.recipe.id,
|
|
||||||
name: item.recipe.name,
|
|
||||||
// The slot's own portion count vs. the recipe's as-written yield — the
|
|
||||||
// optimizer scales technique-clause quantities by the ratio, same
|
|
||||||
// reasoning as `shopping-list.service.ts`'s `aggregateShoppingList`.
|
|
||||||
portions: item.portions,
|
|
||||||
recipePortions: item.recipe.portions,
|
|
||||||
steps,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Builds the household's optimized cooking plan for the week covering
|
|
||||||
* `date` — every recipe planned that week, reorganized into ordered phases
|
|
||||||
* that pool shared prep and float passive cooks into the background (see
|
|
||||||
* `cooking-optimizer.ts`). `date` follows the same convention as
|
|
||||||
* `planning.service.ts`'s `getPlanningForDate` (a caller-parsed `?date=`,
|
|
||||||
* not necessarily a Monday).
|
|
||||||
*
|
|
||||||
* Like `getShoppingListForDate` and unlike `getPlanningForDate`, this
|
|
||||||
* **never** returns `null` — no household and "no planning covers this week
|
|
||||||
* yet" both degrade to an empty `phases`/`recipes` on an otherwise normal
|
|
||||||
* {@link OptimizedCookingPlanView} (the week's date range is always
|
|
||||||
* computable from `date` alone).
|
|
||||||
*/
|
|
||||||
export async function getCookingPlanForDate(
|
|
||||||
houseId: number | null,
|
|
||||||
date: DateTime,
|
|
||||||
): Promise<OptimizedCookingPlanView> {
|
|
||||||
try {
|
|
||||||
const weekStart = getWeekStart(toDateOnly(date));
|
|
||||||
const weekFinish = weekStart.plus({ days: 6 });
|
|
||||||
const emptyPlan: OptimizedCookingPlanView = {
|
|
||||||
startDate: weekStart.toJSDate().toISOString(),
|
|
||||||
finishDate: weekFinish.toJSDate().toISOString(),
|
|
||||||
recipes: [],
|
|
||||||
phases: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
if (houseId === null) {
|
|
||||||
return emptyPlan;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Same "covering range" lookup as getShoppingListForDate — see
|
|
||||||
// getPlanningForDate's doc comment for the UTC-midnight `Date` rationale.
|
|
||||||
const dateOnly = toDateOnly(date).toJSDate();
|
|
||||||
const planning = await prisma.planning.findFirst({
|
|
||||||
where: {
|
|
||||||
houseId,
|
|
||||||
startDate: { lte: dateOnly },
|
|
||||||
finishDate: { gte: dateOnly },
|
|
||||||
},
|
|
||||||
orderBy: { startDate: "desc" },
|
|
||||||
include: cookingSessionPlanningInclude(),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!planning) {
|
|
||||||
return emptyPlan;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { recipes, phases } = optimizeCookingPlan(planning.items.map(toOptimizerRecipe));
|
|
||||||
return {
|
|
||||||
startDate: planning.startDate.toISOString(),
|
|
||||||
finishDate: planning.finishDate.toISOString(),
|
|
||||||
recipes,
|
|
||||||
phases,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
// Rethrown as-is — `wrapAsyncHandler`/the error middleware handles it,
|
|
||||||
// this service layer just isn't allowed a bare `await` per the repo's
|
|
||||||
// async/try-catch convention.
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -3,14 +3,12 @@ import {
|
||||||
auditBatchQuerySchema,
|
auditBatchQuerySchema,
|
||||||
submitTrainingSuggestionsSchema,
|
submitTrainingSuggestionsSchema,
|
||||||
workerBatchQuerySchema,
|
workerBatchQuerySchema,
|
||||||
workerHeartbeatSchema,
|
|
||||||
} from "@batch-cooking/shared";
|
} from "@batch-cooking/shared";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { requireInternalWorker } from "../../middlewares/require-internal-worker.js";
|
import { requireInternalWorker } from "../../middlewares/require-internal-worker.js";
|
||||||
import {
|
import {
|
||||||
getAuditBatch,
|
getAuditBatch,
|
||||||
getPendingCorrections,
|
getPendingCorrections,
|
||||||
recordWorkerHeartbeat,
|
|
||||||
submitTrainingSuggestions,
|
submitTrainingSuggestions,
|
||||||
} from "./tech-step-worker.service.js";
|
} from "./tech-step-worker.service.js";
|
||||||
|
|
||||||
|
|
@ -49,19 +47,3 @@ techStepWorkerRouter.post(
|
||||||
res.status(201).json(await submitTrainingSuggestions(input));
|
res.status(201).json(await submitTrainingSuggestions(input));
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* Liveness ping from the worker (which has no inbound HTTP surface of its
|
|
||||||
* own) — upserts its `WorkerHeartbeat` row so the admin monitoring board
|
|
||||||
* can show it as up / stale / down and surface its last job result. Sent
|
|
||||||
* on boot, on every scheduler tick, and after each job.
|
|
||||||
*/
|
|
||||||
techStepWorkerRouter.post(
|
|
||||||
"/heartbeat",
|
|
||||||
requireInternalWorker,
|
|
||||||
wrapAsyncHandler(async (req, res) => {
|
|
||||||
const input = workerHeartbeatSchema.parse(req.body);
|
|
||||||
await recordWorkerHeartbeat(input);
|
|
||||||
res.status(200).json({ ok: true });
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,7 @@ import {
|
||||||
type PendingTechStepCorrectionView,
|
type PendingTechStepCorrectionView,
|
||||||
type SubmitTrainingSuggestionsInput,
|
type SubmitTrainingSuggestionsInput,
|
||||||
type TechStepAuditClauseView,
|
type TechStepAuditClauseView,
|
||||||
type WorkerHeartbeatInput,
|
|
||||||
} from "@batch-cooking/shared";
|
} from "@batch-cooking/shared";
|
||||||
import type { Prisma } from "@prisma/client";
|
|
||||||
import { prisma } from "../../db/prisma.js";
|
import { prisma } from "../../db/prisma.js";
|
||||||
import {
|
import {
|
||||||
CONFIDENCE_THRESHOLD,
|
CONFIDENCE_THRESHOLD,
|
||||||
|
|
@ -201,44 +199,3 @@ export async function submitTrainingSuggestions(
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
throw err; // see recipe.service.ts's equivalent catch comment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The one worker with a `WorkerHeartbeat` row today — a fixed key, not
|
|
||||||
* something the caller supplies (only one worker exists, and letting it
|
|
||||||
* name itself would just be a spoofing surface behind the same shared
|
|
||||||
* secret).
|
|
||||||
*/
|
|
||||||
const WORKER_KEY = "tech-step-llm-worker";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Upserts `services/tech-step-llm-worker`'s heartbeat row (see
|
|
||||||
* `POST /internal/tech-steps/heartbeat`). Every ping bumps `lastSeenAt`; a
|
|
||||||
* `"job"` ping also records `lastRunAt` + a small `lastResult` summary so
|
|
||||||
* the admin monitoring board can show what the worker last did and whether
|
|
||||||
* it worked.
|
|
||||||
*/
|
|
||||||
export async function recordWorkerHeartbeat(input: WorkerHeartbeatInput): Promise<void> {
|
|
||||||
try {
|
|
||||||
const now = new Date();
|
|
||||||
const jobResult: Prisma.InputJsonValue | undefined =
|
|
||||||
input.event === "job"
|
|
||||||
? { job: input.job ?? null, ok: input.ok ?? null, counts: input.counts ?? {} }
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
await prisma.workerHeartbeat.upsert({
|
|
||||||
where: { workerKey: WORKER_KEY },
|
|
||||||
create: {
|
|
||||||
workerKey: WORKER_KEY,
|
|
||||||
lastSeenAt: now,
|
|
||||||
lastRunAt: input.event === "job" ? now : null,
|
|
||||||
lastResult: jobResult,
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
lastSeenAt: now,
|
|
||||||
...(input.event === "job" ? { lastRunAt: now, lastResult: jobResult } : {}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see recipe.service.ts's equivalent catch comment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import {
|
||||||
type PlanningView,
|
type PlanningView,
|
||||||
} from "@batch-cooking/shared";
|
} from "@batch-cooking/shared";
|
||||||
import { prisma } from "../../db/prisma.js";
|
import { prisma } from "../../db/prisma.js";
|
||||||
import { analytics } from "../../lib/analytics.service.js";
|
|
||||||
import { assertRecipeVisible } from "../recipe/recipe.service.js";
|
import { assertRecipeVisible } from "../recipe/recipe.service.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -158,11 +157,6 @@ export async function addPlanningItem(
|
||||||
include: { recipe: { select: { id: true, name: true } } },
|
include: { recipe: { select: { id: true, name: true } } },
|
||||||
});
|
});
|
||||||
|
|
||||||
analytics.recordEvent("planning.item_added", {
|
|
||||||
actorId: viewerId,
|
|
||||||
context: { recipeId: input.recipeId, portions: input.portions },
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
weekDay: item.weekDay,
|
weekDay: item.weekDay,
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import {
|
||||||
} from "@batch-cooking/shared";
|
} from "@batch-cooking/shared";
|
||||||
import type { Prisma } from "@prisma/client";
|
import type { Prisma } from "@prisma/client";
|
||||||
import { prisma } from "../../db/prisma.js";
|
import { prisma } from "../../db/prisma.js";
|
||||||
import { analytics } from "../../lib/analytics.service.js";
|
|
||||||
import { assertRecipeVisible, toStepTechStepViews } from "./recipe.service.js";
|
import { assertRecipeVisible, toStepTechStepViews } from "./recipe.service.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -473,16 +472,6 @@ export async function submitTechStepCorrection(
|
||||||
return { correction: createdCorrection, techSteps: freshTechSteps };
|
return { correction: createdCorrection, techSteps: freshTechSteps };
|
||||||
});
|
});
|
||||||
|
|
||||||
analytics.recordEvent("tech_step.correction_submitted", {
|
|
||||||
actorId: correctorId,
|
|
||||||
context: {
|
|
||||||
recipeId,
|
|
||||||
stepId,
|
|
||||||
previousTechStepId: input.previousTechStepId ?? null,
|
|
||||||
correctedTechStepId: input.correctedTechStepId ?? null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return { correction: toCorrectionView(correction), techSteps: toStepTechStepViews(techSteps) };
|
return { correction: toCorrectionView(correction), techSteps: toStepTechStepViews(techSteps) };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err; // see loadVisibleStepOrThrow's catch comment
|
throw err; // see loadVisibleStepOrThrow's catch comment
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
import { HttpError } from "@batch-cooking/error-tools";
|
import { HttpError } from "@batch-cooking/error-tools";
|
||||||
import {
|
import {
|
||||||
type AllergyView,
|
type AllergyView,
|
||||||
|
|
@ -15,7 +14,6 @@ import {
|
||||||
} from "@batch-cooking/shared";
|
} from "@batch-cooking/shared";
|
||||||
import type { Prisma } from "@prisma/client";
|
import type { Prisma } from "@prisma/client";
|
||||||
import { prisma } from "../../db/prisma.js";
|
import { prisma } from "../../db/prisma.js";
|
||||||
import { analytics } from "../../lib/analytics.service.js";
|
|
||||||
import {
|
import {
|
||||||
type TechStepMatch,
|
type TechStepMatch,
|
||||||
techStepClassifier,
|
techStepClassifier,
|
||||||
|
|
@ -110,12 +108,6 @@ export function toIngredientView(ingredient: IngredientWithDetails): IngredientV
|
||||||
kind: allergy.category.kind,
|
kind: allergy.category.kind,
|
||||||
})),
|
})),
|
||||||
diets: ingredient.diets.map(({ diet }) => ({ id: diet.id, key: diet.key })),
|
diets: ingredient.diets.map(({ diet }) => ({ id: diet.id, key: diet.key })),
|
||||||
// A placeholder line round-trips as a normal `Ingredient` with a real
|
|
||||||
// id — the frontend tells it apart by this flag (badge, no
|
|
||||||
// allergen/diet info) and shows `displayName` verbatim instead of
|
|
||||||
// looking up an i18n label that doesn't exist for a `placeholder:` key.
|
|
||||||
isPlaceholder: ingredient.isPlaceholder,
|
|
||||||
displayName: ingredient.displayName,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -549,69 +541,6 @@ async function matchStepsTechSteps<T extends { description: string }>(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** One resolved ingredient line — every `placeholderName` has been turned into a real (placeholder) `ingredientId`, ready for a `RecipeIngredient` create. */
|
|
||||||
interface ResolvedIngredientLine {
|
|
||||||
ingredientId: number;
|
|
||||||
quantity: number;
|
|
||||||
unitId: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Turns each `input.ingredients` line into a {@link ResolvedIngredientLine}:
|
|
||||||
* a line that already carries an `ingredientId` (a catalog pick, or an
|
|
||||||
* existing placeholder round-tripping through an edit) passes through
|
|
||||||
* unchanged; a `placeholderName` line gets a fresh placeholder `Ingredient`
|
|
||||||
* row (`isPlaceholder: true`, a generated `placeholder:<uuid>` key, the
|
|
||||||
* typed text as `displayName`, stamped with `authorId`/now) created via
|
|
||||||
* `tx` — so it rolls back together with the recipe if anything later in the
|
|
||||||
* same transaction fails, never leaving an orphan behind.
|
|
||||||
*
|
|
||||||
* Returns the created placeholders separately so the caller can emit one
|
|
||||||
* `ingredient.placeholder_created` analytics event per row *after* the
|
|
||||||
* transaction commits (the recipe id it wants in the event context doesn't
|
|
||||||
* exist yet in here).
|
|
||||||
*/
|
|
||||||
async function resolveIngredientLines(
|
|
||||||
lines: CreateRecipeInput["ingredients"],
|
|
||||||
authorId: number,
|
|
||||||
tx: Prisma.TransactionClient,
|
|
||||||
): Promise<{
|
|
||||||
resolved: ResolvedIngredientLine[];
|
|
||||||
createdPlaceholders: { id: number; name: string }[];
|
|
||||||
}> {
|
|
||||||
try {
|
|
||||||
const resolved: ResolvedIngredientLine[] = [];
|
|
||||||
const createdPlaceholders: { id: number; name: string }[] = [];
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.ingredientId !== undefined) {
|
|
||||||
resolved.push({
|
|
||||||
ingredientId: line.ingredientId,
|
|
||||||
quantity: line.quantity,
|
|
||||||
unitId: line.unitId,
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// `recipeIngredientInputSchema`'s refine guarantees the other branch.
|
|
||||||
const name = (line.placeholderName ?? "").trim();
|
|
||||||
const placeholder = await tx.ingredient.create({
|
|
||||||
data: {
|
|
||||||
key: `placeholder:${randomUUID()}`,
|
|
||||||
isPlaceholder: true,
|
|
||||||
displayName: name,
|
|
||||||
createdById: authorId,
|
|
||||||
createdAt: new Date(),
|
|
||||||
},
|
|
||||||
select: { id: true },
|
|
||||||
});
|
|
||||||
resolved.push({ ingredientId: placeholder.id, quantity: line.quantity, unitId: line.unitId });
|
|
||||||
createdPlaceholders.push({ id: placeholder.id, name });
|
|
||||||
}
|
|
||||||
return { resolved, createdPlaceholders };
|
|
||||||
} catch (err) {
|
|
||||||
throw err; // see suitableForHouseholdWhere()'s catch comment above
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createRecipeInternal(
|
async function createRecipeInternal(
|
||||||
input: CreateRecipeInput,
|
input: CreateRecipeInput,
|
||||||
authorId: number,
|
authorId: number,
|
||||||
|
|
@ -619,9 +548,7 @@ async function createRecipeInternal(
|
||||||
source: { sourceId: number; externalId: string; locale: string } | null,
|
source: { sourceId: number; externalId: string; locale: string } | null,
|
||||||
): Promise<RecipeView> {
|
): Promise<RecipeView> {
|
||||||
try {
|
try {
|
||||||
await assertIngredientsExist(
|
await assertIngredientsExist(input.ingredients.map((i) => i.ingredientId));
|
||||||
input.ingredients.map((i) => i.ingredientId).filter((id): id is number => id !== undefined),
|
|
||||||
);
|
|
||||||
await assertUnitsExist(input.ingredients.map((i) => i.unitId));
|
await assertUnitsExist(input.ingredients.map((i) => i.unitId));
|
||||||
await assertDietsExist(input.dietIds);
|
await assertDietsExist(input.dietIds);
|
||||||
// Matched up front (one call per step, in parallel) rather than inline
|
// Matched up front (one call per step, in parallel) rather than inline
|
||||||
|
|
@ -634,16 +561,7 @@ async function createRecipeInternal(
|
||||||
source?.locale ?? DEFAULT_TECH_STEP_LOCALE,
|
source?.locale ?? DEFAULT_TECH_STEP_LOCALE,
|
||||||
);
|
);
|
||||||
|
|
||||||
// One transaction so the free-text placeholder `Ingredient` rows and the
|
const created = await prisma.recipe.create({
|
||||||
// recipe that references them commit together — a failed recipe create
|
|
||||||
// must never leave orphan placeholders behind.
|
|
||||||
const { created, createdPlaceholders } = await prisma.$transaction(async (tx) => {
|
|
||||||
const { resolved, createdPlaceholders } = await resolveIngredientLines(
|
|
||||||
input.ingredients,
|
|
||||||
authorId,
|
|
||||||
tx,
|
|
||||||
);
|
|
||||||
const created = await tx.recipe.create({
|
|
||||||
data: {
|
data: {
|
||||||
name: input.name,
|
name: input.name,
|
||||||
description: input.description ?? null,
|
description: input.description ?? null,
|
||||||
|
|
@ -655,10 +573,10 @@ async function createRecipeInternal(
|
||||||
sourceId: source?.sourceId ?? null,
|
sourceId: source?.sourceId ?? null,
|
||||||
externalId: source?.externalId ?? null,
|
externalId: source?.externalId ?? null,
|
||||||
ingredients: {
|
ingredients: {
|
||||||
create: resolved.map((line) => ({
|
create: input.ingredients.map((ingredient) => ({
|
||||||
ingredientId: line.ingredientId,
|
ingredientId: ingredient.ingredientId,
|
||||||
quantity: line.quantity,
|
quantity: ingredient.quantity,
|
||||||
unitId: line.unitId,
|
unitId: ingredient.unitId,
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
steps: {
|
steps: {
|
||||||
|
|
@ -698,20 +616,6 @@ async function createRecipeInternal(
|
||||||
},
|
},
|
||||||
include: recipeInclude(authorId),
|
include: recipeInclude(authorId),
|
||||||
});
|
});
|
||||||
return { created, createdPlaceholders };
|
|
||||||
});
|
|
||||||
|
|
||||||
analytics.recordEvent(source === null ? "recipe.created" : "recipe.imported", {
|
|
||||||
actorId: authorId,
|
|
||||||
context: { recipeId: created.id, sourceId: source?.sourceId ?? null },
|
|
||||||
});
|
|
||||||
for (const placeholder of createdPlaceholders) {
|
|
||||||
analytics.recordEvent("ingredient.placeholder_created", {
|
|
||||||
actorId: authorId,
|
|
||||||
context: { name: placeholder.name, ingredientId: placeholder.id, recipeId: created.id },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return toRecipeView(created);
|
return toRecipeView(created);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err; // see suitableForHouseholdWhere()'s catch comment above
|
throw err; // see suitableForHouseholdWhere()'s catch comment above
|
||||||
|
|
@ -739,30 +643,16 @@ export async function updateRecipe(
|
||||||
): Promise<RecipeView> {
|
): Promise<RecipeView> {
|
||||||
try {
|
try {
|
||||||
await assertIsAuthor(id, viewerId, viewerHouseId);
|
await assertIsAuthor(id, viewerId, viewerHouseId);
|
||||||
await assertIngredientsExist(
|
await assertIngredientsExist(input.ingredients.map((i) => i.ingredientId));
|
||||||
input.ingredients
|
|
||||||
.map((i) => i.ingredientId)
|
|
||||||
.filter((ingredientId): ingredientId is number => ingredientId !== undefined),
|
|
||||||
);
|
|
||||||
await assertUnitsExist(input.ingredients.map((i) => i.unitId));
|
await assertUnitsExist(input.ingredients.map((i) => i.unitId));
|
||||||
await assertDietsExist(input.dietIds);
|
await assertDietsExist(input.dietIds);
|
||||||
const stepsWithTechSteps = await matchStepsTechSteps(input.steps, DEFAULT_TECH_STEP_LOCALE);
|
const stepsWithTechSteps = await matchStepsTechSteps(input.steps, DEFAULT_TECH_STEP_LOCALE);
|
||||||
|
|
||||||
// Interactive transaction (not the array form) so any new free-text
|
await prisma.$transaction([
|
||||||
// placeholder rows are created in the same atomic unit as the
|
prisma.recipeIngredient.deleteMany({ where: { recipeId: id } }),
|
||||||
// delete+recreate of the recipe's content. An *existing* placeholder
|
prisma.step.deleteMany({ where: { recipeId: id } }),
|
||||||
// line round-trips by its real `ingredientId` and is left untouched;
|
prisma.recipeDiet.deleteMany({ where: { recipeId: id } }),
|
||||||
// only a brand-new `placeholderName` line creates a row here.
|
prisma.recipe.update({
|
||||||
const createdPlaceholders = await prisma.$transaction(async (tx) => {
|
|
||||||
await tx.recipeIngredient.deleteMany({ where: { recipeId: id } });
|
|
||||||
await tx.step.deleteMany({ where: { recipeId: id } });
|
|
||||||
await tx.recipeDiet.deleteMany({ where: { recipeId: id } });
|
|
||||||
const { resolved, createdPlaceholders } = await resolveIngredientLines(
|
|
||||||
input.ingredients,
|
|
||||||
viewerId,
|
|
||||||
tx,
|
|
||||||
);
|
|
||||||
await tx.recipe.update({
|
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
name: input.name,
|
name: input.name,
|
||||||
|
|
@ -771,10 +661,10 @@ export async function updateRecipe(
|
||||||
portions: input.portions,
|
portions: input.portions,
|
||||||
visibility: input.visibility,
|
visibility: input.visibility,
|
||||||
ingredients: {
|
ingredients: {
|
||||||
create: resolved.map((line) => ({
|
create: input.ingredients.map((ingredient) => ({
|
||||||
ingredientId: line.ingredientId,
|
ingredientId: ingredient.ingredientId,
|
||||||
quantity: line.quantity,
|
quantity: ingredient.quantity,
|
||||||
unitId: line.unitId,
|
unitId: ingredient.unitId,
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
steps: {
|
steps: {
|
||||||
|
|
@ -796,16 +686,8 @@ export async function updateRecipe(
|
||||||
},
|
},
|
||||||
diets: { create: input.dietIds.map((dietId) => ({ dietId })) },
|
diets: { create: input.dietIds.map((dietId) => ({ dietId })) },
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
return createdPlaceholders;
|
]);
|
||||||
});
|
|
||||||
|
|
||||||
for (const placeholder of createdPlaceholders) {
|
|
||||||
analytics.recordEvent("ingredient.placeholder_created", {
|
|
||||||
actorId: viewerId,
|
|
||||||
context: { name: placeholder.name, ingredientId: placeholder.id, recipeId: id },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return toRecipeView(await findRecipeOrThrow(id, viewerId));
|
return toRecipeView(await findRecipeOrThrow(id, viewerId));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
||||||
|
|
@ -133,16 +133,10 @@ export async function getSources(): Promise<SourceView[]> {
|
||||||
* and compatible diet regimes (see `IngredientDiet`) — same flattening
|
* and compatible diet regimes (see `IngredientDiet`) — same flattening
|
||||||
* approach as {@link getAllergies}. Ingredients with no linked
|
* approach as {@link getAllergies}. Ingredients with no linked
|
||||||
* allergen/diet come back with `allergens: []`/`diets: []`.
|
* allergen/diet come back with `allergens: []`/`diets: []`.
|
||||||
*
|
|
||||||
* Excludes placeholder rows (`Ingredient.isPlaceholder` — the free-text
|
|
||||||
* ingredients users type when the catalog falls short): this is the
|
|
||||||
* *browsable* catalog, and a placeholder is a per-recipe-line stand-in, not
|
|
||||||
* a real entry anyone should be able to pick again.
|
|
||||||
*/
|
*/
|
||||||
export async function getIngredients(): Promise<IngredientView[]> {
|
export async function getIngredients(): Promise<IngredientView[]> {
|
||||||
try {
|
try {
|
||||||
const ingredients = await prisma.ingredient.findMany({
|
const ingredients = await prisma.ingredient.findMany({
|
||||||
where: { isPlaceholder: false },
|
|
||||||
include: {
|
include: {
|
||||||
allergies: { include: { allergy: { include: { category: true } } } },
|
allergies: { include: { allergy: { include: { category: true } } } },
|
||||||
diets: { include: { diet: true } },
|
diets: { include: { diet: true } },
|
||||||
|
|
@ -165,9 +159,6 @@ export async function getIngredients(): Promise<IngredientView[]> {
|
||||||
id: diet.id,
|
id: diet.id,
|
||||||
key: diet.key,
|
key: diet.key,
|
||||||
})),
|
})),
|
||||||
// Always a real catalog row here (placeholders are filtered out above).
|
|
||||||
isPlaceholder: false,
|
|
||||||
displayName: null,
|
|
||||||
}));
|
}));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err; // see getDiets()'s catch comment above
|
throw err; // see getDiets()'s catch comment above
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ import { HttpError } from "@batch-cooking/error-tools";
|
||||||
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
import { wrapAsyncHandler } from "@batch-cooking/express-tools";
|
||||||
import { ErrorCode, getShoppingListSchema } from "@batch-cooking/shared";
|
import { ErrorCode, getShoppingListSchema } from "@batch-cooking/shared";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { analytics } from "../../lib/analytics.service.js";
|
|
||||||
import { type AuthLocals, requireAuth } from "../../middlewares/require-auth.js";
|
import { type AuthLocals, requireAuth } from "../../middlewares/require-auth.js";
|
||||||
import { getShoppingListForDate } from "./shopping-list.service.js";
|
import { getShoppingListForDate } from "./shopping-list.service.js";
|
||||||
|
|
||||||
|
|
@ -32,7 +31,6 @@ shoppingListRouter.get(
|
||||||
}
|
}
|
||||||
|
|
||||||
const shoppingList = await getShoppingListForDate(res.locals.userProfile.houseId, date);
|
const shoppingList = await getShoppingListForDate(res.locals.userProfile.houseId, date);
|
||||||
analytics.recordEvent("shopping_list.viewed", { actorId: res.locals.userProfile.id });
|
|
||||||
res.status(200).json(shoppingList);
|
res.status(200).json(shoppingList);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
import { prisma } from "../db/prisma.js";
|
|
||||||
import { pruneOrphanPlaceholders } from "../modules/admin/admin-catalog.service.js";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes every placeholder `Ingredient` row (`Ingredient.isPlaceholder`)
|
|
||||||
* that no recipe references any more — the debris left behind when a recipe
|
|
||||||
* edit drops a placeholder line (the `RecipeIngredient` row goes, the
|
|
||||||
* `Ingredient` row stays). The admin catalog view has a button for this
|
|
||||||
* too; this script is the same operation for a cron / one-off cleanup:
|
|
||||||
*
|
|
||||||
* pnpm --filter api exec tsx src/scripts/prune-orphan-placeholders.ts
|
|
||||||
*
|
|
||||||
* Delegates to `admin-catalog.service.ts` so the "what counts as an orphan"
|
|
||||||
* rule lives in exactly one place.
|
|
||||||
*/
|
|
||||||
async function main(): Promise<void> {
|
|
||||||
const { deleted } = await pruneOrphanPlaceholders();
|
|
||||||
console.info(`Pruned ${deleted} orphan placeholder ingredient(s).`);
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
.then(() => prisma.$disconnect())
|
|
||||||
.catch(async (err) => {
|
|
||||||
console.error(err);
|
|
||||||
await prisma.$disconnect();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
|
|
@ -48,7 +48,7 @@ export async function resetDatabase() {
|
||||||
"recipe_ingredient", "step_tech_step", "step", "tech_step",
|
"recipe_ingredient", "step_tech_step", "step", "tech_step",
|
||||||
"recipe", "ingredients", "sources", "unit",
|
"recipe", "ingredients", "sources", "unit",
|
||||||
"user_profiles", "diet", "house",
|
"user_profiles", "diet", "house",
|
||||||
"admin_users", "analytics_events", "worker_heartbeats"
|
"admin_users"
|
||||||
RESTART IDENTITY CASCADE;
|
RESTART IDENTITY CASCADE;
|
||||||
`);
|
`);
|
||||||
await seedReferenceData(prisma);
|
await seedReferenceData(prisma);
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
import { expect } from "chai";
|
|
||||||
import { normalizePlaceholderName } from "../src/modules/admin/admin-catalog.service.js";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pure unit tests for {@link normalizePlaceholderName} — the grouping key
|
|
||||||
* that collapses near-duplicate placeholder spellings into one catalog gap.
|
|
||||||
* No database, so this file can run standalone (`mocha --no-config`) as
|
|
||||||
* well as inside the full suite.
|
|
||||||
*/
|
|
||||||
describe("normalizePlaceholderName", () => {
|
|
||||||
it("lower-cases, strips accents and collapses whitespace", () => {
|
|
||||||
expect(normalizePlaceholderName(" Piment d'Espelette ")).to.equal("piment d espelette");
|
|
||||||
expect(normalizePlaceholderName("PIMENT D’ESPELETTE")).to.equal("piment d espelette");
|
|
||||||
expect(normalizePlaceholderName("piment d espelette")).to.equal("piment d espelette");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("neutralises punctuation to a single space", () => {
|
|
||||||
expect(normalizePlaceholderName("sel & poivre")).to.equal("sel poivre");
|
|
||||||
expect(normalizePlaceholderName("sel, poivre")).to.equal("sel poivre");
|
|
||||||
expect(normalizePlaceholderName("fleur-de-sel")).to.equal("fleur de sel");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("keeps digits (a quantity baked into the name still distinguishes it)", () => {
|
|
||||||
expect(normalizePlaceholderName("Chocolat 70%")).to.equal("chocolat 70");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("maps an all-punctuation / empty string to an empty key", () => {
|
|
||||||
expect(normalizePlaceholderName("")).to.equal("");
|
|
||||||
expect(normalizePlaceholderName(" ")).to.equal("");
|
|
||||||
expect(normalizePlaceholderName("--- ///")).to.equal("");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,141 +0,0 @@
|
||||||
import { faker } from "@faker-js/faker";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import request from "supertest";
|
|
||||||
import { createApp } from "../src/app.js";
|
|
||||||
import { prisma } from "../src/db/prisma.js";
|
|
||||||
import { hashAdminPassword } from "../src/modules/admin/admin-auth.service.js";
|
|
||||||
import { resetDatabase } from "../test-support/reset-db.js";
|
|
||||||
|
|
||||||
async function seedAdmin(): Promise<{ email: string; password: string }> {
|
|
||||||
const email = faker.internet.email().toLowerCase();
|
|
||||||
const password = faker.internet.password({ length: 16 });
|
|
||||||
await prisma.adminUser.create({
|
|
||||||
data: { email, name: faker.person.fullName(), passwordHash: await hashAdminPassword(password) },
|
|
||||||
});
|
|
||||||
return { email, password };
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A recipe with one placeholder ingredient line whose `displayName` is `name`. Returns the placeholder `Ingredient` id. */
|
|
||||||
async function seedPlaceholderRecipe(name: string): Promise<number> {
|
|
||||||
const author = await prisma.userProfile.create({
|
|
||||||
data: {
|
|
||||||
firstName: "T",
|
|
||||||
lastName: "A",
|
|
||||||
email: `${faker.string.uuid()}@example.test`,
|
|
||||||
passwordHash: "x",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const unit = await prisma.unit.findFirstOrThrow({ where: { key: "piece" } });
|
|
||||||
const placeholder = await prisma.ingredient.create({
|
|
||||||
data: {
|
|
||||||
key: `placeholder:${faker.string.uuid()}`,
|
|
||||||
isPlaceholder: true,
|
|
||||||
displayName: name,
|
|
||||||
createdById: author.id,
|
|
||||||
createdAt: new Date(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
await prisma.recipe.create({
|
|
||||||
data: {
|
|
||||||
name: faker.lorem.words(3),
|
|
||||||
authorId: author.id,
|
|
||||||
portions: 2,
|
|
||||||
ingredients: { create: [{ ingredientId: placeholder.id, quantity: 1, unitId: unit.id }] },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return placeholder.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* `/admin/catalog/*` — the off-catalog ingredient review. Every route is
|
|
||||||
* behind `requireAdmin`; the list groups placeholder rows by normalized
|
|
||||||
* name, `mark-reviewed` stamps `reviewedAt`, `prune-orphans` deletes rows
|
|
||||||
* no recipe references any more.
|
|
||||||
*/
|
|
||||||
describe("Admin catalog — off-catalog ingredients", () => {
|
|
||||||
const app = createApp();
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await resetDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
async function adminAgent() {
|
|
||||||
const { email, password } = await seedAdmin();
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/admin/auth/login").send({ email, password });
|
|
||||||
return agent;
|
|
||||||
}
|
|
||||||
|
|
||||||
it("rejects every route without an admin session", async () => {
|
|
||||||
const get = await request(app).get("/admin/catalog/placeholders");
|
|
||||||
expect(get.status).to.equal(401);
|
|
||||||
const patch = await request(app)
|
|
||||||
.patch("/admin/catalog/placeholders/mark-reviewed")
|
|
||||||
.send({ ingredientIds: [1] });
|
|
||||||
expect(patch.status).to.equal(401);
|
|
||||||
const post = await request(app).post("/admin/catalog/placeholders/prune-orphans");
|
|
||||||
expect(post.status).to.equal(401);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("groups two spellings of the same missing ingredient into one row", async () => {
|
|
||||||
await seedPlaceholderRecipe("Piment d'Espelette");
|
|
||||||
await seedPlaceholderRecipe("piment d espelette");
|
|
||||||
await seedPlaceholderRecipe("Sumac");
|
|
||||||
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent.get("/admin/catalog/placeholders");
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body).to.have.length(2);
|
|
||||||
|
|
||||||
const espelette = res.body.find(
|
|
||||||
(g: { normalizedName: string }) => g.normalizedName === "piment d espelette",
|
|
||||||
);
|
|
||||||
expect(espelette.recipeCount).to.equal(2);
|
|
||||||
expect(espelette.ingredientIds).to.have.length(2);
|
|
||||||
expect(espelette.displayNames).to.have.members(["Piment d'Espelette", "piment d espelette"]);
|
|
||||||
// Impact-ordered: the 2-recipe gap before the 1-recipe one.
|
|
||||||
expect(res.body[0].normalizedName).to.equal("piment d espelette");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("mark-reviewed stamps reviewedAt and moves the group out of the default list", async () => {
|
|
||||||
const id = await seedPlaceholderRecipe("Galanga");
|
|
||||||
const agent = await adminAgent();
|
|
||||||
|
|
||||||
const patched = await agent
|
|
||||||
.patch("/admin/catalog/placeholders/mark-reviewed")
|
|
||||||
.send({ ingredientIds: [id] });
|
|
||||||
expect(patched.status).to.equal(200);
|
|
||||||
expect(patched.body.reviewed).to.equal(1);
|
|
||||||
expect(
|
|
||||||
(await prisma.ingredient.findUniqueOrThrow({ where: { id } })).reviewedAt,
|
|
||||||
).to.be.an.instanceOf(Date);
|
|
||||||
|
|
||||||
const pending = await agent.get("/admin/catalog/placeholders");
|
|
||||||
expect(pending.body).to.have.length(0);
|
|
||||||
const reviewed = await agent.get("/admin/catalog/placeholders").query({ reviewed: "true" });
|
|
||||||
expect(reviewed.body).to.have.length(1);
|
|
||||||
expect(reviewed.body[0].allReviewed).to.equal(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prune-orphans deletes only placeholder rows with no recipe left", async () => {
|
|
||||||
await seedPlaceholderRecipe("Encore utilisé");
|
|
||||||
await prisma.ingredient.create({
|
|
||||||
data: {
|
|
||||||
key: `placeholder:${faker.string.uuid()}`,
|
|
||||||
isPlaceholder: true,
|
|
||||||
displayName: "Orphelin",
|
|
||||||
createdAt: new Date(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent.post("/admin/catalog/placeholders/prune-orphans");
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body.deleted).to.equal(1);
|
|
||||||
expect(await prisma.ingredient.count({ where: { isPlaceholder: true } })).to.equal(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,149 +0,0 @@
|
||||||
import type { SignupInput } from "@batch-cooking/shared";
|
|
||||||
import { faker } from "@faker-js/faker";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import request from "supertest";
|
|
||||||
import { createApp } from "../src/app.js";
|
|
||||||
import { env } from "../src/config/env.js";
|
|
||||||
import { prisma } from "../src/db/prisma.js";
|
|
||||||
import { hashAdminPassword } from "../src/modules/admin/admin-auth.service.js";
|
|
||||||
import { bucketByDay } from "../src/modules/admin/admin-metrics.service.js";
|
|
||||||
import { resetDatabase } from "../test-support/reset-db.js";
|
|
||||||
|
|
||||||
function buildSignupPayload(): SignupInput {
|
|
||||||
const firstName = faker.person.firstName();
|
|
||||||
const lastName = faker.person.lastName();
|
|
||||||
return {
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
email: faker.internet.email({ firstName, lastName }).toLowerCase(),
|
|
||||||
password: faker.internet.password({ length: 16 }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function seedAdmin(): Promise<{ email: string; password: string }> {
|
|
||||||
const email = faker.internet.email().toLowerCase();
|
|
||||||
const password = faker.internet.password({ length: 16 });
|
|
||||||
await prisma.adminUser.create({
|
|
||||||
data: { email, name: faker.person.fullName(), passwordHash: await hashAdminPassword(password) },
|
|
||||||
});
|
|
||||||
return { email, password };
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Retries `check` until it stops throwing or `timeoutMs` elapses — `analytics.recordEvent` writes its row fire-and-forget, so a test observing it has to poll briefly. */
|
|
||||||
async function eventually(check: () => Promise<void>, timeoutMs = 2000): Promise<void> {
|
|
||||||
const start = Date.now();
|
|
||||||
for (;;) {
|
|
||||||
try {
|
|
||||||
await check();
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
if (Date.now() - start > timeoutMs) throw err;
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const adminSecretConfigured = env.ADMIN_JWT_SECRET !== undefined;
|
|
||||||
|
|
||||||
describe("Admin metrics", () => {
|
|
||||||
describe("bucketByDay (pure)", () => {
|
|
||||||
const since = new Date("2026-08-01T00:00:00.000Z");
|
|
||||||
|
|
||||||
it("returns one zero-filled bucket per day, in date order", () => {
|
|
||||||
const result = bucketByDay([], since, 3);
|
|
||||||
expect(result).to.deep.equal([
|
|
||||||
{ date: "2026-08-01", count: 0 },
|
|
||||||
{ date: "2026-08-02", count: 0 },
|
|
||||||
{ date: "2026-08-03", count: 0 },
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("counts dates into their UTC day and ignores dates outside the window", () => {
|
|
||||||
const result = bucketByDay(
|
|
||||||
[
|
|
||||||
new Date("2026-08-01T09:00:00Z"),
|
|
||||||
new Date("2026-08-01T23:30:00Z"),
|
|
||||||
new Date("2026-08-03T00:00:00Z"),
|
|
||||||
new Date("2026-07-31T23:59:59Z"), // before the window
|
|
||||||
new Date("2026-08-10T00:00:00Z"), // after the window
|
|
||||||
],
|
|
||||||
since,
|
|
||||||
3,
|
|
||||||
);
|
|
||||||
expect(result).to.deep.equal([
|
|
||||||
{ date: "2026-08-01", count: 2 },
|
|
||||||
{ date: "2026-08-02", count: 0 },
|
|
||||||
{ date: "2026-08-03", count: 1 },
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /admin/metrics", () => {
|
|
||||||
const app = createApp();
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await resetDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects a request with no admin session with 401", async () => {
|
|
||||||
const res = await request(app).get("/admin/metrics");
|
|
||||||
expect(res.status).to.equal(401);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns a snapshot reflecting seeded data, plus zero-filled series", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { email, password } = await seedAdmin();
|
|
||||||
|
|
||||||
// Two end users sign up (also emits `user.signup` analytics events).
|
|
||||||
const userA = request.agent(app);
|
|
||||||
const userB = request.agent(app);
|
|
||||||
await userA.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
await userB.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
|
|
||||||
const adminAgent = request.agent(app);
|
|
||||||
await adminAgent.post("/admin/auth/login").send({ email, password });
|
|
||||||
|
|
||||||
const res = await adminAgent.get("/admin/metrics").query({ days: 14 });
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body.rangeDays).to.equal(14);
|
|
||||||
expect(res.body.snapshot.users).to.equal(2);
|
|
||||||
expect(res.body.snapshot.admins).to.equal(1);
|
|
||||||
expect(res.body.snapshot.recipes).to.equal(0);
|
|
||||||
|
|
||||||
// 14 daily buckets, each series zero-filled to that length.
|
|
||||||
expect(res.body.series.signups).to.have.length(14);
|
|
||||||
expect(
|
|
||||||
res.body.series.signups.every((b: { count: number }) => typeof b.count === "number"),
|
|
||||||
).to.equal(true);
|
|
||||||
// Two signups today → the last bucket counts them.
|
|
||||||
const signupTotal = res.body.series.signups.reduce(
|
|
||||||
(sum: number, b: { count: number }) => sum + b.count,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
expect(signupTotal).to.equal(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("records a user.signup analytics event (fire-and-forget, never blocks signup)", async () => {
|
|
||||||
const agent = request.agent(app);
|
|
||||||
const signupRes = await agent.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
expect(signupRes.status).to.equal(201);
|
|
||||||
|
|
||||||
// `>= 1`, not `=== 1`: `recordEvent` is fire-and-forget, so an insert
|
|
||||||
// from an earlier test's signup could in principle land in this
|
|
||||||
// window too — the point here is that the instrumentation fires and
|
|
||||||
// the signup itself was never blocked by it.
|
|
||||||
await eventually(async () => {
|
|
||||||
const count = await prisma.analyticsEvent.count({ where: { type: "user.signup" } });
|
|
||||||
expect(count).to.be.greaterThan(0);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,160 +0,0 @@
|
||||||
import { faker } from "@faker-js/faker";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import request from "supertest";
|
|
||||||
import { createApp } from "../src/app.js";
|
|
||||||
import { env } from "../src/config/env.js";
|
|
||||||
import { prisma } from "../src/db/prisma.js";
|
|
||||||
import { hashAdminPassword } from "../src/modules/admin/admin-auth.service.js";
|
|
||||||
import { resetDatabase } from "../test-support/reset-db.js";
|
|
||||||
|
|
||||||
const SECRET_HEADER = "X-Internal-Worker-Secret";
|
|
||||||
const VALID_STATUSES = ["up", "degraded", "down", "unknown"];
|
|
||||||
|
|
||||||
async function seedAdmin(): Promise<{ email: string; password: string }> {
|
|
||||||
const email = faker.internet.email().toLowerCase();
|
|
||||||
const password = faker.internet.password({ length: 16 });
|
|
||||||
await prisma.adminUser.create({
|
|
||||||
data: { email, name: faker.person.fullName(), passwordHash: await hashAdminPassword(password) },
|
|
||||||
});
|
|
||||||
return { email, password };
|
|
||||||
}
|
|
||||||
|
|
||||||
const adminSecretConfigured = env.ADMIN_JWT_SECRET !== undefined;
|
|
||||||
const workerSecretConfigured = env.INTERNAL_WORKER_SECRET !== undefined;
|
|
||||||
|
|
||||||
describe("Admin monitoring", () => {
|
|
||||||
const app = createApp();
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await resetDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("POST /internal/tech-steps/heartbeat", () => {
|
|
||||||
it("rejects a request with no worker secret with 401", async () => {
|
|
||||||
const res = await request(app).post("/internal/tech-steps/heartbeat").send({ event: "boot" });
|
|
||||||
expect(res.status).to.equal(401);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects a malformed body with 400", async function () {
|
|
||||||
if (!workerSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const res = await request(app)
|
|
||||||
.post("/internal/tech-steps/heartbeat")
|
|
||||||
.set(SECRET_HEADER, env.INTERNAL_WORKER_SECRET as string)
|
|
||||||
.send({ event: "not-a-real-event" });
|
|
||||||
expect(res.status).to.equal(400);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("upserts the worker heartbeat, recording lastRunAt/lastResult for a job ping", async function () {
|
|
||||||
if (!workerSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const res = await request(app)
|
|
||||||
.post("/internal/tech-steps/heartbeat")
|
|
||||||
.set(SECRET_HEADER, env.INTERNAL_WORKER_SECRET as string)
|
|
||||||
.send({
|
|
||||||
event: "job",
|
|
||||||
job: "audit-low-confidence",
|
|
||||||
ok: true,
|
|
||||||
counts: { suggestions: 3 },
|
|
||||||
});
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body).to.deep.equal({ ok: true });
|
|
||||||
|
|
||||||
const stored = await prisma.workerHeartbeat.findUniqueOrThrow({
|
|
||||||
where: { workerKey: "tech-step-llm-worker" },
|
|
||||||
});
|
|
||||||
expect(stored.lastRunAt).to.not.equal(null);
|
|
||||||
expect(stored.lastResult).to.deep.equal({
|
|
||||||
job: "audit-low-confidence",
|
|
||||||
ok: true,
|
|
||||||
counts: { suggestions: 3 },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("leaves lastRunAt null for a boot/tick ping", async function () {
|
|
||||||
if (!workerSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await request(app)
|
|
||||||
.post("/internal/tech-steps/heartbeat")
|
|
||||||
.set(SECRET_HEADER, env.INTERNAL_WORKER_SECRET as string)
|
|
||||||
.send({ event: "boot" });
|
|
||||||
|
|
||||||
const stored = await prisma.workerHeartbeat.findUniqueOrThrow({
|
|
||||||
where: { workerKey: "tech-step-llm-worker" },
|
|
||||||
});
|
|
||||||
expect(stored.lastSeenAt).to.be.instanceOf(Date);
|
|
||||||
expect(stored.lastRunAt).to.equal(null);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /admin/monitoring", () => {
|
|
||||||
it("rejects a request with no admin session with 401", async () => {
|
|
||||||
const res = await request(app).get("/admin/monitoring");
|
|
||||||
expect(res.status).to.equal(401);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns a status board covering all four targets, never crashing on an unreachable probe", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { email, password } = await seedAdmin();
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/admin/auth/login").send({ email, password });
|
|
||||||
|
|
||||||
const res = await agent.get("/admin/monitoring");
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
|
|
||||||
const keys = res.body.services.map((s: { key: string }) => s.key);
|
|
||||||
expect(keys).to.have.members(["postgres", "api", "intent-service", "tech-step-llm-worker"]);
|
|
||||||
for (const service of res.body.services) {
|
|
||||||
expect(VALID_STATUSES).to.include(service.status);
|
|
||||||
}
|
|
||||||
|
|
||||||
const byKey = Object.fromEntries(res.body.services.map((s: { key: string }) => [s.key, s]));
|
|
||||||
// The DB is up during the test run, and the API is answering us.
|
|
||||||
expect(byKey.postgres.status).to.equal("up");
|
|
||||||
expect(byKey.api.status).to.equal("up");
|
|
||||||
// No heartbeat has ever been recorded (resetDatabase truncated it).
|
|
||||||
expect(byKey["tech-step-llm-worker"].status).to.equal("unknown");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("reports the worker as up once it has sent a recent heartbeat", async function () {
|
|
||||||
if (!adminSecretConfigured || !workerSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await request(app)
|
|
||||||
.post("/internal/tech-steps/heartbeat")
|
|
||||||
.set(SECRET_HEADER, env.INTERNAL_WORKER_SECRET as string)
|
|
||||||
.send({ event: "job", job: "transform-corrections", ok: true, counts: { suggestions: 0 } });
|
|
||||||
|
|
||||||
const { email, password } = await seedAdmin();
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/admin/auth/login").send({ email, password });
|
|
||||||
|
|
||||||
const res = await agent.get("/admin/monitoring");
|
|
||||||
const worker = res.body.services.find(
|
|
||||||
(s: { key: string }) => s.key === "tech-step-llm-worker",
|
|
||||||
);
|
|
||||||
expect(worker.status).to.equal("up");
|
|
||||||
expect(worker.lastResult.job).to.equal("transform-corrections");
|
|
||||||
expect(worker.lastRunAt).to.be.a("string");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,308 +0,0 @@
|
||||||
import { ErrorCode } from "@batch-cooking/shared";
|
|
||||||
import { faker } from "@faker-js/faker";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import request from "supertest";
|
|
||||||
import { createApp } from "../src/app.js";
|
|
||||||
import { env } from "../src/config/env.js";
|
|
||||||
import { prisma } from "../src/db/prisma.js";
|
|
||||||
import { hashAdminPassword } from "../src/modules/admin/admin-auth.service.js";
|
|
||||||
import { resetDatabase } from "../test-support/reset-db.js";
|
|
||||||
|
|
||||||
async function seedAdmin(): Promise<{ email: string; password: string }> {
|
|
||||||
const email = faker.internet.email().toLowerCase();
|
|
||||||
const password = faker.internet.password({ length: 16 });
|
|
||||||
await prisma.adminUser.create({
|
|
||||||
data: { email, name: faker.person.fullName(), passwordHash: await hashAdminPassword(password) },
|
|
||||||
});
|
|
||||||
return { email, password };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function techStepId(key: string): Promise<number> {
|
|
||||||
return (await prisma.techStep.findFirstOrThrow({ where: { key } })).id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A recipe + one step + one correction on it, optionally already turned into a suggestion. */
|
|
||||||
async function seedCorrectionAndSuggestion(options: {
|
|
||||||
clause: string;
|
|
||||||
correctedKey: string | null;
|
|
||||||
withSuggestion?: { status: string; synonyms: string[] };
|
|
||||||
}) {
|
|
||||||
const author = await prisma.userProfile.create({
|
|
||||||
data: {
|
|
||||||
firstName: "T",
|
|
||||||
lastName: "A",
|
|
||||||
email: `${faker.string.uuid()}@example.test`,
|
|
||||||
passwordHash: "x",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const recipe = await prisma.recipe.create({
|
|
||||||
data: {
|
|
||||||
name: "R",
|
|
||||||
authorId: author.id,
|
|
||||||
portions: 4,
|
|
||||||
steps: { create: [{ description: options.clause, order: 0 }] },
|
|
||||||
},
|
|
||||||
include: { steps: true },
|
|
||||||
});
|
|
||||||
const step = recipe.steps[0];
|
|
||||||
if (!step) throw new Error("expected a step");
|
|
||||||
|
|
||||||
const correctedKey = options.correctedKey;
|
|
||||||
const correction = await prisma.stepTechStepCorrection.create({
|
|
||||||
data: {
|
|
||||||
stepId: step.id,
|
|
||||||
correctorId: author.id,
|
|
||||||
start: 0,
|
|
||||||
end: options.clause.length,
|
|
||||||
previousTechStepId: null,
|
|
||||||
correctedTechStepId: correctedKey === null ? null : await techStepId(correctedKey),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
let suggestion: { id: number } | null = null;
|
|
||||||
if (options.withSuggestion && correctedKey !== null) {
|
|
||||||
suggestion = await prisma.techStepTrainingSuggestion.create({
|
|
||||||
data: {
|
|
||||||
techStepId: await techStepId(correctedKey),
|
|
||||||
locale: "fr",
|
|
||||||
suggestedSynonyms: options.withSuggestion.synonyms,
|
|
||||||
suggestedUtterances: [],
|
|
||||||
sourceType: "correction",
|
|
||||||
sourceCorrectionId: correction.id,
|
|
||||||
status: options.withSuggestion.status,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return { recipeId: recipe.id, stepId: step.id, correctionId: correction.id, suggestion };
|
|
||||||
}
|
|
||||||
|
|
||||||
const adminSecretConfigured = env.ADMIN_JWT_SECRET !== undefined;
|
|
||||||
|
|
||||||
describe("Admin tech-steps triage", () => {
|
|
||||||
const app = createApp();
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await resetDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
async function adminAgent() {
|
|
||||||
const { email, password } = await seedAdmin();
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/admin/auth/login").send({ email, password });
|
|
||||||
return agent;
|
|
||||||
}
|
|
||||||
|
|
||||||
it("rejects every route without an admin session", async () => {
|
|
||||||
for (const path of [
|
|
||||||
"/admin/tech-steps/suggestions",
|
|
||||||
"/admin/tech-steps/corrections",
|
|
||||||
"/admin/tech-steps/training-data-snippet?techStepKey=simmer",
|
|
||||||
]) {
|
|
||||||
const res = await request(app).get(path);
|
|
||||||
expect(res.status, path).to.equal(401);
|
|
||||||
}
|
|
||||||
const post = await request(app).post("/admin/tech-steps/retrain").send({});
|
|
||||||
expect(post.status).to.equal(401);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /suggestions", () => {
|
|
||||||
it("groups suggestions by technique and filters by status", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await seedCorrectionAndSuggestion({
|
|
||||||
clause: "Faire mijoter",
|
|
||||||
correctedKey: "simmer",
|
|
||||||
withSuggestion: { status: "pending", synonyms: ["laisser frémir"] },
|
|
||||||
});
|
|
||||||
await seedCorrectionAndSuggestion({
|
|
||||||
clause: "Émincer les oignons",
|
|
||||||
correctedKey: "chop",
|
|
||||||
withSuggestion: { status: "applied", synonyms: ["ciseler"] },
|
|
||||||
});
|
|
||||||
|
|
||||||
const agent = await adminAgent();
|
|
||||||
|
|
||||||
const all = await agent.get("/admin/tech-steps/suggestions");
|
|
||||||
expect(all.status).to.equal(200);
|
|
||||||
expect(all.body.map((g: { techStepKey: string }) => g.techStepKey)).to.have.members([
|
|
||||||
"chop",
|
|
||||||
"simmer",
|
|
||||||
]);
|
|
||||||
const simmerGroup = all.body.find((g: { techStepKey: string }) => g.techStepKey === "simmer");
|
|
||||||
expect(simmerGroup.suggestions[0].sourceCorrection.clauseText).to.equal("Faire mijoter");
|
|
||||||
|
|
||||||
const pendingOnly = await agent
|
|
||||||
.get("/admin/tech-steps/suggestions")
|
|
||||||
.query({ status: "pending" });
|
|
||||||
expect(pendingOnly.body).to.have.length(1);
|
|
||||||
expect(pendingOnly.body[0].techStepKey).to.equal("simmer");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("PATCH /suggestions/:id", () => {
|
|
||||||
it("rejects an empty body with 400", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { suggestion } = await seedCorrectionAndSuggestion({
|
|
||||||
clause: "Faire mijoter",
|
|
||||||
correctedKey: "simmer",
|
|
||||||
withSuggestion: { status: "pending", synonyms: ["x"] },
|
|
||||||
});
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent.patch(`/admin/tech-steps/suggestions/${suggestion?.id}`).send({});
|
|
||||||
expect(res.status).to.equal(400);
|
|
||||||
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("404s an unknown id", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent
|
|
||||||
.patch("/admin/tech-steps/suggestions/999999")
|
|
||||||
.send({ status: "applied" });
|
|
||||||
expect(res.status).to.equal(404);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("flips the status and edits the synonyms, reflected in a later GET", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { suggestion } = await seedCorrectionAndSuggestion({
|
|
||||||
clause: "Faire mijoter",
|
|
||||||
correctedKey: "simmer",
|
|
||||||
withSuggestion: { status: "pending", synonyms: ["frémir"] },
|
|
||||||
});
|
|
||||||
const agent = await adminAgent();
|
|
||||||
|
|
||||||
const patched = await agent
|
|
||||||
.patch(`/admin/tech-steps/suggestions/${suggestion?.id}`)
|
|
||||||
.send({ status: "applied", suggestedSynonyms: ["frémir", "mijoter doucement"] });
|
|
||||||
expect(patched.status).to.equal(200);
|
|
||||||
expect(patched.body.status).to.equal("applied");
|
|
||||||
expect(patched.body.suggestedSynonyms).to.deep.equal(["frémir", "mijoter doucement"]);
|
|
||||||
|
|
||||||
const stored = await prisma.techStepTrainingSuggestion.findUniqueOrThrow({
|
|
||||||
where: { id: suggestion?.id },
|
|
||||||
});
|
|
||||||
expect(stored.status).to.equal("applied");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /corrections", () => {
|
|
||||||
it("includes the 'no technique here' removals", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await seedCorrectionAndSuggestion({ clause: "Rien ici", correctedKey: null });
|
|
||||||
await seedCorrectionAndSuggestion({ clause: "Faire mijoter", correctedKey: "simmer" });
|
|
||||||
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent.get("/admin/tech-steps/corrections");
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body).to.have.length(2);
|
|
||||||
|
|
||||||
const removals = await agent
|
|
||||||
.get("/admin/tech-steps/corrections")
|
|
||||||
.query({ hasCorrectedTechStep: "false" });
|
|
||||||
expect(removals.body).to.have.length(1);
|
|
||||||
expect(removals.body[0].clauseText).to.equal("Rien ici");
|
|
||||||
expect(removals.body[0].correctedTechStepKey).to.equal(null);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /training-data-snippet", () => {
|
|
||||||
it("aggregates the applied suggestions' synonyms into a paste-ready block", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await seedCorrectionAndSuggestion({
|
|
||||||
clause: "Faire mijoter",
|
|
||||||
correctedKey: "simmer",
|
|
||||||
withSuggestion: { status: "applied", synonyms: ["frémir", "réduire à feu doux"] },
|
|
||||||
});
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent
|
|
||||||
.get("/admin/tech-steps/training-data-snippet")
|
|
||||||
.query({ techStepKey: "simmer", locale: "fr", status: "applied" });
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body.suggestionCount).to.equal(1);
|
|
||||||
expect(res.body.synonyms).to.deep.equal(["frémir", "réduire à feu doux"]);
|
|
||||||
expect(res.body.snippet).to.include('"frémir"');
|
|
||||||
expect(res.body.snippet).to.include('"synonyms": [');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("POST /retrain", () => {
|
|
||||||
it("runs the F1 gate and returns its result shape (needs tech-step-intent-service)", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.timeout(60000);
|
|
||||||
const agent = await adminAgent();
|
|
||||||
const res = await agent.post("/admin/tech-steps/retrain").send({});
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body).to.have.keys([
|
|
||||||
"f1",
|
|
||||||
"precision",
|
|
||||||
"recall",
|
|
||||||
"minF1",
|
|
||||||
"gatePassed",
|
|
||||||
"backfilled",
|
|
||||||
"marked",
|
|
||||||
]);
|
|
||||||
expect(res.body.minF1).to.equal(0.8);
|
|
||||||
if (res.body.gatePassed) {
|
|
||||||
expect(res.body.backfilled).to.have.keys(["total", "changed"]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns 409 RETRAIN_ALREADY_RUNNING while one is in flight", async function () {
|
|
||||||
if (!adminSecretConfigured) {
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: see above.
|
|
||||||
(this as any).skip();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.timeout(60000);
|
|
||||||
const agent = await adminAgent();
|
|
||||||
// supertest requests are lazy — they only dispatch when awaited/then'd.
|
|
||||||
// Attaching the `.then` here is what actually fires the first request,
|
|
||||||
// so its handler acquires the process-wide lock before the second one
|
|
||||||
// (100ms later) checks it. Swallow its result/rejection — this test
|
|
||||||
// only asserts on the second request.
|
|
||||||
const first = agent
|
|
||||||
.post("/admin/tech-steps/retrain")
|
|
||||||
.send({})
|
|
||||||
.then(
|
|
||||||
(res) => res,
|
|
||||||
(err) => err,
|
|
||||||
);
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
||||||
const second = await agent.post("/admin/tech-steps/retrain").send({});
|
|
||||||
expect(second.status).to.equal(409);
|
|
||||||
expect(second.body.code).to.equal(ErrorCode.RETRAIN_ALREADY_RUNNING);
|
|
||||||
await first;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,214 +0,0 @@
|
||||||
import type { DateTime } from "@batch-cooking/date-tools";
|
|
||||||
import { ErrorCode, type SignupInput } from "@batch-cooking/shared";
|
|
||||||
import { faker } from "@faker-js/faker";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import request from "supertest";
|
|
||||||
import { createApp } from "../src/app.js";
|
|
||||||
import { prisma } from "../src/db/prisma.js";
|
|
||||||
import { TEST_REFERENCE_DATE } from "../test-support/reference-date.js";
|
|
||||||
import { resetDatabase } from "../test-support/reset-db.js";
|
|
||||||
|
|
||||||
/** See `auth.test.ts` — same rationale for generating rather than hardcoding. */
|
|
||||||
function buildSignupPayload(): SignupInput {
|
|
||||||
const firstName = faker.person.firstName();
|
|
||||||
const lastName = faker.person.lastName();
|
|
||||||
return {
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
email: faker.internet.email({ firstName, lastName }).toLowerCase(),
|
|
||||||
password: faker.internet.password({ length: 16 }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** `toISODate()` only returns `null` for an invalid `DateTime` — never the always-valid values here. */
|
|
||||||
function isoDate(date: DateTime): string {
|
|
||||||
const iso = date.toISODate();
|
|
||||||
if (iso === null) throw new Error("Unexpectedly invalid DateTime in a test helper");
|
|
||||||
return iso;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The fixed test "today", as the `YYYY-MM-DD` string the `?date=` query expects. */
|
|
||||||
function today(): string {
|
|
||||||
return isoDate(TEST_REFERENCE_DATE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Resolves a reference row's id by its `reference-seed-data.ts` uid (also its DB `key`) — same helpers as `recipe.test.ts`. */
|
|
||||||
async function ingredientId(key: string): Promise<number> {
|
|
||||||
return (await prisma.ingredient.findFirstOrThrow({ where: { key } })).id;
|
|
||||||
}
|
|
||||||
async function unitId(key: string): Promise<number> {
|
|
||||||
return (await prisma.unit.findFirstOrThrow({ where: { key } })).id;
|
|
||||||
}
|
|
||||||
async function techStepId(key: string): Promise<number> {
|
|
||||||
return (await prisma.techStep.findFirstOrThrow({ where: { key } })).id;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Cooking session", () => {
|
|
||||||
const app = createApp();
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await resetDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /cooking-session", () => {
|
|
||||||
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
|
|
||||||
const res = await request(app).get("/cooking-session").query({ date: today() });
|
|
||||||
|
|
||||||
expect(res.status).to.equal(401);
|
|
||||||
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects a malformed date with 400 VALIDATION_ERROR", async () => {
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
|
|
||||||
const res = await agent.get("/cooking-session").query({ date: "not-a-date" });
|
|
||||||
|
|
||||||
expect(res.status).to.equal(400);
|
|
||||||
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns an empty plan when the profile has no household", async () => {
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
|
|
||||||
const res = await agent.get("/cooking-session").query({ date: today() });
|
|
||||||
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body.recipes).to.deep.equal([]);
|
|
||||||
expect(res.body.phases).to.deep.equal([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns an empty plan when no planning covers that week", async () => {
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
await agent.post("/house").send({ name: "Chez moi" });
|
|
||||||
|
|
||||||
const res = await agent.get("/cooking-session").query({ date: today() });
|
|
||||||
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body.phases).to.deep.equal([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("pools an identical prep step from two planned recipes into one merged-prep task", async () => {
|
|
||||||
const agent = request.agent(app);
|
|
||||||
await agent.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
const houseRes = await agent.post("/house").send({ name: "Chez moi" });
|
|
||||||
const houseId: number = houseRes.body.id;
|
|
||||||
const authorId: number = houseRes.body.adminId;
|
|
||||||
|
|
||||||
const onionId = await ingredientId("onion");
|
|
||||||
const pieceId = await unitId("piece");
|
|
||||||
const chopId = await techStepId("chop");
|
|
||||||
const simmerId = await techStepId("simmer");
|
|
||||||
const mixId = await techStepId("mix");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A recipe: one pure-prep "chop onion" step, then (optionally) an
|
|
||||||
* active "mix" step, then one simmer step. The `withActiveStep` recipe
|
|
||||||
* is still doing hands-on work in the phase after its simmer starts, so
|
|
||||||
* the other recipe's simmer floats into that phase as `background`.
|
|
||||||
*/
|
|
||||||
async function makeRecipe(name: string, onionQty: number, withActiveStep = false) {
|
|
||||||
const chopStep = {
|
|
||||||
order: 0,
|
|
||||||
description: "Émincer les oignons",
|
|
||||||
techSteps: {
|
|
||||||
create: [
|
|
||||||
{
|
|
||||||
techStepId: chopId,
|
|
||||||
order: 0,
|
|
||||||
ingredients: {
|
|
||||||
create: [
|
|
||||||
{
|
|
||||||
ingredientId: onionId,
|
|
||||||
quantity: onionQty,
|
|
||||||
unitId: pieceId,
|
|
||||||
start: 0,
|
|
||||||
end: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const activeStep = {
|
|
||||||
order: 1,
|
|
||||||
description: "Mélanger l'appareil",
|
|
||||||
techSteps: { create: [{ techStepId: mixId, order: 0 }] },
|
|
||||||
};
|
|
||||||
const simmerStep = {
|
|
||||||
order: withActiveStep ? 2 : 1,
|
|
||||||
description: "Faire mijoter",
|
|
||||||
techSteps: { create: [{ techStepId: simmerId, order: 0 }] },
|
|
||||||
};
|
|
||||||
return prisma.recipe.create({
|
|
||||||
data: {
|
|
||||||
name,
|
|
||||||
authorId,
|
|
||||||
portions: 4,
|
|
||||||
steps: {
|
|
||||||
create: withActiveStep ? [chopStep, activeStep, simmerStep] : [chopStep, simmerStep],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const soupe = await makeRecipe("Soupe", 2);
|
|
||||||
const tarte = await makeRecipe("Tarte", 3, true);
|
|
||||||
|
|
||||||
const planning = await prisma.planning.create({
|
|
||||||
data: {
|
|
||||||
houseId,
|
|
||||||
startDate: TEST_REFERENCE_DATE.minus({ days: 2 }).toJSDate(),
|
|
||||||
finishDate: TEST_REFERENCE_DATE.plus({ days: 2 }).toJSDate(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
await prisma.planningItem.createMany({
|
|
||||||
data: [
|
|
||||||
{
|
|
||||||
planningId: planning.id,
|
|
||||||
weekDay: "lundi",
|
|
||||||
meal: "dejeuner",
|
|
||||||
recipeId: soupe.id,
|
|
||||||
portions: 4,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
planningId: planning.id,
|
|
||||||
weekDay: "mardi",
|
|
||||||
meal: "diner",
|
|
||||||
recipeId: tarte.id,
|
|
||||||
portions: 4,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const res = await agent.get("/cooking-session").query({ date: today() });
|
|
||||||
|
|
||||||
expect(res.status).to.equal(200);
|
|
||||||
expect(res.body.recipes.map((r: { name: string }) => r.name)).to.have.members([
|
|
||||||
"Soupe",
|
|
||||||
"Tarte",
|
|
||||||
]);
|
|
||||||
|
|
||||||
const mise = res.body.phases[0];
|
|
||||||
expect(mise.kind).to.equal("mise-en-place");
|
|
||||||
const merged = mise.tasks.filter((t: { kind: string }) => t.kind === "merged-prep");
|
|
||||||
expect(merged).to.have.length(1);
|
|
||||||
expect(merged[0].technique.key).to.equal("chop");
|
|
||||||
expect(merged[0].ingredients[0].ingredient.key).to.equal("onion");
|
|
||||||
expect(merged[0].ingredients[0].quantity).to.equal(5);
|
|
||||||
expect(merged[0].sourceRecipes).to.have.length(2);
|
|
||||||
|
|
||||||
// The simmer steps land in a later phase, and one shows as background.
|
|
||||||
const later = res.body.phases.slice(1);
|
|
||||||
const backgrounds = later.flatMap((p: { background: unknown[] }) => p.background);
|
|
||||||
expect(backgrounds.length).to.be.greaterThan(0);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,196 +0,0 @@
|
||||||
import type { SignupInput } from "@batch-cooking/shared";
|
|
||||||
import { ErrorCode } from "@batch-cooking/shared";
|
|
||||||
import { faker } from "@faker-js/faker";
|
|
||||||
import { expect } from "chai";
|
|
||||||
import request from "supertest";
|
|
||||||
import { createApp } from "../src/app.js";
|
|
||||||
import { prisma } from "../src/db/prisma.js";
|
|
||||||
import { resetDatabase } from "../test-support/reset-db.js";
|
|
||||||
|
|
||||||
/** See `auth.test.ts` — generated, never a real-looking person. */
|
|
||||||
function buildSignupPayload(): SignupInput {
|
|
||||||
const firstName = faker.person.firstName();
|
|
||||||
const lastName = faker.person.lastName();
|
|
||||||
return {
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
email: faker.internet.email({ firstName, lastName }).toLowerCase(),
|
|
||||||
password: faker.internet.password({ length: 16 }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Resolves a reference unit's id by its seed uid (also its DB `key`). */
|
|
||||||
async function unitId(key: string): Promise<number> {
|
|
||||||
return (await prisma.unit.findFirstOrThrow({ where: { key } })).id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Resolves a reference ingredient's id by its seed uid. */
|
|
||||||
async function ingredientId(key: string): Promise<number> {
|
|
||||||
return (await prisma.ingredient.findFirstOrThrow({ where: { key } })).id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The off-catalog ("placeholder") ingredient escape hatch on `POST /recipes`
|
|
||||||
* / `PATCH /recipes/:id` — a line with `placeholderName` instead of
|
|
||||||
* `ingredientId` creates a dedicated `Ingredient` row
|
|
||||||
* (`isPlaceholder: true`) so the recipe still saves, and that row is
|
|
||||||
* surfaced (with its `displayName`) inside the recipe but never in the
|
|
||||||
* browsable catalog.
|
|
||||||
*/
|
|
||||||
describe("Recipes — off-catalog placeholder ingredients", () => {
|
|
||||||
const app = createApp();
|
|
||||||
|
|
||||||
async function signup(): Promise<{ agent: ReturnType<typeof request.agent>; profileId: number }> {
|
|
||||||
const agent = request.agent(app);
|
|
||||||
const res = await agent.post("/auth/signup").send(buildSignupPayload());
|
|
||||||
return { agent, profileId: res.body.id };
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await resetDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("creates a placeholder Ingredient row for a `placeholderName` line and returns it inside the recipe", async () => {
|
|
||||||
const { agent, profileId } = await signup();
|
|
||||||
const piece = await unitId("piece");
|
|
||||||
const tomato = await ingredientId("tomato");
|
|
||||||
|
|
||||||
const res = await agent.post("/recipes").send({
|
|
||||||
name: "Poulet basquaise",
|
|
||||||
portions: 4,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [
|
|
||||||
{ ingredientId: tomato, quantity: 3, unitId: piece },
|
|
||||||
{ placeholderName: "Piment d'Espelette", quantity: 1, unitId: piece },
|
|
||||||
],
|
|
||||||
steps: [{ description: "Tout mélanger" }],
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).to.equal(201);
|
|
||||||
expect(res.body.ingredients).to.have.length(2);
|
|
||||||
|
|
||||||
const placeholderLine = res.body.ingredients.find(
|
|
||||||
(line: { ingredient: { isPlaceholder: boolean } }) => line.ingredient.isPlaceholder,
|
|
||||||
);
|
|
||||||
expect(placeholderLine, "a placeholder line is present").to.not.equal(undefined);
|
|
||||||
expect(placeholderLine.ingredient.displayName).to.equal("Piment d'Espelette");
|
|
||||||
expect(placeholderLine.ingredient.key).to.match(/^placeholder:/);
|
|
||||||
expect(placeholderLine.ingredient.allergens).to.deep.equal([]);
|
|
||||||
expect(placeholderLine.quantity).to.equal(1);
|
|
||||||
|
|
||||||
const row = await prisma.ingredient.findUniqueOrThrow({
|
|
||||||
where: { id: placeholderLine.ingredient.id },
|
|
||||||
});
|
|
||||||
expect(row.isPlaceholder).to.equal(true);
|
|
||||||
expect(row.displayName).to.equal("Piment d'Espelette");
|
|
||||||
expect(row.createdById).to.equal(profileId);
|
|
||||||
expect(row.createdAt).to.be.an.instanceOf(Date);
|
|
||||||
expect(row.reviewedAt).to.equal(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("never lists placeholder rows in GET /reference/ingredients", async () => {
|
|
||||||
const { agent } = await signup();
|
|
||||||
const piece = await unitId("piece");
|
|
||||||
|
|
||||||
await agent.post("/recipes").send({
|
|
||||||
name: "Test",
|
|
||||||
portions: 2,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [{ placeholderName: "Feuille de combava", quantity: 1, unitId: piece }],
|
|
||||||
steps: [{ description: "x" }],
|
|
||||||
});
|
|
||||||
|
|
||||||
const reference = await agent.get("/reference/ingredients");
|
|
||||||
expect(reference.status).to.equal(200);
|
|
||||||
expect(
|
|
||||||
reference.body.some(
|
|
||||||
(i: { isPlaceholder?: boolean; displayName?: string }) =>
|
|
||||||
i.isPlaceholder === true || i.displayName === "Feuille de combava",
|
|
||||||
),
|
|
||||||
).to.equal(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("reuses the existing placeholder row on edit (no duplicate) and drops it when the line is removed", async () => {
|
|
||||||
const { agent } = await signup();
|
|
||||||
const piece = await unitId("piece");
|
|
||||||
const tomato = await ingredientId("tomato");
|
|
||||||
|
|
||||||
const created = await agent.post("/recipes").send({
|
|
||||||
name: "Édition",
|
|
||||||
portions: 2,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [{ placeholderName: "Sumac", quantity: 1, unitId: piece }],
|
|
||||||
steps: [{ description: "x" }],
|
|
||||||
});
|
|
||||||
const placeholderId = created.body.ingredients[0].ingredient.id;
|
|
||||||
|
|
||||||
// Re-submit the same recipe, keeping the placeholder line by its real id.
|
|
||||||
const edited = await agent.patch(`/recipes/${created.body.id}`).send({
|
|
||||||
name: "Édition",
|
|
||||||
portions: 2,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [
|
|
||||||
{ ingredientId: placeholderId, quantity: 2, unitId: piece },
|
|
||||||
{ ingredientId: tomato, quantity: 1, unitId: piece },
|
|
||||||
],
|
|
||||||
steps: [{ description: "x" }],
|
|
||||||
});
|
|
||||||
expect(edited.status).to.equal(200);
|
|
||||||
expect(await prisma.ingredient.count({ where: { isPlaceholder: true } })).to.equal(1);
|
|
||||||
|
|
||||||
// Now edit again, dropping the placeholder line entirely.
|
|
||||||
await agent.patch(`/recipes/${created.body.id}`).send({
|
|
||||||
name: "Édition",
|
|
||||||
portions: 2,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [{ ingredientId: tomato, quantity: 1, unitId: piece }],
|
|
||||||
steps: [{ description: "x" }],
|
|
||||||
});
|
|
||||||
// The row is now an orphan (kept on purpose — the admin catalog view
|
|
||||||
// prunes it), but no *new* placeholder was created.
|
|
||||||
expect(await prisma.ingredient.count({ where: { isPlaceholder: true } })).to.equal(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects a line carrying both ingredientId and placeholderName with 400", async () => {
|
|
||||||
const { agent } = await signup();
|
|
||||||
const piece = await unitId("piece");
|
|
||||||
const tomato = await ingredientId("tomato");
|
|
||||||
|
|
||||||
const res = await agent.post("/recipes").send({
|
|
||||||
name: "Invalide",
|
|
||||||
portions: 2,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [
|
|
||||||
{ ingredientId: tomato, placeholderName: "Tomate", quantity: 1, unitId: piece },
|
|
||||||
],
|
|
||||||
steps: [{ description: "x" }],
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).to.equal(400);
|
|
||||||
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("allows two placeholder lines with the same text (each becomes its own row)", async () => {
|
|
||||||
const { agent } = await signup();
|
|
||||||
const piece = await unitId("piece");
|
|
||||||
|
|
||||||
const res = await agent.post("/recipes").send({
|
|
||||||
name: "Doublons libres",
|
|
||||||
portions: 2,
|
|
||||||
dietIds: [],
|
|
||||||
ingredients: [
|
|
||||||
{ placeholderName: "Herbes de garrigue", quantity: 1, unitId: piece },
|
|
||||||
{ placeholderName: "Herbes de garrigue", quantity: 2, unitId: piece },
|
|
||||||
],
|
|
||||||
steps: [{ description: "x" }],
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).to.equal(201);
|
|
||||||
expect(res.body.ingredients).to.have.length(2);
|
|
||||||
expect(await prisma.ingredient.count({ where: { isPlaceholder: true } })).to.equal(2);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -95,8 +95,6 @@ describe("Reference data", () => {
|
||||||
"reproducible",
|
"reproducible",
|
||||||
"allergens",
|
"allergens",
|
||||||
"diets",
|
"diets",
|
||||||
"isPlaceholder",
|
|
||||||
"displayName",
|
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,99 +0,0 @@
|
||||||
// Mocks the admin API via cy.intercept — no live backend.
|
|
||||||
|
|
||||||
const adminBody = {
|
|
||||||
id: 1,
|
|
||||||
email: "ops@example.com",
|
|
||||||
name: "Ops",
|
|
||||||
createdAt: "2026-08-01T00:00:00.000Z",
|
|
||||||
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
|
||||||
};
|
|
||||||
|
|
||||||
function pendingGroups() {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
normalizedName: "piment d espelette",
|
|
||||||
displayNames: ["Piment d'Espelette", "piment d espelette"],
|
|
||||||
ingredientIds: [11, 12],
|
|
||||||
recipeCount: 2,
|
|
||||||
sampleRecipes: [
|
|
||||||
{ id: 1, name: "Poulet basquaise" },
|
|
||||||
{ id: 2, name: "Piperade" },
|
|
||||||
],
|
|
||||||
firstSeenAt: "2026-08-20T10:00:00.000Z",
|
|
||||||
allReviewed: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
normalizedName: "sumac",
|
|
||||||
displayNames: ["Sumac"],
|
|
||||||
ingredientIds: [13],
|
|
||||||
recipeCount: 1,
|
|
||||||
sampleRecipes: [{ id: 3, name: "Fattoush" }],
|
|
||||||
firstSeenAt: "2026-08-22T10:00:00.000Z",
|
|
||||||
allReviewed: false,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Admin catalog — off-catalog ingredients", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
cy.viewport(1400, 900);
|
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("lists placeholder groups newest-impact first with their recipe count and spelling variants", () => {
|
|
||||||
cy.intercept("GET", "**/admin/catalog/placeholders*", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: pendingGroups(),
|
|
||||||
}).as("getPlaceholders");
|
|
||||||
cy.visit("/admin/catalogue");
|
|
||||||
cy.wait("@getPlaceholders");
|
|
||||||
|
|
||||||
cy.get(".catalog-card").should("have.length", 2);
|
|
||||||
cy.get(".catalog-card").first().should("contain.text", "Piment d'Espelette");
|
|
||||||
cy.contains(".catalog-card", "Piment d'Espelette")
|
|
||||||
.should("contain.text", "2 recette")
|
|
||||||
.and("contain.text", "piment d espelette")
|
|
||||||
.and("contain.text", "Poulet basquaise");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("marks a group reviewed and reloads the list", () => {
|
|
||||||
cy.intercept("GET", "**/admin/catalog/placeholders*", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: pendingGroups(),
|
|
||||||
}).as("getPlaceholders");
|
|
||||||
cy.intercept("PATCH", "**/admin/catalog/placeholders/mark-reviewed", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: { reviewed: 1 },
|
|
||||||
}).as("markReviewed");
|
|
||||||
|
|
||||||
cy.visit("/admin/catalogue");
|
|
||||||
cy.wait("@getPlaceholders");
|
|
||||||
|
|
||||||
cy.contains(".catalog-card", "Sumac").contains("button", "Marquer comme traité").click();
|
|
||||||
|
|
||||||
cy.wait("@markReviewed")
|
|
||||||
.its("request.body")
|
|
||||||
.should("deep.equal", { ingredientIds: [13] });
|
|
||||||
// The page re-fetches the list after the PATCH.
|
|
||||||
cy.get("@getPlaceholders.all").should("have.length.greaterThan", 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("switches to the reviewed archive tab", () => {
|
|
||||||
// Regex, not a glob: the two calls differ only by the `?reviewed=true`
|
|
||||||
// query, and minimatch's `?` is itself a wildcard — a glob can't tell
|
|
||||||
// them apart reliably.
|
|
||||||
cy.intercept("GET", /\/admin\/catalog\/placeholders$/, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: pendingGroups(),
|
|
||||||
});
|
|
||||||
cy.intercept("GET", /\/admin\/catalog\/placeholders\?reviewed=true$/, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: [],
|
|
||||||
}).as("getReviewed");
|
|
||||||
|
|
||||||
cy.visit("/admin/catalogue");
|
|
||||||
cy.contains(".catalog-tabs button", "Traités").click();
|
|
||||||
cy.wait("@getReviewed");
|
|
||||||
cy.contains("Aucun ingrédient hors-catalogue").should("be.visible");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,159 +0,0 @@
|
||||||
// Mocks the admin API via cy.intercept — no live backend.
|
|
||||||
|
|
||||||
const adminBody = {
|
|
||||||
id: 1,
|
|
||||||
email: "ops@example.com",
|
|
||||||
name: "Ops",
|
|
||||||
createdAt: "2026-08-01T00:00:00.000Z",
|
|
||||||
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
|
||||||
};
|
|
||||||
|
|
||||||
function suggestionGroups() {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
techStepKey: "simmer",
|
|
||||||
suggestions: [
|
|
||||||
{
|
|
||||||
id: 11,
|
|
||||||
techStepKey: "simmer",
|
|
||||||
locale: "fr",
|
|
||||||
suggestedSynonyms: ["frémir"],
|
|
||||||
suggestedUtterances: ["laisser cuire tout doucement"],
|
|
||||||
sourceType: "correction",
|
|
||||||
status: "pending",
|
|
||||||
createdAt: "2026-08-20T00:00:00.000Z",
|
|
||||||
sourceCorrection: {
|
|
||||||
id: 5,
|
|
||||||
recipeId: 2,
|
|
||||||
stepId: 7,
|
|
||||||
clauseText: "faire mijoter la sauce",
|
|
||||||
previousTechStepKey: "cook",
|
|
||||||
correctedTechStepKey: "simmer",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
function corrections() {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
id: 5,
|
|
||||||
recipeId: 2,
|
|
||||||
stepId: 7,
|
|
||||||
stepDescription: "Faire mijoter la sauce 20 min.",
|
|
||||||
clauseText: "faire mijoter la sauce",
|
|
||||||
start: 0,
|
|
||||||
end: 21,
|
|
||||||
previousTechStepKey: "cook",
|
|
||||||
correctedTechStepKey: "simmer",
|
|
||||||
createdAt: "2026-08-20T00:00:00.000Z",
|
|
||||||
consumedAt: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 6,
|
|
||||||
recipeId: 3,
|
|
||||||
stepId: 9,
|
|
||||||
stepDescription: "Réserver au frais.",
|
|
||||||
clauseText: "Réserver au frais",
|
|
||||||
start: 0,
|
|
||||||
end: 17,
|
|
||||||
previousTechStepKey: "setAside",
|
|
||||||
correctedTechStepKey: null,
|
|
||||||
createdAt: "2026-08-19T00:00:00.000Z",
|
|
||||||
consumedAt: null,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Admin corrections triage", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
cy.viewport(1400, 1000);
|
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
|
||||||
cy.intercept("GET", "**/admin/tech-steps/suggestions*", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: suggestionGroups(),
|
|
||||||
}).as("getSuggestions");
|
|
||||||
cy.intercept("GET", "**/admin/tech-steps/corrections*", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: corrections(),
|
|
||||||
}).as("getCorrections");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("shows the caveat, groups suggestions by technique, and applies one", () => {
|
|
||||||
cy.intercept("PATCH", "**/admin/tech-steps/suggestions/11", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: { ...suggestionGroups()[0].suggestions[0], status: "applied" },
|
|
||||||
}).as("patch");
|
|
||||||
|
|
||||||
cy.visit("/admin/corrections");
|
|
||||||
cy.wait("@getSuggestions");
|
|
||||||
|
|
||||||
cy.contains(".corrections-caveat", "training_data.py").should("be.visible");
|
|
||||||
cy.contains(".suggestion-group h2", "simmer").should("be.visible");
|
|
||||||
cy.contains(".suggestion-card", "faire mijoter la sauce").should(
|
|
||||||
"contain.text",
|
|
||||||
"cook → simmer",
|
|
||||||
);
|
|
||||||
|
|
||||||
cy.contains(".suggestion-card button", "Appliquer").click();
|
|
||||||
cy.wait("@patch").its("request.body").should("deep.equal", { status: "applied" });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("generates a training_data.py snippet", () => {
|
|
||||||
cy.intercept("GET", "**/admin/tech-steps/training-data-snippet*", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
techStepKey: "simmer",
|
|
||||||
locale: "fr",
|
|
||||||
status: "applied",
|
|
||||||
suggestionCount: 2,
|
|
||||||
synonyms: ["frémir", "réduire"],
|
|
||||||
utterances: [],
|
|
||||||
snippet:
|
|
||||||
'# simmer (fr) — 2 suggestion(s) "applied"\n"synonyms": [\n "frémir",\n "réduire",\n],',
|
|
||||||
},
|
|
||||||
}).as("getSnippet");
|
|
||||||
|
|
||||||
cy.visit("/admin/corrections");
|
|
||||||
cy.get(".corrections-panel input").type("simmer");
|
|
||||||
cy.contains(".corrections-panel button", "Générer").click();
|
|
||||||
cy.wait("@getSnippet");
|
|
||||||
cy.get(".corrections-snippet").should("contain.value", '"synonyms": [');
|
|
||||||
});
|
|
||||||
|
|
||||||
it("runs the F1 gate and shows the result", () => {
|
|
||||||
cy.intercept("POST", "**/admin/tech-steps/retrain", {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
f1: 0.83,
|
|
||||||
precision: 0.8,
|
|
||||||
recall: 0.86,
|
|
||||||
minF1: 0.8,
|
|
||||||
gatePassed: true,
|
|
||||||
backfilled: { total: 120, changed: 4 },
|
|
||||||
marked: { applied: 0, rejected: 0 },
|
|
||||||
},
|
|
||||||
}).as("retrain");
|
|
||||||
|
|
||||||
cy.visit("/admin/corrections");
|
|
||||||
cy.contains(".corrections-panel--retrain button", "Lancer").click();
|
|
||||||
cy.wait("@retrain");
|
|
||||||
cy.contains(".retrain-result", "F1 0.830")
|
|
||||||
.should("have.class", "retrain-result--ok")
|
|
||||||
.and("contain.text", "4/120");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("lists raw corrections including the removals, on the second tab", () => {
|
|
||||||
cy.visit("/admin/corrections");
|
|
||||||
cy.contains(".corrections-tabs button", "Corrections brutes").click();
|
|
||||||
cy.wait("@getCorrections");
|
|
||||||
|
|
||||||
cy.get(".corrections-table tbody tr").should("have.length", 2);
|
|
||||||
cy.contains(".corrections-table tr", "Réserver au frais").should(
|
|
||||||
"contain.text",
|
|
||||||
"setAside → ∅",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,95 +0,0 @@
|
||||||
// Mocks the admin API via cy.intercept — no live backend.
|
|
||||||
|
|
||||||
const adminBody = {
|
|
||||||
id: 1,
|
|
||||||
email: "ops@example.com",
|
|
||||||
name: "Ops",
|
|
||||||
createdAt: "2026-08-01T00:00:00.000Z",
|
|
||||||
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
|
||||||
};
|
|
||||||
|
|
||||||
/** A 3-day series helper for the fixture. */
|
|
||||||
function series(counts: number[]) {
|
|
||||||
return counts.map((count, i) => ({
|
|
||||||
date: `2026-08-${String(10 + i).padStart(2, "0")}`,
|
|
||||||
count,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
function metricsFixture() {
|
|
||||||
return {
|
|
||||||
generatedAt: "2026-08-28T09:00:00.000Z",
|
|
||||||
rangeDays: 30,
|
|
||||||
snapshot: {
|
|
||||||
admins: 2,
|
|
||||||
users: 42,
|
|
||||||
households: 15,
|
|
||||||
activeHouseholds: 9,
|
|
||||||
recipes: 120,
|
|
||||||
recipesManual: 30,
|
|
||||||
recipesImported: 90,
|
|
||||||
recipesBySource: [
|
|
||||||
{ key: "themealdb", label: "TheMealDB", count: 60 },
|
|
||||||
{ key: "marmiton", label: "Marmiton", count: 30 },
|
|
||||||
],
|
|
||||||
plannings: 18,
|
|
||||||
planningItems: 210,
|
|
||||||
steps: 640,
|
|
||||||
detectedTechniques: 900,
|
|
||||||
favorites: 55,
|
|
||||||
corrections: 12,
|
|
||||||
correctionsUnconsumed: 4,
|
|
||||||
correctionsRemoval: 2,
|
|
||||||
trainingSuggestions: 8,
|
|
||||||
trainingSuggestionsByStatus: [{ key: "pending", label: "pending", count: 8 }],
|
|
||||||
trainingSuggestionsBySourceType: [{ key: "correction", label: "correction", count: 8 }],
|
|
||||||
},
|
|
||||||
series: {
|
|
||||||
signups: series([1, 3, 2]),
|
|
||||||
recipesCreated: series([0, 2, 1]),
|
|
||||||
planningItemsAdded: series([4, 1, 5]),
|
|
||||||
correctionsSubmitted: series([0, 0, 1]),
|
|
||||||
trainingSuggestions: series([0, 1, 0]),
|
|
||||||
},
|
|
||||||
events: [{ type: "user.signup", buckets: series([1, 3, 2]) }],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Admin dashboard", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
cy.viewport(1400, 900);
|
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("renders KPI tiles, a chart per series, and the breakdown lists", () => {
|
|
||||||
cy.intercept("GET", "**/admin/metrics*", { statusCode: 200, body: metricsFixture() }).as(
|
|
||||||
"getMetrics",
|
|
||||||
);
|
|
||||||
cy.visit("/admin");
|
|
||||||
cy.wait("@getMetrics").its("request.url").should("include", "days=30");
|
|
||||||
|
|
||||||
// KPI tiles — value + label.
|
|
||||||
cy.contains(".kpi-tile", "Utilisateurs").should("contain.text", "42");
|
|
||||||
cy.contains(".kpi-tile", "Recettes importées").should("contain.text", "90");
|
|
||||||
cy.contains(".kpi-tile", "Corrections à traiter").should("contain.text", "4");
|
|
||||||
|
|
||||||
// One chart card per instrumented series.
|
|
||||||
cy.get(".chart-card").should("have.length", 5);
|
|
||||||
cy.contains(".chart-card", "Inscriptions").should("contain.text", "6 sur 30 j");
|
|
||||||
|
|
||||||
// Breakdown lists.
|
|
||||||
cy.contains(".breakdown", "Recettes importées par source")
|
|
||||||
.should("contain.text", "TheMealDB")
|
|
||||||
.and("contain.text", "Marmiton");
|
|
||||||
cy.contains(".breakdown", "Évènements enregistrés").should("contain.text", "user.signup");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("shows an error state when the metrics request fails", () => {
|
|
||||||
cy.intercept("GET", "**/admin/metrics*", {
|
|
||||||
statusCode: 500,
|
|
||||||
body: { code: 5000, message: "x" },
|
|
||||||
});
|
|
||||||
cy.visit("/admin");
|
|
||||||
cy.contains("Impossible de charger").should("be.visible");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
import { Given } from "@badeball/cypress-cucumber-preprocessor";
|
|
||||||
|
|
||||||
// Admin-specific steps for `admin-login.feature`. Generic navigation / form
|
|
||||||
// steps ("I visit", "I fill in the … field", "I click the button", "I
|
|
||||||
// should see …") are reused from `support/step_definitions/common.steps.ts`;
|
|
||||||
// only the `/admin/*` API mocks live here. Every admin call is stubbed via
|
|
||||||
// `cy.intercept` — this suite never runs a live backend (apps/api's Mocha
|
|
||||||
// suite covers real `/admin/*` behaviour).
|
|
||||||
|
|
||||||
const adminBody = {
|
|
||||||
id: 1,
|
|
||||||
email: "ops@example.com",
|
|
||||||
name: "Ops",
|
|
||||||
createdAt: "2026-08-01T00:00:00.000Z",
|
|
||||||
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
|
||||||
};
|
|
||||||
|
|
||||||
Given("the admin session check returns unauthenticated", () => {
|
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 401, body: { code: 4011, message: "no" } });
|
|
||||||
});
|
|
||||||
|
|
||||||
Given("admin login fails with invalid credentials", () => {
|
|
||||||
cy.intercept("POST", "**/admin/auth/login", {
|
|
||||||
statusCode: 401,
|
|
||||||
body: { code: 4010, message: "Invalid email or password" },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
Given("admin login succeeds as {string}", (name: string) => {
|
|
||||||
const body = { ...adminBody, name, email: `${name.toLowerCase()}@example.com` };
|
|
||||||
cy.intercept("POST", "**/admin/auth/login", { statusCode: 200, body });
|
|
||||||
// After navigate("/admin"), RequireAdmin re-checks the session — from now
|
|
||||||
// on it must report authenticated (last matching intercept wins).
|
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body });
|
|
||||||
});
|
|
||||||
|
|
@ -1,95 +0,0 @@
|
||||||
// Mocks the admin API via cy.intercept — no live backend.
|
|
||||||
|
|
||||||
const adminBody = {
|
|
||||||
id: 1,
|
|
||||||
email: "ops@example.com",
|
|
||||||
name: "Ops",
|
|
||||||
createdAt: "2026-08-01T00:00:00.000Z",
|
|
||||||
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
|
||||||
};
|
|
||||||
|
|
||||||
function monitoringFixture() {
|
|
||||||
return {
|
|
||||||
generatedAt: "2026-08-28T09:15:00.000Z",
|
|
||||||
services: [
|
|
||||||
{
|
|
||||||
key: "postgres",
|
|
||||||
status: "up",
|
|
||||||
latencyMs: 3.2,
|
|
||||||
detail: null,
|
|
||||||
checkedAt: "2026-08-28T09:15:00.000Z",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "api",
|
|
||||||
status: "up",
|
|
||||||
latencyMs: 0,
|
|
||||||
detail: "uptime 3 h 12 min · RSS 120 Mo",
|
|
||||||
checkedAt: "2026-08-28T09:15:00.000Z",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "intent-service",
|
|
||||||
status: "down",
|
|
||||||
latencyMs: null,
|
|
||||||
detail: "fetch failed",
|
|
||||||
checkedAt: "2026-08-28T09:15:00.000Z",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "tech-step-llm-worker",
|
|
||||||
status: "degraded",
|
|
||||||
latencyMs: null,
|
|
||||||
detail: "dernier battement il y a 9 j",
|
|
||||||
checkedAt: "2026-08-28T09:15:00.000Z",
|
|
||||||
lastRunAt: "2026-08-19T03:00:00.000Z",
|
|
||||||
lastResult: { job: "audit-low-confidence", ok: true, counts: { suggestions: 2 } },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Admin monitoring", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
cy.viewport(1400, 900);
|
|
||||||
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
|
||||||
});
|
|
||||||
|
|
||||||
// The page route `/admin/monitoring` and the API path `GET /admin/monitoring`
|
|
||||||
// are identical — a plain `**/admin/monitoring` glob would also match the
|
|
||||||
// `cy.visit()` document request (and whether that's same-origin or not
|
|
||||||
// depends on `VITE_API_URL`, which isn't set in CI). `resourceType: "fetch"`
|
|
||||||
// pins the intercept to the `AdminApiClient` XHR only.
|
|
||||||
const monitoringApi = {
|
|
||||||
method: "GET",
|
|
||||||
url: "**/admin/monitoring",
|
|
||||||
resourceType: "fetch",
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
it("renders one card per service with its status and details", () => {
|
|
||||||
cy.intercept(monitoringApi, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: monitoringFixture(),
|
|
||||||
}).as("getMonitoring");
|
|
||||||
cy.visit("/admin/monitoring");
|
|
||||||
cy.wait("@getMonitoring");
|
|
||||||
|
|
||||||
cy.get(".monitoring-card").should("have.length", 4);
|
|
||||||
|
|
||||||
cy.contains(".monitoring-card", "Base de données")
|
|
||||||
.should("have.class", "monitoring-card--up")
|
|
||||||
.and("contain.text", "3.2 ms");
|
|
||||||
cy.contains(".monitoring-card", "Service NLP (spaCy)")
|
|
||||||
.should("have.class", "monitoring-card--down")
|
|
||||||
.and("contain.text", "Hors service");
|
|
||||||
cy.contains(".monitoring-card", "Worker LLM")
|
|
||||||
.should("have.class", "monitoring-card--degraded")
|
|
||||||
.and("contain.text", "audit-low-confidence");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("shows an error state when the request fails", () => {
|
|
||||||
cy.intercept(monitoringApi, {
|
|
||||||
statusCode: 500,
|
|
||||||
body: { code: 5000, message: "x" },
|
|
||||||
});
|
|
||||||
cy.visit("/admin/monitoring");
|
|
||||||
cy.contains("Impossible de charger").should("be.visible");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,174 +0,0 @@
|
||||||
// Mocks the API via cy.intercept — this job doesn't run a live backend (see
|
|
||||||
// .github/workflows/ci.yml); apps/api's own Mocha suite covers real
|
|
||||||
// `GET /cooking-session` behavior (including the optimizer) against a real
|
|
||||||
// database.
|
|
||||||
|
|
||||||
const authenticatedProfile = {
|
|
||||||
id: 1,
|
|
||||||
firstName: "Alice",
|
|
||||||
lastName: "Martin",
|
|
||||||
email: "alice@example.com",
|
|
||||||
tokenVersion: 0,
|
|
||||||
houseId: 1,
|
|
||||||
dietId: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
// 2026-08-17 is a Monday — frozen so "this week" is deterministic.
|
|
||||||
const TODAY = new Date("2026-08-17T09:00:00Z");
|
|
||||||
|
|
||||||
/** Bare `IngredientView` — only `key` drives the page's label lookup. */
|
|
||||||
function ingredient(key: string) {
|
|
||||||
return {
|
|
||||||
id: 1,
|
|
||||||
key,
|
|
||||||
icon: "VEGETABLE",
|
|
||||||
category: "freshProduce",
|
|
||||||
subcategory: "vegetables",
|
|
||||||
reproducible: false,
|
|
||||||
allergens: [],
|
|
||||||
diets: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A plan with a pooled prep task in mise-en-place and a passive cook floated into a later phase. */
|
|
||||||
function planFixture() {
|
|
||||||
return {
|
|
||||||
startDate: "2026-08-17T00:00:00.000Z",
|
|
||||||
finishDate: "2026-08-23T00:00:00.000Z",
|
|
||||||
recipes: [
|
|
||||||
{ recipeId: 1, name: "Soupe à l'oignon", portions: 4 },
|
|
||||||
{ recipeId: 2, name: "Tarte à l'oignon", portions: 4 },
|
|
||||||
],
|
|
||||||
phases: [
|
|
||||||
{
|
|
||||||
index: 0,
|
|
||||||
kind: "mise-en-place",
|
|
||||||
background: [],
|
|
||||||
tasks: [
|
|
||||||
{
|
|
||||||
id: "prep:chop:onion",
|
|
||||||
kind: "merged-prep",
|
|
||||||
technique: { id: 1, key: "chop" },
|
|
||||||
description: null,
|
|
||||||
ingredients: [
|
|
||||||
{
|
|
||||||
ingredient: ingredient("onion"),
|
|
||||||
quantity: 5,
|
|
||||||
unit: { id: 2, key: "piece", type: "COUNT", toBaseFactor: 1 },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
utensils: [{ id: 1, key: "knife" }],
|
|
||||||
sourceRecipes: [
|
|
||||||
{ recipeId: 1, name: "Soupe à l'oignon", portions: 4 },
|
|
||||||
{ recipeId: 2, name: "Tarte à l'oignon", portions: 4 },
|
|
||||||
],
|
|
||||||
originalSteps: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
index: 1,
|
|
||||||
kind: "cooking",
|
|
||||||
background: [],
|
|
||||||
tasks: [
|
|
||||||
{
|
|
||||||
id: "step:0:1",
|
|
||||||
kind: "step",
|
|
||||||
technique: { id: 5, key: "simmer" },
|
|
||||||
description: "Faire mijoter le bouillon",
|
|
||||||
ingredients: [],
|
|
||||||
utensils: [],
|
|
||||||
sourceRecipes: [{ recipeId: 1, name: "Soupe à l'oignon", portions: 4 }],
|
|
||||||
originalSteps: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
index: 2,
|
|
||||||
kind: "cooking",
|
|
||||||
background: [
|
|
||||||
{
|
|
||||||
id: "bg:step:0:1",
|
|
||||||
technique: { id: 5, key: "simmer" },
|
|
||||||
description: "Faire mijoter le bouillon",
|
|
||||||
recipeId: 1,
|
|
||||||
recipeName: "Soupe à l'oignon",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
tasks: [
|
|
||||||
{
|
|
||||||
id: "step:1:3",
|
|
||||||
kind: "step",
|
|
||||||
technique: { id: 4, key: "bake" },
|
|
||||||
description: "Enfourner la tarte",
|
|
||||||
ingredients: [],
|
|
||||||
utensils: [],
|
|
||||||
sourceRecipes: [{ recipeId: 2, name: "Tarte à l'oignon", portions: 4 }],
|
|
||||||
originalSteps: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Cooking session page", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
cy.viewport(1400, 900);
|
|
||||||
cy.clock(TODAY, ["Date"]);
|
|
||||||
cy.intercept("GET", "**/auth/me", { statusCode: 200, body: authenticatedProfile });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("shows the empty message when nothing is planned that week", () => {
|
|
||||||
cy.intercept("GET", /\/cooking-session\?/, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
startDate: "2026-08-17T00:00:00.000Z",
|
|
||||||
finishDate: "2026-08-23T00:00:00.000Z",
|
|
||||||
recipes: [],
|
|
||||||
phases: [],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
cy.visit("/cuisiner");
|
|
||||||
|
|
||||||
cy.contains("h1", "Cuisiner cette semaine").should("be.visible");
|
|
||||||
cy.contains("Rien de planifié cette semaine à cuisiner").should("be.visible");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("renders each phase, the pooled prep task, and the 'meanwhile' band", () => {
|
|
||||||
cy.intercept("GET", /\/cooking-session\?/, { statusCode: 200, body: planFixture() }).as(
|
|
||||||
"getPlan",
|
|
||||||
);
|
|
||||||
|
|
||||||
cy.visit("/cuisiner?date=2026-08-17");
|
|
||||||
cy.wait("@getPlan").its("request.url").should("include", "date=2026-08-17");
|
|
||||||
|
|
||||||
// Mise en place: one pooled prep task, flagged shared, naming both recipes.
|
|
||||||
cy.contains(".cooking-phase", "Mise en place").should("be.visible");
|
|
||||||
cy.get(".cooking-task--merged-prep")
|
|
||||||
.should("contain.text", "Hacher")
|
|
||||||
.and("contain.text", "Oignon")
|
|
||||||
.and("contain.text", "Mutualisé");
|
|
||||||
cy.contains(".cooking-task--merged-prep", "Soupe à l'oignon").should("exist");
|
|
||||||
|
|
||||||
// A later phase shows the simmering soup as still running in the background.
|
|
||||||
cy.contains(".cooking-phase__background", "Pendant ce temps")
|
|
||||||
.should("contain.text", "Faire mijoter le bouillon")
|
|
||||||
.and("contain.text", "Soupe à l'oignon");
|
|
||||||
|
|
||||||
// Recipe legend is present.
|
|
||||||
cy.contains(".cooking-session__legend-item", "Tarte à l'oignon").should("be.visible");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("shows an error state when the request fails", () => {
|
|
||||||
cy.intercept("GET", /\/cooking-session\?/, {
|
|
||||||
statusCode: 500,
|
|
||||||
body: { code: 5000, message: "boom" },
|
|
||||||
});
|
|
||||||
|
|
||||||
cy.visit("/cuisiner");
|
|
||||||
|
|
||||||
cy.contains("Impossible de charger").should("be.visible");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
Feature: Start cooking an optimized plan
|
|
||||||
As a member of a household with a planned week
|
|
||||||
I want to open an optimized cooking plan from my planning
|
|
||||||
So that shared preparation is pooled and I cook the week efficiently
|
|
||||||
|
|
||||||
Background:
|
|
||||||
Given I am signed in as "Alice" "Martin"
|
|
||||||
And my household id is 1
|
|
||||||
And today is frozen at "2026-08-17T09:00:00.000Z"
|
|
||||||
|
|
||||||
Scenario: The "Commencer à cuisiner" button is disabled while the week is empty
|
|
||||||
Given the planning request returns nothing
|
|
||||||
When I visit "/"
|
|
||||||
Then the "Commencer à cuisiner" button should be disabled
|
|
||||||
|
|
||||||
Scenario: Opening the plan from the planning shows the pooled prep in mise en place
|
|
||||||
Given the planning for this week has recipes "Soupe à l'oignon" and "Tarte à l'oignon"
|
|
||||||
And the cooking plan for "2026-08-17" pools "Hacher" of "Oignon" across both recipes
|
|
||||||
When I visit "/"
|
|
||||||
And I click the button "Commencer à cuisiner"
|
|
||||||
Then the URL should include "/cuisiner"
|
|
||||||
And I should see "Mise en place"
|
|
||||||
And the pooled prep task should mention "Hacher" and "Oignon"
|
|
||||||
And the pooled prep task should be flagged as shared
|
|
||||||
|
|
@ -1,101 +0,0 @@
|
||||||
import { Given, Then } from "@badeball/cypress-cucumber-preprocessor";
|
|
||||||
|
|
||||||
/** Bare `IngredientView` — only `key` drives the page's `catalog.ingredients.*` lookup. */
|
|
||||||
function ingredient(key: string) {
|
|
||||||
return {
|
|
||||||
id: 1,
|
|
||||||
key,
|
|
||||||
icon: "VEGETABLE",
|
|
||||||
category: "freshProduce",
|
|
||||||
subcategory: "vegetables",
|
|
||||||
reproducible: false,
|
|
||||||
allergens: [],
|
|
||||||
diets: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Given(
|
|
||||||
"the planning for this week has recipes {string} and {string}",
|
|
||||||
(first: string, second: string) => {
|
|
||||||
cy.intercept("GET", /\/planning\?/, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
id: 1,
|
|
||||||
startDate: "2026-08-17T00:00:00.000Z",
|
|
||||||
finishDate: "2026-08-23T00:00:00.000Z",
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
weekDay: "lundi",
|
|
||||||
meal: "dejeuner",
|
|
||||||
portions: 4,
|
|
||||||
recipe: { id: 1, name: first },
|
|
||||||
},
|
|
||||||
{ id: 2, weekDay: "mardi", meal: "diner", portions: 4, recipe: { id: 2, name: second } },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
Given(
|
|
||||||
"the cooking plan for {string} pools {string} of {string} across both recipes",
|
|
||||||
(date: string, _techniqueLabel: string, _ingredientLabel: string) => {
|
|
||||||
// The page composes the headline itself from the technique/ingredient
|
|
||||||
// *keys* via i18n — `chop`→"Hacher", `onion`→"Oignon" — so the fixture
|
|
||||||
// carries keys; the `Then` step checks the rendered French labels the
|
|
||||||
// feature line names.
|
|
||||||
cy.intercept("GET", `**/cooking-session?date=${date}`, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
startDate: `${date}T00:00:00.000Z`,
|
|
||||||
finishDate: "2026-08-23T00:00:00.000Z",
|
|
||||||
recipes: [
|
|
||||||
{ recipeId: 1, name: "Soupe à l'oignon", portions: 4 },
|
|
||||||
{ recipeId: 2, name: "Tarte à l'oignon", portions: 4 },
|
|
||||||
],
|
|
||||||
phases: [
|
|
||||||
{
|
|
||||||
index: 0,
|
|
||||||
kind: "mise-en-place",
|
|
||||||
background: [],
|
|
||||||
tasks: [
|
|
||||||
{
|
|
||||||
id: "prep:chop:onion",
|
|
||||||
kind: "merged-prep",
|
|
||||||
technique: { id: 1, key: "chop" },
|
|
||||||
description: null,
|
|
||||||
ingredients: [
|
|
||||||
{
|
|
||||||
ingredient: ingredient("onion"),
|
|
||||||
quantity: 5,
|
|
||||||
unit: { id: 2, key: "piece", type: "COUNT", toBaseFactor: 1 },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
utensils: [],
|
|
||||||
sourceRecipes: [
|
|
||||||
{ recipeId: 1, name: "Soupe à l'oignon", portions: 4 },
|
|
||||||
{ recipeId: 2, name: "Tarte à l'oignon", portions: 4 },
|
|
||||||
],
|
|
||||||
originalSteps: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
Then(
|
|
||||||
"the pooled prep task should mention {string} and {string}",
|
|
||||||
(techniqueLabel: string, ingredientLabel: string) => {
|
|
||||||
cy.get(".cooking-task--merged-prep")
|
|
||||||
.should("contain.text", techniqueLabel)
|
|
||||||
.and("contain.text", ingredientLabel);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
Then("the pooled prep task should be flagged as shared", () => {
|
|
||||||
cy.get(".cooking-task--merged-prep").contains("Mutualisé").should("be.visible");
|
|
||||||
});
|
|
||||||
|
|
@ -111,43 +111,6 @@ describe("Planning grid", () => {
|
||||||
cy.contains("th.today .day-date", "17").should("be.visible");
|
cy.contains("th.today .day-date", "17").should("be.visible");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("disables 'Commencer à cuisiner' on an empty week and enables + navigates it once a recipe is planned", () => {
|
|
||||||
cy.intercept("GET", /\/planning\?/, { statusCode: 200, body: null });
|
|
||||||
cy.visit("/");
|
|
||||||
cy.contains("button", "Commencer à cuisiner").should("be.disabled");
|
|
||||||
|
|
||||||
cy.intercept("GET", /\/planning\?/, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
id: 1,
|
|
||||||
startDate: "2026-08-17T00:00:00.000Z",
|
|
||||||
finishDate: "2026-08-23T00:00:00.000Z",
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
weekDay: "mardi",
|
|
||||||
meal: "diner",
|
|
||||||
portions: 4,
|
|
||||||
recipe: { id: 1, name: "Ratatouille" },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
cy.intercept("GET", /\/cooking-session\?/, {
|
|
||||||
statusCode: 200,
|
|
||||||
body: {
|
|
||||||
startDate: "2026-08-17T00:00:00.000Z",
|
|
||||||
finishDate: "2026-08-23T00:00:00.000Z",
|
|
||||||
recipes: [],
|
|
||||||
phases: [],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
cy.visit("/");
|
|
||||||
cy.contains("button", "Commencer à cuisiner").should("not.be.disabled").click();
|
|
||||||
cy.url().should("include", "/cuisiner");
|
|
||||||
cy.url().should("include", "date=2026-08-17");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("shows a loading state, then an error state when the request fails", () => {
|
it("shows a loading state, then an error state when the request fails", () => {
|
||||||
cy.intercept("GET", /\/planning\?/, {
|
cy.intercept("GET", /\/planning\?/, {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
|
|
|
||||||
|
|
@ -45,21 +45,6 @@ Feature: Recipe form — associating ingredients
|
||||||
And I add a step
|
And I add a step
|
||||||
Then there should be 2 step editor items
|
Then there should be 2 step editor items
|
||||||
|
|
||||||
Scenario: Adds an off-catalog ingredient as a free-text placeholder when nothing in the picker matches
|
|
||||||
Given creating the recipe will succeed and return id 43
|
|
||||||
When I visit "/recettes/nouvelle"
|
|
||||||
And I fill in the "recipe-name" field with "Poulet basquaise"
|
|
||||||
And I search the ingredient picker for "piment d'espelette"
|
|
||||||
Then the picker should offer to add "piment d'espelette" as a placeholder
|
|
||||||
When I add "piment d'espelette" as a placeholder ingredient
|
|
||||||
Then the recipe should include the placeholder ingredient "piment d'espelette"
|
|
||||||
When I fill in the ingredient's quantity with "1" and unit "unité"
|
|
||||||
And I add a step
|
|
||||||
And I fill in the step description with "Tout mélanger."
|
|
||||||
And I click the button "Enregistrer"
|
|
||||||
Then the recipe creation request should have included a placeholder ingredient "piment d'espelette" with quantity 1 and unitId 1
|
|
||||||
And the URL should include "/recettes/43"
|
|
||||||
|
|
||||||
Scenario: Excludes an already-selected ingredient from the picker, and removing it brings it back
|
Scenario: Excludes an already-selected ingredient from the picker, and removing it brings it back
|
||||||
When I visit "/recettes/nouvelle"
|
When I visit "/recettes/nouvelle"
|
||||||
And I select the ingredient "Carotte" from the picker
|
And I select the ingredient "Carotte" from the picker
|
||||||
|
|
|
||||||
|
|
@ -70,31 +70,6 @@ Then(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
Then("the picker should offer to add {string} as a placeholder", (name: string) => {
|
|
||||||
cy.get(".ingredient-picker__add-placeholder").should("contain.text", name);
|
|
||||||
});
|
|
||||||
|
|
||||||
When("I add {string} as a placeholder ingredient", () => {
|
|
||||||
// The search field already holds the query from the previous step, so the
|
|
||||||
// "add « … »" button carries the right name.
|
|
||||||
cy.get(".ingredient-picker__add-placeholder").click();
|
|
||||||
});
|
|
||||||
|
|
||||||
Then("the recipe should include the placeholder ingredient {string}", (name: string) => {
|
|
||||||
cy.contains(".ingredient-row__name", name)
|
|
||||||
.find(".ingredient-row__placeholder-badge")
|
|
||||||
.should("be.visible");
|
|
||||||
});
|
|
||||||
|
|
||||||
Then(
|
|
||||||
"the recipe creation request should have included a placeholder ingredient {string} with quantity {int} and unitId {int}",
|
|
||||||
(placeholderName: string, quantity: number, unitId: number) => {
|
|
||||||
cy.wait("@createRecipe")
|
|
||||||
.its("request.body.ingredients")
|
|
||||||
.should("deep.equal", [{ placeholderName, quantity, unitId }]);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
Given("recipe 7 exists with an egg omelette", () => {
|
Given("recipe 7 exists with an egg omelette", () => {
|
||||||
const existingRecipe = {
|
const existingRecipe = {
|
||||||
id: 7,
|
id: 7,
|
||||||
|
|
|
||||||
|
|
@ -273,10 +273,6 @@ Then("the {string} button should not be disabled", (text: string) => {
|
||||||
cy.contains("button", text).should("not.be.disabled");
|
cy.contains("button", text).should("not.be.disabled");
|
||||||
});
|
});
|
||||||
|
|
||||||
Then("the {string} button should be disabled", (text: string) => {
|
|
||||||
cy.contains("button", text).should("be.disabled");
|
|
||||||
});
|
|
||||||
|
|
||||||
When("I open the account menu", () => {
|
When("I open the account menu", () => {
|
||||||
cy.get(".app-sidebar__account-toggle").click();
|
cy.get(".app-sidebar__account-toggle").click();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@
|
||||||
"react-dom": "^18.3.1",
|
"react-dom": "^18.3.1",
|
||||||
"react-i18next": "^17.0.11",
|
"react-i18next": "^17.0.11",
|
||||||
"react-router-dom": "^7.18.2",
|
"react-router-dom": "^7.18.2",
|
||||||
"recharts": "^2.15.0",
|
|
||||||
"zod": "^3.25.76"
|
"zod": "^3.25.76"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,9 @@
|
||||||
import { Navigate, Outlet, Route, Routes } from "react-router-dom";
|
import { Navigate, Route, Routes } from "react-router-dom";
|
||||||
import { AdminAuthProvider } from "./features/admin/AdminAuthContext";
|
|
||||||
import { RequireAdmin } from "./features/admin/RequireAdmin";
|
|
||||||
import { RedirectIfAuthenticated } from "./features/auth/RedirectIfAuthenticated";
|
import { RedirectIfAuthenticated } from "./features/auth/RedirectIfAuthenticated";
|
||||||
import { RequireAuth } from "./features/auth/RequireAuth";
|
import { RequireAuth } from "./features/auth/RequireAuth";
|
||||||
import { AdminLayout } from "./layouts/AdminLayout";
|
|
||||||
import { AppLayout } from "./layouts/AppLayout";
|
import { AppLayout } from "./layouts/AppLayout";
|
||||||
import { CatalogPage as AdminCatalogPage } from "./pages/admin/catalog/CatalogPage";
|
|
||||||
import { CorrectionsPage as AdminCorrectionsPage } from "./pages/admin/corrections/CorrectionsPage";
|
|
||||||
import { DashboardPage as AdminDashboardPage } from "./pages/admin/dashboard/DashboardPage";
|
|
||||||
import { AdminLoginPage } from "./pages/admin/login/AdminLoginPage";
|
|
||||||
import { MonitoringPage as AdminMonitoringPage } from "./pages/admin/monitoring/MonitoringPage";
|
|
||||||
import { LoginPage } from "./pages/auth/LoginPage";
|
import { LoginPage } from "./pages/auth/LoginPage";
|
||||||
import { SignupPage } from "./pages/auth/SignupPage";
|
import { SignupPage } from "./pages/auth/SignupPage";
|
||||||
import { CookingSessionPage } from "./pages/cooking-session/CookingSessionPage";
|
|
||||||
import { OnboardingAllergensPage } from "./pages/onboarding/OnboardingAllergensPage";
|
import { OnboardingAllergensPage } from "./pages/onboarding/OnboardingAllergensPage";
|
||||||
import { OnboardingDietPage } from "./pages/onboarding/OnboardingDietPage";
|
import { OnboardingDietPage } from "./pages/onboarding/OnboardingDietPage";
|
||||||
import { OnboardingHouseholdPage } from "./pages/onboarding/OnboardingHouseholdPage";
|
import { OnboardingHouseholdPage } from "./pages/onboarding/OnboardingHouseholdPage";
|
||||||
|
|
@ -51,15 +42,6 @@ import { ShoppingListPage } from "./pages/shopping-list/ShoppingListPage";
|
||||||
* `/onboarding/sources` is conditional — only reached when the `foyer` step
|
* `/onboarding/sources` is conditional — only reached when the `foyer` step
|
||||||
* created/joined a household (see `OnboardingHouseholdPage`'s `goToNextStep`);
|
* created/joined a household (see `OnboardingHouseholdPage`'s `goToNextStep`);
|
||||||
* skipped otherwise, straight to `/onboarding/allergenes`.
|
* skipped otherwise, straight to `/onboarding/allergenes`.
|
||||||
*
|
|
||||||
* `/admin/*` is the internal admin surface (metrics, monitoring, correction
|
|
||||||
* triage, off-catalog ingredient review). It's its own top-level group,
|
|
||||||
* wrapped in {@link AdminAuthProvider} so the `GET /admin/auth/me` session
|
|
||||||
* probe only runs under `/admin` — the admin session (cookie `admin_session`,
|
|
||||||
* `ADMIN_JWT_SECRET`) is entirely separate from the end-user one, so this is
|
|
||||||
* never nested under `RequireAuth`. `/admin/login` is the only
|
|
||||||
* unauthenticated admin route; everything else sits under `RequireAdmin` +
|
|
||||||
* `AdminLayout`.
|
|
||||||
*/
|
*/
|
||||||
export function App() {
|
export function App() {
|
||||||
return (
|
return (
|
||||||
|
|
@ -83,7 +65,6 @@ export function App() {
|
||||||
<Route path="/recettes/:id" element={<RecipesPage />} />
|
<Route path="/recettes/:id" element={<RecipesPage />} />
|
||||||
<Route path="/recettes/:id/modifier" element={<RecipeFormPage />} />
|
<Route path="/recettes/:id/modifier" element={<RecipeFormPage />} />
|
||||||
<Route path="/liste-de-courses" element={<ShoppingListPage />} />
|
<Route path="/liste-de-courses" element={<ShoppingListPage />} />
|
||||||
<Route path="/cuisiner" element={<CookingSessionPage />} />
|
|
||||||
<Route path="/parametres/compte" element={<AccountSettingsPage />} />
|
<Route path="/parametres/compte" element={<AccountSettingsPage />} />
|
||||||
<Route path="/parametres/preferences" element={<PreferencesPage />} />
|
<Route path="/parametres/preferences" element={<PreferencesPage />} />
|
||||||
<Route path="/parametres/foyer" element={<HouseholdSettingsPage />} />
|
<Route path="/parametres/foyer" element={<HouseholdSettingsPage />} />
|
||||||
|
|
@ -139,28 +120,6 @@ export function App() {
|
||||||
</RedirectIfAuthenticated>
|
</RedirectIfAuthenticated>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Route
|
|
||||||
element={
|
|
||||||
<AdminAuthProvider>
|
|
||||||
<Outlet />
|
|
||||||
</AdminAuthProvider>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Route path="/admin/login" element={<AdminLoginPage />} />
|
|
||||||
<Route
|
|
||||||
path="/admin"
|
|
||||||
element={
|
|
||||||
<RequireAdmin>
|
|
||||||
<AdminLayout />
|
|
||||||
</RequireAdmin>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Route index element={<AdminDashboardPage />} />
|
|
||||||
<Route path="monitoring" element={<AdminMonitoringPage />} />
|
|
||||||
<Route path="corrections" element={<AdminCorrectionsPage />} />
|
|
||||||
<Route path="catalogue" element={<AdminCatalogPage />} />
|
|
||||||
</Route>
|
|
||||||
</Route>
|
|
||||||
<Route path="*" element={<Navigate to="/" replace />} />
|
<Route path="*" element={<Navigate to="/" replace />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,192 +0,0 @@
|
||||||
import {
|
|
||||||
type AdminLoginInput,
|
|
||||||
type AdminUserView,
|
|
||||||
type ApiErrorResponse,
|
|
||||||
type CatalogPlaceholderGroupView,
|
|
||||||
type CorrectionAdminView,
|
|
||||||
ErrorCode,
|
|
||||||
type MarkPlaceholdersReviewedInput,
|
|
||||||
type MetricsView,
|
|
||||||
type MonitoringView,
|
|
||||||
type PruneOrphansResultView,
|
|
||||||
type RetrainRequestInput,
|
|
||||||
type RetrainResultView,
|
|
||||||
type TrainingDataSnippetView,
|
|
||||||
type TrainingSuggestionAdminView,
|
|
||||||
type TrainingSuggestionGroupView,
|
|
||||||
type UpdateTrainingSuggestionInput,
|
|
||||||
} from "@batch-cooking/shared";
|
|
||||||
|
|
||||||
/** Builds a `?a=b&c=d` string from defined values only. */
|
|
||||||
function query(params: Record<string, string | undefined>): string {
|
|
||||||
const entries = Object.entries(params).filter(
|
|
||||||
(entry): entry is [string, string] => entry[1] !== undefined && entry[1] !== "",
|
|
||||||
);
|
|
||||||
return entries.length === 0 ? "" : `?${new URLSearchParams(entries).toString()}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base URL of the API — the same `VITE_API_URL` the user-facing
|
|
||||||
* {@link apiClient} reads (see `api/client.ts`). Defaults to `""` (same
|
|
||||||
* origin) — correct behind a shared reverse proxy; native dev overrides it
|
|
||||||
* to `http://localhost:3000` in `apps/web/.env`. The `/admin/*` surface is
|
|
||||||
* served by the same API process as the rest of the app.
|
|
||||||
*/
|
|
||||||
const ADMIN_API_BASE_URL: string = import.meta.env.VITE_API_URL ?? "";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thrown by {@link AdminApiClient} on any non-2xx response — carries the
|
|
||||||
* {@link ErrorCode} the API returned. Distinct from `api/client.ts`'s
|
|
||||||
* `ApiError` (same shape) so a file importing both never has a name clash;
|
|
||||||
* the admin transport layer stays independent of the user-facing one.
|
|
||||||
*/
|
|
||||||
export class AdminApiError extends Error {
|
|
||||||
public readonly status: number;
|
|
||||||
public readonly code: ErrorCode;
|
|
||||||
public readonly fieldErrors?: Record<string, string[] | undefined>;
|
|
||||||
|
|
||||||
public constructor(status: number, body: ApiErrorResponse) {
|
|
||||||
super(body.message);
|
|
||||||
this.name = "AdminApiError";
|
|
||||||
this.status = status;
|
|
||||||
this.code = body.code;
|
|
||||||
this.fieldErrors = body.details;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thin fetch wrapper around the `/admin/*` endpoints — same design as
|
|
||||||
* apps/web's `ApiClient` (a class for cohesion/extensibility, one shared
|
|
||||||
* stateless instance). Every request sends credentials so the
|
|
||||||
* `admin_session` httpOnly cookie round-trips.
|
|
||||||
*/
|
|
||||||
export class AdminApiClient {
|
|
||||||
/**
|
|
||||||
* Performs a JSON request against the admin API and returns the parsed body.
|
|
||||||
*
|
|
||||||
* @throws {AdminApiError} if the response status is not in the 2xx range.
|
|
||||||
*/
|
|
||||||
private async _request<TResponseBody>(
|
|
||||||
path: string,
|
|
||||||
options: RequestInit = {},
|
|
||||||
): Promise<TResponseBody> {
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${ADMIN_API_BASE_URL}${path}`, {
|
|
||||||
...options,
|
|
||||||
credentials: "include",
|
|
||||||
headers: { "Content-Type": "application/json", ...options.headers },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const body = (await response.json().catch(() => null)) as ApiErrorResponse | null;
|
|
||||||
throw new AdminApiError(
|
|
||||||
response.status,
|
|
||||||
body ?? { code: ErrorCode.INTERNAL_ERROR, message: "Something went wrong" },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.status === 204) {
|
|
||||||
return undefined as TResponseBody;
|
|
||||||
}
|
|
||||||
return (await response.json()) as TResponseBody;
|
|
||||||
} catch (err) {
|
|
||||||
// Rethrown as-is — callers surface it their own way; this is just the
|
|
||||||
// one place the fetch/`await` sits in a try/catch per the repo's rule.
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Verifies admin credentials and starts an admin session. */
|
|
||||||
public login(input: AdminLoginInput): Promise<AdminUserView> {
|
|
||||||
return this._request("/admin/auth/login", { method: "POST", body: JSON.stringify(input) });
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Ends the current admin session. */
|
|
||||||
public logout(): Promise<void> {
|
|
||||||
return this._request("/admin/auth/logout", { method: "POST" });
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Fetches the currently authenticated admin — rejects with `NOT_AUTHENTICATED` if there's no session. */
|
|
||||||
public me(): Promise<AdminUserView> {
|
|
||||||
return this._request("/admin/auth/me");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Usage metrics for the dashboard — snapshot totals + `days` (7–365) of daily time series. */
|
|
||||||
public getMetrics(days: number): Promise<MetricsView> {
|
|
||||||
return this._request(`/admin/metrics?days=${days}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Live health of Postgres, the API, the intent-service and the LLM worker — polled by the monitoring board. */
|
|
||||||
public getMonitoring(): Promise<MonitoringView> {
|
|
||||||
return this._request("/admin/monitoring");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Training suggestions, grouped by technique, filtered by the given (all-optional) criteria. */
|
|
||||||
public getSuggestions(filters: {
|
|
||||||
status?: string;
|
|
||||||
sourceType?: string;
|
|
||||||
techStepKey?: string;
|
|
||||||
locale?: string;
|
|
||||||
}): Promise<TrainingSuggestionGroupView[]> {
|
|
||||||
return this._request(`/admin/tech-steps/suggestions${query(filters)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Raw user corrections, including the "no technique here" removals. */
|
|
||||||
public getCorrections(filters: {
|
|
||||||
consumed?: string;
|
|
||||||
hasCorrectedTechStep?: string;
|
|
||||||
}): Promise<CorrectionAdminView[]> {
|
|
||||||
return this._request(`/admin/tech-steps/corrections${query(filters)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Edits a suggestion's proposed synonyms/utterances and/or its status. */
|
|
||||||
public updateSuggestion(
|
|
||||||
id: number,
|
|
||||||
body: UpdateTrainingSuggestionInput,
|
|
||||||
): Promise<TrainingSuggestionAdminView> {
|
|
||||||
return this._request(`/admin/tech-steps/suggestions/${id}`, {
|
|
||||||
method: "PATCH",
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The ready-to-paste `training_data.py` block aggregating suggestions for one technique/locale/status. */
|
|
||||||
public getTrainingDataSnippet(params: {
|
|
||||||
techStepKey: string;
|
|
||||||
locale?: string;
|
|
||||||
status?: string;
|
|
||||||
}): Promise<TrainingDataSnippetView> {
|
|
||||||
return this._request(`/admin/tech-steps/training-data-snippet${query(params)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Runs the F1 gate + backfill (+ marks suggestion ids). Rejects with `RETRAIN_ALREADY_RUNNING` if one is in flight. */
|
|
||||||
public retrain(body: RetrainRequestInput): Promise<RetrainResultView> {
|
|
||||||
return this._request("/admin/tech-steps/retrain", {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Off-catalog ingredient "placeholders" users typed, grouped by normalized name. `reviewed` omitted/`"false"` = the still-to-triage list, `"true"` = the archive. */
|
|
||||||
public getPlaceholders(reviewed?: "true" | "false"): Promise<CatalogPlaceholderGroupView[]> {
|
|
||||||
return this._request(`/admin/catalog/placeholders${query({ reviewed })}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Marks the given placeholder ingredient ids as triaged (`reviewedAt`). */
|
|
||||||
public markPlaceholdersReviewed(
|
|
||||||
body: MarkPlaceholdersReviewedInput,
|
|
||||||
): Promise<{ reviewed: number }> {
|
|
||||||
return this._request("/admin/catalog/placeholders/mark-reviewed", {
|
|
||||||
method: "PATCH",
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Deletes placeholder rows no recipe references any more. */
|
|
||||||
public pruneOrphanPlaceholders(): Promise<PruneOrphansResultView> {
|
|
||||||
return this._request("/admin/catalog/placeholders/prune-orphans", { method: "POST" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Single shared instance — stateless, same reasoning as apps/web's `apiClient`. */
|
|
||||||
export const adminApiClient = new AdminApiClient();
|
|
||||||
|
|
@ -9,7 +9,6 @@ import {
|
||||||
type HouseView,
|
type HouseView,
|
||||||
type IngredientView,
|
type IngredientView,
|
||||||
type LoginInput,
|
type LoginInput,
|
||||||
type OptimizedCookingPlanView,
|
|
||||||
type PlanningItemView,
|
type PlanningItemView,
|
||||||
type PlanningView,
|
type PlanningView,
|
||||||
type PreferencesView,
|
type PreferencesView,
|
||||||
|
|
@ -178,19 +177,6 @@ export class ApiClient {
|
||||||
return this._request(`/shopping-list?date=${date}`);
|
return this._request(`/shopping-list?date=${date}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches the current user's household's optimized cooking plan for the
|
|
||||||
* week covering `date` (`YYYY-MM-DD`) — every recipe planned that week
|
|
||||||
* reorganized into ordered phases (shared prep pooled, passive cooks
|
|
||||||
* floated into the background). Like {@link getShoppingListForWeek} and
|
|
||||||
* unlike {@link getPlanningForWeek}, never resolves to `null`: no
|
|
||||||
* household or nothing planned both come back as a normal plan with
|
|
||||||
* empty `recipes`/`phases`.
|
|
||||||
*/
|
|
||||||
public getCookingPlanForWeek(date: string): Promise<OptimizedCookingPlanView> {
|
|
||||||
return this._request(`/cooking-session?date=${date}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Reference list of dietary regimes to pick from (signup wizard, `/foyer`). Public — no session required. */
|
/** Reference list of dietary regimes to pick from (signup wizard, `/foyer`). Public — no session required. */
|
||||||
public getDiets(): Promise<DietView[]> {
|
public getDiets(): Promise<DietView[]> {
|
||||||
return this._request("/reference/diets");
|
return this._request("/reference/diets");
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ import { Dialog } from "../../components/ui/Dialog";
|
||||||
import { errorMessageService } from "../../services/error-message.service";
|
import { errorMessageService } from "../../services/error-message.service";
|
||||||
import { DietTagSelect } from "../recipes/badges/DietTagSelect";
|
import { DietTagSelect } from "../recipes/badges/DietTagSelect";
|
||||||
import { IngredientPicker } from "../recipes/ingredients/IngredientPicker";
|
import { IngredientPicker } from "../recipes/ingredients/IngredientPicker";
|
||||||
import { ingredientLabel } from "../recipes/ingredients/ingredient-label";
|
|
||||||
import { RecipeDetailPanel, type RecipeDetailState } from "../recipes/RecipeDetailPanel";
|
import { RecipeDetailPanel, type RecipeDetailState } from "../recipes/RecipeDetailPanel";
|
||||||
import { RecipeTable } from "../recipes/RecipeTable";
|
import { RecipeTable } from "../recipes/RecipeTable";
|
||||||
import {
|
import {
|
||||||
|
|
@ -448,7 +447,7 @@ export function RecipePickerDialog({
|
||||||
<div className="recipe-picker__chips">
|
<div className="recipe-picker__chips">
|
||||||
{selectedIngredients.map((ingredient) => (
|
{selectedIngredients.map((ingredient) => (
|
||||||
<span key={ingredient.id} className="filter-chip">
|
<span key={ingredient.id} className="filter-chip">
|
||||||
{ingredientLabel(ingredient, t)}
|
{t(`catalog.ingredients.${ingredient.key}`)}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ import { IngredientPicker } from "../recipes/ingredients/IngredientPicker";
|
||||||
// picker" visual family, even though this field lives in the profile
|
// picker" visual family, even though this field lives in the profile
|
||||||
// feature).
|
// feature).
|
||||||
import { IngredientTypeIcon } from "../recipes/ingredients/ingredient-icons";
|
import { IngredientTypeIcon } from "../recipes/ingredients/ingredient-icons";
|
||||||
import { ingredientLabel } from "../recipes/ingredients/ingredient-label";
|
|
||||||
import "../recipes/recipes.scss";
|
import "../recipes/recipes.scss";
|
||||||
import "./profile-forms.scss";
|
import "./profile-forms.scss";
|
||||||
|
|
||||||
|
|
@ -55,7 +54,7 @@ export function DislikedIngredientsField({
|
||||||
<span aria-hidden="true">
|
<span aria-hidden="true">
|
||||||
<IngredientTypeIcon icon={ingredient.icon} />
|
<IngredientTypeIcon icon={ingredient.icon} />
|
||||||
</span>
|
</span>
|
||||||
{ingredientLabel(ingredient, t)}
|
{t(`catalog.ingredients.${ingredient.key}`)}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => remove(ingredient.id)}
|
onClick={() => remove(ingredient.id)}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import { SourceLinkIcon } from "../../layouts/nav-icons";
|
||||||
import { errorMessageService } from "../../services/error-message.service";
|
import { errorMessageService } from "../../services/error-message.service";
|
||||||
import { AllergenBadges } from "./badges/AllergenBadges";
|
import { AllergenBadges } from "./badges/AllergenBadges";
|
||||||
import { FavoriteStarButton } from "./badges/FavoriteStarButton";
|
import { FavoriteStarButton } from "./badges/FavoriteStarButton";
|
||||||
import { ingredientLabel } from "./ingredients/ingredient-label";
|
|
||||||
import { StepDescription } from "./steps/StepDescription";
|
import { StepDescription } from "./steps/StepDescription";
|
||||||
import "./recipes.scss";
|
import "./recipes.scss";
|
||||||
|
|
||||||
|
|
@ -181,7 +180,7 @@ export function RecipeDetailPanel({
|
||||||
<ul className="disliked-badges">
|
<ul className="disliked-badges">
|
||||||
{dislikedIngredients.map((ingredient) => (
|
{dislikedIngredients.map((ingredient) => (
|
||||||
<li key={ingredient.id} className="disliked-badge">
|
<li key={ingredient.id} className="disliked-badge">
|
||||||
🚫 {ingredientLabel(ingredient, t)}
|
🚫 {t(`catalog.ingredients.${ingredient.key}`)}
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ import { AllergenBadges } from "../badges/AllergenBadges";
|
||||||
import { DietBadges } from "../badges/DietBadges";
|
import { DietBadges } from "../badges/DietBadges";
|
||||||
import { ReproducibleBadge } from "../badges/ReproducibleBadge";
|
import { ReproducibleBadge } from "../badges/ReproducibleBadge";
|
||||||
import { CategoryIcon, IngredientTypeIcon, SubcategoryIcon } from "./ingredient-icons";
|
import { CategoryIcon, IngredientTypeIcon, SubcategoryIcon } from "./ingredient-icons";
|
||||||
import { ingredientLabel } from "./ingredient-label";
|
|
||||||
import "../recipes.scss";
|
import "../recipes.scss";
|
||||||
|
|
||||||
/** "No filter at this level" — a UI-only pseudo-value, never sent to/received from the API (see {@link INGREDIENT_CATEGORIES}/{@link INGREDIENT_CATEGORY_SUBCATEGORIES} for the real, closed sets). */
|
/** "No filter at this level" — a UI-only pseudo-value, never sent to/received from the API (see {@link INGREDIENT_CATEGORIES}/{@link INGREDIENT_CATEGORY_SUBCATEGORIES} for the real, closed sets). */
|
||||||
|
|
@ -41,25 +40,15 @@ const ALL = "ALL" as const;
|
||||||
* just browsing/searching by name. A labeled checkbox menu rather than two
|
* just browsing/searching by name. A labeled checkbox menu rather than two
|
||||||
* bare icon-only toggle buttons — those turned out too ambiguous on their
|
* bare icon-only toggle buttons — those turned out too ambiguous on their
|
||||||
* own (unclear what each icon meant without a label attached).
|
* own (unclear what each icon meant without a label attached).
|
||||||
*
|
|
||||||
* `onAddPlaceholder`, when passed, turns the "aucun ingrédient trouvé"
|
|
||||||
* empty state into an escape hatch: with a non-empty search query, an
|
|
||||||
* "ajouter « … » comme ingrédient à compléter" button lets the user add a
|
|
||||||
* free-text line so the catalog gap doesn't block them (see
|
|
||||||
* `Ingredient.isPlaceholder` in schema.prisma). Omitted on pickers where
|
|
||||||
* that makes no sense — a profile's disliked-ingredients field, a search
|
|
||||||
* filter.
|
|
||||||
*/
|
*/
|
||||||
export function IngredientPicker({
|
export function IngredientPicker({
|
||||||
ingredients,
|
ingredients,
|
||||||
excludeIds,
|
excludeIds,
|
||||||
onSelect,
|
onSelect,
|
||||||
onAddPlaceholder,
|
|
||||||
}: {
|
}: {
|
||||||
ingredients: IngredientView[];
|
ingredients: IngredientView[];
|
||||||
excludeIds: number[];
|
excludeIds: number[];
|
||||||
onSelect: (ingredient: IngredientView) => void;
|
onSelect: (ingredient: IngredientView) => void;
|
||||||
onAddPlaceholder?: (name: string) => void;
|
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [category, setCategory] = useState<IngredientCategory | typeof ALL>(ALL);
|
const [category, setCategory] = useState<IngredientCategory | typeof ALL>(ALL);
|
||||||
|
|
@ -91,26 +80,17 @@ export function IngredientPicker({
|
||||||
// key — searching "œuf" should find "Œuf" the way it always has, not
|
// key — searching "œuf" should find "Œuf" the way it always has, not
|
||||||
// require typing its key.
|
// require typing its key.
|
||||||
if (normalizedQuery.length > 0) {
|
if (normalizedQuery.length > 0) {
|
||||||
const label = ingredientLabel(ingredient, t).toLowerCase();
|
const label = t(`catalog.ingredients.${ingredient.key}`).toLowerCase();
|
||||||
if (!label.includes(normalizedQuery)) return false;
|
if (!label.includes(normalizedQuery)) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
const trimmedQuery = query.trim();
|
|
||||||
|
|
||||||
function handleSelect(ingredient: IngredientView) {
|
function handleSelect(ingredient: IngredientView) {
|
||||||
onSelect(ingredient);
|
onSelect(ingredient);
|
||||||
setQuery("");
|
setQuery("");
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleAddPlaceholder() {
|
|
||||||
if (onAddPlaceholder && trimmedQuery.length > 0) {
|
|
||||||
onAddPlaceholder(trimmedQuery);
|
|
||||||
setQuery("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="ingredient-picker">
|
<div className="ingredient-picker">
|
||||||
<div className="ingredient-picker__search">
|
<div className="ingredient-picker__search">
|
||||||
|
|
@ -191,18 +171,7 @@ export function IngredientPicker({
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{visible.length === 0 ? (
|
{visible.length === 0 ? (
|
||||||
<div className="ingredient-picker__empty">
|
<p className="ingredient-picker__empty">{t("recipes.form.noIngredientFound")}</p>
|
||||||
<p>{t("recipes.form.noIngredientFound")}</p>
|
|
||||||
{onAddPlaceholder && trimmedQuery.length > 0 && (
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="ingredient-picker__add-placeholder"
|
|
||||||
onClick={handleAddPlaceholder}
|
|
||||||
>
|
|
||||||
{t("recipes.form.addPlaceholderButton", { name: trimmedQuery })}
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
) : (
|
) : (
|
||||||
<div className="ingredient-picker__grid">
|
<div className="ingredient-picker__grid">
|
||||||
{visible.map((ingredient) => (
|
{visible.map((ingredient) => (
|
||||||
|
|
@ -215,7 +184,9 @@ export function IngredientPicker({
|
||||||
<span className="ingredient-picker__card-icon" aria-hidden="true">
|
<span className="ingredient-picker__card-icon" aria-hidden="true">
|
||||||
<IngredientTypeIcon icon={ingredient.icon} />
|
<IngredientTypeIcon icon={ingredient.icon} />
|
||||||
</span>
|
</span>
|
||||||
<span className="ingredient-picker__card-name">{ingredientLabel(ingredient, t)}</span>
|
<span className="ingredient-picker__card-name">
|
||||||
|
{t(`catalog.ingredients.${ingredient.key}`)}
|
||||||
|
</span>
|
||||||
{showAllergens && <AllergenBadges allergens={ingredient.allergens} />}
|
{showAllergens && <AllergenBadges allergens={ingredient.allergens} />}
|
||||||
{showDiets && <DietBadges diets={ingredient.diets} />}
|
{showDiets && <DietBadges diets={ingredient.diets} />}
|
||||||
{showReproducible && <ReproducibleBadge reproducible={ingredient.reproducible} />}
|
{showReproducible && <ReproducibleBadge reproducible={ingredient.reproducible} />}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import { AllergenBadges } from "../badges/AllergenBadges";
|
||||||
import { DietBadges } from "../badges/DietBadges";
|
import { DietBadges } from "../badges/DietBadges";
|
||||||
import { ReproducibleBadge } from "../badges/ReproducibleBadge";
|
import { ReproducibleBadge } from "../badges/ReproducibleBadge";
|
||||||
import { IngredientTypeIcon } from "./ingredient-icons";
|
import { IngredientTypeIcon } from "./ingredient-icons";
|
||||||
import { ingredientLabel } from "./ingredient-label";
|
|
||||||
import "../recipes.scss";
|
import "../recipes.scss";
|
||||||
|
|
||||||
/** One selected ingredient line in the recipe form — the ingredient itself (picked via `IngredientPicker`) plus its quantity/unit for this recipe. Quantity is kept as a raw string while editing (not parsed to a number until submit) so an in-progress/invalid value doesn't fight the input; `unit` picks from `unitsCatalog` (a closed reference list, see `Unit` in schema.prisma) rather than free text. */
|
/** One selected ingredient line in the recipe form — the ingredient itself (picked via `IngredientPicker`) plus its quantity/unit for this recipe. Quantity is kept as a raw string while editing (not parsed to a number until submit) so an in-progress/invalid value doesn't fight the input; `unit` picks from `unitsCatalog` (a closed reference list, see `Unit` in schema.prisma) rather than free text. */
|
||||||
|
|
@ -37,25 +36,13 @@ export function IngredientRow({
|
||||||
// them, or why).
|
// them, or why).
|
||||||
const unitMissing = unitId === null;
|
const unitMissing = unitId === null;
|
||||||
const needsAttention = unitMissing || duplicate;
|
const needsAttention = unitMissing || duplicate;
|
||||||
// A free-text placeholder line (the catalog had nothing matching) — shown
|
|
||||||
// with an "à compléter" badge and none of the allergen/diet/reproducible
|
|
||||||
// badges, which carry no meaning until a maintainer promotes it to a real
|
|
||||||
// catalog entry. Its quantity/unit still work exactly like any other line.
|
|
||||||
const isPlaceholder = ingredient.isPlaceholder;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li className={needsAttention ? "ingredient-row ingredient-row--incomplete" : "ingredient-row"}>
|
<li className={needsAttention ? "ingredient-row ingredient-row--incomplete" : "ingredient-row"}>
|
||||||
<span className="ingredient-row__icon" aria-hidden="true">
|
<span className="ingredient-row__icon" aria-hidden="true">
|
||||||
<IngredientTypeIcon icon={ingredient.icon} />
|
<IngredientTypeIcon icon={ingredient.icon} />
|
||||||
</span>
|
</span>
|
||||||
<span className="ingredient-row__name">
|
<span className="ingredient-row__name">{t(`catalog.ingredients.${ingredient.key}`)}</span>
|
||||||
{ingredientLabel(ingredient, t)}
|
|
||||||
{isPlaceholder && (
|
|
||||||
<span className="ingredient-row__placeholder-badge">
|
|
||||||
{t("recipes.form.placeholderBadge")}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</span>
|
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
min="0"
|
min="0"
|
||||||
|
|
@ -81,16 +68,12 @@ export function IngredientRow({
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
{!isPlaceholder && (
|
|
||||||
<>
|
|
||||||
<AllergenBadges allergens={ingredient.allergens} />
|
<AllergenBadges allergens={ingredient.allergens} />
|
||||||
<DietBadges diets={ingredient.diets} />
|
<DietBadges diets={ingredient.diets} />
|
||||||
<ReproducibleBadge
|
<ReproducibleBadge
|
||||||
reproducible={ingredient.reproducible}
|
reproducible={ingredient.reproducible}
|
||||||
searchLabel={ingredientLabel(ingredient, t)}
|
searchLabel={t(`catalog.ingredients.${ingredient.key}`)}
|
||||||
/>
|
/>
|
||||||
</>
|
|
||||||
)}
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="ingredient-row__remove"
|
className="ingredient-row__remove"
|
||||||
|
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
import type { IngredientView } from "@batch-cooking/shared";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The display name for an ingredient line.
|
|
||||||
*
|
|
||||||
* A real catalog ingredient has no name in the database — its French label
|
|
||||||
* lives in i18n under `catalog.ingredients.<key>` — so it's resolved
|
|
||||||
* through `t`. A **placeholder** (`IngredientView.isPlaceholder`, the
|
|
||||||
* free text a user typed when the catalog fell short — see
|
|
||||||
* `Ingredient.isPlaceholder` in schema.prisma) has its name stored on the
|
|
||||||
* row as `displayName` and no i18n key at all, so it's shown verbatim.
|
|
||||||
*
|
|
||||||
* Every place that used to inline `t(\`catalog.ingredients.${ingredient.key}\`)`
|
|
||||||
* goes through this instead, so a placeholder never renders as the raw,
|
|
||||||
* missing translation key `catalog.ingredients.placeholder:<uuid>`.
|
|
||||||
*
|
|
||||||
* `t` is passed in (rather than calling `useTranslation` here) so this
|
|
||||||
* stays a plain function usable from non-component code and unit-testable
|
|
||||||
* without mounting i18next — same split as `shopping-list.ts`.
|
|
||||||
*/
|
|
||||||
export function ingredientLabel(
|
|
||||||
ingredient: Pick<IngredientView, "key" | "displayName">,
|
|
||||||
t: (key: string) => string,
|
|
||||||
): string {
|
|
||||||
return ingredient.displayName ?? t(`catalog.ingredients.${ingredient.key}`);
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue