Skip to content
This repository has been archived by the owner on Feb 8, 2025. It is now read-only.

Commit

Permalink
feat: remove employee hierarchy when locking employees (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jun 12, 2021
1 parent 294bbaa commit 0b987f8
Showing 1 changed file with 56 additions and 30 deletions.
86 changes: 56 additions & 30 deletions app/Services/Company/Adminland/Employee/LockEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Services\BaseService;
use App\Jobs\LogEmployeeAudit;
use App\Models\Company\Employee;
use App\Models\Company\DirectReport;
use App\Services\Company\Adminland\Expense\DisallowEmployeeToManageExpenses;
use App\Jobs\CheckIfPendingExpenseShouldBeMovedToAccountingWhenManagerChanges;

Expand Down Expand Up @@ -36,44 +37,32 @@ public function rules(): array
*/
public function execute(array $data): void
{
$this->validateRules($data);
$this->data = $data;
$this->validate();
$this->lockEmployee();
$this->removeAccountantRole();
$this->changePotentialExpensesStatuses();
$this->removeEmployeeHierarchy();
$this->log();
}

$this->author($data['author_id'])
->inCompany($data['company_id'])
private function validate(): void
{
$this->validateRules($this->data);

$this->author($this->data['author_id'])
->inCompany($this->data['company_id'])
->asAtLeastHR()
->canExecuteService();

$this->data = $data;

$this->employee = $this->validateEmployeeBelongsToCompany($data);
$this->employee = $this->validateEmployeeBelongsToCompany($this->data);
}

private function lockEmployee(): void
{
Employee::where('id', $this->employee->id)->update([
'locked' => true,
]);

$this->removeAccountantRole();

$this->changePotentialExpensesStatuses();

LogAccountAudit::dispatch([
'company_id' => $data['company_id'],
'action' => 'employee_locked',
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'audited_at' => Carbon::now(),
'objects' => json_encode([
'employee_name' => $this->employee->name,
]),
])->onQueue('low');

LogEmployeeAudit::dispatch([
'employee_id' => $data['employee_id'],
'action' => 'employee_locked',
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'audited_at' => Carbon::now(),
'objects' => json_encode([]),
])->onQueue('low');
}

/**
Expand All @@ -97,4 +86,41 @@ private function changePotentialExpensesStatuses(): void
CheckIfPendingExpenseShouldBeMovedToAccountingWhenManagerChanges::dispatch($this->employee->company)
->onQueue('low');
}

/**
* Remove any link of hierarchy between this employee and other potential employee.
*/
private function removeEmployeeHierarchy(): void
{
DirectReport::where('company_id', $this->data['company_id'])
->where('employee_id', $this->employee->id)
->delete();

DirectReport::where('company_id', $this->data['company_id'])
->where('manager_id', $this->employee->id)
->delete();
}

private function log(): void
{
LogAccountAudit::dispatch([
'company_id' => $this->data['company_id'],
'action' => 'employee_locked',
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'audited_at' => Carbon::now(),
'objects' => json_encode([
'employee_name' => $this->employee->name,
]),
])->onQueue('low');

LogEmployeeAudit::dispatch([
'employee_id' => $this->data['employee_id'],
'action' => 'employee_locked',
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'audited_at' => Carbon::now(),
'objects' => json_encode([]),
])->onQueue('low');
}
}

0 comments on commit 0b987f8

Please sign in to comment.