Skip to content

Commit

Permalink
Change logic of tags_ids filter from AND to OR
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Nov 8, 2024
1 parent 45daeea commit 544233a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/V1/TimeEntry/TimeEntryIndexRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion app/Service/TimeEntryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Service/TimeEntryFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Service;

use App\Models\Tag;
use App\Models\TimeEntry;
use App\Service\TimeEntryFilter;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCaseWithDatabase;

#[CoversClass(TimeEntryFilter::class)]
#[UsesClass(TimeEntryFilter::class)]
class TimeEntryFilterTest extends TestCaseWithDatabase
{
public function test_add_tag_ids_filter_is_or(): void
{
// Arrange
$builder = TimeEntry::query();
$filter = new TimeEntryFilter($builder);
$timEntryNoTag = TimeEntry::factory()->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);

}
}

0 comments on commit 544233a

Please sign in to comment.