Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix AggregateFilterable #108

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Rules/AggregateFilterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ public function resource($resource)
*/
protected function buildValidationRules($attribute, $value)
{
if (is_null($this->resource)) {
$aggregateResource = $this->resource->relation($value['relation'])?->resource();

if (is_null($aggregateResource)) {
return [];
}

return (new SearchRules($this->resource, app()->make(RestRequest::class), false))
->filtersRules(
$this->resource,
$attribute
$aggregateResource,
$attribute.'.filters'
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Rules/IsNestedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
$fail('The '.$attribute.' field is not valid.');
}
}

public function __toString()
{
return 'nested_field';
}
}
2 changes: 0 additions & 2 deletions src/Rules/SearchRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ protected function aggregatesRules(\Lomkit\Rest\Http\Resource $resource, string
$prefix.'.*' => [
AggregateField::make()
->resource($resource),
],
$prefix.'.*.filters' => [
AggregateFilterable::make()
->resource($resource),
],
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/Controllers/SearchAggregatesOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,54 @@ public function test_getting_a_list_of_resources_aggregating_by_sum_number_with_
);
}

public function test_getting_a_list_of_resources_aggregating_by_sum_number_with_other_number_filter(): void
{
$matchingModel = ModelFactory::new()
->has(
BelongsToManyRelationFactory::new()
->count(20)
)
->create()->fresh();
$matchingModel2 = ModelFactory::new()
->has(
BelongsToManyRelationFactory::new()
->count(20)
)
->create()->fresh();

Gate::policy(Model::class, GreenPolicy::class);
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);

$response = $this->post(
'/api/models/search',
[
'search' => [
'aggregates' => [
[
'relation' => 'belongsToManyRelation',
'type' => 'sum',
'field' => 'number',
'filters' => [
['field' => 'other_number', 'operator' => '<', 'value' => 200],
],
],
],
],
],
['Accept' => 'application/json']
);

$this->assertResourcePaginated(
$response,
[$matchingModel, $matchingModel2],
new ModelResource(),
[
['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')],
['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')],
]
);
}

public function test_getting_a_list_of_resources_aggregating_by_count_with_filters(): void
{
$matchingModel = ModelFactory::new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class BelongsToManyRelationFactory extends Factory
public function definition()
{
return [
'number' => fake()->numberBetween(-5000, 5000),
'number' => fake()->numberBetween(-5000, 5000),
'other_number' => fake()->numberBetween(-5000, 5000),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function up()
Schema::create('belongs_to_many_relations', function (Blueprint $table) {
$table->id();
$table->integer('number')->default(0);
$table->integer('other_number')->default(0);
$table->timestamps();
});
}
Expand Down
1 change: 1 addition & 0 deletions tests/Support/Rest/Resources/BelongsToManyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function fields(RestRequest $request): array
return [
'id',
'number',
'other_number',
];
}
}
Loading