Skip to content

Commit 43e1cf2

Browse files
RotationConfigForm: Don't increase priority when editing a rotation
The priority of existing rotations should only be increased when creating a new rotation
1 parent 2e0dab2 commit 43e1cf2

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

application/forms/RotationConfigForm.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,19 @@ public function loadRotation(int $rotationId): self
280280
/**
281281
* Insert a new rotation in the database
282282
*
283-
* @param int $priority The priority of the rotation
283+
* @param ?int $priority The priority of the rotation to add.
284+
*
285+
* If no priority is specified, the priority of all existing schedule rotations is increased by 1, and the
286+
* new rotation is added with the lowest priority (zero).
284287
*
285288
* @return Generator<int, DateTime> The first handoff of the rotation, as value
286289
*/
287-
private function createRotation(int $priority): Generator
290+
private function createRotation(int $priority = null): Generator
288291
{
289292
$data = $this->getValues();
290293
$data['options'] = Json::encode($data['options']);
291294
$data['schedule_id'] = $this->scheduleId;
292-
$data['priority'] = $priority;
295+
$data['priority'] = $priority ?? 0;
293296

294297
$members = array_map(function ($member) {
295298
return explode(':', $member, 2);
@@ -312,17 +315,19 @@ private function createRotation(int $priority): Generator
312315

313316
$changedAt = (int) (new DateTime())->format("Uv");
314317

315-
$rotationsToMove = Rotation::on($this->db)
316-
->columns('id')
317-
->filter(Filter::equal('schedule_id', $this->scheduleId))
318-
->orderBy('priority', SORT_DESC);
319-
320-
foreach ($rotationsToMove as $rotation) {
321-
$this->db->update(
322-
'rotation',
323-
['priority' => new Expression('priority + 1'), 'changed_at' => $changedAt],
324-
['id = ?' => $rotation->id]
325-
);
318+
if ($priority === null) {
319+
$rotationsToMove = Rotation::on($this->db)
320+
->columns('id')
321+
->filter(Filter::equal('schedule_id', $this->scheduleId))
322+
->orderBy('priority', SORT_DESC);
323+
324+
foreach ($rotationsToMove as $rotation) {
325+
$this->db->update(
326+
'rotation',
327+
['priority' => new Expression('priority + 1'), 'changed_at' => $changedAt],
328+
['id = ?' => $rotation->id]
329+
);
330+
}
326331
}
327332

328333
$data['changed_at'] = $changedAt;
@@ -395,7 +400,7 @@ public function addRotation(): void
395400
$transactionStarted = $this->db->beginTransaction();
396401
}
397402

398-
$this->createRotation(0)->send(true);
403+
$this->createRotation()->send(true);
399404

400405
if ($transactionStarted) {
401406
$this->db->commitTransaction();

0 commit comments

Comments
 (0)