Skip to content

Commit

Permalink
Merge branch 'main' of github.com:inikoo/aiku
Browse files Browse the repository at this point in the history
  • Loading branch information
YudhistiraA committed Oct 1, 2024
2 parents cb5f2e3 + 377e1af commit 7f15661
Show file tree
Hide file tree
Showing 40 changed files with 1,102 additions and 443 deletions.
102 changes: 102 additions & 0 deletions app/Actions/CRM/CustomerNote/StoreCustomerNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/*
* Author: Raul Perusquia <raul@inikoo.com>
* Created: Tue, 01 Oct 2024 10:17:17 Malaysia Time, Kuala Lumpur, Malaysia
* Copyright (c) 2024, Raul A Perusquia Flores
*/

namespace App\Actions\CRM\CustomerNote;

use App\Actions\OrgAction;
use App\Actions\Traits\WithModelAddressActions;
use App\Models\CRM\Customer;
use App\Models\CRM\CustomerNote;
use App\Models\SysAdmin\User;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
use OwenIt\Auditing\Resolvers\IpAddressResolver;
use OwenIt\Auditing\Resolvers\UrlResolver;
use OwenIt\Auditing\Resolvers\UserAgentResolver;
use OwenIt\Auditing\Resolvers\UserResolver;

class StoreCustomerNote extends OrgAction
{
use WithModelAddressActions;


public function handle(Customer $customer, array $modelData): CustomerNote
{
/** @var User $user */
$user = UserResolver::resolve();

data_set($modelData, 'group_id', $customer->group_id);
data_set($modelData, 'organisation_id', $customer->organisation_id);
data_set($modelData, 'shop_id', $customer->shop_id);
data_set($modelData, 'customer_id', $customer->id);

data_set($modelData, 'user_type', class_basename($user), overwrite: false);
data_set($modelData, 'user_id', $user->id, overwrite: false);

data_set($modelData, 'auditable_type', 'Customer');
data_set($modelData, 'auditable_id', $customer->id);

data_set($modelData, 'tags', ['customer_notes']);
data_set($modelData, 'event', 'note_created');
data_set($modelData, 'new_values', ['note' => $modelData['note']]);

data_set($modelData, 'url', UrlResolver::resolve($customer));
data_set($modelData, 'ip_address', IpAddressResolver::resolve($customer));
data_set($modelData, 'user_agent', UserAgentResolver::resolve($customer));


/** @var CustomerNote $CustomerNote */
$CustomerNote = $customer->customerNotes()->create($modelData);


return $CustomerNote;
}

public function authorize(ActionRequest $request): bool
{
if ($this->asAction) {
return true;
}

return $request->user()->hasPermissionTo("crm.{$this->shop->id}.edit");
}

public function rules(): array
{
$rules = [
'note' => ['required', 'string', 'max:1024'],
];

if (!$this->strict) {
$rules['user_type'] = ['sometimes', Rule::in(['User', 'WebUser'])];
$rules['user_id'] = ['required', 'integer'];
}

return $rules;
}


public function action(Customer $customer, array $modelData, int $hydratorsDelay = 0, bool $strict = true): CustomerNote
{
$this->asAction = true;
$this->strict = $strict;
$this->hydratorsDelay = $hydratorsDelay;
$this->initialisationFromShop($customer->shop, $modelData);

return $this->handle($customer, $this->validatedData);
}

public function asController(Customer $customer, ActionRequest $request): CustomerNote
{
$this->asAction = true;
$this->initialisationFromShop($customer->shop, $request);

return $this->handle($customer, $this->validatedData);
}


}
68 changes: 68 additions & 0 deletions app/Actions/CRM/CustomerNote/UpdateCustomerNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/*
* Author: Raul Perusquia <raul@inikoo.com>
* Created: Tue, 01 Oct 2024 10:17:17 Malaysia Time, Kuala Lumpur, Malaysia
* Copyright (c) 2024, Raul A Perusquia Flores
*/

namespace App\Actions\CRM\CustomerNote;

use App\Actions\OrgAction;
use App\Actions\Traits\WithActionUpdate;
use App\Models\Catalogue\Shop;
use App\Models\CRM\CustomerNote;
use App\Models\SysAdmin\Organisation;
use Lorisleiva\Actions\ActionRequest;

class UpdateCustomerNote extends OrgAction
{
use WithActionUpdate;

public function handle(CustomerNote $customerNote, array $modelData): CustomerNote
{
return $this->update($customerNote, $modelData, ['data']);
}

public function authorize(ActionRequest $request): bool
{
if ($this->asAction) {
return true;
}

return $request->user()->hasPermissionTo("crm.{$this->shop->id}.edit");
}

public function rules(): array
{
$rules = [
'note' => ['sometimes', 'string', 'max:1024'],
];

if (!$this->strict) {
$rules['note'] = ['sometimes', 'string', 'max:4096'];
}

return $rules;
}


public function asController(Organisation $organisation, Shop $shop, CustomerNote $customerNote, ActionRequest $request): CustomerNote
{
$this->initialisationFromShop($shop, $request);

return $this->handle($customerNote, $this->validatedData);
}

public function action(CustomerNote $customerNote, array $modelData, int $hydratorsDelay = 0, bool $strict = true): CustomerNote
{
$this->asAction = true;
$this->strict = $strict;
$this->hydratorsDelay = $hydratorsDelay;
$this->setRawAttributes($modelData);
$this->initialisationFromShop($customerNote->shop, $modelData);

return $this->handle($customerNote, $this->validatedData);
}


}
19 changes: 11 additions & 8 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,13 +85,8 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ
);
}

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

SyncEmployeeJobPositions::run($employee, $jobPositions);
EmployeeHydrateWeekWorkingHours::dispatch($employee);
GroupHydrateEmployees::dispatch($employee->group);
OrganisationHydrateEmployees::dispatch($organisation);
Expand All @@ -106,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 @@ -188,6 +190,7 @@ public function rules(): array

public function action(Organisation|Workplace $parent, array $modelData, int $hydratorsDelay = 0, bool $strict = true): Employee
{

$this->asAction = true;
$this->strict = $strict;
$this->hydratorsDelay = $hydratorsDelay;
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 @@ -108,7 +115,7 @@ public function rules(): array
),

],
'employment_start_at' => ['sometimes', 'nullable', 'date'],
'state.employment_start_at' => ['sometimes', 'nullable', 'date'],
'work_email' => [
'sometimes',
'nullable',
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
Loading

0 comments on commit 7f15661

Please sign in to comment.