commit21
This commit is contained in:
@@ -13,6 +13,12 @@ class SupplierImprovementWorkflow
|
||||
{
|
||||
public function canParticipate(User $actor, SupplierEvaluation $evaluation): bool
|
||||
{
|
||||
if ($actor->tenant_id === null || $actor->tenant_id !== $evaluation->tenant_id
|
||||
|| ! $actor->hasFunctions(SupplierEvaluationWorkflow::ROLES)
|
||||
|| app(OrganizationAccess::class)->maintenanceOnly($actor)
|
||||
|| ($actor->departmentScoped() && ($actor->department_id === null || $actor->department_id !== $evaluation->department_id))) {
|
||||
return false;
|
||||
}
|
||||
$guard = app(ConflictGuard::class);
|
||||
if ($guard->hasConflict($actor->id, $evaluation->supplier_id, null)) {
|
||||
return false;
|
||||
@@ -26,6 +32,21 @@ class SupplierImprovementWorkflow
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canAct(User $actor, SupplierEvaluation $evaluation, SupplierImprovement $measure, string $action): bool
|
||||
{
|
||||
if ($evaluation->status !== 'approved' || $measure->tenant_id !== $actor->tenant_id
|
||||
|| $measure->supplier_evaluation_id !== $evaluation->id || ! $this->canParticipate($actor, $evaluation)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return match ($action) {
|
||||
'plan' => in_array($measure->status, ['open', 'returned'], true),
|
||||
'execute' => $measure->responsible_id === $actor->id && in_array($measure->status, ['open', 'returned'], true),
|
||||
'review' => $measure->status === 'review' && ! in_array($actor->id, $measure->contributors, true),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function snapshot(SupplierImprovement $measure): array
|
||||
{
|
||||
@@ -75,7 +96,7 @@ class SupplierImprovementWorkflow
|
||||
abort_if($measure->status === 'closed', 409, 'A medida já está encerrada e preservada.');
|
||||
$before = $this->snapshot($measure);
|
||||
if ($command === 'plan') {
|
||||
abort_unless(in_array($measure->status, ['open', 'returned'], true), 409);
|
||||
abort_unless($this->canAct($actor, $evaluation, $measure, 'plan'), 409);
|
||||
$responsible = $this->assignee($actor, $evaluation, (int) $data['responsible_id']);
|
||||
$measure->fill(['title' => $data['title'], 'description' => $data['description'], 'responsible_id' => $responsible->id, 'due_on' => $data['due_on'],
|
||||
'contributors' => array_values(array_unique([...$measure->contributors, $actor->id, $responsible->id]))]);
|
||||
@@ -86,7 +107,7 @@ class SupplierImprovementWorkflow
|
||||
throw ValidationException::withMessages(['reason' => 'Justifique a alteração do plano, prazo ou responsável.']);
|
||||
}
|
||||
} elseif (in_array($command, ['progress', 'submit'], true)) {
|
||||
abort_unless($actor->id === $measure->responsible_id && in_array($measure->status, ['open', 'returned'], true), 403);
|
||||
abort_unless($this->canAct($actor, $evaluation, $measure, 'execute'), 403);
|
||||
$measure->fill(['result' => $data['result'] ?? '', 'evidence' => $data['evidence'] ?? '']);
|
||||
if ($command === 'submit') {
|
||||
if (trim($measure->result ?? '') === '' || trim($measure->evidence ?? '') === '') {
|
||||
@@ -99,7 +120,7 @@ class SupplierImprovementWorkflow
|
||||
}
|
||||
} else {
|
||||
abort_unless(in_array($command, ['close', 'return'], true) && $measure->status === 'review', 409);
|
||||
abort_if(in_array($actor->id, $measure->contributors, true), 403, 'O encerramento exige outra pessoa, independente da preparação e execução.');
|
||||
abort_unless($this->canAct($actor, $evaluation, $measure, 'review'), 403, 'O encerramento exige outra pessoa, independente da preparação e execução.');
|
||||
if (mb_strlen(trim($data['reason'] ?? '')) < 10) {
|
||||
throw ValidationException::withMessages(['reason' => 'Fundamente a decisão sobre a execução.']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user