From 544233a52d615bcb42a0e00a7f22aa51bca4bceb Mon Sep 17 00:00:00 2001 From: Constantin Graf Date: Fri, 8 Nov 2024 12:11:16 +0100 Subject: [PATCH] Change logic of tags_ids filter from AND to OR --- .../TimeEntryAggregateExportRequest.php | 2 +- .../TimeEntry/TimeEntryAggregateRequest.php | 2 +- .../TimeEntry/TimeEntryIndexExportRequest.php | 2 +- .../V1/TimeEntry/TimeEntryIndexRequest.php | 2 +- app/Service/TimeEntryFilter.php | 6 ++- tests/Unit/Service/TimeEntryFilterTest.php | 44 +++++++++++++++++++ 6 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/Service/TimeEntryFilterTest.php diff --git a/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php b/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php index 0b5cc34b..7e805f0e 100644 --- a/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php +++ b/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateExportRequest.php @@ -108,7 +108,7 @@ public function rules(): array return $builder->whereBelongsTo($this->organization, 'organization'); })->uuid(), ], - // Filter by tag IDs, tag IDs are AND combined + // Filter by tag IDs, tag IDs are OR combined 'tag_ids' => [ 'array', 'min:1', diff --git a/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateRequest.php b/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateRequest.php index c36d7e92..f1be9c66 100644 --- a/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateRequest.php +++ b/app/Http/Requests/V1/TimeEntry/TimeEntryAggregateRequest.php @@ -95,7 +95,7 @@ public function rules(): array return $builder->whereBelongsTo($this->organization, 'organization'); })->uuid(), ], - // Filter by tag IDs, tag IDs are AND combined + // Filter by tag IDs, tag IDs are OR combined 'tag_ids' => [ 'array', 'min:1', diff --git a/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php b/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php index 5de41b4b..4385ef84 100644 --- a/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php +++ b/app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php @@ -68,7 +68,7 @@ public function rules(): array return $builder->whereBelongsTo($this->organization, 'organization'); }), ], - // Filter by tag IDs, tag IDs are AND combined + // Filter by tag IDs, tag IDs are OR combined 'tag_ids' => [ 'array', 'min:1', diff --git a/app/Http/Requests/V1/TimeEntry/TimeEntryIndexRequest.php b/app/Http/Requests/V1/TimeEntry/TimeEntryIndexRequest.php index 9de7fb06..55052296 100644 --- a/app/Http/Requests/V1/TimeEntry/TimeEntryIndexRequest.php +++ b/app/Http/Requests/V1/TimeEntry/TimeEntryIndexRequest.php @@ -72,7 +72,7 @@ public function rules(): array return $builder->whereBelongsTo($this->organization, 'organization'); })->uuid(), ], - // Filter by tag IDs, tag IDs are AND combined + // Filter by tag IDs, tag IDs are OR combined 'tag_ids' => [ 'array', 'min:1', diff --git a/app/Service/TimeEntryFilter.php b/app/Service/TimeEntryFilter.php index 7f35fe3a..34e518dc 100644 --- a/app/Service/TimeEntryFilter.php +++ b/app/Service/TimeEntryFilter.php @@ -133,7 +133,11 @@ public function addTagIdsFilter(?array $tagIds): self if ($tagIds === null) { return $this; } - $this->builder->whereJsonContains('tags', $tagIds); + $this->builder->where(function (Builder $builder) use ($tagIds): void { + foreach ($tagIds as $tagId) { + $builder->orWhereJsonContains('tags', $tagId); + } + }); return $this; } diff --git a/tests/Unit/Service/TimeEntryFilterTest.php b/tests/Unit/Service/TimeEntryFilterTest.php new file mode 100644 index 00000000..8c8df4bf --- /dev/null +++ b/tests/Unit/Service/TimeEntryFilterTest.php @@ -0,0 +1,44 @@ +create(); + $tag1 = Tag::factory()->create(); + $timeEntryWithTag1 = TimeEntry::factory()->create([ + 'tags' => [$tag1->getKey()], + ]); + $tag2 = Tag::factory()->create(); + $timeEntryWithTag2 = TimeEntry::factory()->create([ + 'tags' => [$tag2->getKey()], + ]); + $timeEntryWithAllTags = TimeEntry::factory()->create([ + 'tags' => [$tag1->getKey(), $tag2->getKey()], + ]); + + // Act + $filter->addTagIdsFilter([$tag1->getKey(), $tag2->getKey()]); + + // Assert + $timeEntries = $builder->get(); + $this->assertCount(3, $timeEntries); + + } +}