feat: complete administration and reporting
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domain\Reservations\Services;
|
||||
|
||||
use App\Domain\Administration\Services\ReservationPolicy;
|
||||
use App\Domain\Approvals\Models\ApprovalAction;
|
||||
use App\Domain\Audit\Models\AuditLog;
|
||||
use App\Domain\Identity\Enums\RoleName;
|
||||
@@ -20,7 +21,10 @@ use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ReservationService
|
||||
{
|
||||
public function __construct(private readonly AvailabilityService $availability) {}
|
||||
public function __construct(
|
||||
private readonly AvailabilityService $availability,
|
||||
private readonly ReservationPolicy $policy,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{purpose:string, usage_location:string, starts_at:string, ends_at:string, asset_ids:list<int>} $data
|
||||
@@ -29,8 +33,10 @@ class ReservationService
|
||||
{
|
||||
$startsAt = CarbonImmutable::parse($data['starts_at']);
|
||||
$endsAt = CarbonImmutable::parse($data['ends_at']);
|
||||
$this->validatePolicy($startsAt, $endsAt, count($data['asset_ids']));
|
||||
[$bufferStartsAt, $bufferEndsAt] = $this->availability->bufferedPeriod($startsAt, $endsAt);
|
||||
|
||||
$reservation = DB::transaction(function () use ($requester, $data, $startsAt, $endsAt): Reservation {
|
||||
$reservation = DB::transaction(function () use ($requester, $data, $startsAt, $endsAt, $bufferStartsAt, $bufferEndsAt): Reservation {
|
||||
$assets = SnipeAsset::query()
|
||||
->whereKey($data['asset_ids'])
|
||||
->lockForUpdate()
|
||||
@@ -58,7 +64,7 @@ class ReservationService
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $endsAt,
|
||||
'status' => ReservationStatus::PendingApproval,
|
||||
'approval_deadline' => now()->addHours(48),
|
||||
'approval_deadline' => now()->addHours($this->policy->integer('reservation.approval_deadline_hours')),
|
||||
]);
|
||||
|
||||
foreach ($assets as $asset) {
|
||||
@@ -66,6 +72,8 @@ class ReservationService
|
||||
'snipe_asset_id' => $asset->id,
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $endsAt,
|
||||
'buffer_starts_at' => $bufferStartsAt,
|
||||
'buffer_ends_at' => $bufferEndsAt,
|
||||
'status' => ReservationItemStatus::Held,
|
||||
]);
|
||||
}
|
||||
@@ -156,7 +164,7 @@ class ReservationService
|
||||
|
||||
$locked->update([
|
||||
'status' => ReservationStatus::PendingApproval,
|
||||
'approval_deadline' => now()->addHours(48),
|
||||
'approval_deadline' => now()->addHours($this->policy->integer('reservation.approval_deadline_hours')),
|
||||
]);
|
||||
ApprovalAction::create([
|
||||
'reservation_id' => $locked->id,
|
||||
@@ -258,4 +266,21 @@ class ReservationService
|
||||
|
||||
return $reference;
|
||||
}
|
||||
|
||||
private function validatePolicy(CarbonImmutable $startsAt, CarbonImmutable $endsAt, int $assetCount): void
|
||||
{
|
||||
$minNotice = $this->policy->integer('reservation.min_notice_hours');
|
||||
$maxDuration = $this->policy->integer('reservation.max_duration_hours');
|
||||
$maxAssets = $this->policy->integer('reservation.max_assets');
|
||||
|
||||
if ($startsAt->lessThan(now()->addHours($minNotice))) {
|
||||
throw ValidationException::withMessages(['starts_at' => "A requisição exige pelo menos {$minNotice} horas de antecedência."]);
|
||||
}
|
||||
if ($startsAt->diffInMinutes($endsAt) > $maxDuration * 60) {
|
||||
throw ValidationException::withMessages(['ends_at' => "A duração máxima permitida é de {$maxDuration} horas."]);
|
||||
}
|
||||
if ($assetCount > $maxAssets) {
|
||||
throw ValidationException::withMessages(['asset_ids' => "Selecione no máximo {$maxAssets} equipamentos."]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user