crowdLists()->create($data); activity('crowd_list') ->causedBy($createdBy) ->performedOn($crowdList) ->withProperties(['attributes' => $crowdList->only($crowdList->getFillable())]) ->log('crowd_list.created'); return $crowdList; } public function update(CrowdList $crowdList, array $data, User $updatedBy): CrowdList { $oldValues = $crowdList->only(array_keys($data)); $crowdList->update($data); activity('crowd_list') ->causedBy($updatedBy) ->performedOn($crowdList) ->withProperties([ 'old' => $oldValues, 'new' => $crowdList->only(array_keys($data)), ]) ->log('crowd_list.updated'); return $crowdList->fresh(); } public function delete(CrowdList $crowdList, User $deletedBy): void { activity('crowd_list') ->causedBy($deletedBy) ->performedOn($crowdList) ->withProperties(['attributes' => $crowdList->only($crowdList->getFillable())]) ->log('crowd_list.deleted'); $crowdList->delete(); } /** * @throws ValidationException */ public function addPerson(CrowdList $crowdList, Person $person, User $addedBy): void { if ($crowdList->persons()->where('person_id', $person->id)->exists()) { throw ValidationException::withMessages([ 'person_id' => ['This person is already on this crowd list.'], ]); } if ($crowdList->max_persons !== null) { $currentCount = $crowdList->persons()->count(); if ($currentCount >= $crowdList->max_persons) { throw ValidationException::withMessages([ 'person_id' => ['This crowd list has reached its maximum capacity of ' . $crowdList->max_persons . ' persons.'], ]); } } $crowdList->persons()->attach($person->id, [ 'added_at' => now(), 'added_by_user_id' => $addedBy->id, ]); $wasAutoApproved = false; if ($crowdList->auto_approve && $person->status !== 'approved') { $person->update(['status' => 'approved']); $wasAutoApproved = true; } activity('crowd_list') ->causedBy($addedBy) ->performedOn($crowdList) ->withProperties([ 'person_id' => $person->id, 'person_name' => $person->name, 'auto_approved' => $wasAutoApproved, ]) ->log('crowd_list.person_added'); } public function removePerson(CrowdList $crowdList, Person $person, User $removedBy): void { $crowdList->persons()->detach($person->id); activity('crowd_list') ->causedBy($removedBy) ->performedOn($crowdList) ->withProperties([ 'person_id' => $person->id, 'person_name' => $person->name, ]) ->log('crowd_list.person_removed'); } }