Skip to content

Commit c40158d

Browse files
committed
♻️ get the right resource on the build aggregate filter rules
1 parent 557d275 commit c40158d

File tree

6 files changed

+60
-13
lines changed

6 files changed

+60
-13
lines changed

src/Rules/AggregateFilterable.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,17 @@ public function resource($resource)
6363
*/
6464
protected function buildValidationRules($attribute, $value)
6565
{
66-
$rules = [];
66+
$aggregateResource = $this->resource->relation($value['relation'])?->resource();
6767

68-
foreach ($this->data['search']['aggregates'] as $aggregate) {
69-
$resource = $this->resource->relation($aggregate['relation'])?->resource();
70-
71-
if (is_null($this->resource)) {
72-
continue;
73-
}
74-
75-
array_push($rules, (new SearchRules($resource, app()->make(RestRequest::class), false))->filtersRules($resource, $attribute));
68+
if (is_null($aggregateResource)) {
69+
return [];
7670
}
7771

78-
return $rules;
72+
return (new SearchRules($this->resource, app()->make(RestRequest::class), false))
73+
->filtersRules(
74+
$aggregateResource,
75+
$attribute.'.filters'
76+
);
7977
}
8078

8179
/**

src/Rules/SearchRules.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,9 @@ protected function aggregatesRules(\Lomkit\Rest\Http\Resource $resource, string
297297
$prefix.'.*' => [
298298
AggregateField::make()
299299
->resource($resource),
300-
],
301-
$prefix.'.*.filters' => [
302300
AggregateFilterable::make()
303301
->resource($resource),
304-
],
302+
]
305303
];
306304
}
307305
}

tests/Feature/Controllers/SearchAggregatesOperationsTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,54 @@ public function test_getting_a_list_of_resources_aggregating_by_sum_number_with_
603603
);
604604
}
605605

606+
public function test_getting_a_list_of_resources_aggregating_by_sum_number_with_other_number_filter(): void
607+
{
608+
$matchingModel = ModelFactory::new()
609+
->has(
610+
BelongsToManyRelationFactory::new()
611+
->count(20)
612+
)
613+
->create()->fresh();
614+
$matchingModel2 = ModelFactory::new()
615+
->has(
616+
BelongsToManyRelationFactory::new()
617+
->count(20)
618+
)
619+
->create()->fresh();
620+
621+
Gate::policy(Model::class, GreenPolicy::class);
622+
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);
623+
624+
$response = $this->post(
625+
'/api/models/search',
626+
[
627+
'search' => [
628+
'aggregates' => [
629+
[
630+
'relation' => 'belongsToManyRelation',
631+
'type' => 'sum',
632+
'field' => 'number',
633+
'filters' => [
634+
['field' => 'other_number', 'operator' => '<', 'value' => 200],
635+
],
636+
],
637+
],
638+
],
639+
],
640+
['Accept' => 'application/json']
641+
);
642+
643+
$this->assertResourcePaginated(
644+
$response,
645+
[$matchingModel, $matchingModel2],
646+
new ModelResource(),
647+
[
648+
['belongs_to_many_relation_sum_number' => $matchingModel->belongsToManyRelation()->orderBy('belongs_to_many_relations.number', 'desc')->where('belongs_to_many_relations.other_number', '<', 200)->sum('belongs_to_many_relations.number')],
649+
['belongs_to_many_relation_sum_number' => $matchingModel2->belongsToManyRelation()->orderBy('belongs_to_many_relations.number', 'desc')->where('belongs_to_many_relations.other_number', '<', 200)->sum('belongs_to_many_relations.number')],
650+
]
651+
);
652+
}
653+
606654
public function test_getting_a_list_of_resources_aggregating_by_count_with_filters(): void
607655
{
608656
$matchingModel = ModelFactory::new()

tests/Support/Database/Factories/BelongsToManyRelationFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function definition()
2323
{
2424
return [
2525
'number' => fake()->numberBetween(-5000, 5000),
26+
'other_number' => fake()->numberBetween(-5000, 5000),
2627
];
2728
}
2829
}

tests/Support/Database/migrations/2023_02_00_000000_create_belongs_to_many_relations_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
Schema::create('belongs_to_many_relations', function (Blueprint $table) {
1616
$table->id();
1717
$table->integer('number')->default(0);
18+
$table->integer('other_number')->default(0);
1819
$table->timestamps();
1920
});
2021
}

tests/Support/Rest/Resources/BelongsToManyResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function fields(RestRequest $request): array
2626
return [
2727
'id',
2828
'number',
29+
'other_number'
2930
];
3031
}
3132
}

0 commit comments

Comments
 (0)