Skip to content

Commit 5bb1ed1

Browse files
committed
fix: deletion of other participants when editing project
1 parent 6311699 commit 5bb1ed1

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

app/Http/Controllers/ProjectController.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -241,40 +241,38 @@ public function update(Request $request, Project $project) {
241241
$project->saveOrFail();
242242
if (!$project->id or !$project->hasSubmittedClosure()) {
243243
// Don't update participants if closure is submitted
244-
foreach (['organizers' => 'organizer', 'staff' => 'staff', 'attendees' => 'attendee'] as $roleField => $role) {
245-
if ($request->filled($roleField)) {
246-
// Note: these lines of code suffers from n+1 performance issue
247-
$inputParticipants = new Collection($request->input($roleField, []));
248-
$newParticipants = new Collection();
249-
$users = User::whereIn('student_id', [...$inputParticipants->pluck('student_id'), ...$existingParticipants->pluck('student_id')])
250-
->get();
251-
foreach ($inputParticipants as $student) {
252-
// Add / edit existing
253-
if (!empty($student['student_id'])) {
254-
$user = $users->where('student_id', $student['student_id'])->first(); /* ?? User::firstOrCreate(['email' => $student['email']], [
255-
'name' => ($student['title'] ?? '') . $student['first_name'] . ' ' . $student['last_name'],
256-
'student_id' => $student['student_id'],
257-
]); */
258-
if ($participant = $existingParticipants->where('user_id', $user->id)->first()) {
259-
// Existing
260-
if ($participant->type != $role) {
261-
$participant->type = $role;
262-
$participant->save();
263-
}
264-
} else {
265-
$newParticipants->push(['user_id' => $user->id, 'type' => $role]);
244+
// Edit page only allows editing of organizers, not other types of participant.
245+
$roleField = 'organizers';
246+
$role = 'organizer';
247+
if ($request->filled($roleField)) {
248+
$inputParticipants = new Collection($request->input($roleField, []));
249+
$newParticipants = new Collection();
250+
$users = User::whereIn('student_id', [...$inputParticipants->pluck('student_id'), ...$existingParticipants->pluck('student_id')])
251+
->get();
252+
foreach ($inputParticipants as $student) {
253+
// Add / edit existing
254+
if (!empty($student['student_id'])) {
255+
$user = $users->firstWhere('student_id', $student['student_id']); /* ?? User::firstOrCreate(['email' => $student['email']], [
256+
'name' => ($student['title'] ?? '') . $student['first_name'] . ' ' . $student['last_name'],
257+
'student_id' => $student['student_id'],
258+
]); */
259+
if ($participant = $existingParticipants->where('user_id', $user->id)->first()) {
260+
// Existing
261+
if ($participant->type != $role) {
262+
$participant->type = $role;
263+
$participant->save();
266264
}
265+
} else {
266+
$newParticipants->push(['user_id' => $user->id, 'type' => $role]);
267267
}
268268
}
269-
if ($newParticipants->isNotEmpty()) {
270-
$project->participants()->createMany($newParticipants);
271-
}
272-
// Delete unused existing
273-
$existingParticipants->whereNotIn('user_id', $users->whereIn('student_id', $inputParticipants->pluck('student_id'))->pluck('id'))
274-
->where('type', $role)->each->delete();
275-
} elseif ($existingParticipants->isNotEmpty()) {
276-
$project->participants()->where('type', $role)->delete();
277269
}
270+
if ($newParticipants->isNotEmpty()) {
271+
$project->participants()->createMany($newParticipants);
272+
}
273+
// Delete unused existings
274+
$deleteIds = $users->whereIn('student_id', $inputParticipants->pluck('student_id'))->pluck('id');
275+
$project->participants()->whereNotIn('user_id', $deleteIds)->where('type', $role)->delete();
278276
}
279277
}
280278

0 commit comments

Comments
 (0)