* #23 - wip * #23 - wip * #23 - wip * #23 - wip * #23 - fix * #23 - ecs fix * #23 - fix * #23 - fix * #23 - cr fix
This commit is contained in:
57
app/Helpers/YearPeriodRetriever.php
Normal file
57
app/Helpers/YearPeriodRetriever.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
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($this->session->get(static::SESSION_KEY));
|
||||
|
||||
return $yearPeriod !== null ? $yearPeriod : $this->current();
|
||||
}
|
||||
|
||||
public function current(): YearPeriod
|
||||
{
|
||||
return YearPeriod::current();
|
||||
}
|
||||
|
||||
public function links(): array
|
||||
{
|
||||
$current = $this->selected();
|
||||
|
||||
$years = YearPeriod::query()->whereIn("year", $this->offset($current->year))->get();
|
||||
$navigation = $years->map(fn(YearPeriod $yearPeriod) => $this->toNavigation($yearPeriod));
|
||||
|
||||
return [
|
||||
"current" => $current->year,
|
||||
"navigation" => $navigation->toArray(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function offset(int $year): array
|
||||
{
|
||||
return range($year - 2, $year + 2);
|
||||
}
|
||||
|
||||
protected function toNavigation(YearPeriod $yearPeriod): array
|
||||
{
|
||||
return [
|
||||
"year" => $yearPeriod->year,
|
||||
"link" => route("year-periods.select", $yearPeriod->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
22
app/Http/Controllers/SelectYearPeriodController.php
Normal file
22
app/Http/Controllers/SelectYearPeriodController.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Toby\Helpers\YearPeriodRetriever;
|
||||
use Toby\Models\YearPeriod;
|
||||
|
||||
class SelectYearPeriodController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, YearPeriod $yearPeriod): RedirectResponse
|
||||
{
|
||||
$request->session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id);
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with("success", __("Selected year period has been changed"));
|
||||
}
|
||||
}
|
||||
34
app/Http/Controllers/VacationLimitController.php
Normal file
34
app/Http/Controllers/VacationLimitController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Inertia\Response;
|
||||
use Toby\Http\Requests\VacationLimitRequest;
|
||||
use Toby\Http\Resources\VacationLimitResource;
|
||||
use Toby\Models\VacationLimit;
|
||||
|
||||
class VacationLimitController extends Controller
|
||||
{
|
||||
public function edit(): Response
|
||||
{
|
||||
return inertia("VacationLimits", [
|
||||
"limits" => VacationLimitResource::collection(VacationLimit::query()->with("user")->get()),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(VacationLimitRequest $request): RedirectResponse
|
||||
{
|
||||
$data = $request->data();
|
||||
|
||||
foreach ($request->vacationLimits() as $limit) {
|
||||
$limit->update($data[$limit->id]);
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with("success", __("Vacation limits have been updated"));
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,16 @@ namespace Toby\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Middleware;
|
||||
use Toby\Helpers\YearPeriodRetriever;
|
||||
use Toby\Http\Resources\UserResource;
|
||||
|
||||
class HandleInertiaRequests extends Middleware
|
||||
{
|
||||
public function __construct(
|
||||
protected YearPeriodRetriever $yearPeriodRetriever,
|
||||
) {
|
||||
}
|
||||
|
||||
public function share(Request $request): array
|
||||
{
|
||||
$user = $request->user();
|
||||
@@ -22,6 +28,7 @@ class HandleInertiaRequests extends Middleware
|
||||
"success" => $request->session()->get("success"),
|
||||
"error" => $request->session()->get("error"),
|
||||
],
|
||||
"years" => fn() => $user ? $this->yearPeriodRetriever->links() : [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
33
app/Http/Requests/VacationLimitRequest.php
Normal file
33
app/Http/Requests/VacationLimitRequest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Collection;
|
||||
use Toby\Models\VacationLimit;
|
||||
|
||||
class VacationLimitRequest extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
"items" => ["required", "array"],
|
||||
"items.*.id" => ["required", "exists:vacation_limits,id"],
|
||||
"items.*.days" => ["nullable", "integer", "min:0"],
|
||||
];
|
||||
}
|
||||
|
||||
public function vacationLimits(): Collection
|
||||
{
|
||||
return VacationLimit::query()->find($this->collect("items")->pluck("id"));
|
||||
}
|
||||
|
||||
public function data(): array
|
||||
{
|
||||
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,7 +8,7 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserResource extends JsonResource
|
||||
{
|
||||
public static $wrap = false;
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
|
||||
22
app/Http/Resources/VacationLimitResource.php
Normal file
22
app/Http/Resources/VacationLimitResource.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class VacationLimitResource extends JsonResource
|
||||
{
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"user" => new UserResource($this->user),
|
||||
"hasVacation" => $this->hasVacation(),
|
||||
"days" => $this->days,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@ namespace Toby\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Toby\Enums\EmploymentForm;
|
||||
|
||||
/**
|
||||
@@ -19,6 +21,7 @@ use Toby\Enums\EmploymentForm;
|
||||
* @property string $avatar
|
||||
* @property EmploymentForm $employment_form
|
||||
* @property Carbon $employment_date
|
||||
* @property Collection $vacationLimits
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@@ -43,6 +46,11 @@ class User extends Authenticatable
|
||||
"remember_token",
|
||||
];
|
||||
|
||||
public function vacationLimits(): HasMany
|
||||
{
|
||||
return $this->hasMany(VacationLimit::class);
|
||||
}
|
||||
|
||||
public function scopeSearch(Builder $query, ?string $text): Builder
|
||||
{
|
||||
if ($text === null) {
|
||||
@@ -53,4 +61,11 @@ class User extends Authenticatable
|
||||
->where("name", "LIKE", "%{$text}%")
|
||||
->orWhere("email", "LIKE", "%{$text}%");
|
||||
}
|
||||
|
||||
public function saveAvatar(string $path): void
|
||||
{
|
||||
$this->avatar = $path;
|
||||
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
37
app/Models/VacationLimit.php
Normal file
37
app/Models/VacationLimit.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property User $user
|
||||
* @property YearPeriod $yearPeriod
|
||||
* @property int $days
|
||||
*/
|
||||
class VacationLimit extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public function hasVacation(): bool
|
||||
{
|
||||
return $this->days !== null;
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function yearPeriod(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(YearPeriod::class);
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,13 @@ namespace Toby\Models;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $year
|
||||
* @property Collection $vacationLimits
|
||||
*/
|
||||
class YearPeriod extends Model
|
||||
{
|
||||
@@ -27,4 +30,9 @@ class YearPeriod extends Model
|
||||
|
||||
return $year;
|
||||
}
|
||||
|
||||
public function vacationLimits(): HasMany
|
||||
{
|
||||
return $this->hasMany(VacationLimit::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,20 +6,24 @@ namespace Toby\Observers;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Toby\Helpers\UserAvatarGenerator;
|
||||
use Toby\Helpers\YearPeriodRetriever;
|
||||
use Toby\Models\User;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
public function __construct(
|
||||
protected UserAvatarGenerator $generator,
|
||||
protected YearPeriodRetriever $yearPeriodRetriever,
|
||||
) {
|
||||
}
|
||||
|
||||
public function created(User $user): void
|
||||
{
|
||||
$user->avatar = $this->generator->generateFor($user);
|
||||
$user->saveAvatar($this->generator->generateFor($user));
|
||||
|
||||
$user->save();
|
||||
$user->vacationLimits()->create([
|
||||
"year_period_id" => $this->yearPeriodRetriever->current()->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updating(User $user): void
|
||||
|
||||
28
app/Observers/YearPeriodObserver.php
Normal file
28
app/Observers/YearPeriodObserver.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Observers;
|
||||
|
||||
use Toby\Helpers\UserAvatarGenerator;
|
||||
use Toby\Models\User;
|
||||
use Toby\Models\YearPeriod;
|
||||
|
||||
class YearPeriodObserver
|
||||
{
|
||||
public function __construct(
|
||||
protected UserAvatarGenerator $generator,
|
||||
) {
|
||||
}
|
||||
|
||||
public function created(YearPeriod $yearPeriod): void
|
||||
{
|
||||
$users = User::all();
|
||||
|
||||
foreach ($users as $user) {
|
||||
$yearPeriod->vacationLimits()->create([
|
||||
"user_id" => $user->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,15 @@ namespace Toby\Providers;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Toby\Models\User;
|
||||
use Toby\Observers\UserObserver;
|
||||
use Toby\Models\VacationLimit;
|
||||
use Toby\Scopes\SelectedYearPeriodScope;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
User::observe(UserObserver::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);
|
||||
}
|
||||
}
|
||||
23
app/Scopes/SelectedYearPeriodScope.php
Normal file
23
app/Scopes/SelectedYearPeriodScope.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Scopes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Toby\Helpers\YearPeriodRetriever;
|
||||
|
||||
class SelectedYearPeriodScope implements Scope
|
||||
{
|
||||
public function __construct(
|
||||
protected YearPeriodRetriever $yearPeriodRetriever,
|
||||
) {
|
||||
}
|
||||
|
||||
public function apply(Builder $builder, Model $model): Builder
|
||||
{
|
||||
return $builder->where("year_period_id", $this->yearPeriodRetriever->selected()->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user