Skip to content

Commit

Permalink
feat(search): default sort by signDate desc, date from/to params
Browse files Browse the repository at this point in the history
  • Loading branch information
brmn committed Apr 5, 2021
1 parent 683372b commit 8657caf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
18 changes: 16 additions & 2 deletions app/Bot/Purchases/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Arr;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
use Carbon\Carbon;
use Exception;
use InvalidArgumentException;
use Log;
Expand All @@ -29,13 +30,18 @@
*/
final class Search extends Command
{
protected const COMMAND = '/search';
protected const DESC = '/search inn=10_or_12_digits [perpage=10] [page=1] [query]';
protected const COMMAND = '/searchtest';
protected const DESC = <<<'TAG'
/searchtest inn=10_or_12_digits [perpage=10] [page=1] [datefrom=yyyy-mm-dd] [dateto=yyyy-mm-dd] [query]
TAG;


protected const VALIDATION_RULES = [
'inn' => ['required'],
'perpage' => ['int', 'min:1', 'max:50'],
'page' => ['int', 'min:1', 'max:10'],
'datefrom' => ['date_format:Y-m-d', 'lte:dateto'],
'dateto' => ['date_format:Y-m-d'],
self::REST_OF_THE_QUERY => ['string', 'max:100'],
];

Expand Down Expand Up @@ -123,6 +129,14 @@ private function makeQuery(BotMan $bot): PurchasesSearchQuery
$typedParams['page'] = (int)$params['page'];
}

if (Arr::has($params, 'datefrom')) {
$typedParams['datefrom'] = Carbon::createFromFormat('Y-m-d', $params['datefrom']);
}

if (Arr::has($params, 'dateto')) {
$typedParams['dateto'] = Carbon::createFromFormat('Y-m-d', $params['dateto']);
}

return new PurchasesSearchQuery($typedParams);
}

Expand Down
20 changes: 17 additions & 3 deletions app/Usecases/Purchases/DTO/PurchasesSearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
namespace App\Usecases\Purchases\DTO;

use App\Shared\ValueObjects\Inn;
use Carbon\Carbon;
use Spatie\DataTransferObject\DataTransferObject;

class PurchasesSearchQuery extends DataTransferObject
final class PurchasesSearchQuery extends DataTransferObject
{
public Inn $inn;
public int $perpage = 10;
public int $page = 1;
public int $perpage;
public int $page;
public Carbon $dateFrom;
public Carbon $dateTo;
public string $sortBy = '-signDate';

public ?string $query = null;

public function __construct(array $parameters = [])
{
$parameters['perpage'] ??= 50;
$parameters['page'] ??= 1;
$parameters['dateFrom'] ??= Carbon::createFromFormat('Y-m-d', '2000-01-01');
$parameters['dateTo'] ??= Carbon::now();

parent::__construct($parameters);
}
}
7 changes: 7 additions & 0 deletions app/Usecases/Purchases/UserSearchesPurchases.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function process(User $user, PurchasesSearchQuery $query): PurchasesSearc
'customerinn' => $query->inn->getValue(),
'perpage' => $query->perpage,
'page' => $query->page,
'daterange' => $this->getDateRange($query),
'sort' => $query->sortBy,
]
),
'query' => $query,
Expand All @@ -55,4 +57,9 @@ public function process(User $user, PurchasesSearchQuery $query): PurchasesSearc
throw $e;
}
}

private function getDateRange(PurchasesSearchQuery $query): string
{
return "{$query->dateFrom->format('d.m.Y')}-{$this->$query->format('d.m.Y')}";
}
}

0 comments on commit 8657caf

Please sign in to comment.