fix: add option to ignore overlapping internships if they have the same ID

This commit is contained in:
Veronika Fehérvíziová
2025-10-31 22:43:59 +01:00
parent 107cf8a7ea
commit 5065b2ad8d

View File

@@ -183,10 +183,7 @@ class InternshipController extends Controller
} }
$this->validateNewInternship($request); $this->validateNewInternship($request);
$this->checkOverlap($user->id, $request->start, $request->end, $internship->id);
if($internship->start !== $request->start || $internship->end !== $request->end) {
$this->checkOverlap($user->id, $request->start, $request->end);
}
$internship->update($request->except(['user_id'])); $internship->update($request->except(['user_id']));
return response()->noContent(); 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) $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) { ->where(function ($query) use ($start_date, $end_date) {
$query->whereBetween('start', [$start_date, $end_date]) $query->whereBetween('start', [$start_date, $end_date])
->orWhereBetween('end', [$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.' 'message' => 'You already have an internship during this period.'
], 400)); ], 400));
} }
} }
} }