diff --git a/README.md b/README.md index 2c24801..995d11e 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ Built with **Next.js, TypeScript, Node.js, Apollo Server, GraphQL, and PostgreSQ ## Learn the project +Start with the [build-from-zero lesson](docs/technical-manual.md#10-build-it-from-zero-a-teaching-sequence), then [build the React/CSS interface](docs/technical-manual.md#11-build-the-interface-from-layout-to-interaction) and [trace FR-1041 from click to SQL](docs/technical-manual.md#12-follow-fr-1041-from-a-click-to-sql-and-back). The manual includes exercises with executable solutions and explains the demo's limits. + - [Technical manual](docs/technical-manual.md): setup, architecture, contracts, failure labs, extension exercises, and interview answers. - [Product story](docs/product-story.md): the hypothetical workflow, intended value, limitations, and a 60–90 second demo script. diff --git a/docs/technical-manual.md b/docs/technical-manual.md index 6c08f05..5593cd2 100644 --- a/docs/technical-manual.md +++ b/docs/technical-manual.md @@ -2,6 +2,10 @@ This manual teaches the **Dispatch** demo from first run to maintenance. Read the [product story](product-story.md) for the problem it illustrates and the [interview walkthrough](INTERVIEW-WALKTHROUGH.md) for a shorter presentation. Source links below are the implementation, not claims about a production deployment. +**Start learning here:** [first lesson and build sequence](#10-build-it-from-zero-a-teaching-sequence), then [build the UI](#11-build-the-interface-from-layout-to-interaction), then [trace FR-1041 end to end](#12-follow-fr-1041-from-a-click-to-sql-and-back), and [practice with solutions](#13-hands-on-exercises-with-solutions). Sections 1–9 remain the reference manual for running, operating, and extending the project. + +This is a reconstruction of how to build the **current code**, not a claim about the historical order in which it was written. You can learn the design without assuming it was deployed in production. Use a separate checkout, unused port, and separate data directory for experiments. Do not reset a workspace someone is using for a live walkthrough. + ## 1. Vocabulary and first principles An **order** is a customer label, a sample destination, and one or more SKU/quantity lines. A **SKU** identifies a product. **On-hand** means physically counted stock in this model; **reserved** means promised to an allocated order. **Available** is `on_hand - reserved`. Allocation reserves stock; it does not purchase a label or ship goods. @@ -161,7 +165,7 @@ The audit failure test wraps the transaction adapter and throws at INSERT INTO a ### Diagnose symptoms -A 503 health response means database readiness failed; inspect the server log and connection/directory configuration first. A cross-origin 403 often means the browser/proxy origin does not match the effective server origin. Preserve the host consistently and check proxy configuration. A stale preview is expected under concurrent changes; route rechecks stock. A duplicate embedded server can lock or corrupt its files: stop duplicate processes and use a fresh data directory. Never delete a live shared database to fix a demo. +A 503 health response means database readiness failed; inspect the server log and connection/directory configuration first. A cross-origin 403 means the browser origin must be compared with the request protocol and actual Host, including port; see [request-origin.ts](../src/lib/request-origin.ts). Both `localhost` and `127.0.0.1` work when the browser uses the same spelling for the page and its request. They are not interchangeable origins. A stale preview is expected under concurrent changes; route rechecks stock. A duplicate embedded server can lock or corrupt its files: stop duplicate processes and use a fresh data directory. Never delete a live shared database to fix a demo. ## 8. Design choices and extension exercises @@ -187,3 +191,378 @@ A 503 health response means database readiness failed; inspect the server log an **What does the test suite prove?** Reproducible domain behavior, rollback, separate-connection contention in PostgreSQL, and browser flows. It does not prove arbitrary production load, real carrier correctness, or authenticated multitenancy. **What would you change first for production?** Establish real identity/data boundaries and external contracts, then normalized durable reservation ownership and migrations. Preserve the tested atomicity while adding operational limits; do not begin with cosmetic rewrites. + +## 10. Build it from zero: a teaching sequence + +### First lesson: separate a screen from a decision + +Start with a piece of paper. Draw two warehouse boxes and write “45 phones, 28 headsets” in Buffalo and “32 phones, 40 headsets” in Reno. Write FR-1041 beside them: four phones and two headsets to New York. Both boxes can cover the full order. Given quotes of $18.12 and $33.26, choose Buffalo. Cross out **available** 45/28 and replace it with 41/26. Do not change on-hand 45/28: the goods have been promised, not shipped. + +Now open three files side by side: + +1. [dashboard.tsx](../src/components/dashboard.tsx), the button with `act(() => routeOrder(selected.id), ...)`: what the person does. +2. [client.ts](../src/lib/client.ts), `routeOrder`: what the browser asks the server to do. +3. [routing.ts](../src/lib/routing.ts), `RoutingService.route`: what the server must verify before making a promise. + +The browser may display an old preview. The server must decide using current inventory. That distinction is the foundation of this project. You do not need to understand every React hook or SQL statement before understanding it. + +### Toolchain in plain language + +**JavaScript** runs the program. **TypeScript** adds types checked during development; those types disappear at runtime. **Node.js** runs JavaScript on the server. **React** turns state into interface elements. **Next.js** supplies the web server, file-based routes, React build pipeline, and production packaging. **npm** installs packages and runs the commands in package.json. + +**GraphQL** is the typed request language between browser and server. **Apollo Server** validates and executes that schema. It is not a separate deployed service here. **Zod** validates actual runtime input in the service, including callers that do not use GraphQL. **PostgreSQL** stores records and provides transactions; **pg** connects to a PostgreSQL server, while **PGlite** runs an embedded PostgreSQL engine. **Lucide React** supplies the icons. **Prettier** formats source. **tsx** lets Node execute the TypeScript tests. **Playwright** drives desktop and mobile browser tests. + +Use the checked-in [package.json](../package.json) and [package-lock.json](../package-lock.json), not a list of unpinned “latest” installs. The lockfile specifies resolved versions; `npm ci` reproduces them. Node must satisfy `>=22.13`; CI uses Node 22. A successful Node 24 local run does not replace that CI coverage. + +| Command | What it does | When to use it | +| ----------------------- | ---------------------------------------------------------------------------- | -------------------------------------------------------- | +| `npm ci` | Installs the lockfile's dependency tree | After cloning or dependency changes | +| `npm run dev` | Starts Next development mode | Editing and learning | +| `npm run build` | Compiles the production app and checks types | Before production-mode testing | +| `npm start` | Serves the existing production build | Stable local walkthrough | +| `npm run typecheck` | Runs TypeScript without emitting JS | Catch mismatched types | +| `npm test` | Executes `tests/*.test.ts` with Node + tsx | Domain, API, origin and embedded audit checks | +| `npm run check` | Typecheck followed by unit/service tests | Normal pre-commit check | +| `npm run test:postgres` | Runs server-database integration tests | Numeric audit history and separate-pool contention | +| `npm run test:e2e` | Starts an isolated production server and runs browser tests | UI/HTTP regression checks | +| `npm run format:check` | Checks formatting without edits | Before committing | +| `npm run format` | Applies Prettier to the project | Intentional formatting pass; inspect the diff | +| `npm run db:reset` | Deletes all sessions in the configured database after its confirmation guard | Disposable databases only; not a troubleshooting default | + +There is no separate lint script, CSS framework, ORM, or client-side state library in this project. + +### Repository map + +```text +src/app/layout.tsx HTML shell, page metadata, global CSS import +src/app/page.tsx / route: renders Dashboard +src/app/globals.css design tokens, layout, components, responsive rules +src/app/api/graphql/route.ts POST transport, cookie, Apollo execution +src/app/api/health/route.ts database readiness +src/components/dashboard.tsx all four views, dialogs, state, Metric helper +src/lib/model.ts types and catalog/destination/warehouse fixtures +src/lib/shipping.ts ShippingProvider interface and rate simulator +src/lib/database.ts SQL adapters and schema initialization +src/lib/seed.ts six initial orders and inventory +src/lib/routing.ts policy and transactional application service +src/lib/graphql.ts GraphQL schema, resolvers, limits and errors +src/lib/client.ts browser fetch and GraphQL operations +src/lib/request-origin.ts exact request-origin policy +tests/ service, audit, origin, PostgreSQL and browser tests +scripts/reset.ts explicit all-session reset utility +docs/ this manual, API, architecture, operations and walkthrough +``` + +[tsconfig.json](../tsconfig.json) enables strict TypeScript and maps `@/*` to `src/*`; this is why `@/lib/model` works. [next.config.ts](../next.config.ts) keeps pg/PGlite as server external packages and removes the X-Powered-By header. [playwright.config.ts](../playwright.config.ts) selects Chromium desktop and mobile profiles and uses `E2E_PORT`. [.github/workflows/ci.yml](../.github/workflows/ci.yml) provisions PostgreSQL 17 and runs formatting, types, tests, build, and browser checks. Build output, data, dependencies and environment files are ignored by [.gitignore](../.gitignore). + +### A sequence for rebuilding, with a checkpoint at each step + +Use a new learning folder; keep the working reference repository beside it. These are implementation milestones, not commands that automatically generate the complete product. + +1. **Create the skeleton.** Copy package.json, package-lock.json, tsconfig.json, next-env.d.ts and next.config.ts from the reference. Run `npm ci`. Add `src/app/layout.tsx`, `page.tsx` and a small globals.css. The layout returns `
{children}`; the page can initially render `