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