Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/Http/Requests/Notice/NoticeUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function rules(): array
],

'instrument_type' => [
'sometimes',
'required',
Comment thread
jeff-doliveira1 marked this conversation as resolved.
function (string $attribute, mixed $value, \Closure $fail) use ($notice) {
if ($notice) {
$currentValue = $notice->instrument_type instanceof \BackedEnum
Expand All @@ -52,9 +52,7 @@ function (string $attribute, mixed $value, \Closure $fail) use ($notice) {
}
}
},
Rule::when(fn () => filled($this->input('instrument_type')), [
new Enum(InstrumentType::class),
]),
new Enum(InstrumentType::class),
],

'name' => ['sometimes', 'string'],
Expand Down
40 changes: 22 additions & 18 deletions app/Services/NoticeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,34 @@ public function update(Notice $notice, array $data): Notice
{
return DB::transaction(function () use ($notice, $data) {
if (array_key_exists('instrument_type', $data)) {
$projectIds = Project::where('notice_id', $notice->id)
->orderBy('id')
->lockForUpdate()
->pluck('id');
if (is_null($data['instrument_type'])) {
unset($data['instrument_type']);
} else {
$projectIds = Project::where('notice_id', $notice->id)
->orderBy('id')
->lockForUpdate()
->pluck('id');

$lockedNotice = Notice::where('id', $notice->id)
->lockForUpdate()
->first();
$lockedNotice = Notice::where('id', $notice->id)
->lockForUpdate()
->first();

$currentType = $lockedNotice->instrument_type instanceof \BackedEnum
? $lockedNotice->instrument_type->value
: $lockedNotice->instrument_type;
$currentType = $lockedNotice->instrument_type instanceof \BackedEnum
? $lockedNotice->instrument_type->value
: $lockedNotice->instrument_type;

$instrumentTypeChanged = $data['instrument_type'] !== $currentType;
$instrumentTypeChanged = $data['instrument_type'] !== $currentType;

$lockedNotice->update($data);
$lockedNotice->update($data);

if ($instrumentTypeChanged) {
Formalization::whereIn('project_id', $projectIds)
->lockForUpdate()
->update(['term_number' => null]);
}
if ($instrumentTypeChanged) {
Formalization::whereIn('project_id', $projectIds)
->lockForUpdate()
->update(['term_number' => null]);
}

return $lockedNotice;
return $lockedNotice;
}
}

$notice->update($data);
Expand Down
7 changes: 1 addition & 6 deletions resources/js/Pages/Notices/NupDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ const valorExtenso = computed(() => {
</v-col>

<v-col cols="12" md="6">
<FormField
label="Tipo de instrumento"
:error="form.errors.instrument_type"
required
clearable
>
<FormField label="Tipo de instrumento" :error="form.errors.instrument_type" required>
<SelectField
v-model="form.instrument_type"
:items="instrumentTypes"
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Projects/Partials/ProjectNoticeEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const saveAll = () => {
:items="instrumentTypes"
:error="form.errors.instrument_type"
data-cy="instrument-type-show-all-information"
required
/>
<EditableField
v-model="form.process_manager"
Expand Down
64 changes: 58 additions & 6 deletions tests/Feature/Notice/UpdateValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature\Notice;

use App\Enums\InstrumentType;
use App\Enums\MonitoringReportRequestDeadline;
use App\Models\Notice;
use App\Models\User;
Expand Down Expand Up @@ -44,21 +45,31 @@ public function test_user_without_role_cannot_update_notice(): void

public function test_fomentation_role_can_update_notice(): void
{
$notice = Notice::factory()->create();
$notice = Notice::factory()->create([
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), ['nup' => $notice->nup])
->patch(route('notices.update', $notice), [
'nup' => $notice->nup,
'instrument_type' => $notice->instrument_type,
])
->assertRedirect();
}

public function test_super_admin_role_can_update_notice(): void
{
$admin = User::factory()->create();
$admin->assignRole('super_admin');
$notice = Notice::factory()->create();
$notice = Notice::factory()->create([
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($admin)
->patch(route('notices.update', $notice), ['nup' => $notice->nup])
->patch(route('notices.update', $notice), [
'nup' => $notice->nup,
'instrument_type' => $notice->instrument_type,
])
->assertRedirect();
}

Expand All @@ -72,11 +83,13 @@ public function test_budget_allocation_nup_must_be_unique(): void

$notice = Notice::factory()->create([
'budget_allocation_nup' => '99999.000001/2024-99',
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'budget_allocation_nup' => $existing->budget_allocation_nup,
'instrument_type' => $notice->instrument_type,
])
->assertSessionHasErrors('budget_allocation_nup');
}
Expand All @@ -85,11 +98,13 @@ public function test_budget_allocation_nup_can_be_updated_to_same_value(): void
{
$notice = Notice::factory()->create([
'budget_allocation_nup' => '12345.678901/2024-01',
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'budget_allocation_nup' => $notice->budget_allocation_nup,
'instrument_type' => $notice->instrument_type,
])
->assertRedirect()
->assertSessionHasNoErrors();
Expand All @@ -103,22 +118,27 @@ public function test_nup_must_be_unique(): void

$notice = Notice::factory()->create([
'nup' => '99999.000001/2024-99',
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'nup' => $existing->nup,
'instrument_type' => $notice->instrument_type,
])
->assertSessionHasErrors('nup');
}

public function test_monitoring_report_request_deadline_can_be_updated(): void
{
$notice = Notice::factory()->create();
$notice = Notice::factory()->create([
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'monitoring_report_request_deadline' => MonitoringReportRequestDeadline::MECENAS->value,
'instrument_type' => $notice->instrument_type,
])
->assertRedirect()
->assertSessionHasNoErrors();
Expand All @@ -131,12 +151,44 @@ public function test_monitoring_report_request_deadline_can_be_updated(): void

public function test_monitoring_report_request_deadline_must_be_a_supported_value(): void
{
$notice = Notice::factory()->create();
$notice = Notice::factory()->create([
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'monitoring_report_request_deadline' => 'INVALIDO',
'instrument_type' => $notice->instrument_type,
])
->assertSessionHasErrors('monitoring_report_request_deadline');
}

// ─── Instrument Type ────────────────────────────────────────────────────────

public function test_instrument_type_is_required_on_update(): void
{
$notice = Notice::factory()->create([
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'nup' => $notice->nup,
// instrument_type omitido intencionalmente
])
->assertSessionHasErrors('instrument_type');
}

public function test_instrument_type_cannot_be_null_on_update(): void
{
$notice = Notice::factory()->create([
'instrument_type' => InstrumentType::EXECUCAO_CULTURAL->value,
]);

$this->actingAs($this->userWithRole)
->patch(route('notices.update', $notice), [
'instrument_type' => null,
])
->assertSessionHasErrors('instrument_type');
}
}
Loading