This commit is contained in:
Adrian Hopek 2022-01-24 11:21:00 +01:00
parent 03af084c04
commit 36b98e1e67
13 changed files with 54 additions and 39 deletions

View File

@ -4,16 +4,22 @@ declare(strict_types=1);
namespace Toby\Helpers; namespace Toby\Helpers;
use Illuminate\Contracts\Session\Session;
use Toby\Models\YearPeriod; use Toby\Models\YearPeriod;
class YearPeriodRetriever class YearPeriodRetriever
{ {
public const SESSION_KEY = "selected_year_period"; public const SESSION_KEY = "selected_year_period";
public function __construct(
protected Session $session,
) {
}
public function selected(): YearPeriod public function selected(): YearPeriod
{ {
/** @var YearPeriod $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(); return $yearPeriod !== null ? $yearPeriod : $this->current();
} }

View File

@ -15,8 +15,7 @@ class VacationLimitRequest extends FormRequest
return [ return [
"items" => ["required", "array"], "items" => ["required", "array"],
"items.*.id" => ["required", "exists:vacation_limits,id"], "items.*.id" => ["required", "exists:vacation_limits,id"],
"items.*.hasVacation" => ["required", "boolean"], "items.*.days" => ["nullable", "integer", "min:0"],
"items.*.days" => ["exclude_if:items.*.hasVacation,false", "required", "integer", "min:0"],
]; ];
} }
@ -25,13 +24,10 @@ class VacationLimitRequest extends FormRequest
return VacationLimit::query()->find($this->collect("items")->pluck("id")); 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 => [ return $this->collect("items")
$item["id"] => [ ->keyBy("id")
"has_vacation" => $item["hasVacation"], ->toArray();
"days" => $item["days"],
],
]);
} }
} }

View File

@ -8,7 +8,7 @@ use Illuminate\Http\Resources\Json\JsonResource;
class UserFormDataResource extends JsonResource class UserFormDataResource extends JsonResource
{ {
public static $wrap = false; public static $wrap = null;
public function toArray($request): array public function toArray($request): array
{ {

View File

@ -8,14 +8,14 @@ use Illuminate\Http\Resources\Json\JsonResource;
class VacationLimitResource extends JsonResource class VacationLimitResource extends JsonResource
{ {
public static $wrap = false; public static $wrap = null;
public function toArray($request): array public function toArray($request): array
{ {
return [ return [
"id" => $this->id, "id" => $this->id,
"user" => new UserResource($this->user), "user" => new UserResource($this->user),
"hasVacation" => $this->has_vacation, "hasVacation" => $this->hasVacation(),
"days" => $this->days, "days" => $this->days,
]; ];
} }

View File

@ -12,7 +12,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property int $id * @property int $id
* @property User $user * @property User $user
* @property YearPeriod $yearPeriod * @property YearPeriod $yearPeriod
* @property bool $has_vacation
* @property int $days * @property int $days
*/ */
class VacationLimit extends Model class VacationLimit extends Model
@ -21,9 +20,10 @@ class VacationLimit extends Model
protected $guarded = []; protected $guarded = [];
protected $casts = [ public function hasVacation(): bool
"has_vacation" => "boolean", {
]; return $this->days !== null;
}
public function user(): BelongsTo public function user(): BelongsTo
{ {

View File

@ -6,20 +6,13 @@ namespace Toby\Providers;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Toby\Models\User;
use Toby\Models\VacationLimit; use Toby\Models\VacationLimit;
use Toby\Models\YearPeriod;
use Toby\Observers\UserObserver;
use Toby\Observers\YearPeriodObserver;
use Toby\Scopes\SelectedYearPeriodScope; use Toby\Scopes\SelectedYearPeriodScope;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
public function boot(): void public function boot(): void
{ {
User::observe(UserObserver::class);
YearPeriod::observe(YearPeriodObserver::class);
Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y")); Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y"));
VacationLimit::addGlobalScope($this->app->make(SelectedYearPeriodScope::class)); VacationLimit::addGlobalScope($this->app->make(SelectedYearPeriodScope::class));

View 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);
}
}

View File

@ -42,5 +42,6 @@ return [
Toby\Providers\EventServiceProvider::class, Toby\Providers\EventServiceProvider::class,
Toby\Providers\RouteServiceProvider::class, Toby\Providers\RouteServiceProvider::class,
Toby\Providers\TelescopeServiceProvider::class, Toby\Providers\TelescopeServiceProvider::class,
Toby\Providers\ObserverServiceProvider::class,
], ],
]; ];

View File

@ -15,7 +15,6 @@ return new class() extends Migration {
$table->id(); $table->id();
$table->foreignIdFor(User::class)->constrained()->cascadeOnDelete(); $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete();
$table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete();
$table->boolean("has_vacation")->default(false);
$table->integer("days")->nullable(); $table->integer("days")->nullable();
$table->timestamps(); $table->timestamps();
}); });

View File

@ -162,7 +162,6 @@ export default {
.transform(data => ({ .transform(data => ({
items: data.items.map(item => ({ items: data.items.map(item => ({
id: item.id, id: item.id,
hasVacation: item.hasVacation,
days: item.hasVacation ? item.days : null, days: item.hasVacation ? item.days : null,
})), })),
})) }))

View File

@ -14,17 +14,25 @@ class SelectYearPeriodTest extends FeatureTestCase
{ {
use DatabaseMigrations; use DatabaseMigrations;
protected YearPeriodRetriever $yearPeriodRetriever;
protected function setUp(): void
{
parent::setUp();
$this->yearPeriodRetriever = $this->app->make(YearPeriodRetriever::class);
}
public function testUserCanSelectNextYearPeriod(): void public function testUserCanSelectNextYearPeriod(): void
{ {
$nextYearPeriod = $this->createYearPeriod(Carbon::now()->year + 1); $nextYearPeriod = $this->createYearPeriod(Carbon::now()->year + 1);
$user = User::factory()->create(); $user = User::factory()->create();
$yearPeriodRetriever = new YearPeriodRetriever();
$this->actingAs($user) $this->actingAs($user)
->post("/year-periods/{$nextYearPeriod->id}/select") ->post("/year-periods/{$nextYearPeriod->id}/select")
->assertRedirect(); ->assertRedirect();
$this->assertSame($nextYearPeriod->id, $yearPeriodRetriever->selected()->id); $this->assertSame($nextYearPeriod->id, $this->yearPeriodRetriever->selected()->id);
} }
public function testUserCannotSelectNextYearPeriodIfDoesntExist(): void public function testUserCannotSelectNextYearPeriodIfDoesntExist(): void
@ -38,9 +46,8 @@ class SelectYearPeriodTest extends FeatureTestCase
public function testIfUserDoesntSelectAnyYearPeriodCurrentActsAsSelected(): void public function testIfUserDoesntSelectAnyYearPeriodCurrentActsAsSelected(): void
{ {
$yearPeriodRetriever = new YearPeriodRetriever(); $currentYearPeriod = $this->yearPeriodRetriever->current();
$currentYearPeriod = $yearPeriodRetriever->current();
$this->assertSame($currentYearPeriod->id, $yearPeriodRetriever->selected()->id); $this->assertSame($currentYearPeriod->id, $this->yearPeriodRetriever->selected()->id);
} }
} }

View File

@ -41,17 +41,14 @@ class VacationLimitTest extends FeatureTestCase
$data = [ $data = [
[ [
"id" => $limit1->id, "id" => $limit1->id,
"hasVacation" => true,
"days" => 25, "days" => 25,
], ],
[ [
"id" => $limit2->id, "id" => $limit2->id,
"hasVacation" => false,
"days" => null, "days" => null,
], ],
[ [
"id" => $limit3->id, "id" => $limit3->id,
"hasVacation" => true,
"days" => 20, "days" => 20,
], ],
]; ];
@ -64,19 +61,16 @@ class VacationLimitTest extends FeatureTestCase
$this->assertDatabaseHas("vacation_limits", [ $this->assertDatabaseHas("vacation_limits", [
"id" => $limit1->id, "id" => $limit1->id,
"has_vacation" => true,
"days" => 25, "days" => 25,
]); ]);
$this->assertDatabaseHas("vacation_limits", [ $this->assertDatabaseHas("vacation_limits", [
"id" => $limit2->id, "id" => $limit2->id,
"has_vacation" => false,
"days" => null, "days" => null,
]); ]);
$this->assertDatabaseHas("vacation_limits", [ $this->assertDatabaseHas("vacation_limits", [
"id" => $limit3->id, "id" => $limit3->id,
"has_vacation" => true,
"days" => 20, "days" => 20,
]); ]);
} }

View File

@ -31,7 +31,7 @@ class YearPeriodRetrieverTest extends TestCase
$this->current = Carbon::now(); $this->current = Carbon::now();
Carbon::setTestNow($this->current); Carbon::setTestNow($this->current);
$this->yearPeriodRetriever = new YearPeriodRetriever(); $this->yearPeriodRetriever = $this->app->make(YearPeriodRetriever::class);
$this->previousYearPeriod = $this->createYearPeriod($this->current->year - 1); $this->previousYearPeriod = $this->createYearPeriod($this->current->year - 1);
$this->currentYearPeriod = $this->createCurrentYearPeriod(); $this->currentYearPeriod = $this->createCurrentYearPeriod();
@ -43,7 +43,7 @@ class YearPeriodRetrieverTest extends TestCase
$this->assertSame($this->currentYearPeriod->id, $this->yearPeriodRetriever->current()->id); $this->assertSame($this->currentYearPeriod->id, $this->yearPeriodRetriever->current()->id);
} }
public function testRetrievesCurrentYearPeriodWhenNoSelected(): void public function testRetrievesCurrentYearPeriodWhenNoneIsSelected(): void
{ {
$this->clearSelectedYearPeriod(); $this->clearSelectedYearPeriod();