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