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