diff --git a/src/Domain/Operator/Projections/Operator.php b/src/Domain/Operator/Projections/Operator.php index 735e020d..4bbaeee6 100644 --- a/src/Domain/Operator/Projections/Operator.php +++ b/src/Domain/Operator/Projections/Operator.php @@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Query\Expression; use Spatie\EventSourcing\Projections\Projection; use TTBooking\TicketAllocator\Domain\Ticket\Projections\Ticket; use TTBooking\TicketAllocator\Models\OperatorTeam; @@ -82,6 +83,28 @@ protected function name(): Attribute return Attribute::get(fn ($value) => $value ?? $this->user->name); } + /** + * Not decrement(): `col - n` on an unsigned zero fails before any GREATEST() around it. + */ + public function decrementToZero(string $column, int $amount = 1): int|false + { + $this->{$column} = max(0, $this->{$column} - $amount); + + if ($this->fireModelEvent('updating') === false) { + return false; + } + + $wrapped = $this->getConnection()->getQueryGrammar()->wrap($column); + + return tap($this->setKeysForSaveQuery($this->newQueryWithoutScopes())->update([ + $column => new Expression("GREATEST($wrapped, $amount) - $amount"), + ]), function () use ($column) { + $this->syncChanges(); + $this->fireModelEvent('updated', false); + $this->syncOriginalAttributes($column); + }); + } + /** * @return BelongsTo */ diff --git a/src/Domain/Operator/Projectors/OperatorProjector.php b/src/Domain/Operator/Projectors/OperatorProjector.php index 2627ee60..49652432 100644 --- a/src/Domain/Operator/Projectors/OperatorProjector.php +++ b/src/Domain/Operator/Projectors/OperatorProjector.php @@ -104,8 +104,8 @@ public function onTicketBound(TicketEvents\TicketBound $event): void } $operator = $ticket->operator?->writeable(); - $operator?->decrement('bound_tickets'); - $operator?->decrement('total_complexity', $ticket->complexity); + $operator?->decrementToZero('bound_tickets'); + $operator?->decrementToZero('total_complexity', $ticket->complexity); $newOperator->increment('bound_tickets'); $newOperator->increment('total_complexity', $ticket->complexity); @@ -119,8 +119,15 @@ public function onTicketMetricsAdjusted(TicketEvents\TicketMetricsAdjusted $even return; } - $totalComplexity = max(0, $operator->total_complexity + $event->adjustments->complexity); - $operator->update(['total_complexity' => $totalComplexity]); + // Runs before TicketProjector (weight 5), so $ticket->complexity is still the old value. + // Apply the ticket's actual change: it is clamped at zero, the raw adjustment is not. + $delta = max(0, $ticket->complexity + $event->adjustments->complexity) - $ticket->complexity; + + if ($delta > 0) { + $operator->increment('total_complexity', $delta); + } elseif ($delta < 0) { + $operator->decrementToZero('total_complexity', -$delta); + } } public function onTicketUnbound(TicketEvents\TicketUnbound $event): void @@ -130,8 +137,8 @@ public function onTicketUnbound(TicketEvents\TicketUnbound $event): void return; } - $operator->decrement('bound_tickets'); - $operator->decrement('total_complexity', $ticket->complexity); + $operator->decrementToZero('bound_tickets'); + $operator->decrementToZero('total_complexity', $ticket->complexity); } public function onTicketClosed(TicketEvents\TicketClosed $event): void @@ -141,7 +148,7 @@ public function onTicketClosed(TicketEvents\TicketClosed $event): void return; } - $operator->decrement('bound_tickets'); - $operator->decrement('total_complexity', $ticket->complexity); + $operator->decrementToZero('bound_tickets'); + $operator->decrementToZero('total_complexity', $ticket->complexity); } }