3.4 KiB
3.4 KiB
Task D.5.3: Lead Assignment -> Create Enquiry Foundation
Objective
Establish the first production lead-assignment flow that preserves crm_leads as the
marketing-owned source record while creating or reusing a linked crm_enquiries record for the
sales execution workspace.
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-d52-lead-domain-service-api-foundation.mddocs/security/crm-authorization-boundaries.mdsrc/features/crm/leads/server/service.tssrc/features/crm/enquiries/server/service.tssrc/features/crm/customers/server/service.tssrc/features/foundation/audit-log/service.tssrc/features/foundation/document-sequence/service.ts
What Changed
- Added lead bootstrap and assignment fields to
crm_leads:descriptionproduct_typepriorityestimated_valueassigned_sales_owner_idassigned_atassigned_byassignment_remark
- Added assignment.service.ts
with:
assignLead()- internal
createEnquiryFromLead()
- Added route.ts
for
POST /api/crm/leads/[id]/assign - Extended lead DTOs and schemas so new lead records can carry the data needed to bootstrap an enquiry
- Added
assignedto seededcrm_lead_status - Tightened enquiry-owner validation so assignees must be sales-assignable members, not merely any organization member
Assignment Behavior
- route permission is
crm.lead.assign - service validates:
- lead exists and is active
- actor can access the lead through resolved CRM scope
- assignee belongs to the same organization and is a sales-assignable member
- if an active enquiry already exists for the lead:
- no second enquiry is created
- the existing enquiry is returned
- lead assignment metadata is synchronized
- if no active enquiry exists:
- enquiry code generation reuses
generateNextDocumentCode()withdocumentType = crm_enquiry - enquiry is created with
lead_id = crm_leads.id - enquiry owner compatibility fields use the existing enquiry model:
assigned_to_user_idassigned_atassigned_byassignment_remark
- enquiry code generation reuses
Lead -> Enquiry Bootstrap Mapping
When creating the enquiry from a lead, the flow copies:
customerIdcontactIddescriptionprojectNameprojectLocationproductTypeestimatedValuepriority
The enquiry title is derived from lead.projectName and falls back to Lead <code> when the
project name is empty.
Audit Actions
This task records:
assign_leadoncrm_leadcreate_enquiry_from_leadoncrm_enquiry
Captured linkage includes:
leadIdenquiryIdsalesOwnerId
Compatibility Notes
This task intentionally does not change:
- quotation flows
- approval flows
- dashboard datasets
- reporting datasets
- won/lost lifecycle flows
- legacy enquiry list/detail APIs outside the stricter assignee validation
crm_enquiries.pipeline_stage remains in place for compatibility, but lead assignment now creates a
dedicated enquiry record instead of mutating a lead record in place.