Skip to content

Commit

Permalink
Merge pull request #102 from heseya/feature/MPT-2755-8-0-0
Browse files Browse the repository at this point in the history
Added updated from and to filters to orders
  • Loading branch information
daVitekPL authored Nov 27, 2024
2 parents 49edf26 + f6c1c8a commit 64f1cc1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
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 @@ -93,6 +95,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 @@ -55,6 +55,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 @@ -613,6 +613,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 @@ -649,6 +685,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 @@ -696,6 +768,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

0 comments on commit 64f1cc1

Please sign in to comment.