Stages
π Overviewβ
internal/api/v1/crm/services/stage.service.js focuses on per-stage operations: listing stage+automation payloads, adding or updating stage definitions, ensuring stage-level safety during deletions, and aggregating deal totals when bulk edits run. The controllers authorize requests; this service validates pipeline access, interacts with the pipeline model helpers, recalculates deal counts, and formats responses for the dashboard.
ποΈ Collections Usedβ
π Full Schema: See Database Collections Documentation
crm.pipelineβ
- Operations: Fetch pipeline, call helper methods (
getStagesWithAutomations,addStage,updateStages,removeStage) - Model:
shared/models/pipeline.js - Usage Context: Guarantees account ownership before mutating stages
crm.pipeline.stagesβ
- Operations: Read/write via pipeline helper responses
- Model:
shared/models/pipeline-stage.js - Usage Context: Holds stage metadata, automation references, and ordering
crm.dealsβ
- Operations: Aggregations during bulk updates to compute counts and totals per stage
- Model:
shared/models/deal.js - Usage Context: Determines whether stages contain deals (deletion guard) and returns currency-adjusted totals
π Data Flowβ
flowchart TD
A[Stage request] --> B{Validate pipeline + account}
B -->|invalid| Z[400 INVALID_PIPELINE]
B --> C[pipeline helper (get/add/update/remove)]
C --> D[cleanRecord + hydrate automations]
D --> E[Optional deal aggregation]
E --> F[CurrencyUtil.convert totals]
F --> G[Response]
subgraph Delete Guard
H[Delete request] --> I[Find deals in stage]
I -->|has deals| Y[400 STAGE_NOT_EMPTY]
I -->|empty| J[pipeline.removeStage]
J --> G
end
π§ Business Logic & Functionsβ
Stage Retrievalβ
getStage({ id, account_id })β
- Loads pipeline stages and automations via
getStagesWithAutomations, converts each to plain objects withcleanRecord, and returns the hydrated list. - Returns a guarded error if no stages exist (a data anomaly) to prompt support intervention.
getOneStage({ id, stage_id, account_id })β
- Retrieves a single stage with its automations for edit forms; sanitizes the payload before returning and throws
RESOURCE_NOT_FOUNDwhen ids are invalid.
Creation & Updatesβ
postStage({ id, account_id, uid, body })β
- Validates pipeline scope and delegates to
pipeline.addStage, returning the cleaned stage plus hydrated automations.
putStage({ id, account_id, uid, dealFilter, dealQuery, body, currencyCode })β
- Performs bulk stage updates (rename, reorder, automation edits). After pipeline mutation it aggregates deals within the pipeline to calculate per-stage counts and currency-converted totals.
updateOneStage({ id, stage_id, account_id, uid, body })β
- Updates a single stage via
pipeline.updateStage, handles automation validation errors, and returns the cleaned stage.
Deletionβ
deleteOneStage({ id, stage_id, account_id })β
- Blocks deletion when any deals remain in the stage, returning
STAGE_NOT_EMPTYwith actionable guidance. - Executes
pipeline.removeStageonce safe, responding withSUCCESSorRESOURCE_NOT_FOUNDwhen the stage is missing.
π Integration Pointsβ
Internal Dependenciesβ
pipelinemodel helpers for all stage CRUDgenerateFilterObjto align stage bulk updates with deal filters from the UICurrencyUtilfor summarizing deal totals in the account currencystageUtils.cleanRecordto remove Mongo internals from responsesloggerto capture pipeline helper errors
External Servicesβ
- Noneβstage service only touches Mongo collections and shared utilities.
π§ͺ Edge Cases & Special Handlingβ
- Pipeline ownership: Every entry point validates
_id+account_idbefore running helpers. - Bulk update filters: When deal filters include search terms, the service rebuilds regex matchers to align with deal list behaviour.
- Currency totals: Deal totals are recalculated after every bulk update so UI widgets remain accurate.
- Automation validation:
INVALID_AUTOMATION_UPDATEbubbles through untouched so the UI can refresh stale payloads.
β οΈ Important Notesβ
- π§± Stage deletions are irreversibleβensure clients warn users before calling
deleteOneStage. - π Always refresh pipeline data after bulk updates; ordering and totals may shift.
- π Respect visibility rules upstream. Stage service assumes the controller already validated user scope.