@@ -280,16 +280,19 @@ public function loadRotation(int $rotationId): self
280
280
/**
281
281
* Insert a new rotation in the database
282
282
*
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).
284
287
*
285
288
* @return Generator<int, DateTime> The first handoff of the rotation, as value
286
289
*/
287
- private function createRotation (int $ priority ): Generator
290
+ private function createRotation (int $ priority = null ): Generator
288
291
{
289
292
$ data = $ this ->getValues ();
290
293
$ data ['options ' ] = Json::encode ($ data ['options ' ]);
291
294
$ data ['schedule_id ' ] = $ this ->scheduleId ;
292
- $ data ['priority ' ] = $ priority ;
295
+ $ data ['priority ' ] = $ priority ?? 0 ;
293
296
294
297
$ members = array_map (function ($ member ) {
295
298
return explode (': ' , $ member , 2 );
@@ -312,17 +315,19 @@ private function createRotation(int $priority): Generator
312
315
313
316
$ changedAt = (int ) (new DateTime ())->format ("Uv " );
314
317
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
+ }
326
331
}
327
332
328
333
$ data ['changed_at ' ] = $ changedAt ;
@@ -395,7 +400,7 @@ public function addRotation(): void
395
400
$ transactionStarted = $ this ->db ->beginTransaction ();
396
401
}
397
402
398
- $ this ->createRotation (0 )->send (true );
403
+ $ this ->createRotation ()->send (true );
399
404
400
405
if ($ transactionStarted ) {
401
406
$ this ->db ->commitTransaction ();
0 commit comments