3.4 KiB
3.4 KiB
Task D.5.2: Lead Domain Service & API Foundation
Objective
Create the first production Lead service and API slice on top of the D.5.1 lead foundation without changing legacy enquiry behavior.
Review Completed
AGENTS.mddocs/standards/project-foundations.mddocs/standards/task-catalog.mddocs/adr/0018-lead-enquiry-domain-separation.mddocs/implementation/task-d5-lead-enquiry-domain-separation.mddocs/implementation/task-d51-lead-foundation-stabilization.mdsrc/db/schema.tssrc/features/crm/enquiries/server/service.tssrc/features/crm/customers/server/service.tssrc/features/crm/security/server/service.tssrc/features/foundation/audit-log/service.tssrc/features/foundation/document-sequence/service.tssrc/features/foundation/master-options/service.ts
What Was Added
- Lead type contracts in types.ts
- Production lead service in service.ts
- Lead request schemas in lead.schema.ts
- Route handlers:
Service Behavior
crm_leadsis now the source of truth for lead CRUD- lead code generation reuses
generateNextDocumentCode()withdocumentType = crm_lead - lead detail returns
suggestedSalesOwnerIdfromcrm_customers.owner_user_idwhen a linked customer exists - route handlers do not query Drizzle directly
- access is enforced in the service layer through resolved CRM access converted into
CrmSecurityContext
Follow-up Persistence Decision
Current schema has:
crm_leadscrm_enquiry_followups- no
crm_lead_followups
Task D.5.2 also prohibits:
- new tables
- new migrations
Because of that, this slice implements lead follow-ups by reusing the audit foundation as the persistence stream for lead follow-up events:
- POST
/api/crm/leads/[id]/followupswritescreate_lead_followup - GET
/api/crm/leads/[id]/followupsreads those audit events back as lead follow-up history - the latest follow-up status is synchronized into
crm_leads.followup_status
This keeps D.5.2 inside its no-migration boundary while still making the API operational.
Audit Actions
This task records:
create_leadupdate_leaddelete_leadview_leadcreate_lead_followup
Intentional Non-Scope Preservation
This task did not change:
- enquiry CRUD
- enquiry assignment flow
- dashboard datasets
- reporting datasets
- won/lost synchronization
crm_enquiries.pipeline_stage- lead UI pages that still point at the legacy enquiry-based workspace
Verification
Planned verification target for this slice:
npm exec tsc --noEmit
Notes For D.5.3+
- lead UI still needs to migrate away from enquiry-backed components
- lead assignment and lead-to-enquiry conversion remain for later D.5 phases
- if future phases need richer editable follow-up history than audit-backed event replay, a dedicated ADR-backed persistence decision is still needed