Files
Investidor-V2/app/Services/DeskServiceAvailability.php

48 lines
1.8 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/** One applicable edition per service. Existing requests keep their immutable version FK. */
final class DeskServiceAvailability
{
public function scheduled(): bool
{
return Schema::hasColumn('desk_service_versions', 'effective_from');
}
public function current(): Builder
{
$query = DB::table('desk_service_versions')->where('status', 'published')
->where(fn (Builder $date) => $date->whereNull('published_at')->orWhere('published_at', '<=', now()))
->selectRaw('DISTINCT ON (code) *')->orderBy('code');
if ($this->scheduled()) {
$query->where(fn (Builder $date) => $date->whereNull('effective_from')->orWhere('effective_from', '<=', now()->toDateString()))
->orderByRaw('effective_from DESC NULLS LAST');
}
return $query->orderByDesc('version');
}
public function upcoming(): Builder
{
$query = DB::table('desk_service_versions');
if (! $this->scheduled()) {
return $query->whereRaw('false')->select('code', 'version')->selectRaw('NULL::date AS effective_from');
}
return $query->where('status', 'published')->where('effective_from', '>', now()->toDateString())
->where(fn (Builder $date) => $date->whereNull('published_at')->orWhere('published_at', '<=', now()))
->selectRaw('DISTINCT ON (code,effective_from) code,version,effective_from')->orderBy('code')->orderBy('effective_from')->orderByDesc('version');
}
/** @return literal-string */
public function dateProjection(): string
{
return $this->scheduled() ? 'effective_from' : 'NULL::date AS effective_from';
}
}