Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListAnalyticsAlertsQuery
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Analytics\Application\Queries;
6
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use ImovelZapAi\Analytics\Domain\Enums\AlertStatus;
9use ImovelZapAi\Analytics\Infrastructure\Persistence\Eloquent\AnalyticsAlertModel;
10
11final class ListAnalyticsAlertsQuery
12{
13    /**
14     * @param  array{status?: string, limit?: int, per_page?: int}  $filters
15     */
16    public function execute(string $tenantId, array $filters = []): LengthAwarePaginator|array
17    {
18        $query = AnalyticsAlertModel::query()
19            ->where('tenant_id', $tenantId)
20            ->orderByDesc('triggered_at');
21
22        if (isset($filters['status'])) {
23            $status = AlertStatus::tryFrom((string) $filters['status']);
24            if ($status !== null) {
25                $query->where('status', $status->value);
26            }
27        }
28
29        if (isset($filters['limit'])) {
30            return $query->limit((int) $filters['limit'])->get()->all();
31        }
32
33        return $query->paginate((int) ($filters['per_page'] ?? 20));
34    }
35}