- small changes (#98)
* - added some test * - cr fix * wip * wip * Update resources/js/Shared/MainMenu.vue Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com> * fix Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl> Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
This commit is contained in:
		| @@ -6,15 +6,12 @@ namespace Toby\Architecture\Providers; | ||||
|  | ||||
| use Illuminate\Support\ServiceProvider; | ||||
| use Toby\Eloquent\Models\User; | ||||
| use Toby\Eloquent\Models\YearPeriod; | ||||
| use Toby\Eloquent\Observers\UserObserver; | ||||
| use Toby\Eloquent\Observers\YearPeriodObserver; | ||||
|  | ||||
| class ObserverServiceProvider extends ServiceProvider | ||||
| { | ||||
|     public function boot(): void | ||||
|     { | ||||
|         User::observe(UserObserver::class); | ||||
|         YearPeriod::observe(YearPeriodObserver::class); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										33
									
								
								app/Domain/Actions/CreateUserAction.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								app/Domain/Actions/CreateUserAction.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Domain\Actions; | ||||
|  | ||||
| use Toby\Eloquent\Models\User; | ||||
| use Toby\Eloquent\Models\YearPeriod; | ||||
|  | ||||
| class CreateUserAction | ||||
| { | ||||
|     public function execute(array $data): User | ||||
|     { | ||||
|         $user = new User($data); | ||||
|  | ||||
|         $user->save(); | ||||
|  | ||||
|         $this->createVacationLimitsFor($user); | ||||
|  | ||||
|         return $user; | ||||
|     } | ||||
|  | ||||
|     protected function createVacationLimitsFor(User $user): void | ||||
|     { | ||||
|         $yearPeriods = YearPeriod::all(); | ||||
|  | ||||
|         foreach ($yearPeriods as $yearPeriod) { | ||||
|             $user->vacationLimits()->create([ | ||||
|                 "year_period_id" => $yearPeriod->id, | ||||
|             ]); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -2,22 +2,30 @@ | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace Toby\Eloquent\Observers; | ||||
| namespace Toby\Domain\Actions; | ||||
| 
 | ||||
| use Toby\Domain\PolishHolidaysRetriever; | ||||
| use Toby\Eloquent\Models\User; | ||||
| use Toby\Eloquent\Models\YearPeriod; | ||||
| 
 | ||||
| class YearPeriodObserver | ||||
| class CreateYearPeriodAction | ||||
| { | ||||
|     public function __construct( | ||||
|         protected PolishHolidaysRetriever $polishHolidaysRetriever, | ||||
|     ) {} | ||||
| 
 | ||||
|     public function created(YearPeriod $yearPeriod): void | ||||
|     public function execute(int $year): YearPeriod | ||||
|     { | ||||
|         $yearPeriod = new YearPeriod([ | ||||
|             "year" => $year, | ||||
|         ]); | ||||
| 
 | ||||
|         $yearPeriod->save(); | ||||
| 
 | ||||
|         $this->createVacationLimitsFor($yearPeriod); | ||||
|         $this->createHolidaysFor($yearPeriod); | ||||
| 
 | ||||
|         return $yearPeriod; | ||||
|     } | ||||
| 
 | ||||
|     protected function createVacationLimitsFor(YearPeriod $yearPeriod): void | ||||
| @@ -5,7 +5,6 @@ declare(strict_types=1); | ||||
| namespace Toby\Domain; | ||||
|  | ||||
| use Carbon\CarbonPeriod; | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Illuminate\Support\Collection; | ||||
| use Toby\Eloquent\Helpers\YearPeriodRetriever; | ||||
| @@ -55,7 +54,7 @@ class CalendarGenerator | ||||
|     { | ||||
|         return Vacation::query() | ||||
|             ->whereBetween("date", [$period->start, $period->end]) | ||||
|             ->whereRelation("vacationRequest", fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates())) | ||||
|             ->approved() | ||||
|             ->with("vacationRequest") | ||||
|             ->get() | ||||
|             ->groupBy(fn(Vacation $vacation) => $vacation->date->toDateString()); | ||||
|   | ||||
| @@ -25,7 +25,6 @@ use PhpOffice\PhpSpreadsheet\Style\Fill; | ||||
| use PhpOffice\PhpSpreadsheet\Style\NumberFormat; | ||||
| use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | ||||
| use Toby\Domain\Enums\VacationType; | ||||
| use Toby\Domain\States\VacationRequest\Approved; | ||||
| use Toby\Eloquent\Models\Holiday; | ||||
| use Toby\Eloquent\Models\User; | ||||
| use Toby\Eloquent\Models\Vacation; | ||||
| @@ -189,7 +188,7 @@ class TimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, With | ||||
|         return $user->vacations() | ||||
|             ->with("vacationRequest") | ||||
|             ->whereBetween("date", [$period->start, $period->end]) | ||||
|             ->whereRelation("vacationRequest", "state", Approved::$name) | ||||
|             ->approved() | ||||
|             ->get() | ||||
|             ->groupBy( | ||||
|                 [ | ||||
|   | ||||
| @@ -88,7 +88,7 @@ class UserVacationStatsRetriever | ||||
|         $limit = $user->vacationLimits() | ||||
|             ->where("year_period_id", $yearPeriod->id) | ||||
|             ->first() | ||||
|             ->days; | ||||
|             ?->days; | ||||
|  | ||||
|         return $limit ?? 0; | ||||
|     } | ||||
|   | ||||
| @@ -30,13 +30,15 @@ class YearPeriodRetriever | ||||
|  | ||||
|     public function links(): array | ||||
|     { | ||||
|         $current = $this->selected(); | ||||
|         $selected = $this->selected(); | ||||
|         $current = $this->current(); | ||||
|  | ||||
|         $years = YearPeriod::query()->whereIn("year", $this->offset($current->year))->get(); | ||||
|         $years = YearPeriod::query()->whereIn("year", $this->offset($selected->year))->get(); | ||||
|         $navigation = $years->map(fn(YearPeriod $yearPeriod) => $this->toNavigation($yearPeriod)); | ||||
|  | ||||
|         return [ | ||||
|             "current" => $current->year, | ||||
|             "current" => $this->toNavigation($current), | ||||
|             "selected" => $this->toNavigation($selected), | ||||
|             "navigation" => $navigation->toArray(), | ||||
|         ]; | ||||
|     } | ||||
|   | ||||
| @@ -71,18 +71,6 @@ class User extends Authenticatable | ||||
|         return $this->hasMany(Vacation::class); | ||||
|     } | ||||
|  | ||||
|     public function scopeSearch(Builder $query, ?string $text): Builder | ||||
|     { | ||||
|         if ($text === null) { | ||||
|             return $query; | ||||
|         } | ||||
|  | ||||
|         return $query | ||||
|             ->where("first_name", "ILIKE", $text) | ||||
|             ->orWhere("last_name", "ILIKE", $text) | ||||
|             ->orWhere("email", "ILIKE", $text); | ||||
|     } | ||||
|  | ||||
|     public function getAvatar(): string | ||||
|     { | ||||
|         return $this->getAvatarGenerator() | ||||
| @@ -108,6 +96,28 @@ class User extends Authenticatable | ||||
|             ->exists(); | ||||
|     } | ||||
|  | ||||
|     public function scopeSearch(Builder $query, ?string $text): Builder | ||||
|     { | ||||
|         if ($text === null) { | ||||
|             return $query; | ||||
|         } | ||||
|  | ||||
|         return $query | ||||
|             ->where("first_name", "ILIKE", $text) | ||||
|             ->orWhere("last_name", "ILIKE", $text) | ||||
|             ->orWhere("email", "ILIKE", $text); | ||||
|     } | ||||
|  | ||||
|     public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder | ||||
|     { | ||||
|         return $query->whereRelation( | ||||
|             "vacationlimits", | ||||
|             fn(Builder $query) => $query | ||||
|                 ->where("year_period_id", $yearPeriod->id) | ||||
|                 ->whereNotNull("days"), | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     protected function getAvatarName(): string | ||||
|     { | ||||
|         return mb_substr($this->first_name, 0, 1) . mb_substr($this->last_name, 0, 1); | ||||
|   | ||||
| @@ -4,10 +4,12 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Eloquent\Models; | ||||
|  | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Toby\Domain\VacationRequestStatesRetriever; | ||||
|  | ||||
| /** | ||||
|  * @property int $id | ||||
| @@ -40,4 +42,12 @@ class Vacation extends Model | ||||
|     { | ||||
|         return $this->belongsTo(YearPeriod::class); | ||||
|     } | ||||
|  | ||||
|     public function scopeApproved(Builder $query): Builder | ||||
|     { | ||||
|         return $query->whereRelation( | ||||
|             "vacationRequest", | ||||
|             fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates()), | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -7,7 +7,6 @@ namespace Toby\Eloquent\Observers; | ||||
| use Illuminate\Contracts\Hashing\Hasher; | ||||
| use Illuminate\Support\Str; | ||||
| use Toby\Eloquent\Models\User; | ||||
| use Toby\Eloquent\Models\YearPeriod; | ||||
|  | ||||
| class UserObserver | ||||
| { | ||||
| @@ -23,15 +22,4 @@ class UserObserver | ||||
|          */ | ||||
|         $user->password = $this->hash->make(Str::random(40)); | ||||
|     } | ||||
|  | ||||
|     public function created(User $user): void | ||||
|     { | ||||
|         $yearPeriods = YearPeriod::all(); | ||||
|  | ||||
|         foreach ($yearPeriods as $yearPeriod) { | ||||
|             $user->vacationLimits()->create([ | ||||
|                 "year_period_id" => $yearPeriod->id, | ||||
|             ]); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,7 +4,6 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Infrastructure\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Inertia\Response; | ||||
| @@ -29,10 +28,7 @@ class DashboardController extends Controller | ||||
|         $absences = Vacation::query() | ||||
|             ->with(["user", "vacationRequest"]) | ||||
|             ->whereDate("date", $now) | ||||
|             ->whereRelation( | ||||
|                 "vacationRequest", | ||||
|                 fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates()), | ||||
|             ) | ||||
|             ->approved() | ||||
|             ->get(); | ||||
|  | ||||
|         if ($user->can("listAll", VacationRequest::class)) { | ||||
|   | ||||
| @@ -36,6 +36,6 @@ class GoogleController extends Controller | ||||
|  | ||||
|         $auth->guard()->login($user, true); | ||||
|  | ||||
|         return redirect()->route("dashboard"); | ||||
|         return redirect()->intended(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,7 +4,6 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Infrastructure\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Http\Request; | ||||
| use Inertia\Response; | ||||
| use Toby\Domain\Enums\Month; | ||||
| @@ -26,10 +25,7 @@ class MonthlyUsageController extends Controller | ||||
|         $currentUser = $request->user(); | ||||
|  | ||||
|         $users = User::query() | ||||
|             ->whereRelation( | ||||
|                 "vacationlimits", | ||||
|                 fn(Builder $query) => $query->where("year_period_id", $currentYearPeriod->id)->whereNotNull("days"), | ||||
|             ) | ||||
|             ->withVacationLimitIn($currentYearPeriod) | ||||
|             ->where("id", "!=", $currentUser->id) | ||||
|             ->orderBy("last_name") | ||||
|             ->orderBy("first_name") | ||||
|   | ||||
| @@ -8,6 +8,7 @@ use Illuminate\Auth\Access\AuthorizationException; | ||||
| use Illuminate\Http\RedirectResponse; | ||||
| use Illuminate\Http\Request; | ||||
| use Inertia\Response; | ||||
| use Toby\Domain\Actions\CreateUserAction; | ||||
| use Toby\Domain\Enums\EmploymentForm; | ||||
| use Toby\Domain\Enums\Role; | ||||
| use Toby\Eloquent\Models\User; | ||||
| @@ -54,11 +55,11 @@ class UserController extends Controller | ||||
|     /** | ||||
|      * @throws AuthorizationException | ||||
|      */ | ||||
|     public function store(UserRequest $request): RedirectResponse | ||||
|     public function store(UserRequest $request, CreateUserAction $createUserAction): RedirectResponse | ||||
|     { | ||||
|         $this->authorize("manageUsers"); | ||||
|  | ||||
|         User::query()->create($request->data()); | ||||
|         $createUserAction->execute($request->data()); | ||||
|  | ||||
|         return redirect() | ||||
|             ->route("users.index") | ||||
|   | ||||
| @@ -103,10 +103,7 @@ class VacationRequestController extends Controller | ||||
|             ->paginate(); | ||||
|  | ||||
|         $users = User::query() | ||||
|             ->whereRelation( | ||||
|                 "vacationlimits", | ||||
|                 fn(Builder $query) => $query->where("year_period_id", $yearPeriod->id)->whereNotNull("days"), | ||||
|             ) | ||||
|             ->withVacationLimitIn($yearPeriod) | ||||
|             ->orderBy("last_name") | ||||
|             ->orderBy("first_name") | ||||
|             ->get(); | ||||
| @@ -164,10 +161,7 @@ class VacationRequestController extends Controller | ||||
|         $yearPeriod = $yearPeriodRetriever->selected(); | ||||
|  | ||||
|         $users = User::query() | ||||
|             ->whereRelation( | ||||
|                 "vacationlimits", | ||||
|                 fn(Builder $query) => $query->where("year_period_id", $yearPeriod->id)->whereNotNull("days"), | ||||
|             ) | ||||
|             ->withVacationLimitIn($yearPeriod) | ||||
|             ->orderBy("last_name") | ||||
|             ->orderBy("first_name") | ||||
|             ->get(); | ||||
|   | ||||
| @@ -8,6 +8,7 @@ use Illuminate\Bus\Queueable; | ||||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||||
| use Illuminate\Foundation\Bus\Dispatchable; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Toby\Domain\Actions\CreateYearPeriodAction; | ||||
| use Toby\Eloquent\Models\YearPeriod; | ||||
|  | ||||
| class CheckYearPeriod implements ShouldQueue | ||||
| @@ -15,30 +16,17 @@ class CheckYearPeriod implements ShouldQueue | ||||
|     use Dispatchable; | ||||
|     use Queueable; | ||||
|  | ||||
|     public function handle(): void | ||||
|     public function handle(CreateYearPeriodAction $createYearPeriodAction): void | ||||
|     { | ||||
|         $currentYearPeriod = YearPeriod::current(); | ||||
|         $now = Carbon::now(); | ||||
|  | ||||
|         if ($currentYearPeriod === null) { | ||||
|             $this->createCurrentYearPeriod(); | ||||
|             $createYearPeriodAction->execute($now->year); | ||||
|         } | ||||
|  | ||||
|         if (YearPeriod::query()->max("year") === Carbon::now()->year) { | ||||
|             $this->createNextYearPeriod(); | ||||
|         if (YearPeriod::query()->max("year") === $now->year) { | ||||
|             $createYearPeriodAction->execute($now->year + 1); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     protected function createCurrentYearPeriod(): void | ||||
|     { | ||||
|         YearPeriod::query()->create([ | ||||
|             "year" => Carbon::now()->year, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     protected function createNextYearPeriod(): void | ||||
|     { | ||||
|         YearPeriod::query()->create([ | ||||
|             "year" => Carbon::now()->year + 1, | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user