Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RolePlayDashboardController
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 progression
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 totals
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Controllers\v2\RolePlay;
4
5use Illuminate\Http\Request;
6use App\Traits\SubscriptionTrait;
7use Illuminate\Http\JsonResponse;
8use App\Http\Controllers\Controller;
9use App\Http\Models\RolePlayProjects;
10use App\Services\RolePlay\DashboardMetricService;
11
12class RolePlayDashboardController extends Controller
13{
14    use SubscriptionTrait;
15
16    public function __construct(
17        private readonly DashboardMetricService $metricService
18    ) {}
19
20    public function progression(Request $request): JsonResponse
21    {
22        $user = $request->user();
23
24        $progressionStats = $this->metricService->getProgressionStats($user);
25
26        return response()->json([
27            'status' => 'success',
28            'data' => $progressionStats,
29        ]);
30    }
31
32    public function totals(Request $request): JsonResponse
33    {
34        $user = $request->user();
35
36        $thisMonthStart = now()->startOfMonth();
37        $thisWeekStart = now()->startOfWeek();
38
39        $lastWeekStart = now()->startOfWeek()->subWeek();
40        $lastWeekEnd = now()->startOfWeek()->subSecond();
41
42        $lastWeekStats = $this->metricService->getConversationStatsForPeriod($user, $lastWeekStart, $lastWeekEnd);
43        $thisWeekStats = $this->metricService->getConversationStatsForPeriod($user, $thisWeekStart, now()->endOfWeek());
44        $thisMonthStats = $this->metricService->getConversationStatsForPeriod($user, $thisMonthStart, now()->endOfMonth());
45
46        $allTimeStats = $this->metricService->getConversationStatsForPeriod($user, null, null);
47        $allTimeStats['projectsCount'] = RolePlayProjects::where('user_id', $user->id)->count();
48
49        $historicalAverages = $this->metricService->getHistoricalWeeklyAverageStats($user);
50
51        $evolution = [
52            'conversationsCount' => $this->metricService->calculateEvolution($thisWeekStats['conversationsCount'], $historicalAverages['conversationsCount']),
53            'practiceTimeSeconds' => $this->metricService->calculateEvolution($thisWeekStats['practiceTimeSeconds'], $historicalAverages['practiceTimeSeconds']),
54            'averageScore' => $this->metricService->calculateEvolution($thisWeekStats['averageScore'], $historicalAverages['averageScore']),
55        ];
56
57        return response()->json([
58            'status' => 'success',
59            'data' => [
60                'allTimes' => $allTimeStats,
61                'lastWeek' => $lastWeekStats,
62                'evolution' => $evolution,
63                'thisWeek' => $thisWeekStats,
64                'thisMonth' => $thisMonthStats,
65            ],
66        ]);
67    }
68}