* #28 - holidays management * #28 - fix * #28 - fix * #28 - fix * #28 - fix * #28 - fix * #28 - fix * #28 - cr fix
This commit is contained in:
		
							
								
								
									
										34
									
								
								app/Helpers/PolishHolidaysRetriever.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								app/Helpers/PolishHolidaysRetriever.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Helpers; | ||||
|  | ||||
| use Illuminate\Support\Carbon; | ||||
| use Illuminate\Support\Collection; | ||||
| use Toby\Models\YearPeriod; | ||||
| use Yasumi\Holiday; | ||||
| use Yasumi\Yasumi; | ||||
|  | ||||
| class PolishHolidaysRetriever | ||||
| { | ||||
|     protected const PROVIDER_KEY = "Poland"; | ||||
|     protected const LANG_KEY = "pl"; | ||||
|  | ||||
|     public function getForYearPeriod(YearPeriod $yearPeriod): Collection | ||||
|     { | ||||
|         $polishProvider = Yasumi::create(static::PROVIDER_KEY, $yearPeriod->year); | ||||
|  | ||||
|         $holidays = $polishProvider->getHolidays(); | ||||
|  | ||||
|         return $this->prepareHolidays($holidays); | ||||
|     } | ||||
|  | ||||
|     protected function prepareHolidays(array $holidays): Collection | ||||
|     { | ||||
|         return collect($holidays)->map(fn(Holiday $holiday) => [ | ||||
|             "name" => $holiday->getName([static::LANG_KEY]), | ||||
|             "date" => Carbon::createFromTimestamp($holiday->getTimestamp()), | ||||
|         ])->values(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										65
									
								
								app/Http/Controllers/HolidayController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								app/Http/Controllers/HolidayController.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\RedirectResponse; | ||||
| use Inertia\Response; | ||||
| use Toby\Http\Requests\HolidayRequest; | ||||
| use Toby\Http\Resources\HolidayFormDataResource; | ||||
| use Toby\Http\Resources\HolidayResource; | ||||
| use Toby\Models\Holiday; | ||||
|  | ||||
| class HolidayController extends Controller | ||||
| { | ||||
|     public function index(): Response | ||||
|     { | ||||
|         $holidays = Holiday::query() | ||||
|             ->orderBy("date") | ||||
|             ->get(); | ||||
|  | ||||
|         return inertia("Holidays/Index", [ | ||||
|             "holidays" => HolidayResource::collection($holidays), | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function create(): Response | ||||
|     { | ||||
|         return inertia("Holidays/Create"); | ||||
|     } | ||||
|  | ||||
|     public function store(HolidayRequest $request): RedirectResponse | ||||
|     { | ||||
|         Holiday::query()->create($request->data()); | ||||
|  | ||||
|         return redirect() | ||||
|             ->route("holidays.index") | ||||
|             ->with("success", __("Holiday has been created")); | ||||
|     } | ||||
|  | ||||
|     public function edit(Holiday $holiday): Response | ||||
|     { | ||||
|         return inertia("Holidays/Edit", [ | ||||
|             "holiday" => new HolidayFormDataResource($holiday), | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function update(HolidayRequest $request, Holiday $holiday): RedirectResponse | ||||
|     { | ||||
|         $holiday->update($request->data()); | ||||
|  | ||||
|         return redirect() | ||||
|             ->route("holidays.index") | ||||
|             ->with("success", __("Holiday has been updated")); | ||||
|     } | ||||
|  | ||||
|     public function destroy(Holiday $holiday): RedirectResponse | ||||
|     { | ||||
|         $holiday->delete(); | ||||
|  | ||||
|         return redirect() | ||||
|             ->route("holidays.index") | ||||
|             ->with("success", __("Holiday has been deleted")); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										37
									
								
								app/Http/Requests/HolidayRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								app/Http/Requests/HolidayRequest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Http\Requests; | ||||
|  | ||||
| use Illuminate\Foundation\Http\FormRequest; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Illuminate\Validation\Rule; | ||||
| use Toby\Models\YearPeriod; | ||||
| use Toby\Rules\YearPeriodExists; | ||||
|  | ||||
| class HolidayRequest extends FormRequest | ||||
| { | ||||
|     public function rules(): array | ||||
|     { | ||||
|         return [ | ||||
|             "name" => ["required", "min:3", "max:150"], | ||||
|             "date" => ["required", | ||||
|                 "date_format:Y-m-d", | ||||
|                 Rule::unique("holidays", "date")->ignore($this->holiday), | ||||
|                 new YearPeriodExists(), | ||||
|             ], | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|     public function data(): array | ||||
|     { | ||||
|         $date = $this->get("date"); | ||||
|  | ||||
|         return [ | ||||
|             "name" => $this->get("name"), | ||||
|             "date" => $date, | ||||
|             "year_period_id" => YearPeriod::findByYear(Carbon::create($date)->year)->id, | ||||
|         ]; | ||||
|     } | ||||
| } | ||||
| @@ -18,7 +18,7 @@ class UserRequest extends FormRequest | ||||
|             "lastName" => ["required", "min:3", "max:80"], | ||||
|             "email" => ["required", "email", Rule::unique("users", "email")->ignore($this->user)], | ||||
|             "employmentForm" => ["required", new Enum(EmploymentForm::class)], | ||||
|             "employmentDate" => ["required", "date"], | ||||
|             "employmentDate" => ["required", "date_format:Y-m-d"], | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|   | ||||
							
								
								
									
										21
									
								
								app/Http/Resources/HolidayFormDataResource.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								app/Http/Resources/HolidayFormDataResource.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Http\Resources; | ||||
|  | ||||
| use Illuminate\Http\Resources\Json\JsonResource; | ||||
|  | ||||
| class HolidayFormDataResource extends JsonResource | ||||
| { | ||||
|     public static $wrap = null; | ||||
|  | ||||
|     public function toArray($request): array | ||||
|     { | ||||
|         return [ | ||||
|             "id" => $this->id, | ||||
|             "name" => $this->name, | ||||
|             "date" => $this->date->toDateString(), | ||||
|         ]; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										22
									
								
								app/Http/Resources/HolidayResource.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/Http/Resources/HolidayResource.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Http\Resources; | ||||
|  | ||||
| use Illuminate\Http\Resources\Json\JsonResource; | ||||
|  | ||||
| class HolidayResource extends JsonResource | ||||
| { | ||||
|     public static $wrap = null; | ||||
|  | ||||
|     public function toArray($request): array | ||||
|     { | ||||
|         return [ | ||||
|             "id" => $this->id, | ||||
|             "name" => $this->name, | ||||
|             "displayDate" => $this->date->toDisplayString(), | ||||
|             "dayOfWeek" => $this->date->dayName, | ||||
|         ]; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										32
									
								
								app/Models/Holiday.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								app/Models/Holiday.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| <?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; | ||||
| use Illuminate\Support\Carbon; | ||||
|  | ||||
| /** | ||||
|  * @property int $id | ||||
|  * @property string $name | ||||
|  * @property Carbon $date | ||||
|  * @property YearPeriod $yearPeriod | ||||
|  */ | ||||
| class Holiday extends Model | ||||
| { | ||||
|     use HasFactory; | ||||
|  | ||||
|     protected $guarded = []; | ||||
|  | ||||
|     protected $casts = [ | ||||
|         "date" => "date", | ||||
|     ]; | ||||
|  | ||||
|     public function yearPeriod(): BelongsTo | ||||
|     { | ||||
|         return $this->belongsTo(YearPeriod::class); | ||||
|     } | ||||
| } | ||||
| @@ -34,7 +34,7 @@ class User extends Authenticatable | ||||
|  | ||||
|     protected $casts = [ | ||||
|         "employment_form" => EmploymentForm::class, | ||||
|         "employment_date" => "datetime", | ||||
|         "employment_date" => "date", | ||||
|     ]; | ||||
|  | ||||
|     protected $hidden = [ | ||||
|   | ||||
| @@ -4,29 +4,33 @@ declare(strict_types=1); | ||||
|  | ||||
| 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\Carbon; | ||||
| use Illuminate\Support\Collection; | ||||
|  | ||||
| /** | ||||
|  * @property int $id | ||||
|  * @property int $year | ||||
|  * @property Collection $vacationLimits | ||||
|  * @property Collection $holidays | ||||
|  */ | ||||
| class YearPeriod extends Model | ||||
| { | ||||
|     use HasFactory; | ||||
|  | ||||
|     protected $fillable = [ | ||||
|         "year", | ||||
|     ]; | ||||
|     protected $guarded = []; | ||||
|  | ||||
|     public static function current(): ?static | ||||
|     { | ||||
|         return static::findByYear(Carbon::now()->year); | ||||
|     } | ||||
|  | ||||
|     public static function findByYear(int $year): ?static | ||||
|     { | ||||
|         /** @var YearPeriod $year */ | ||||
|         $year = static::query()->where("year", Carbon::now()->year)->first(); | ||||
|         $year = static::query()->where("year", $year)->first(); | ||||
|  | ||||
|         return $year; | ||||
|     } | ||||
| @@ -35,4 +39,9 @@ class YearPeriod extends Model | ||||
|     { | ||||
|         return $this->hasMany(VacationLimit::class); | ||||
|     } | ||||
|  | ||||
|     public function holidays(): HasMany | ||||
|     { | ||||
|         return $this->hasMany(Holiday::class); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,7 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Observers; | ||||
|  | ||||
| use Toby\Helpers\PolishHolidaysRetriever; | ||||
| use Toby\Helpers\UserAvatarGenerator; | ||||
| use Toby\Models\User; | ||||
| use Toby\Models\YearPeriod; | ||||
| @@ -12,10 +13,17 @@ class YearPeriodObserver | ||||
| { | ||||
|     public function __construct( | ||||
|         protected UserAvatarGenerator $generator, | ||||
|         protected PolishHolidaysRetriever $polishHolidaysRetriever, | ||||
|     ) { | ||||
|     } | ||||
|  | ||||
|     public function created(YearPeriod $yearPeriod): void | ||||
|     { | ||||
|         $this->createVacationLimitsFor($yearPeriod); | ||||
|         $this->createHolidaysFor($yearPeriod); | ||||
|     } | ||||
|  | ||||
|     protected function createVacationLimitsFor(YearPeriod $yearPeriod): void | ||||
|     { | ||||
|         $users = User::all(); | ||||
|  | ||||
| @@ -25,4 +33,16 @@ class YearPeriodObserver | ||||
|             ]); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     protected function createHolidaysFor(YearPeriod $yearPeriod): void | ||||
|     { | ||||
|         $holidays = $this->polishHolidaysRetriever->getForYearPeriod($yearPeriod); | ||||
|  | ||||
|         foreach ($holidays as $holiday) { | ||||
|             $yearPeriod->holidays()->create([ | ||||
|                 "name" => $holiday["name"], | ||||
|                 "date" => $holiday["date"], | ||||
|             ]); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ namespace Toby\Providers; | ||||
|  | ||||
| use Illuminate\Support\Carbon; | ||||
| use Illuminate\Support\ServiceProvider; | ||||
| use Toby\Models\Holiday; | ||||
| use Toby\Models\VacationLimit; | ||||
| use Toby\Scopes\SelectedYearPeriodScope; | ||||
|  | ||||
| @@ -15,6 +16,9 @@ class AppServiceProvider extends ServiceProvider | ||||
|     { | ||||
|         Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y")); | ||||
|  | ||||
|         VacationLimit::addGlobalScope($this->app->make(SelectedYearPeriodScope::class)); | ||||
|         $selectedYearPeriodScope = $this->app->make(SelectedYearPeriodScope::class); | ||||
|  | ||||
|         VacationLimit::addGlobalScope($selectedYearPeriodScope); | ||||
|         Holiday::addGlobalScope($selectedYearPeriodScope); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										24
									
								
								app/Rules/YearPeriodExists.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								app/Rules/YearPeriodExists.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Rules; | ||||
|  | ||||
| use Illuminate\Contracts\Validation\Rule; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Toby\Models\YearPeriod; | ||||
|  | ||||
| class YearPeriodExists implements Rule | ||||
| { | ||||
|     public function passes($attribute, $value): bool | ||||
|     { | ||||
|         $yearPeriod = YearPeriod::findByYear(Carbon::create($value)->year); | ||||
|  | ||||
|         return $yearPeriod !== null; | ||||
|     } | ||||
|  | ||||
|     public function message(): string | ||||
|     { | ||||
|         return "The year period for given year doesn't exist."; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user