From 5065b2ad8decc1032b85652bc21410ed27d00fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veronika=20Feh=C3=A9rv=C3=ADziov=C3=A1?= <128744051+VeronikaFeherviziova@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:43:59 +0100 Subject: [PATCH] fix: add option to ignore overlapping internships if they have the same ID --- .../app/Http/Controllers/InternshipController.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/app/Http/Controllers/InternshipController.php b/backend/app/Http/Controllers/InternshipController.php index 7c32f5e..6d1ddde 100644 --- a/backend/app/Http/Controllers/InternshipController.php +++ b/backend/app/Http/Controllers/InternshipController.php @@ -183,10 +183,7 @@ class InternshipController extends Controller } $this->validateNewInternship($request); - - if($internship->start !== $request->start || $internship->end !== $request->end) { - $this->checkOverlap($user->id, $request->start, $request->end); - } + $this->checkOverlap($user->id, $request->start, $request->end, $internship->id); $internship->update($request->except(['user_id'])); return response()->noContent(); @@ -224,8 +221,13 @@ class InternshipController extends Controller ]); } - private function checkOverlap(int $user_id, string $start_date, string $end_date) { + private function checkOverlap(int $user_id, string $start_date, string $end_date, ?int $current_id = null) { $existingInternship = Internship::where('user_id', $user_id) + // check if the two internships do not have the same ID + ->when($current_id, function ($query) use ($current_id) { + $query->where('id', '!=', $current_id); + }) + // check if the start/end period collides with another internship ->where(function ($query) use ($start_date, $end_date) { $query->whereBetween('start', [$start_date, $end_date]) ->orWhereBetween('end', [$start_date, $end_date]) @@ -241,5 +243,5 @@ class InternshipController extends Controller 'message' => 'You already have an internship during this period.' ], 400)); } - } + } }