diff --git a/app/Http/Requests/Notice/NoticeUpdateRequest.php b/app/Http/Requests/Notice/NoticeUpdateRequest.php index 25b4aa03..84da0cbd 100644 --- a/app/Http/Requests/Notice/NoticeUpdateRequest.php +++ b/app/Http/Requests/Notice/NoticeUpdateRequest.php @@ -40,7 +40,7 @@ public function rules(): array ], 'instrument_type' => [ - 'sometimes', + 'required', function (string $attribute, mixed $value, \Closure $fail) use ($notice) { if ($notice) { $currentValue = $notice->instrument_type instanceof \BackedEnum @@ -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'], diff --git a/app/Services/NoticeService.php b/app/Services/NoticeService.php index f50aa607..048a4b1c 100644 --- a/app/Services/NoticeService.php +++ b/app/Services/NoticeService.php @@ -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); diff --git a/resources/js/Pages/Notices/NupDialog.vue b/resources/js/Pages/Notices/NupDialog.vue index aec47870..e360b3e8 100644 --- a/resources/js/Pages/Notices/NupDialog.vue +++ b/resources/js/Pages/Notices/NupDialog.vue @@ -105,12 +105,7 @@ const valorExtenso = computed(() => { - + { :items="instrumentTypes" :error="form.errors.instrument_type" data-cy="instrument-type-show-all-information" + required /> 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(); } @@ -55,10 +61,15 @@ 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(); } @@ -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'); } @@ -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(); @@ -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(); @@ -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'); + } }