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

Added updated from and to filters to orders #101

Merged
merged 1 commit into from
Nov 27, 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
14 changes: 14 additions & 0 deletions app/Criteria/WhereUpdatedAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Criteria;

use Heseya\Searchable\Criteria\Criterion;
use Illuminate\Database\Eloquent\Builder;

class WhereUpdatedAfter extends Criterion
{
public function query(Builder $query): Builder
{
return $query->where('updated_at', '>=', $this->value);
}
}
19 changes: 19 additions & 0 deletions app/Criteria/WhereUpdatedBefore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Criteria;

use Heseya\Searchable\Criteria\Criterion;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

class WhereUpdatedBefore extends Criterion
{
public function query(Builder $query): Builder
{
if (!Str::contains($this->value, ':')) {
$this->value = Str::before($this->value, 'T') . 'T23:59:59';
}

return $query->where('updated_at', '<=', $this->value);
}
}
2 changes: 2 additions & 0 deletions app/Http/Requests/OrderIndexRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function rules(): array
'paid' => ['sometimes', 'boolean'],
'from' => ['nullable', 'date'],
'to' => ['nullable', 'date', 'after_or_equal:from'],
'updated_from' => ['nullable', 'date'],
'updated_to' => ['nullable', 'date', 'after_or_equal:updated_from'],
'metadata' => ['nullable', 'array'],
'metadata_private' => ['nullable', 'array'],
'ids' => ['array'],
Expand Down
4 changes: 4 additions & 0 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use App\Criteria\WhereCreatedBefore;
use App\Criteria\WhereHasStatusHidden;
use App\Criteria\WhereInIds;
use App\Criteria\WhereUpdatedAfter;
use App\Criteria\WhereUpdatedBefore;
use App\Enums\PaymentStatus;
use App\Enums\ShippingType;
use App\Models\Contracts\SortableContract;
Expand Down Expand Up @@ -88,6 +90,8 @@ final class Order extends Model implements SortableContract
'paid',
'from' => WhereCreatedAfter::class,
'to' => WhereCreatedBefore::class,
'updated_from' => WhereUpdatedAfter::class,
'updated_to' => WhereUpdatedBefore::class,
'metadata' => MetadataSearch::class,
'metadata_private' => MetadataPrivateSearch::class,
'ids' => WhereInIds::class,
Expand Down
12 changes: 12 additions & 0 deletions docs/paths/Orders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ Orders:
schema:
type: string
format: date-time
- name: updated_from
in: query
description: 'Date datetime updated from'
schema:
type: string
format: date-time
- name: updated_to
in: query
description: 'Date datetime updated to'
schema:
type: string
format: date-time
- name: metadata
in: query
description: search by metadata
Expand Down
119 changes: 119 additions & 0 deletions tests/Feature/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,42 @@ public function testIndexSearchByFrom($user): void
->assertJsonFragment(['id' => $order2->getKey()]);
}

/**
* @dataProvider authProvider
*/
public function testIndexSearchByUpdatedFrom($user): void
{
$this->{$user}->givePermissionTo('orders.show');

$status = Status::factory()->create();

$from = $this->order->updated_at;

Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => Carbon::yesterday(),
])->create();

$order2 = Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => Carbon::tomorrow(),
])->create();

$response = $this
->actingAs($this->{$user})
->json('GET', '/orders', [
'updated_from' => $from,
]);

$response
->assertOk()
->assertJsonCount(2, 'data')
->assertJsonFragment(['id' => $this->order->getKey()])
->assertJsonFragment(['id' => $order2->getKey()]);
}

/**
* @dataProvider authProvider
*/
Expand Down Expand Up @@ -581,6 +617,42 @@ public function testIndexSearchByTo($user): void
->assertJsonFragment(['id' => $order1->getKey()]);
}

/**
* @dataProvider authProvider
*/
public function testIndexSearchByUpdatedTo($user): void
{
$this->{$user}->givePermissionTo('orders.show');

$status = Status::factory()->create();

$to = $this->order->updated_at;

$order1 = Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => Carbon::yesterday(),
])->create();

Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => Carbon::tomorrow(),
])->create();

$response = $this
->actingAs($this->{$user})
->json('GET', '/orders', [
'updated_to' => $to,
]);

$response
->assertOk()
->assertJsonCount(2, 'data')
->assertJsonFragment(['id' => $this->order->getKey()])
->assertJsonFragment(['id' => $order1->getKey()]);
}

/**
* @dataProvider authProvider
*/
Expand Down Expand Up @@ -628,6 +700,53 @@ public function testIndexSearchByFromTo($user): void
->assertJsonFragment(['id' => $order->getKey()]);
}

/**
* @dataProvider authProvider
*/
public function testIndexSearchByUpdatedFromUpdatedTo($user): void
{
$this->{$user}->givePermissionTo('orders.show');

$status = Status::factory()->create();

$now = Carbon::create(2020, 02, 02, 10);

$this->travelTo($now);

$from = $now->subHour();
$to = $now->addHour();

Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => $now->subDay(),
])->create();

Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => $now->addDay(),
])->create();

$order = Order::factory([
'shipping_method_id' => $this->shippingMethod->getKey(),
'status_id' => $status->getKey(),
'updated_at' => $now->subMinutes(30),
])->create();

$response = $this
->actingAs($this->{$user})
->json('GET', '/orders', [
'updated_from' => $from,
'updated_to' => $to,
]);

$response
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonFragment(['id' => $order->getKey()]);
}

/**
* @dataProvider authProvider
*/
Expand Down
Loading