Skip to main content

Aggregate design canvas: SlotPlan

Synced from slotting-optimization

This page is a copy of docs/docs/ddd/aggregate-design-canvas.md on develop, derived from that repository's code. Edit it there, then re-sync.

ddd-crew Aggregate Design Canvas v1.1 for the only aggregate of the context, slotplan.SlotPlan (internal/domain/slotplan).

1. Name​

SlotPlan: one proposal of SKU-to-forward-slot assignments for one site and demand window, and the decision taken on it.

2. Description​

Created complete by Generate from the planner's result, then only its decision changes. Its content (assignments, moves, unassigned) is written once and never edited. It is identified by plan-<uuid> and belongs to one site.

3. State transitions​

Source: internal/domain/slotplan/plan.go (Generate, Approve, Reject, Supersede), internal/domain/slotplan/values.go (State).

4. Enforced invariants​

Checked by Restore, which Generate also goes through, so a corrupt row can never become a live aggregate.

InvariantError
Plan id is plan- plus a non-empty suffix; site, policy, SKUs and slot codes are 1 to 64 characters with no whitespace, control characters or /ErrInvalidPlanID, ErrInvalidSiteID, ErrInvalidPolicy, ErrInvalidSKU, ErrInvalidSlotCode
Window from before to, both setErrInvalidWindow
A SKU appears in at most one assignment; a slot holds at most one SKUErrDuplicateSKU, ErrDuplicateSlot
Every assignment has a valid ABC class, picks >= 1, units >= 0ErrInvalidABCClass, ErrInvalidMetric
Moves: one per SKU, a known kind; Assign has only to, equal to the SKU's slot; Relocate has different from and to, to equal to the SKU's slot; Vacate has only from and the SKU is not assignedErrInvalidMoveKind, ErrInvalidMove
Unassigned: one per SKU, a known reason, never an assigned SKUErrInvalidReason, ErrInvalidUnassigned
Each state has exactly one legal set of decision timestamps (Draft none, Approved approvedAt, Rejected rejectedAt, Superseded approvedAt and supersededAt); a reject reason only on Rejected; supersedes only on Approved or Superseded and never the plan itselfErrInvalidState, ErrInvalidSnapshot, ErrInvalidSupersedes
generatedAt set and version >= 1ErrInvalidSnapshot
Only a Draft can be approved or rejectedErrNotDraft (409 plan-not-draft)
Only an Approved plan can be supersededErrNotApproved
Reject reason, trimmed, at most 500 charactersErrRejectReasonTooLong

5. Corrective policies (rules that span aggregates)​

RuleWhere it is enforced
At most one Approved plan per siteApprovePlan supersedes and saves the previous Approved plan before approving the new one, in the same unit of work; the partial unique index uq_slot_plans_one_approved_per_site backs it; the loser of a race gets 409 approved-plan-conflict
Stickiness against the previous mapGeneratePlan reads the Approved plan's map and hands it to the planner as Previous

6. Handled commands and created events​

CommandPre-conditionEventPayload highlights
Generate(NewPlan, now)the proposal is validSlotPlanGeneratedwindow, policy, assignment, move and unassigned counts
Approve(supersedes, now)state DraftSlotPlanApprovedfull {sku, slot} map, moves, supersedes_plan_id
Reject(reason, now)state Draft, reason lengthSlotPlanRejectedreason
Supersede(now)state Approvednone(carried by the newer SlotPlanApproved)

Every event carries the plan id, site, aggregate version after the change and the time (slotplan.Header).

7. Throughput​

MetricEstimate
Command handling ratelow: a planner generates and decides a few plans per site per day or week
Total number of clientsa handful of planners, the console and an agent (none wired yet)
Concurrency conflict chancelow; the realistic one is two approvals of one site at once, handled by the version guard and the unique index

8. Size​

MetricEstimate
Event growth ratethree events per plan at most (generated, then approved or rejected)
Lifetimeforever: plans are the audit trail and are never deleted
Number of entitiesone row in slot_plans plus one row per assignment, move and unassigned SKU, bounded by the number of forward slots and candidate SKUs of the site

Design notes​

  • The plan is computed outside the aggregate by the pure planning.Planner; the aggregate only validates and records it. That keeps the policy replaceable (SlotRankingPolicy) without touching the aggregate.
  • Optimistic concurrency: SlotPlanRepository.Save(p, loadedVersion) updates only WHERE version = loadedVersion (409 concurrent-modification otherwise).
  • Assignments, moves and unassigned entries are stored sorted by SKU, so the same proposal always yields the same plan.