Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ListProposalsQuery | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| execute | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Proposals\Application\Queries; |
| 6 | |
| 7 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 8 | use ImovelZapAi\Proposals\Infrastructure\Persistence\Eloquent\ProposalModel; |
| 9 | |
| 10 | final class ListProposalsQuery |
| 11 | { |
| 12 | /** @param array{status?: string|null, search?: string|null, per_page?: int} $filters */ |
| 13 | public function execute(string $tenantId, array $filters = []): LengthAwarePaginator |
| 14 | { |
| 15 | $query = ProposalModel::query() |
| 16 | ->where('tenant_id', $tenantId) |
| 17 | ->with(['contact', 'property', 'deal', 'responsible']) |
| 18 | ->latest(); |
| 19 | |
| 20 | if (! empty($filters['status'])) { |
| 21 | $query->where('status', $filters['status']); |
| 22 | } |
| 23 | if (! empty($filters['search'])) { |
| 24 | $search = '%'.$filters['search'].'%'; |
| 25 | $query->where(function ($q) use ($search): void { |
| 26 | $q->where('title', 'ilike', $search)->orWhere('number', 'ilike', $search); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | return $query->paginate((int) ($filters['per_page'] ?? 20)); |
| 31 | } |
| 32 | } |