Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ListSignatureRequestsQuery
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Signatures\Application\Queries;
6
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use ImovelZapAi\Signatures\Infrastructure\Persistence\Eloquent\SignatureRequestModel;
9
10final class ListSignatureRequestsQuery
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 = SignatureRequestModel::query()
16            ->where('tenant_id', $tenantId)
17            ->with(['document', 'deal', 'signers', 'responsible'])
18            ->latest();
19
20        if (! empty($filters['status'])) {
21            $query->where('status', $filters['status']);
22        }
23
24        if (! empty($filters['search'])) {
25            $search = '%'.$filters['search'].'%';
26            $query->where(function ($q) use ($search): void {
27                $q->where('title', 'ilike', $search)
28                    ->orWhereHas('document', function ($documentQuery) use ($search): void {
29                        $documentQuery->where('number', 'ilike', $search)
30                            ->orWhere('title', 'ilike', $search);
31                    });
32            });
33        }
34
35        return $query->paginate((int) ($filters['per_page'] ?? 20));
36    }
37}