32 lines
1.3 KiB
PHP
32 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Requisition;
|
|
use App\Models\User;
|
|
|
|
/** Read the existing approval queue; never assign authority or change the workflow. */
|
|
class ApprovalRouting
|
|
{
|
|
/** @return array<string,mixed> */
|
|
public function current(Requisition $requisition, User $viewer): array
|
|
{
|
|
abort_unless($viewer->tenant_id === $requisition->tenant_id, 404);
|
|
Requisition::query()->visibleTo($viewer)->findOrFail($requisition->id);
|
|
$step = $requisition->status === 'pending' ? $requisition->steps->firstWhere('decision', 'pending') : null;
|
|
if ($step === null) {
|
|
return ['status' => 'inactive', 'step_id' => null, 'role' => null, 'people' => [], 'checked_at' => now()->toIso8601String()];
|
|
}
|
|
$people = [];
|
|
foreach (User::query()->where('tenant_id', $requisition->tenant_id)->orderBy('name')->orderBy('id')->get() as $person) {
|
|
if (! app(OrganizationAccess::class)->maintenanceOnly($person) && $requisition->canDecide($person)) {
|
|
$people[] = $person->only(['id', 'name']);
|
|
}
|
|
}
|
|
|
|
return ['status' => $people === [] ? 'unavailable' : 'available', 'step_id' => $step->id, 'role' => $step->role,
|
|
'task_key' => 'requisition.approve.'.$step->role,
|
|
'people' => $people, 'checked_at' => now()->toIso8601String()];
|
|
}
|
|
}
|