# Changelog

All material changes to the application. Dates are IST.

## 2026-10-18 — Review remediation

Fixes for every blocking and high-severity finding in
[docs/CODE_REVIEW_2026-10-18.md](docs/CODE_REVIEW_2026-10-18.md).

### Access control

- **Department/TEAM/TERRITORY-scoped roles can write again.** `assertAccessible` now falls
  back to the record owner's org placement, which is what callers were already fetching.
  A Sales Manager could see a customer and still be denied "New quote"; every
  customer-anchored write (opportunity, quotation, order, invoice, contract, subscription,
  ticket) was affected.
- **`Opportunity.territoryId` added**, with a migration that backfills from the customer's
  owner. The missing column was a type error that broke `tsc` and left territory-scoped
  users unable to reach a deal.
- **Territory deletion** now refuses while opportunities still reference it.

### Money

- **Duplicate payment references rejected.** `(customerId, referenceNumber)` is unique; a
  repeat returns 409 naming the existing receipt instead of creating a second one.
- **A discounted quotation cannot convert without approval.** Order creation re-checks
  `approvedAt`, in the request and again inside the locked transaction.

### Data boundaries

- **Vendors no longer appear in the customer list.** Filtering on a JSON marker with `NOT`
  returned NULL for rows without JSON and excluded every real customer, so vendor-ness is
  now a real indexed `Customer.isVendor` column, backfilled.
- **Vendor list pagination** passed `total`, `page` and `pageSize` in the wrong order.
- **`GET /customers/:id`** added (only `/360` existed), scoped and field-filtered.

### Reliability

- **Queue health is visible.** `/health/ready` reports queued, running, failed and
  dead-letter counts plus the oldest queued age. `admin.jobs` can list, retry and purge;
  dead letters had no API surface at all while 1,264 sat in the table.
- **Mail can leave the container.** `SMTP_HOST` / `IMAP_HOST` overrides; compose points
  them at `mailpit`.
- **OAuth failure storm stopped.** Mail and calendar sync back off 15 minutes after a
  failure instead of retrying every five minutes with a permanently invalid token.

### Performance

- **Session resolution** no longer reads the entire user table on every request or walks it
  with a `filter` per node; one recursive query returns the reporting tree.
- **Sharing and field-security caches** moved from per-process memory to Redis with explicit
  invalidation, so instances no longer disagree for ten seconds.

### Reporting

- **Schema drift reconciled.** The migration history did not create `Invoice.publicToken`,
  `PaymentCheckout`, the WhatsApp tables or several columns, so `migrate deploy` on a fresh
  database produced a client that could not query it. 14 of 112 saved reports failed at run
  time. A reconcile migration was generated and applied; all 112 now run.
- **Unknown report columns fail loudly** instead of rendering a table with rows and no headers.
- CI now proves migrations reproduce the schema.

### Tooling

- `npm test` runs the 61 `*.check.ts` files and both static guards, not just `tsc --noEmit`.
- Three check scripts that never exited were fixed; a default chat model that resolved to
  an empty string was fixed.
- CI adds a MySQL service, migration reproducibility, the behavioural suite and guards.
- One package manager (npm); `pnpm-lock.yaml` and `pnpm-workspace.yaml` removed.
- `npm run dev` starts API, web and worker together.
- README versions and claims match the code; historical status documents archived.
- **Docker:** the `web` service had never been created in the local stack. It is now built and
  running on :5810 with `/api/v1` proxied to the API. The web image enforces typecheck and lint
  during build (`ignoreBuildErrors` / `ignoreDuringBuilds` removed).
- `/health/ready` degrades on dead letters from the last 24 hours, not on historical ones.
