Skip to content

Commit

Permalink
Merge branch 'main' into #231-add-new-tab-to-menu-about-benefits-report
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/Infrastructure/Http/Middleware/HandleInertiaRequests.php
  • Loading branch information
EwelinaSkrzypacz committed Sep 26, 2023
2 parents d10cbf0 + 6a0e723 commit c92bcfe
Show file tree
Hide file tree
Showing 86 changed files with 2,149 additions and 343 deletions.
21 changes: 0 additions & 21 deletions app/Architecture/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
namespace Toby\Architecture\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Toby\Domain\Enums\Role;
use Toby\Domain\Policies\KeyPolicy;
use Toby\Domain\Policies\VacationRequestPolicy;
use Toby\Eloquent\Models\Key;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;

class AuthServiceProvider extends ServiceProvider
Expand All @@ -19,22 +16,4 @@ class AuthServiceProvider extends ServiceProvider
VacationRequest::class => VacationRequestPolicy::class,
Key::class => KeyPolicy::class,
];

public function boot(): void
{
Gate::before(function (User $user) {
if ($user->role === Role::Administrator) {
return true;
}
});

Gate::define("manageUsers", fn(User $user): bool => $user->role === Role::AdministrativeApprover);
Gate::define("manageHolidays", fn(User $user): bool => $user->role === Role::AdministrativeApprover);
Gate::define("manageVacationLimits", fn(User $user): bool => $user->role === Role::AdministrativeApprover);
Gate::define("generateTimesheet", fn(User $user): bool => $user->role === Role::AdministrativeApprover);
Gate::define("listMonthlyUsage", fn(User $user): bool => $user->role === Role::AdministrativeApprover);
Gate::define("manageResumes", fn(User $user): bool => $user->role === Role::TechnicalApprover);
Gate::define("manageBenefits", fn(User $user): bool => $user->role === Role::AdministrativeApprover);
Gate::define("seePendingRequests", fn(User $user): bool => $user->role === Role::AdministrativeApprover || $user->role === Role::TechnicalApprover);
}
}
17 changes: 17 additions & 0 deletions app/Domain/Actions/SyncUserPermissionsWithRoleAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Toby\Domain\Actions;

use Toby\Eloquent\Models\User;

class SyncUserPermissionsWithRoleAction
{
public function execute(User $user): User
{
$user->syncPermissions($user->role->permissions());

return $user;
}
}
23 changes: 23 additions & 0 deletions app/Domain/Actions/UpdateUserPermissionsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Toby\Domain\Actions;

use Toby\Eloquent\Models\User;

class UpdateUserPermissionsAction
{
public function execute(User $user, array $permissions): User
{
foreach ($permissions as $permission => $value) {
if ($value) {
$user->givePermissionTo($permission);
} else {
$user->revokePermissionTo($permission);
}
}

return $user;
}
}
7 changes: 2 additions & 5 deletions app/Domain/Actions/VacationRequest/ApproveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Toby\Domain\Actions\VacationRequest;

use Toby\Domain\Enums\Role;
use Spatie\Permission\Models\Permission;
use Toby\Domain\Events\VacationRequestChanged;
use Toby\Domain\Notifications\VacationRequestStatusChangedNotification;
use Toby\Domain\VacationRequestStateManager;
Expand Down Expand Up @@ -35,10 +35,7 @@ public function execute(VacationRequest $vacationRequest, ?User $user = null): v

protected function notify(VacationRequest $vacationRequest): void
{
$users = User::query()
->where("id", "!=", $vacationRequest->user->id)
->whereIn("role", [Role::TechnicalApprover, Role::AdministrativeApprover, Role::Administrator])
->get();
$users = Permission::findByName("receiveVacationRequestStatusChangedNotification")->users()->get();

foreach ($users as $user) {
$user->notify(new VacationRequestStatusChangedNotification($vacationRequest, $user));
Expand Down
7 changes: 2 additions & 5 deletions app/Domain/Actions/VacationRequest/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Toby\Domain\Actions\VacationRequest;

use Toby\Domain\Enums\Role;
use Spatie\Permission\Models\Permission;
use Toby\Domain\Events\VacationRequestChanged;
use Toby\Domain\Notifications\VacationRequestStatusChangedNotification;
use Toby\Domain\VacationRequestStateManager;
Expand Down Expand Up @@ -35,10 +35,7 @@ public function execute(VacationRequest $vacationRequest, User $user): void

protected function notify(VacationRequest $vacationRequest): void
{
$users = User::query()
->where("id", "!=", $vacationRequest->user->id)
->whereIn("role", [Role::TechnicalApprover, Role::AdministrativeApprover, Role::Administrator])
->get();
$users = Permission::findByName("receiveVacationRequestStatusChangedNotification")->users()->get();

foreach ($users as $user) {
$user->notify(new VacationRequestStatusChangedNotification($vacationRequest, $user));
Expand Down
6 changes: 5 additions & 1 deletion app/Domain/Actions/VacationRequest/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ protected function createVacationRequest(array $data, User $creator): VacationRe

$vacationRequest->save();

$days = $this->workDaysCalculator->calculateDays($vacationRequest->from, $vacationRequest->to);
$days = $this->workDaysCalculator->calculateDays(
$vacationRequest->from,
$vacationRequest->to,
$vacationRequest->type,
);

foreach ($days as $day) {
$vacationRequest->vacations()->create([
Expand Down
6 changes: 3 additions & 3 deletions app/Domain/Actions/VacationRequest/RejectAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Toby\Domain\Actions\VacationRequest;

use Toby\Domain\Enums\Role;
use Spatie\Permission\Models\Permission;
use Toby\Domain\Events\VacationRequestChanged;
use Toby\Domain\Notifications\VacationRequestStatusChangedNotification;
use Toby\Domain\VacationRequestStateManager;
Expand All @@ -28,9 +28,9 @@ public function execute(VacationRequest $vacationRequest, User $user): void

protected function notify(VacationRequest $vacationRequest): void
{
$users = User::query()
$users = Permission::findByName("receiveVacationRequestStatusChangedNotification")
->users()
->where("id", "!=", $vacationRequest->user->id)
->whereIn("role", [Role::TechnicalApprover, Role::AdministrativeApprover, Role::Administrator])
->get();

foreach ($users as $user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Toby\Domain\Actions\VacationRequest;

use Toby\Domain\Enums\Role;
use Spatie\Permission\Models\Permission;
use Toby\Domain\Notifications\VacationRequestWaitsForApprovalNotification;
use Toby\Domain\VacationRequestStateManager;
use Toby\Domain\VacationTypeConfigRetriever;
Expand All @@ -24,16 +24,18 @@ public function execute(VacationRequest $vacationRequest): void
$this->stateManager->waitForAdministrative($vacationRequest);

if ($this->configRetriever->isVacation($vacationRequest->type)) {
$this->notifyAdminApprovers($vacationRequest);
$this->notifyAuthorizedUsers($vacationRequest);
}
}

protected function notifyAdminApprovers(VacationRequest $vacationRequest): void
protected function notifyAuthorizedUsers(VacationRequest $vacationRequest): void
{
$users = User::query()
->whereIn("role", [Role::AdministrativeApprover, Role::Administrator])
$users = Permission::findByName("receiveVacationRequestWaitsForApprovalNotification")
->users()
->get();

$users = $users->filter(fn(User $user): bool => $user->can("acceptAsAdminApprover", $vacationRequest));

foreach ($users as $user) {
$user->notify(new VacationRequestWaitsForApprovalNotification($vacationRequest, $user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Toby\Domain\Actions\VacationRequest;

use Toby\Domain\Enums\Role;
use Spatie\Permission\Models\Permission;
use Toby\Domain\Notifications\VacationRequestWaitsForApprovalNotification;
use Toby\Domain\VacationRequestStateManager;
use Toby\Domain\VacationTypeConfigRetriever;
Expand All @@ -30,10 +30,12 @@ public function execute(VacationRequest $vacationRequest): void

protected function notifyTechApprovers(VacationRequest $vacationRequest): void
{
$users = User::query()
->whereIn("role", [Role::TechnicalApprover, Role::Administrator])
$users = Permission::findByName("receiveVacationRequestWaitsForApprovalNotification")
->users()
->get();

$users = $users->filter(fn(User $user): bool => $user->can("acceptAsTechApprover", $vacationRequest));

foreach ($users as $user) {
$user->notify(new VacationRequestWaitsForApprovalNotification($vacationRequest, $user));
}
Expand Down
3 changes: 1 addition & 2 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Toby\Eloquent\Models\Holiday;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\Vacation;
use Toby\Eloquent\Models\VacationRequest;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Resources\BirthdayResource;
use Toby\Infrastructure\Http\Resources\HolidayResource;
Expand Down Expand Up @@ -85,7 +84,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array

public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): JsonResource
{
if ($user->can("listAll", VacationRequest::class)) {
if ($user->can("listAllRequests")) {
$vacationRequests = $yearPeriod->vacationRequests()
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
->latest("updated_at")
Expand Down
5 changes: 5 additions & 0 deletions app/Domain/Enums/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public static function casesToSelect(): array
],
)->toArray();
}

public function permissions(): array
{
return config("permission.permission_roles")[$this->value];
}
}
1 change: 1 addition & 0 deletions app/Domain/Enums/VacationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum VacationType: string
case Sick = "sick_vacation";
case Absence = "absence";
case RemoteWork = "remote_work";
case Delegation = "delegation";

public function label(): string
{
Expand Down
8 changes: 1 addition & 7 deletions app/Domain/Policies/KeyPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@

namespace Toby\Domain\Policies;

use Toby\Domain\Enums\Role;
use Toby\Eloquent\Models\Key;
use Toby\Eloquent\Models\User;

class KeyPolicy
{
public function manage(User $user): bool
{
return $user->role === Role::AdministrativeApprover;
}

public function give(User $user, Key $key): bool
{
if ($key->user()->is($user)) {
return true;
}

return $user->role === Role::AdministrativeApprover;
return $user->hasPermissionTo("manageKeys");
}
}
25 changes: 8 additions & 17 deletions app/Domain/Policies/VacationRequestPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Toby\Domain\Policies;

use Toby\Domain\Enums\Role;
use Toby\Domain\Enums\VacationType;
use Toby\Domain\States\VacationRequest\Created;
use Toby\Domain\States\VacationRequest\WaitingForAdministrative;
Expand All @@ -14,34 +13,25 @@

class VacationRequestPolicy
{
public function listAll(User $user): bool
{
return in_array($user->role, [Role::AdministrativeApprover, Role::TechnicalApprover], true);
}

public function createOnBehalfOfEmployee(User $user): bool
{
return $user->role === Role::AdministrativeApprover;
}

public function acceptAsAdminApprover(User $user): bool
{
return $user->role === Role::AdministrativeApprover;
return $user->hasPermissionTo("manageRequestsAsAdministrativeApprover");
}

public function acceptAsTechApprover(User $user): bool
{
return $user->role === Role::TechnicalApprover;
return $user->hasPermissionTo("manageRequestsAsTechnicalApprover");
}

public function skipFlow(User $user): bool
{
return $user->role === Role::AdministrativeApprover;
return $user->hasPermissionTo("manageRequestsAsAdministrativeApprover");
}

public function reject(User $user): bool
{
return in_array($user->role, [Role::AdministrativeApprover, Role::TechnicalApprover], true);
return $user->hasPermissionTo("manageRequestsAsAdministrativeApprover") ||
$user->hasPermissionTo("manageRequestsAsTechnicalApprover");
}

public function cancel(User $user, VacationRequest $vacationRequest): bool
Expand All @@ -58,7 +48,7 @@ public function cancel(User $user, VacationRequest $vacationRequest): bool
return true;
}

return $user->role === Role::AdministrativeApprover;
return $user->hasPermissionTo("manageRequestsAsAdministrativeApprover");
}

public function show(User $user, VacationRequest $vacationRequest): bool
Expand All @@ -67,6 +57,7 @@ public function show(User $user, VacationRequest $vacationRequest): bool
return true;
}

return in_array($user->role, [Role::TechnicalApprover, Role::AdministrativeApprover], true);
return $user->hasPermissionTo("manageRequestsAsAdministrativeApprover") ||
$user->hasPermissionTo("manageRequestsAsTechnicalApprover");
}
}
40 changes: 40 additions & 0 deletions app/Domain/UnavailableDaysRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Toby\Domain\Enums\VacationType;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\YearPeriod;

class UnavailableDaysRetriever
{
public function __construct(
protected UserVacationStatsRetriever $vacationStatsRetriever,
protected VacationTypeConfigRetriever $configRetriever,
) {}

public function getUnavailableDays(User $user, YearPeriod $yearPeriod, ?VacationType $vacationType = null): Collection
{
$unavailableDays = $user->vacations()
->whereBelongsTo($yearPeriod)
->whereRelation(
"vacationRequest",
fn(Builder $query): Builder => $query->noStates(VacationRequestStatesRetriever::failedStates()),
)
->pluck("date");

if (!$vacationType || !$this->configRetriever->isDuringNonWorkDays($vacationType)) {
$unavailableDays->push(...$yearPeriod->weekends());
$unavailableDays->push(...$yearPeriod->holidays()->pluck("date"));
}

return $unavailableDays
->unique()
->sort()
->values();
}
}
1 change: 1 addition & 0 deletions app/Domain/UserVacationStatsRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function getOtherApprovedVacationDays(User $user, YearPeriod $yearPeriod)
fn(Builder $query): Builder => $query
->whereIn("type", $this->getNotLimitableVacationTypes())
->whereNot("type", VacationType::RemoteWork)
->whereNot("type", VacationType::Delegation)
->states(VacationRequestStatesRetriever::successStates()),
)
->count();
Expand Down
Loading

0 comments on commit c92bcfe

Please sign in to comment.