feat: complete approval workflow
This commit is contained in:
@@ -9,8 +9,10 @@ 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\Domain\Reservations\Models\ReservationItem;
|
||||
use App\Domain\Reservations\Services\AvailabilityService;
|
||||
use App\Models\User;
|
||||
use App\Notifications\ReservationDecisionNotification;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -23,7 +25,7 @@ class ApprovalService
|
||||
{
|
||||
$this->authorize($actor);
|
||||
|
||||
return DB::transaction(function () use ($reservation, $actor, $comment): Reservation {
|
||||
$approved = DB::transaction(function () use ($reservation, $actor, $comment): Reservation {
|
||||
$locked = $this->lockPending($reservation);
|
||||
$items = $locked->items()->with('asset')->get();
|
||||
SnipeAsset::query()->whereKey($items->pluck('snipe_asset_id'))->orderBy('id')->lockForUpdate()->get();
|
||||
@@ -51,13 +53,17 @@ class ApprovalService
|
||||
|
||||
return $locked->load('requester', 'items.asset');
|
||||
}, 3);
|
||||
|
||||
$approved->requester->notify(new ReservationDecisionNotification($approved, 'approved', $comment));
|
||||
|
||||
return $approved;
|
||||
}
|
||||
|
||||
public function reject(Reservation $reservation, User $actor, string $reason): Reservation
|
||||
{
|
||||
$this->authorize($actor);
|
||||
|
||||
return DB::transaction(function () use ($reservation, $actor, $reason): Reservation {
|
||||
$rejected = DB::transaction(function () use ($reservation, $actor, $reason): Reservation {
|
||||
$locked = $this->lockPending($reservation);
|
||||
$this->transition($locked, $actor, ReservationStatus::Rejected, 'rejected', $reason, [
|
||||
'rejected_by' => $actor->id,
|
||||
@@ -70,13 +76,17 @@ class ApprovalService
|
||||
|
||||
return $locked->load('requester', 'items.asset');
|
||||
}, 3);
|
||||
|
||||
$rejected->requester->notify(new ReservationDecisionNotification($rejected, 'rejected', $reason));
|
||||
|
||||
return $rejected;
|
||||
}
|
||||
|
||||
public function requestInformation(Reservation $reservation, User $actor, string $comment): Reservation
|
||||
{
|
||||
$this->authorize($actor);
|
||||
|
||||
return DB::transaction(function () use ($reservation, $actor, $comment): Reservation {
|
||||
$awaitingInformation = DB::transaction(function () use ($reservation, $actor, $comment): Reservation {
|
||||
$locked = $this->lockPending($reservation);
|
||||
$this->transition($locked, $actor, ReservationStatus::AwaitingInformation, 'information_requested', $comment, [
|
||||
'decision_reason' => $comment,
|
||||
@@ -85,6 +95,81 @@ class ApprovalService
|
||||
|
||||
return $locked->load('requester', 'items.asset');
|
||||
}, 3);
|
||||
|
||||
$awaitingInformation->requester->notify(
|
||||
new ReservationDecisionNotification($awaitingInformation, 'information_requested', $comment)
|
||||
);
|
||||
|
||||
return $awaitingInformation;
|
||||
}
|
||||
|
||||
public function substituteAsset(
|
||||
Reservation $reservation,
|
||||
ReservationItem $item,
|
||||
SnipeAsset $replacement,
|
||||
User $actor,
|
||||
string $reason,
|
||||
): Reservation {
|
||||
$this->authorize($actor);
|
||||
|
||||
$updated = DB::transaction(function () use ($reservation, $item, $replacement, $actor, $reason): Reservation {
|
||||
$locked = $this->lockPending($reservation);
|
||||
$lockedItem = ReservationItem::query()->whereKey($item->id)->lockForUpdate()->firstOrFail();
|
||||
if ($lockedItem->reservation_id !== $locked->id || $lockedItem->status !== ReservationItemStatus::Held) {
|
||||
throw ValidationException::withMessages(['replacement_asset_id' => 'O item já não pode ser substituído.']);
|
||||
}
|
||||
|
||||
$assets = SnipeAsset::query()
|
||||
->whereKey([$lockedItem->snipe_asset_id, $replacement->id])
|
||||
->orderBy('id')
|
||||
->lockForUpdate()
|
||||
->get()
|
||||
->keyBy('id');
|
||||
$original = $assets->get($lockedItem->snipe_asset_id);
|
||||
$newAsset = $assets->get($replacement->id);
|
||||
|
||||
if ($original === null || $newAsset === null || ! $this->sameCategory($original, $newAsset)) {
|
||||
throw ValidationException::withMessages(['replacement_asset_id' => 'Selecione um equipamento da mesma categoria.']);
|
||||
}
|
||||
if ($locked->items()->where('snipe_asset_id', $newAsset->id)->exists()
|
||||
|| ! $this->availability->isAvailable($newAsset, $lockedItem->starts_at, $lockedItem->ends_at)) {
|
||||
throw ValidationException::withMessages(['replacement_asset_id' => 'O equipamento substituto não está disponível.']);
|
||||
}
|
||||
|
||||
$lockedItem->update(['status' => ReservationItemStatus::Cancelled]);
|
||||
$locked->items()->create([
|
||||
'snipe_asset_id' => $newAsset->id,
|
||||
'starts_at' => $lockedItem->starts_at,
|
||||
'ends_at' => $lockedItem->ends_at,
|
||||
'status' => ReservationItemStatus::Held,
|
||||
'notes' => "Substitui {$original->asset_tag}: {$reason}",
|
||||
]);
|
||||
ApprovalAction::create([
|
||||
'reservation_id' => $locked->id,
|
||||
'actor_id' => $actor->id,
|
||||
'action' => 'asset_substituted',
|
||||
'previous_status' => $locked->status->value,
|
||||
'new_status' => $locked->status->value,
|
||||
'comment' => $reason,
|
||||
]);
|
||||
AuditLog::create([
|
||||
'actor_id' => $actor->id,
|
||||
'event' => 'reservation.asset_substituted',
|
||||
'auditable_type' => $locked->getMorphClass(),
|
||||
'auditable_id' => $locked->id,
|
||||
'metadata' => [
|
||||
'original_asset_id' => $original->id,
|
||||
'replacement_asset_id' => $newAsset->id,
|
||||
'reason' => $reason,
|
||||
],
|
||||
]);
|
||||
|
||||
return $locked->load('requester', 'items.asset');
|
||||
}, 3);
|
||||
|
||||
$updated->requester->notify(new ReservationDecisionNotification($updated, 'asset_substituted', $reason));
|
||||
|
||||
return $updated;
|
||||
}
|
||||
|
||||
private function authorize(User $actor): void
|
||||
@@ -94,6 +179,15 @@ class ApprovalService
|
||||
}
|
||||
}
|
||||
|
||||
private function sameCategory(SnipeAsset $original, SnipeAsset $replacement): bool
|
||||
{
|
||||
if ($original->category_id !== null && $replacement->category_id !== null) {
|
||||
return $original->category_id === $replacement->category_id;
|
||||
}
|
||||
|
||||
return filled($original->category_name) && $original->category_name === $replacement->category_name;
|
||||
}
|
||||
|
||||
private function lockPending(Reservation $reservation): Reservation
|
||||
{
|
||||
$locked = Reservation::query()->whereKey($reservation->id)->lockForUpdate()->firstOrFail();
|
||||
|
||||
Reference in New Issue
Block a user