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
ListDealsQuery
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
6
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
6
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Sales\Application\Queries;
6
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealModel;
9
10final class ListDealsQuery
11{
12    /**
13     * @param  array{
14     *     stage_id?: string|null,
15     *     stage_key?: string|null,
16     *     responsible_user_id?: int|null,
17     *     contact_id?: string|null,
18     *     open_only?: bool|null,
19     *     per_page?: int
20     * }  $filters
21     */
22    public function execute(string $tenantId, array $filters = []): LengthAwarePaginator
23    {
24        $query = DealModel::query()
25            ->where('tenant_id', $tenantId)
26            ->with(['stage', 'contact', 'property', 'responsible'])
27            ->latest('last_activity_at')
28            ->latest();
29
30        if (! empty($filters['stage_id'])) {
31            $query->where('stage_id', $filters['stage_id']);
32        }
33
34        if (! empty($filters['stage_key'])) {
35            $query->whereHas('stage', fn ($q) => $q->where('key', $filters['stage_key']));
36        }
37
38        if (! empty($filters['responsible_user_id'])) {
39            $query->where('responsible_user_id', $filters['responsible_user_id']);
40        }
41
42        if (! empty($filters['contact_id'])) {
43            $query->where('contact_id', $filters['contact_id']);
44        }
45
46        if (($filters['open_only'] ?? false) === true) {
47            $query->whereNull('closed_at');
48        }
49
50        return $query->paginate((int) ($filters['per_page'] ?? 20));
51    }
52}