From 2baa101a8fb619a7032c6ea6319ba559a3d6df28 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 26 Dec 2023 12:54:21 +0100 Subject: [PATCH] Random shit: Fix selection to exclude parallel shifts --- src/Controllers/ShiftsController.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Controllers/ShiftsController.php b/src/Controllers/ShiftsController.php index 7f25a07e..3459442d 100644 --- a/src/Controllers/ShiftsController.php +++ b/src/Controllers/ShiftsController.php @@ -10,7 +10,9 @@ use Engelsystem\Http\Response; use Engelsystem\Helpers\Authenticator; use Engelsystem\Http\UrlGeneratorInterface; use Engelsystem\Models\Shifts\Shift; +use Engelsystem\Models\Shifts\ShiftEntry; use Engelsystem\Models\User\User; +use Illuminate\Contracts\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Collection as DbCollection; use Illuminate\Database\Query\JoinClause; @@ -65,7 +67,8 @@ class ShiftsController extends BaseController ->where('angel_types.restricted', false) ->orWhereNot('confirm_user_id', false); }); - $shifts = $user->shiftEntries()->select('shift_id'); + /** @var ShiftEntry[]|DbCollection $shiftEntries */ + $shiftEntries = $user->shiftEntries()->with('shift')->get(); $freeShifts = Shift::query() ->select('shifts.*') @@ -80,7 +83,7 @@ class ShiftsController extends BaseController ->whereNotNull('schedule_shift.shift_id'); }) // Not already signed in - ->whereNotIn('shifts.id', $shifts) + ->whereNotIn('shifts.id', $shiftEntries->pluck('shift_id')) // Same angel types ->where(function (EloquentBuilder $query) use ($angelTypes): void { $query @@ -99,6 +102,13 @@ class ShiftsController extends BaseController ->limit(10) ->orderBy('start'); + foreach ($shiftEntries as $entry) { + $freeShifts->where(function (QueryBuilder $query) use ($entry) { + $query->where('end', '<=', $entry->shift->start); + $query->orWhere('start', '>=', $entry->shift->end); + }); + } + return $freeShifts->get(); }