commit21
This commit is contained in:
@@ -10,6 +10,21 @@ use Illuminate\Validation\ValidationException;
|
||||
|
||||
class SupplierResponseWorkflow
|
||||
{
|
||||
public function canAct(User $actor, SupplierEvaluation $evaluation, SupplierResponse $record, string $action): bool
|
||||
{
|
||||
if ($evaluation->status !== 'approved' || $record->tenant_id !== $actor->tenant_id
|
||||
|| $record->supplier_evaluation_id !== $evaluation->id
|
||||
|| ! app(SupplierImprovementWorkflow::class)->canParticipate($actor, $evaluation)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return match ($action) {
|
||||
'edit' => in_array($record->status, ['draft', 'returned'], true),
|
||||
'review' => $record->status === 'review' && ! in_array($actor->id, $record->contributors, true),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function snapshot(SupplierResponse $record): array
|
||||
{
|
||||
@@ -55,6 +70,7 @@ class SupplierResponseWorkflow
|
||||
$before = $this->snapshot($record);
|
||||
if ($command === 'save') {
|
||||
abort_unless(in_array($record->status, ['draft', 'returned'], true), 409);
|
||||
abort_unless($this->canAct($actor, $evaluation, $record, 'edit'), 403);
|
||||
$payload = $this->normalise($data['payload']);
|
||||
if ($payload === $record->payload) {
|
||||
return $record;
|
||||
@@ -62,6 +78,7 @@ class SupplierResponseWorkflow
|
||||
$record->fill(['payload' => $payload, 'contributors' => array_values(array_unique([...$record->contributors, $actor->id]))]);
|
||||
} elseif ($command === 'submit') {
|
||||
abort_unless(in_array($record->status, ['draft', 'returned'], true), 409);
|
||||
abort_unless($this->canAct($actor, $evaluation, $record, 'edit'), 403);
|
||||
$errors = [];
|
||||
foreach (['received_on', 'source_reference', 'statement'] as $key) {
|
||||
if (trim($record->payload[$key] ?? '') === '') {
|
||||
@@ -77,7 +94,7 @@ class SupplierResponseWorkflow
|
||||
$record->fill(['status' => 'review', 'contributors' => array_values(array_unique([...$record->contributors, $actor->id]))]);
|
||||
} else {
|
||||
abort_unless(in_array($command, ['return', 'decide'], true) && $record->status === 'review', 409);
|
||||
abort_if(in_array($actor->id, $record->contributors, true), 403, 'A análise deve ser concluída por outra pessoa que não preparou o registo.');
|
||||
abort_unless($this->canAct($actor, $evaluation, $record, 'review'), 403, 'A análise deve ser concluída por outra pessoa que não preparou o registo.');
|
||||
if (mb_strlen(trim($data['reason'] ?? '')) < 10) {
|
||||
throw ValidationException::withMessages(['reason' => 'Fundamente a análise ou a devolução.']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user