Skip to content

Commit

Permalink
working on refactoring hr (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
inikoo committed Oct 1, 2024
1 parent 033fb19 commit 4aedb7b
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 198 deletions.
28 changes: 11 additions & 17 deletions app/Actions/HumanResources/Employee/StoreEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
use App\Actions\SysAdmin\Group\Hydrators\GroupHydrateEmployees;
use App\Actions\SysAdmin\Organisation\Hydrators\OrganisationHydrateEmployees;
use App\Actions\SysAdmin\User\StoreUser;
use App\Actions\Traits\WithPreparePositionsForValidation;
use App\Actions\Traits\WithReorganisePositions;
use App\Enums\HumanResources\Employee\EmployeeStateEnum;
use App\Models\HumanResources\Employee;
use App\Models\HumanResources\JobPosition;
use App\Models\HumanResources\Workplace;
use App\Models\SysAdmin\Organisation;
use App\Rules\AlphaDashDot;
Expand All @@ -31,10 +32,12 @@

class StoreEmployee extends OrgAction
{
use HasPositionsRules;
use WithPreparePositionsForValidation;
use WithReorganisePositions;

public function handle(Organisation|Workplace $parent, array $modelData): Employee
{

if (class_basename($parent) === 'Workplace') {
$organisation = $parent->organisation;
data_set($modelData, 'organisation_id', $organisation->id);
Expand All @@ -50,6 +53,8 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ

$positions = Arr::get($modelData, 'positions', []);
data_forget($modelData, 'positions');
$positions = $this->reorganisePositionsSlugsToIds($positions);


/** @var Employee $employee */
$employee = $parent->employees()->create($modelData);
Expand All @@ -61,6 +66,7 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ


if (Arr::get($credentials, 'username')) {

StoreUser::make()->action(
$employee,
[
Expand All @@ -79,18 +85,8 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ
);
}

$jobPositions = [];
SyncEmployeeJobPositions::run($employee, $positions);

foreach ($positions as $positionData) {

/** @var JobPosition $jobPosition */
$jobPosition = $this->organisation->jobPositions()->firstWhere('code', $positionData['code']);
if($jobPosition){
$jobPositions[$jobPosition->id] = $positionData['scopes'];
}
}

SyncEmployeeJobPositions::run($employee, $jobPositions);
EmployeeHydrateWeekWorkingHours::dispatch($employee);
GroupHydrateEmployees::dispatch($employee->group);
OrganisationHydrateEmployees::dispatch($organisation);
Expand All @@ -111,6 +107,7 @@ public function authorize(ActionRequest $request): bool

public function prepareForValidation(ActionRequest $request): void
{
$this->preparePositionsForValidation();
if (!$this->get('username')) {
$this->set('username', null);
}
Expand Down Expand Up @@ -159,7 +156,7 @@ public function rules(): array
'job_title' => ['sometimes', 'nullable', 'string', 'max:256'],
'state' => ['required', Rule::enum(EmployeeStateEnum::class)],
'positions' => ['sometimes', 'array'],
'positions.*.code' => ['sometimes', 'string'],
'positions.*.slug' => ['sometimes', 'string'],
'positions.*.scopes' => ['sometimes', 'array'],
'positions.*.scopes.organisations.slug.*' => ['sometimes', Rule::exists('organisations', 'slug')->where('group_id', $this->organisation->group_id)],
'positions.*.scopes.warehouses.slug.*' => ['sometimes', Rule::exists('warehouses', 'slug')->where('organisation_id', $this->organisation->id)],
Expand Down Expand Up @@ -211,9 +208,6 @@ public function action(Organisation|Workplace $parent, array $modelData, int $hy

public function asController(Organisation $organisation, ActionRequest $request): Employee
{



$this->initialisation($organisation, $request);

return $this->handle($organisation, $this->validatedData);
Expand Down

This file was deleted.

28 changes: 17 additions & 11 deletions app/Actions/HumanResources/Employee/UpdateEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
namespace App\Actions\HumanResources\Employee;

use App\Actions\HumanResources\Employee\Search\EmployeeRecordSearch;
use App\Actions\HumanResources\Employee\Traits\HasEmployeePositionGenerator;
use App\Actions\HumanResources\JobPosition\SyncEmployeeJobPositions;
use App\Actions\OrgAction;
use App\Actions\SysAdmin\Group\Hydrators\GroupHydrateEmployees;
use App\Actions\SysAdmin\Organisation\Hydrators\OrganisationHydrateEmployees;
use App\Actions\SysAdmin\User\UpdateUser;
use App\Actions\Traits\WithPreparePositionsForValidation;
use App\Actions\Traits\WithActionUpdate;
use App\Actions\Traits\WithReorganisePositions;
use App\Enums\HumanResources\Employee\EmployeeStateEnum;
use App\Enums\SysAdmin\User\UserAuthTypeEnum;
use App\Http\Resources\HumanResources\EmployeeResource;
Expand All @@ -31,25 +32,31 @@
class UpdateEmployee extends OrgAction
{
use WithActionUpdate;
use HasPositionsRules;
use HasEmployeePositionGenerator;
use WithPreparePositionsForValidation;
use WithReorganisePositions;

protected bool $asAction = false;

private Employee $employee;

public function handle(Employee $employee, array $modelData): Employee
{
if (Arr::exists($modelData, 'positions')) {
$jobPositions = $this->generatePositions($employee->organisation, $modelData);
SyncEmployeeJobPositions::run($employee, $jobPositions);
Arr::forget($modelData, 'positions');
}

$positions = Arr::get($modelData, 'positions', []);
data_forget($modelData, 'positions');
$positions = $this->reorganisePositionsSlugsToIds($positions);

SyncEmployeeJobPositions::run($employee, $positions);

$credentials = Arr::only($modelData, ['username', 'password', 'auth_type','user_model_status']);

data_forget($modelData, ['username', 'password', 'auth_type','user_model_status']);
data_forget($modelData, 'username');
data_forget($modelData, 'password');
data_forget($modelData, 'auth_type');
data_forget($modelData, 'user_model_status');




$employee = $this->update($employee, $modelData, ['data', 'salary']);

Expand Down Expand Up @@ -147,7 +154,7 @@ public function rules(): array
'job_title' => ['sometimes', 'nullable', 'string', 'max:256'],
'state' => ['sometimes', 'required', new Enum(EmployeeStateEnum::class)],
'positions' => ['sometimes', 'array'],
'positions.*.code' => ['sometimes', 'string'],
'positions.*.slug' => ['sometimes', 'string'],
'positions.*.scopes' => ['sometimes', 'array'],
'positions.*.scopes.warehouses.slug.*' => ['sometimes', Rule::exists('warehouses', 'slug')->where('organisation_id', $this->organisation->id)],
'positions.*.scopes.fulfilments.slug.*' => ['sometimes', Rule::exists('fulfilments', 'slug')->where('organisation_id', $this->organisation->id)],
Expand Down Expand Up @@ -186,7 +193,6 @@ public function rules(): array

];
$rules['password'] = ['sometimes', 'required', app()->isLocal() || app()->environment('testing') ? null : Password::min(8)->uncompromised()];

$rules['user_model_status'] = ['sometimes', 'boolean'];

}
Expand Down
5 changes: 2 additions & 3 deletions app/Actions/HumanResources/Employee/ValidatePinEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace App\Actions\HumanResources\Employee;

use App\Actions\HumanResources\Employee\Traits\HasEmployeePositionGenerator;
use App\Actions\OrgAction;
use App\Actions\Traits\WithPreparePositionsForValidation;
use App\Actions\Traits\WithActionUpdate;
use App\Http\Resources\HumanResources\EmployeeHanResource;
use App\Models\HumanResources\Employee;
Expand All @@ -17,8 +17,7 @@
class ValidatePinEmployee extends OrgAction
{
use WithActionUpdate;
use HasPositionsRules;
use HasEmployeePositionGenerator;
use WithPreparePositionsForValidation;

protected bool $asAction = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public function handle(Employee $employee, array $jobPositions): void
);
}

foreach ($employee->users as $user) {
SyncRolesFromJobPositions::run($user);
}

if (count($newJobPositionsIds) || count($removeJobPositions)) {
if ($user = $employee->getUser()) {
SyncRolesFromJobPositions::run($user);
}


EmployeeHydrateJobPositionsShare::run($employee);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public function handle(User $user, array $jobPositions): void

foreach ($newJobPositionsIds as $jobPositionId) {

$jobPosition = JobPosition::find($jobPositionId);

$pseudoJobPositionsData = [
'group_id' => $user->group_id,
'scopes' => $jobPositions[$jobPositionId]['scopes'],
'organisation_id' => $jobPositions[$jobPositionId]['organisation_id']
'scopes' => $jobPositions[$jobPositionId],
'organisation_id' => $jobPosition->organisation_id
];


Expand Down
30 changes: 16 additions & 14 deletions app/Actions/SysAdmin/Guest/StoreGuest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
use App\Actions\SysAdmin\Group\Hydrators\GroupHydrateGuests;
use App\Actions\SysAdmin\Guest\Hydrators\GuestHydrateUniversalSearch;
use App\Actions\SysAdmin\User\StoreUser;
use App\Models\HumanResources\JobPosition;
use App\Actions\Traits\WithPreparePositionsForValidation;
use App\Actions\Traits\WithReorganisePositions;
use App\Models\SysAdmin\Group;
use App\Models\SysAdmin\Guest;
use App\Rules\AlphaDashDot;
Expand All @@ -23,20 +24,23 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Validator;
use Lorisleiva\Actions\ActionRequest;

class StoreGuest extends GrpAction
{
use WithPreparePositionsForValidation;
use WithReorganisePositions;
private bool $validatePhone = false;

public function handle(Group $group, array $modelData): Guest
{
$positions = Arr::get($modelData, 'positions', []);
data_forget($modelData, 'positions');
$positions = $this->reorganisePositionsSlugsToIds($positions);


/** @var Guest $guest */
Expand All @@ -63,18 +67,9 @@ public function handle(Group $group, array $modelData): Guest
]
);

$jobPositions = [];
foreach ($positions as $positionData) {
$jobPosition = JobPosition::firstWhere('slug', $positionData['slug']);
$jobPositions[$jobPosition->id] = [
'scopes' => $positionData['scopes'],
'organisation_id' => $jobPosition->organisation_id
];


}

SyncUserJobPositions::run($user, $jobPositions);
SyncUserJobPositions::run($user, $positions);


GroupHydrateGuests::dispatch($group);
Expand All @@ -99,6 +94,13 @@ public function prepareForValidation(): void
if ($this->get('phone')) {
$this->set('phone', preg_replace('/[^0-9+]/', '', $this->get('phone')));
}

if ($this->get('positions')) {
$this->set('phone', preg_replace('/[^0-9+]/', '', $this->get('phone')));
}

$this->preparePositionsForValidation();

}

public function afterValidator(Validator $validator, ActionRequest $request): void
Expand Down Expand Up @@ -139,7 +141,7 @@ public function rules(): array
'email' => ['sometimes', 'nullable', 'email'],
'positions' => ['sometimes', 'array'],

'positions.*.code' => ['sometimes', 'string'],
'positions.*.slug' => ['sometimes', 'string'],
'positions.*.scopes' => ['sometimes', 'array'],

'positions.*.scopes.organisations.slug.*' => ['sometimes', Rule::exists('organisations', 'slug')->where('group_id', $this->group->id)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function handle(User $user): void
$authorisedProductions = [];



foreach ($user->getAllPermissions() as $permission) {
if ($permission->scope_type === 'Organisation') {
$authorisedOrganisations[$permission->scope_id] = ['org_id' => $permission->scope_id];
Expand Down Expand Up @@ -69,6 +70,7 @@ public function handle(User $user): void
'number_authorised_warehouses' => count($authorisedWarehouses),
'number_authorised_productions' => count($authorisedProductions),
];

$user->update($stats);

foreach ($user->group->organisations as $organisation) {
Expand Down
Loading

0 comments on commit 4aedb7b

Please sign in to comment.