Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetOrCreateDashboardLayoutAction
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 defaultWidgets
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Analytics\Application\Actions;
6
7use App\Models\User;
8use ImovelZapAi\Analytics\Domain\Enums\DashboardRole;
9use ImovelZapAi\Analytics\Domain\Enums\WidgetType;
10use ImovelZapAi\Analytics\Infrastructure\Persistence\Eloquent\AnalyticsDashboardLayoutModel;
11
12final class GetOrCreateDashboardLayoutAction
13{
14    public function execute(string $tenantId, User $user, DashboardRole $role): AnalyticsDashboardLayoutModel
15    {
16        $existing = AnalyticsDashboardLayoutModel::query()
17            ->where('tenant_id', $tenantId)
18            ->where('user_id', $user->id)
19            ->first();
20
21        if ($existing !== null) {
22            return $existing;
23        }
24
25        return AnalyticsDashboardLayoutModel::query()->create([
26            'tenant_id' => $tenantId,
27            'user_id' => $user->id,
28            'role_preset' => $role->value,
29            'widgets' => $this->defaultWidgets($role),
30            'updated_at' => now(),
31        ]);
32    }
33
34    /** @return list<array{type: string, title: string, config: array<string, mixed>}> */
35    public function defaultWidgets(DashboardRole $role): array
36    {
37        $widgets = [
38            ['type' => WidgetType::KpiCards->value, 'title' => 'Indicadores principais', 'config' => []],
39            ['type' => WidgetType::Funnel->value, 'title' => 'Funil', 'config' => []],
40            ['type' => WidgetType::Ranking->value, 'title' => 'Ranking', 'config' => []],
41        ];
42
43        if ($role === DashboardRole::Manager || $role === DashboardRole::Owner) {
44            $widgets[] = ['type' => WidgetType::PeriodCompare->value, 'title' => 'Comparativo de período', 'config' => []];
45            $widgets[] = ['type' => WidgetType::AlertsList->value, 'title' => 'Alertas', 'config' => []];
46        }
47
48        if ($role === DashboardRole::Owner) {
49            $widgets[] = ['type' => WidgetType::VisitsCalendarStub->value, 'title' => 'Calendário de visitas', 'config' => []];
50        }
51
52        return $widgets;
53    }
54}