Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AIDAPP-422]: Introduce bulk service request action to change status #393

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ enum SystemServiceRequestClassification: string implements HasLabel, Classificat
case Closed = 'closed';

#[AllowTransitionTo(self::Open)]
#[AllowTransitionTo(self::Closed)]
#[AllowTransitionTo(self::InProgress)]
#[AllowTransitionTo(self::Closed)]
#[AllowTransitionTo(self::Custom)]
case Waiting = 'waiting';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

/*
<COPYRIGHT>

Copyright © 2016-2025, Canyon GBS LLC. All rights reserved.

Aiding App™ is licensed under the Elastic License 2.0. For more details,
see <https://github.com/canyongbs/aidingapp/blob/main/LICENSE.>

Notice:

- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Aiding App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.

For more information or inquiries please visit our website at
<https://www.canyongbs.com> or contact us via email at [email protected].

</COPYRIGHT>
*/

namespace AidingApp\ServiceManagement\Filament\Resources\ServiceRequestResource\Actions;

use AidingApp\ServiceManagement\Enums\SystemServiceRequestClassification;
use AidingApp\ServiceManagement\Models\ServiceRequest;
use AidingApp\ServiceManagement\Models\ServiceRequestStatus;
use App\Models\Authenticatable;
use App\Support\BulkProcessingMachine;
use Closure;
use Filament\Forms\Components\Select;
use Filament\Tables\Actions\BulkAction;
use Illuminate\Database\Eloquent\Collection;

class ChangeServiceRequestStatusBulkAction
{
public static function make(): BulkAction
{
return BulkAction::make('changeServiceRequestStatus')
->label('Change status')
->icon('heroicon-m-pencil-square')
->modalHeading('Bulk change service request statuses')
->modalWidth('md')
->form([
Select::make('statusId')
->label('New status')
->options(ServiceRequestStatus::query()->pluck('name', 'id'))
->exists(ServiceRequestStatus::class, 'id')
->required(),
])
->action(function (array $data, Collection $records) {
$records->loadMissing(['priority.type.managers', 'respondent', 'status']);

$user = auth()->user();
$isUserSuperAdmin = $user->hasRole(Authenticatable::SUPER_ADMIN_ROLE);
$canUserUpdateServiceRequest = $user->can('service_request.*.update');

BulkProcessingMachine::make($records->all())
->check(function (ServiceRequest $serviceRequest) use ($user, $canUserUpdateServiceRequest): ?Closure {
$user = auth()->user();

if (
$user->hasLicense($serviceRequest->respondent->getLicenseType()) &&
$canUserUpdateServiceRequest
) {
return null;
}

return function (int $count): string {
if ($count === 1) {
return '1 service request cannot be updated because you do not have permission.';
}

return "{$count} service requests cannot be updated because you do not have permission.";
};
})
->check(function (ServiceRequest $serviceRequest): ?Closure {
if ($serviceRequest?->status?->classification !== SystemServiceRequestClassification::Closed) {
return null;
}

return function (int $count): string {
if ($count === 1) {
return '1 service request is closed and cannot be edited.';
}

return "{$count} service requests are closed and cannot be edited.";
};
})
->check(function (ServiceRequest $serviceRequest) use ($user, $isUserSuperAdmin): ?Closure {
if ($isUserSuperAdmin) {
return null;
}

$team = $user->teams->first();

if ($serviceRequest?->priority?->type?->managers?->contains('id', $team?->getKey())) {
return null;
}

return function (int $count): string {
if ($count === 1) {
return "You don't have permission to update 1 service request because you're not a manager of its type.";
}

return "You don't have permission to update {$count} service requests because you're not a manager of their types.";
};
})
->process(function (ServiceRequest $serviceRequest) use ($data) {
$serviceRequest->status()->associate($data['statusId']);
$serviceRequest->save();
})
->sendNotification(function (int $successCount): string {
if (! $successCount) {
return 'No service request statuses have been updated.';
}

if ($successCount === 1) {
return '1 service request status has been updated.';
}

return "{$successCount} service request statuses have been updated.";
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use AidingApp\ServiceManagement\Enums\SlaComplianceStatus;
use AidingApp\ServiceManagement\Enums\SystemServiceRequestClassification;
use AidingApp\ServiceManagement\Filament\Resources\ServiceRequestResource;
use AidingApp\ServiceManagement\Filament\Resources\ServiceRequestResource\Actions\ChangeServiceRequestStatusBulkAction;
use AidingApp\ServiceManagement\Models\ServiceRequest;
use AidingApp\ServiceManagement\Models\ServiceRequestPriority;
use App\Filament\Tables\Columns\IdColumn;
Expand Down Expand Up @@ -166,6 +167,7 @@ public function table(Table $table): Table
])
->bulkActions([
BulkActionGroup::make([
ChangeServiceRequestStatusBulkAction::make(),
DeleteBulkAction::make()
->action(function ($records) {
$deletedRecordsCount = ServiceRequest::query()
Expand Down
157 changes: 157 additions & 0 deletions app/Support/BulkProcessingMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/*
<COPYRIGHT>

Copyright © 2016-2025, Canyon GBS LLC. All rights reserved.

Aiding App™ is licensed under the Elastic License 2.0. For more details,
see <https://github.com/canyongbs/aidingapp/blob/main/LICENSE.>

Notice:

- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Aiding App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.

For more information or inquiries please visit our website at
<https://www.canyongbs.com> or contact us via email at [email protected].

</COPYRIGHT>
*/

namespace App\Support;

use Closure;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Model;
use Throwable;

class BulkProcessingMachine
{
/** @var array<Model> */
protected array $records;

/** @var array<Closure> */
protected array $checks;

protected int $successCount;

protected int $exceptionsCount;

/** @var array<string> */
protected array $failureMessages = [];

public static function make(array $records): static
{
$static = app(static::class);
$static->records($records);

return $static;
}

public function check(Closure $callback): static
{
$this->checks[] = $callback;

return $this;
}

public function records(array $records): static
{
$this->records = $records;

return $this;
}

public function process(Closure $callback): static
{
$this->successCount = 0;
$this->exceptionsCount = 0;

$checkFailures = [];

foreach ($this->records as $record) {
foreach ($this->checks as $checkIndex => $check) {
$failureMessageCallback = $check($record);

if (! ($failureMessageCallback instanceof Closure)) {
continue;
}

$checkFailures[$checkIndex] ??= ['failureMessageCallback' => $failureMessageCallback, 'count' => 0];
$checkFailures[$checkIndex]['count']++;

continue 2;
}

try {
$callback($record);

$this->successCount++;
} catch (Throwable $exception) {
$this->exceptionsCount++;

report($exception);
}
}

$this->failureMessages = array_map(
fn (array $failure): string => ($failure['failureMessageCallback'] instanceof Closure)
? $failure['failureMessageCallback']($failure['count'])
: $failure['failureMessageCallback'],
$checkFailures,
);

return $this;
}

public function sendNotification(Closure $callback): void
{
$failureMessages = $this->failureMessages;

if ($this->exceptionsCount) {
$failureMessages[] = ($this->exceptionsCount === 1)
? 'There was an unknown failure.'
: "There were {$this->exceptionsCount} unknown failures.";
}

$notification = $callback($this->successCount, $failureMessages);

if (blank($notification)) {
return;
}

if (is_string($notification)) {
$notification = Notification::make()->title($notification);

if ($failureMessages) {
$notification->body(implode(' ', $failureMessages));
}
}

if (! $this->successCount) {
$notification->danger();
} elseif ($failureMessages) {
$notification->warning();
} else {
$notification->success();
}

$notification->send();
}
}