#23 - cr fix
This commit is contained in:
@@ -4,16 +4,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace Toby\Helpers;
|
||||
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Toby\Models\YearPeriod;
|
||||
|
||||
class YearPeriodRetriever
|
||||
{
|
||||
public const SESSION_KEY = "selected_year_period";
|
||||
|
||||
public function __construct(
|
||||
protected Session $session,
|
||||
) {
|
||||
}
|
||||
|
||||
public function selected(): YearPeriod
|
||||
{
|
||||
/** @var YearPeriod $yearPeriod */
|
||||
$yearPeriod = YearPeriod::query()->find(session()->get(static::SESSION_KEY));
|
||||
$yearPeriod = YearPeriod::query()->find($this->session->get(static::SESSION_KEY));
|
||||
|
||||
return $yearPeriod !== null ? $yearPeriod : $this->current();
|
||||
}
|
||||
|
@@ -15,8 +15,7 @@ class VacationLimitRequest extends FormRequest
|
||||
return [
|
||||
"items" => ["required", "array"],
|
||||
"items.*.id" => ["required", "exists:vacation_limits,id"],
|
||||
"items.*.hasVacation" => ["required", "boolean"],
|
||||
"items.*.days" => ["exclude_if:items.*.hasVacation,false", "required", "integer", "min:0"],
|
||||
"items.*.days" => ["nullable", "integer", "min:0"],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -25,13 +24,10 @@ class VacationLimitRequest extends FormRequest
|
||||
return VacationLimit::query()->find($this->collect("items")->pluck("id"));
|
||||
}
|
||||
|
||||
public function data(): Collection
|
||||
public function data(): array
|
||||
{
|
||||
return $this->collect("items")->mapWithKeys(fn(array $item): array => [
|
||||
$item["id"] => [
|
||||
"has_vacation" => $item["hasVacation"],
|
||||
"days" => $item["days"],
|
||||
],
|
||||
]);
|
||||
return $this->collect("items")
|
||||
->keyBy("id")
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserFormDataResource extends JsonResource
|
||||
{
|
||||
public static $wrap = false;
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
|
@@ -8,14 +8,14 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class VacationLimitResource extends JsonResource
|
||||
{
|
||||
public static $wrap = false;
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"user" => new UserResource($this->user),
|
||||
"hasVacation" => $this->has_vacation,
|
||||
"hasVacation" => $this->hasVacation(),
|
||||
"days" => $this->days,
|
||||
];
|
||||
}
|
||||
|
@@ -12,7 +12,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
* @property int $id
|
||||
* @property User $user
|
||||
* @property YearPeriod $yearPeriod
|
||||
* @property bool $has_vacation
|
||||
* @property int $days
|
||||
*/
|
||||
class VacationLimit extends Model
|
||||
@@ -21,9 +20,10 @@ class VacationLimit extends Model
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
"has_vacation" => "boolean",
|
||||
];
|
||||
public function hasVacation(): bool
|
||||
{
|
||||
return $this->days !== null;
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
|
@@ -6,20 +6,13 @@ namespace Toby\Providers;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Toby\Models\User;
|
||||
use Toby\Models\VacationLimit;
|
||||
use Toby\Models\YearPeriod;
|
||||
use Toby\Observers\UserObserver;
|
||||
use Toby\Observers\YearPeriodObserver;
|
||||
use Toby\Scopes\SelectedYearPeriodScope;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
User::observe(UserObserver::class);
|
||||
YearPeriod::observe(YearPeriodObserver::class);
|
||||
|
||||
Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y"));
|
||||
|
||||
VacationLimit::addGlobalScope($this->app->make(SelectedYearPeriodScope::class));
|
||||
|
20
app/Providers/ObserverServiceProvider.php
Normal file
20
app/Providers/ObserverServiceProvider.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Toby\Models\User;
|
||||
use Toby\Models\YearPeriod;
|
||||
use Toby\Observers\UserObserver;
|
||||
use Toby\Observers\YearPeriodObserver;
|
||||
|
||||
class ObserverServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
User::observe(UserObserver::class);
|
||||
YearPeriod::observe(YearPeriodObserver::class);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user