@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/** Narrow scheduled deadline execution, never a login or authority to approve. */
|
||||
final class RequisitionExpiryRun
|
||||
{
|
||||
private ?int $tenant = null;
|
||||
|
||||
private ?int $request = null;
|
||||
|
||||
private ?int $owner = null;
|
||||
|
||||
public function tenantId(): ?int
|
||||
{
|
||||
return $this->tenant;
|
||||
}
|
||||
|
||||
public function isActor(User $actor): bool
|
||||
{
|
||||
return $this->tenant !== null && ! auth()->check() && $actor->tenant_id === $this->tenant && $actor->id === $this->owner;
|
||||
}
|
||||
|
||||
public function assertRequest(int $id): void
|
||||
{
|
||||
abort_unless($this->request === $id, 403);
|
||||
}
|
||||
|
||||
public function lock(User $actor): User
|
||||
{
|
||||
abort_unless($this->isActor($actor) && DB::transactionLevel() > 0, 403);
|
||||
$source = DB::table('requisitions')->where('tenant_id', $this->tenant)->where('id', $this->request)->lockForUpdate()->firstOrFail();
|
||||
abort_unless($source->requester_id === $this->owner, 403);
|
||||
|
||||
return User::query()->where('tenant_id', $this->tenant)->whereKey($this->owner)->lockForUpdate()->firstOrFail();
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function evidence(): array
|
||||
{
|
||||
return ['mode' => 'scheduled_approval_deadline', 'tenant_id' => $this->tenant, 'requisition_id' => $this->request,
|
||||
'requester_id' => $this->owner, 'attribution' => 'request_owner_for_context_only; not_an_interactive_action'];
|
||||
}
|
||||
|
||||
public function execute(int $id, callable $callback): mixed
|
||||
{
|
||||
abort_unless(app(RequisitionExpiry::class)->ready() && app()->runningInConsole() && ! auth()->check()
|
||||
&& ! request()->hasSession() && $this->tenant === null && app(ReplenishmentRun::class)->tenantId() === null, 403);
|
||||
$source = DB::table('requisitions')->where('id', $id)->where('status', 'pending')->firstOrFail();
|
||||
$this->tenant = $source->tenant_id;
|
||||
$this->request = $source->id;
|
||||
$this->owner = $source->requester_id;
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
$this->tenant = $this->request = $this->owner = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user