feat: complete approval workflow
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domain\Reservations\Models;
|
||||
|
||||
use App\Domain\Approvals\Models\ApprovalAction;
|
||||
use App\Domain\Reservations\Enums\ReservationStatus;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
@@ -57,4 +58,10 @@ class Reservation extends Model
|
||||
{
|
||||
return $this->hasMany(ReservationItem::class);
|
||||
}
|
||||
|
||||
/** @return HasMany<ApprovalAction, $this> */
|
||||
public function approvalActions(): HasMany
|
||||
{
|
||||
return $this->hasMany(ApprovalAction::class)->latest('created_at');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,19 @@
|
||||
|
||||
namespace App\Domain\Reservations\Services;
|
||||
|
||||
use App\Domain\Approvals\Models\ApprovalAction;
|
||||
use App\Domain\Audit\Models\AuditLog;
|
||||
use App\Domain\Identity\Enums\RoleName;
|
||||
use App\Domain\Inventory\Models\SnipeAsset;
|
||||
use App\Domain\Reservations\Enums\ReservationItemStatus;
|
||||
use App\Domain\Reservations\Enums\ReservationStatus;
|
||||
use App\Domain\Reservations\Models\Reservation;
|
||||
use App\Models\User;
|
||||
use App\Notifications\ReservationSubmittedNotification;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -26,7 +30,7 @@ class ReservationService
|
||||
$startsAt = CarbonImmutable::parse($data['starts_at']);
|
||||
$endsAt = CarbonImmutable::parse($data['ends_at']);
|
||||
|
||||
return DB::transaction(function () use ($requester, $data, $startsAt, $endsAt): Reservation {
|
||||
$reservation = DB::transaction(function () use ($requester, $data, $startsAt, $endsAt): Reservation {
|
||||
$assets = SnipeAsset::query()
|
||||
->whereKey($data['asset_ids'])
|
||||
->lockForUpdate()
|
||||
@@ -75,8 +79,19 @@ class ReservationService
|
||||
'metadata' => ['asset_ids' => $assets->modelKeys()],
|
||||
]);
|
||||
|
||||
return $reservation->load('items.asset');
|
||||
return $reservation->load('requester', 'items.asset');
|
||||
}, 3);
|
||||
|
||||
$approvers = User::query()
|
||||
->where('active', true)
|
||||
->whereHas('roles', fn ($query) => $query->whereIn('name', [
|
||||
RoleName::Approver->value,
|
||||
RoleName::Administrator->value,
|
||||
]))
|
||||
->get();
|
||||
Notification::send($approvers, new ReservationSubmittedNotification($reservation));
|
||||
|
||||
return $reservation;
|
||||
}
|
||||
|
||||
public function cancel(Reservation $reservation, User $actor, string $reason): Reservation
|
||||
@@ -124,6 +139,61 @@ class ReservationService
|
||||
}, 3);
|
||||
}
|
||||
|
||||
public function respondToInformation(Reservation $reservation, User $actor, string $response): Reservation
|
||||
{
|
||||
if ($reservation->requester_id !== $actor->id) {
|
||||
throw new AuthorizationException('Apenas o requisitante pode responder a este pedido.');
|
||||
}
|
||||
|
||||
$updated = DB::transaction(function () use ($reservation, $actor, $response): Reservation {
|
||||
$locked = Reservation::query()->whereKey($reservation->id)->lockForUpdate()->firstOrFail();
|
||||
if (! $locked->status->canTransitionTo(ReservationStatus::PendingApproval)
|
||||
|| $locked->status !== ReservationStatus::AwaitingInformation) {
|
||||
throw ValidationException::withMessages([
|
||||
'response' => 'Esta requisição não aguarda esclarecimentos.',
|
||||
]);
|
||||
}
|
||||
|
||||
$locked->update([
|
||||
'status' => ReservationStatus::PendingApproval,
|
||||
'approval_deadline' => now()->addHours(48),
|
||||
]);
|
||||
ApprovalAction::create([
|
||||
'reservation_id' => $locked->id,
|
||||
'actor_id' => $actor->id,
|
||||
'action' => 'information_provided',
|
||||
'previous_status' => ReservationStatus::AwaitingInformation->value,
|
||||
'new_status' => ReservationStatus::PendingApproval->value,
|
||||
'comment' => $response,
|
||||
]);
|
||||
AuditLog::create([
|
||||
'actor_id' => $actor->id,
|
||||
'event' => 'reservation.information_provided',
|
||||
'auditable_type' => $locked->getMorphClass(),
|
||||
'auditable_id' => $locked->id,
|
||||
'before' => ['status' => ReservationStatus::AwaitingInformation->value],
|
||||
'after' => ['status' => ReservationStatus::PendingApproval->value],
|
||||
'metadata' => ['response' => $response],
|
||||
]);
|
||||
|
||||
return $locked->load('requester', 'items.asset');
|
||||
}, 3);
|
||||
|
||||
$approvers = User::query()
|
||||
->where('active', true)
|
||||
->whereHas('roles', fn ($query) => $query->whereIn('name', [
|
||||
RoleName::Approver->value,
|
||||
RoleName::Administrator->value,
|
||||
]))
|
||||
->get();
|
||||
Notification::send(
|
||||
$approvers,
|
||||
new ReservationSubmittedNotification($updated, 'information_provided'),
|
||||
);
|
||||
|
||||
return $updated;
|
||||
}
|
||||
|
||||
public function expirePending(int $limit = 100): int
|
||||
{
|
||||
$reservationIds = Reservation::query()
|
||||
|
||||
Reference in New Issue
Block a user