feat: establish reservation portal foundation
This commit is contained in:
32
app/Domain/Reservations/Services/AvailabilityService.php
Normal file
32
app/Domain/Reservations/Services/AvailabilityService.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Reservations\Services;
|
||||
|
||||
use App\Domain\Inventory\Models\SnipeAsset;
|
||||
use App\Domain\Reservations\Enums\ReservationItemStatus;
|
||||
use DateTimeInterface;
|
||||
|
||||
class AvailabilityService
|
||||
{
|
||||
public function isAvailable(SnipeAsset $asset, DateTimeInterface $startsAt, DateTimeInterface $endsAt): bool
|
||||
{
|
||||
if (! $asset->active || ! $asset->requestable || $asset->assigned_id !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hasReservationConflict = $asset->reservationItems()
|
||||
->whereIn('status', ReservationItemStatus::blockingValues())
|
||||
->where('starts_at', '<', $endsAt)
|
||||
->where('ends_at', '>', $startsAt)
|
||||
->exists();
|
||||
|
||||
if ($hasReservationConflict) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! $asset->blocks()
|
||||
->where('starts_at', '<', $endsAt)
|
||||
->where('ends_at', '>', $startsAt)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
89
app/Domain/Reservations/Services/ReservationService.php
Normal file
89
app/Domain/Reservations/Services/ReservationService.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Reservations\Services;
|
||||
|
||||
use App\Domain\Audit\Models\AuditLog;
|
||||
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 Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ReservationService
|
||||
{
|
||||
public function __construct(private readonly AvailabilityService $availability) {}
|
||||
|
||||
/**
|
||||
* @param array{purpose:string, usage_location:string, starts_at:string, ends_at:string, asset_ids:list<int>} $data
|
||||
*/
|
||||
public function createPending(User $requester, array $data): Reservation
|
||||
{
|
||||
$startsAt = CarbonImmutable::parse($data['starts_at']);
|
||||
$endsAt = CarbonImmutable::parse($data['ends_at']);
|
||||
|
||||
return DB::transaction(function () use ($requester, $data, $startsAt, $endsAt): Reservation {
|
||||
$assets = SnipeAsset::query()
|
||||
->whereKey($data['asset_ids'])
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
if ($assets->count() !== count(array_unique($data['asset_ids']))) {
|
||||
throw ValidationException::withMessages(['asset_ids' => 'Um ou mais equipamentos já não estão disponíveis.']);
|
||||
}
|
||||
|
||||
$unavailable = $assets->filter(
|
||||
fn (SnipeAsset $asset): bool => ! $this->availability->isAvailable($asset, $startsAt, $endsAt)
|
||||
);
|
||||
|
||||
if ($unavailable->isNotEmpty()) {
|
||||
throw ValidationException::withMessages([
|
||||
'asset_ids' => 'Conflito de disponibilidade: '.$unavailable->pluck('name')->join(', ').'.',
|
||||
]);
|
||||
}
|
||||
|
||||
$reservation = Reservation::create([
|
||||
'reference' => $this->nextReference(),
|
||||
'requester_id' => $requester->id,
|
||||
'purpose' => $data['purpose'],
|
||||
'usage_location' => $data['usage_location'],
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $endsAt,
|
||||
'status' => ReservationStatus::PendingApproval,
|
||||
'approval_deadline' => now()->addHours(48),
|
||||
]);
|
||||
|
||||
foreach ($assets as $asset) {
|
||||
$reservation->items()->create([
|
||||
'snipe_asset_id' => $asset->id,
|
||||
'starts_at' => $startsAt,
|
||||
'ends_at' => $endsAt,
|
||||
'status' => ReservationItemStatus::Held,
|
||||
]);
|
||||
}
|
||||
|
||||
AuditLog::create([
|
||||
'actor_id' => $requester->id,
|
||||
'event' => 'reservation.submitted',
|
||||
'auditable_type' => $reservation->getMorphClass(),
|
||||
'auditable_id' => $reservation->id,
|
||||
'after' => $reservation->only(['reference', 'purpose', 'usage_location', 'starts_at', 'ends_at', 'status']),
|
||||
'metadata' => ['asset_ids' => $assets->modelKeys()],
|
||||
]);
|
||||
|
||||
return $reservation->load('items.asset');
|
||||
}, 3);
|
||||
}
|
||||
|
||||
private function nextReference(): string
|
||||
{
|
||||
do {
|
||||
$reference = 'REQ-'.now()->format('Ymd').'-'.Str::upper(Str::random(6));
|
||||
} while (Reservation::where('reference', $reference)->exists());
|
||||
|
||||
return $reference;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user