Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListVisitsQuery
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
8.05
0.00% covered (danger)
0.00%
0 / 1
 execute
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
8.05
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Visits\Application\Queries;
6
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use ImovelZapAi\Visits\Infrastructure\Persistence\Eloquent\VisitModel;
9
10final class ListVisitsQuery
11{
12    /**
13     * @param  array{
14     *     status?: string|null,
15     *     responsible_user_id?: int|null,
16     *     property_id?: string|null,
17     *     contact_id?: string|null,
18     *     from?: string|null,
19     *     to?: string|null,
20     *     per_page?: int
21     * }  $filters
22     */
23    public function execute(string $tenantId, array $filters = []): LengthAwarePaginator
24    {
25        $query = VisitModel::query()
26            ->where('tenant_id', $tenantId)
27            ->with(['contact', 'property', 'responsible'])
28            ->latest('scheduled_start')
29            ->latest();
30
31        if (! empty($filters['status'])) {
32            $query->where('status', $filters['status']);
33        }
34
35        if (! empty($filters['responsible_user_id'])) {
36            $query->where('responsible_user_id', $filters['responsible_user_id']);
37        }
38
39        if (! empty($filters['property_id'])) {
40            $query->where('property_id', $filters['property_id']);
41        }
42
43        if (! empty($filters['contact_id'])) {
44            $query->where('contact_id', $filters['contact_id']);
45        }
46
47        if (! empty($filters['from'])) {
48            $query->where('scheduled_start', '>=', $filters['from']);
49        }
50
51        if (! empty($filters['to'])) {
52            $query->where('scheduled_start', '<=', $filters['to']);
53        }
54
55        return $query->paginate((int) ($filters['per_page'] ?? 20));
56    }
57}