Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListListingOpportunitiesQuery
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
0.00% covered (danger)
0.00%
0 / 1
 execute
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Owners\Application\Queries;
6
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use ImovelZapAi\Owners\Infrastructure\Persistence\Eloquent\ListingOpportunityModel;
9
10final class ListListingOpportunitiesQuery
11{
12    /**
13     * @param  array{
14     *     stage_key?: string|null,
15     *     owner_id?: string|null,
16     *     open_only?: bool|null,
17     *     per_page?: int
18     * }  $filters
19     */
20    public function execute(string $tenantId, array $filters = []): LengthAwarePaginator
21    {
22        $query = ListingOpportunityModel::query()
23            ->where('tenant_id', $tenantId)
24            ->with(['stage', 'owner', 'property', 'responsible'])
25            ->latest('last_activity_at');
26
27        if (! empty($filters['stage_key'])) {
28            $query->whereHas('stage', fn ($q) => $q->where('key', $filters['stage_key']));
29        }
30        if (! empty($filters['owner_id'])) {
31            $query->where('owner_id', $filters['owner_id']);
32        }
33        if (($filters['open_only'] ?? false) === true) {
34            $query->whereNull('closed_at');
35        }
36
37        return $query->paginate((int) ($filters['per_page'] ?? 20));
38    }
39}