Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Domain/Operator/Projections/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Model, $this>
*/
Expand Down
23 changes: 15 additions & 8 deletions src/Domain/Operator/Projectors/OperatorProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}
Loading