# Checkpoint 337 — Indexed CRM construction-code matching (2026-10-27)

**Story:** BE-003 — Configure company trades, regions and tender thresholds (**PARTIAL**)

## Change

Replaced the scouting path's in-memory scan of every authorized company CRM lead with relational `any` domains over a normalized Odoo projection (`be.crm.lead.code`). The projection is derived from the existing JSON values on CRM leads, stores case-folded search keys, and has a composite `(company_id, code_kind, normalized_code)` index. Existing JSON remains the editable source of truth. Create/write synchronize the projection; unlink cascades; batched post-migration backfills existing CRM leads.

Odoo 20 company-scoped `ir.access` domains restrict direct projection reads to `company_ids`; the scouting relational subquery also explicitly filters the active company. The change removes the Python-side scan and keeps result filtering, counting and paging in PostgreSQL. This is a query-path improvement; no large-volume benchmark has been run.

## Verification

- Clean isolated Odoo install and focused mandate/code tests: **2/2 passed**, including direct denial of a foreign-company code-index row. Disposable database dropped; source database unchanged. See [native QA](CRM_SCOUTING_NATIVE_QA.json).
- Live React → BFF → Odoo workflow: **1/1 passed**. React-created HVAC/Fitout lead was indexed and returned; wrong-trade, wrong-category and disallowed-source leads were excluded. Native readback verified the saved fields. Disposable database and temporary services removed; source database unchanged. See [live QA](CRM_SCOUTING_LIVE_QA.json).
- Upgrade/backfill QA from a clone of `namaa_ksa`: **passed**. A tagged pre-existing CRM lead acquired both expected normalized projection rows during the `.4` → `.6` upgrade. Clone dropped; original database unchanged. See [migration QA](CRM_CODE_INDEX_UPGRADE_QA.json).
- Local demo `namaa_ksa` was upgraded after a validated mode-0600 backup. Installed versions are `be_tender` **20.0.1.0.6** and `namaa_ai_bridge` **20.0.1.0.42**. Service health and React/Odoo HTTP smoke checks passed. The shared demo contained no tagged CRM leads, so the migration added no projection rows there and changed no business records.
- `git diff --check` passed. The previous Checkpoint 336 React unit/browser/build results remain the most recent full frontend test run; no frontend source changed in this increment.

## Remaining acceptance

BE-003 remains PARTIAL pending complete Owner/company A1–A4 acceptance and official tender-feed integration. Performance under production-scale lead and code volumes remains unbenchmarked.
