4.5 KiB
Task D.5: Lead / Enquiry Domain Separation
Status
In progress
Objective
Replace the current single-record lead/enquiry persistence model with a split domain model that preserves marketing leads independently from sales enquiries.
Review Summary
Reviewed before implementation:
AGENTS.mddocs/standards/task-contract-template.mddocs/standards/task-review-checklist.mddocs/standards/task-catalog.mddocs/standards/project-foundations.mddocs/adr/0011-lead-enquiry-ownership-model.mddocs/adr/0016-won-lost-lifecycle-governance.mddocs/implementation/task-d3-lead-enquiry-ownership-refinement.mddocs/implementation/task-d31-leads-enquiries-navigation-separation.mddocs/implementation/task-d4-won-lost-lifecycle-governance.mddocs/implementation/task-k2-pipeline-reports.mddocs/security/crm-authorization-boundaries.mdsrc/db/schema.tssrc/features/crm/enquiries/server/service.ts
Historical Findings
Current production truth
The current production CRM still follows ADR-0011:
crm_enquiriesis the single source of truthpipeline_stagedistinguisheslead,enquiry,closed_won,closed_lost- assigning a lead mutates the same record into an enquiry
- lead workspace and enquiry workspace are only UX separation
Why D.5 is different
Task D.5 explicitly requires:
- dedicated
crm_leads crm_enquiries.lead_id1 Lead -> N Enquiries- direct sales enquiries with nullable
lead_id - dashboard/report split by entity rather than
pipeline_stage
This is not an incremental extension of ADR-0011. It is a domain replacement.
Governance Decision
Because Task D.5 conflicts with an accepted ADR, the first implementation step is to add a replacement ADR:
docs/adr/0018-lead-enquiry-domain-separation.md
This task should not continue with schema or API changes while ADR-0011 remains the only source of truth for this area.
Reuse Constraints
The split-domain refactor must still reuse existing foundations rather than rebuilding CRM from scratch:
- Audit Foundation for migration and lifecycle actions
- Master Data Foundation for lead awareness/status/follow-up/lost-reason options
- Document Sequence Foundation for lead and enquiry code generation
- Authorization Foundation for visibility and scope rules
- Dashboard Foundation for KPI composition
- Reporting Foundation for dataset and export contracts
- Won / Lost Governance Foundation for enquiry outcome transitions
Phase Plan
Phase 0
- freeze ADR-0018
- document the conflict with ADR-0011
- define migration boundaries
Phase 1
- add
crm_leads - add
crm_enquiries.lead_id - add lead master option categories
- add lead document sequence type
Phase 2
- create dedicated lead services and
/api/crm/leads/** - keep enquiry services focused on sales execution
- migrate assignment flow from
pipeline_stagemutation tolead -> create enquiry
Phase 3
- update dashboard datasets to separate lead/enquiry sources
- update report datasets and exports to separate lead/enquiry sources
- preserve quotation, approval, PDF, and pricing foundations
Phase 4
- migrate legacy rows from
crm_enquiries.pipeline_stage - preserve follow-ups, project-party relations, attachments, quotations, and audit history
- remove single-record assumptions from surviving UI and server code
Initial Risks
- this task affects schema, services, APIs, dashboard, reports, permissions, and migration logic at once
- existing lead reports and dashboard KPIs currently depend on
pipeline_stage - current enquiry services bundle lead creation defaults, assignment conversion, and won/lost logic together
- authorization rules for marketing monitoring are currently described against the shared enquiry table
Deliverables Started
- added
docs/adr/0018-lead-enquiry-domain-separation.md - added
docs/implementation/task-d5-lead-enquiry-domain-separation.md
Next Implementation Slice
The next safe code slice is:
- add schema support for
crm_leadsandcrm_enquiries.lead_id - seed new lead option categories and document sequence configuration
- keep existing enquiry routes live while introducing lead-specific route contracts
Verification Target
When code phases begin, verification must include:
npm exec tsc --noEmit- migration validation for existing
crm_enquiries - direct sales enquiry creation with
leadId = null - lead assignment creating enquiries without deleting the lead
- outcome synchronization from enquiry results back to lead outcome