feat: crowd lists audit, enum, factory, service and tests
Audit and complete the Crowd Lists module: - Add CrowdListType enum (internal/external) with proper casts - Create CrowdListService for business logic (add/remove person, max_persons enforcement, auto_approve, activity logging) - Create CrowdListFactory with Dutch names and states - Create AddPersonToCrowdListRequest form request - Fix FormRequests to use Rule::enum instead of hardcoded strings - Fix CrowdListResource to use enum->value and add is_full field - Refactor controller to be thin (delegates to service) - Add eager loading for crowdType and recipientCompany - Write 18 comprehensive tests (CRUD, auth, edge cases) - Update API.md with request/response documentation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
114
api/app/Services/CrowdListService.php
Normal file
114
api/app/Services/CrowdListService.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\CrowdList;
|
||||
use App\Models\Event;
|
||||
use App\Models\Person;
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class CrowdListService
|
||||
{
|
||||
public function create(Event $event, array $data, User $createdBy): CrowdList
|
||||
{
|
||||
$crowdList = $event->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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user