Skip to content

Commit

Permalink
[AIDAPP-436]: Intermittent error occurs when clicking the "Assign to …
Browse files Browse the repository at this point in the history
…Me" button in service request assignments (#403)

* Refactor service request assignment validation to check user permissions directly

* Enhance service request assignment validation to ensure proper user permissions and prevent assignment to non-managers

* Fix service request assignment validation and enhance test coverage for status handling

* Enhance service request assignment validation to restrict assignments to authorized managers only
  • Loading branch information
ketan-canyon authored Feb 5, 2025
1 parent f5b6103 commit 6a53d4c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ public function table(Table $table): Table
->paginated(false)
->headerActions([
Action::make('assign-to-me')
->visible(fn () => auth()->user()->can('update', $this->getOwnerRecord()))
->visible(fn () => auth()->user()->can('update', $this->getOwnerRecord()) && is_null($this->getOwnerRecord()->assignedTo) && in_array(auth()->user()?->getKey(), $this->getOwnerRecord()->priority->type?->managers
->flatMap(fn ($managers) => $managers->users)
->pluck('id')
->toArray()))
->label('Assign To Me')
->color('gray')
->requiresConfirmation()
->action(fn (array $data) => $this->getOwnerRecord()->assignments()->create([
'user_id' => auth()->user()?->id,
'assigned_by_id' => auth()->user()->id ?? null,
'user_id' => auth()->user()?->getKey(),
'assigned_by_id' => auth()->user()->getKey() ?? null,
'assigned_at' => now(),
'status' => ServiceRequestAssignmentStatus::Active,
])),
Expand All @@ -96,7 +99,7 @@ public function table(Table $table): Table
->color('gray')
->action(fn (array $data) => $this->getOwnerRecord()->assignments()->create([
'user_id' => $data['userId'],
'assigned_by_id' => auth()->user()->id ?? null,
'assigned_by_id' => auth()->user()->getKey() ?? null,
'assigned_at' => now(),
'status' => ServiceRequestAssignmentStatus::Active,
]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@

$user = User::factory()->licensed([Contact::getLicenseType()])->create();

$user->givePermissionTo('service_request.*.update');

actingAs($user);

$team = Team::factory()->create();
Expand Down Expand Up @@ -348,6 +350,8 @@

$user = User::factory()->licensed([Contact::getLicenseType()])->create();

$user->givePermissionTo('service_request.*.update');

actingAs($user);

$team = Team::factory()->create();
Expand All @@ -370,12 +374,6 @@
])
->create();

$assignedServiceRequest = ServiceRequestAssignment::factory()->state([
'service_request_id' => $serviceRequestsWithManager->getKey(),
'user_id' => $user->getKey(),
])
->create();

livewire(AssignedToRelationManager::class, [
'ownerRecord' => $serviceRequestsWithManager,
'pageClass' => ManageAssignments::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
use AidingApp\Contact\Models\Contact;
use AidingApp\ServiceManagement\Enums\ServiceRequestAssignmentStatus;
use AidingApp\ServiceManagement\Enums\ServiceRequestTypeAssignmentTypes;
use AidingApp\ServiceManagement\Enums\SystemServiceRequestClassification;
use AidingApp\ServiceManagement\Filament\Resources\ServiceRequestResource;
use AidingApp\ServiceManagement\Filament\Resources\ServiceRequestResource\Pages\CreateServiceRequest;
use AidingApp\ServiceManagement\Models\ServiceRequest;
use AidingApp\ServiceManagement\Models\ServiceRequestPriority;
use AidingApp\ServiceManagement\Models\ServiceRequestStatus;
use AidingApp\ServiceManagement\Models\ServiceRequestType;
use AidingApp\ServiceManagement\Tests\RequestFactories\CreateServiceRequestRequestFactory;
use AidingApp\Team\Models\Team;
Expand Down Expand Up @@ -439,6 +441,7 @@

$user->givePermissionTo('service_request.view-any');
$user->givePermissionTo('service_request.create');
$user->givePermissionTo('service_request.*.update');

$team = Team::factory()->create();

Expand All @@ -460,6 +463,9 @@
->create();

$request = collect(CreateServiceRequestRequestFactory::new()->create([
'status_id' => ServiceRequestStatus::factory()->create([
'classification' => SystemServiceRequestClassification::Open,
])->getKey(),
'priority_id' => ServiceRequestPriority::factory()->create([
'type_id' => $serviceRequestTypesWithManager->getKey(),
])->getKey(),
Expand Down Expand Up @@ -494,6 +500,9 @@
->create();

$request = collect(CreateServiceRequestRequestFactory::new()->create([
'status_id' => ServiceRequestStatus::factory()->create([
'classification' => SystemServiceRequestClassification::Open,
])->getKey(),
'priority_id' => ServiceRequestPriority::factory()->create([
'type_id' => $serviceRequestTypeWithManager->getKey(),
])->getKey(),
Expand Down Expand Up @@ -534,6 +543,7 @@
test('assignment type workload will auto-assign to new service requests', function () {
asSuperAdmin();
$factoryUsers = User::factory()->licensed(LicenseType::cases())->count(5)->create();
$factoryUsers->each(fn ($user) => $user->givePermissionTo('service_request.*.update'));
$team = Team::factory()
->hasAttached($factoryUsers, [], 'users')->create();

Expand All @@ -552,6 +562,9 @@
'priority_id' => ServiceRequestPriority::factory()->create([
'type_id' => $serviceRequestTypeWithManager->getKey(),
])->getKey(),
'status_id' => ServiceRequestStatus::factory()->create([
'classification' => SystemServiceRequestClassification::Open,
])->getKey(),
])->create();
$serviceRequest->assignments()->create([
'user_id' => $factoryUser->getKey(),
Expand All @@ -563,6 +576,9 @@
'priority_id' => ServiceRequestPriority::factory()->create([
'type_id' => $serviceRequestTypeWithManager->getKey(),
])->getKey(),
'status_id' => ServiceRequestStatus::factory()->create([
'classification' => SystemServiceRequestClassification::Open,
])->getKey(),
])->create();
$serviceRequest->assignments()->create([
'user_id' => $factoryUser->getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@
use function Tests\asSuperAdmin;

test('The correct details are displayed on the ListServiceRequests page', function () {
asSuperAdmin();

$user = User::factory()->create();

$user->givePermissionTo('service_request.*.update');

$team = Team::factory()->create();

$user->teams()->attach($team);
Expand Down Expand Up @@ -86,8 +90,6 @@
->count(10)
->create();

asSuperAdmin();

$component = livewire(ListServiceRequests::class);

$component->assertSuccessful()
Expand Down Expand Up @@ -322,6 +324,8 @@

$user = User::factory()->create();

$user->givePermissionTo('service_request.*.update');

$team = Team::factory()->create();

$user->teams()->attach($team);
Expand All @@ -332,6 +336,8 @@

$serviceRequestType->managers()->attach($team);

asSuperAdmin();

$assignedRequest = ServiceRequest::factory()
->has(
factory: ServiceRequestAssignment::factory()
Expand All @@ -348,8 +354,6 @@
])
->create();

asSuperAdmin();

livewire(ListServiceRequests::class)
->assertCanSeeTableRecords([
$unassignedRequest,
Expand Down

0 comments on commit 6a53d4c

Please sign in to comment.