From 401cd4be0abd044d97e4067d53384ca0e5cea7d8 Mon Sep 17 00:00:00 2001 From: Constantin Graf Date: Tue, 22 Oct 2024 16:45:21 +0200 Subject: [PATCH] Fixed setting multiple time entry description to an empty string --- .../Api/V1/TimeEntryController.php | 4 ++ .../Endpoint/Api/V1/TimeEntryEndpointTest.php | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/app/Http/Controllers/Api/V1/TimeEntryController.php b/app/Http/Controllers/Api/V1/TimeEntryController.php index 401f1a2e..ee3a1f59 100644 --- a/app/Http/Controllers/Api/V1/TimeEntryController.php +++ b/app/Http/Controllers/Api/V1/TimeEntryController.php @@ -333,6 +333,10 @@ public function updateMultiple(Organization $organization, TimeEntryUpdateMultip $changes = $request->validated('changes'); + if ($request->has('changes.description')) { + $changes['description'] = $request->input('changes.description') ?? ''; + } + if (isset($changes['member_id']) && ! $canAccessAll && $this->member($organization)->getKey() !== $changes['member_id']) { throw new AuthorizationException; } diff --git a/tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php b/tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php index 0f8a57fa..d9fbb739 100644 --- a/tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php +++ b/tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php @@ -2138,6 +2138,51 @@ public function test_update_multiple_updates_own_time_entries_and_fails_for_time ]); } + public function test_update_multiple_updates_sets_description_to_empty_if_the_client_sends_null(): void + { + // Arrange + $data = $this->createUserWithPermission([ + 'time-entries:update:own', + ]); + $timeEntry1 = TimeEntry::factory()->forMember($data->member)->create([ + 'description' => '', + ]); + $timeEntry2 = TimeEntry::factory()->forMember($data->member)->create([ + 'description' => 'test', + ]); + Passport::actingAs($data->user); + + // Act + $response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [ + 'ids' => [ + $timeEntry1->getKey(), + $timeEntry2->getKey(), + ], + 'changes' => [ + 'description' => null, + ], + ]); + + // Assert + $response->assertValid(); + $response->assertStatus(200); + $response->assertExactJson([ + 'success' => [ + $timeEntry1->getKey(), + $timeEntry2->getKey(), + ], + 'error' => [], + ]); + $this->assertDatabaseHas(TimeEntry::class, [ + 'id' => $timeEntry1->getKey(), + 'description' => '', + ]); + $this->assertDatabaseHas(TimeEntry::class, [ + 'id' => $timeEntry2->getKey(), + 'description' => '', + ]); + } + public function test_update_multiple_updates_all_time_entries_and_fails_for_time_entries_of_other_users_and_and_other_organizations_with_all_time_entries_permission(): void { // Arrange