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'; } }