Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.70% covered (success)
90.70%
39 / 43
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RolePlayFeedbackController
90.70% covered (success)
90.70%
39 / 43
50.00% covered (danger)
50.00%
1 / 2
4.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 totals
90.48% covered (success)
90.48%
38 / 42
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3namespace App\Http\Controllers\v2\RolePlay;
4
5use App\Traits\SubscriptionTrait;
6use Illuminate\Http\JsonResponse;
7use App\Http\Controllers\Controller;
8use App\Services\RolePlay\DashboardMetricService;
9use App\Http\Requests\v2\RolePlay\RolePlayFeedbackTotalsRequest;
10
11class RolePlayFeedbackController extends Controller
12{
13    use SubscriptionTrait;
14
15    public function __construct(
16        private readonly DashboardMetricService $metricService
17    ) {}
18
19    public function totals(RolePlayFeedbackTotalsRequest $request): JsonResponse
20    {
21        $user = $request->user();
22        $period = $request->input('period', 'all_times');
23
24        $startDate = null;
25        $endDate = null;
26
27        switch ($period) {
28            case 'this_week':
29                $startDate = now()->startOfWeek();
30                $endDate = now()->endOfWeek();
31                break;
32            case 'this_month':
33                $startDate = now()->startOfMonth();
34                $endDate = now()->endOfMonth();
35                break;
36        }
37
38        $periodStats = $this->metricService->getConversationStatsForPeriod($user, $startDate, $endDate);
39
40        $thisWeekStart = now()->startOfWeek();
41        $thisWeekEnd = now()->endOfWeek();
42        $thisWeekStats = $this->metricService->getConversationStatsForPeriod($user, $thisWeekStart, $thisWeekEnd);
43
44        $thisMonthStart = now()->startOfMonth();
45        $thisMonthEnd = now()->endOfMonth();
46        $thisMonthStats = $this->metricService->getConversationStatsForPeriod($user, $thisMonthStart, $thisMonthEnd);
47
48        $lastMonthStart = now()->startOfMonth()->subMonth();
49        $lastMonthEnd = now()->endOfMonth()->subMonth();
50        $lastMonthStats = $this->metricService->getConversationStatsForPeriod($user, $lastMonthStart, $lastMonthEnd);
51
52        $lastWeekStart = now()->startOfWeek()->subWeek();
53        $lastWeekEnd = now()->endOfWeek()->subWeek();
54        $lastWeekStats = $this->metricService->getConversationStatsForPeriod($user, $lastWeekStart, $lastWeekEnd);
55
56        $historicalAverages = $this->metricService->getHistoricalWeeklyAverageStats($user);
57
58        $evolution = [
59            'conversationsCount' => $this->metricService->calculateEvolution($thisMonthStats['conversationsCount'], $historicalAverages['conversationsCount']),
60            'practiceTimeSeconds' => $this->metricService->calculateEvolution($thisMonthStats['practiceTimeSeconds'], $historicalAverages['practiceTimeSeconds']),
61            'averageScore' => $this->metricService->calculateEvolution($thisMonthStats['averageScore'], $historicalAverages['averageScore']),
62        ];
63
64        return response()->json([
65            'status' => 'success',
66            'data' => [
67                'period' => $periodStats,
68                'lastWeek' => $lastWeekStats,
69                'thisWeek' => $thisWeekStats,
70                'lastMonth' => $lastMonthStats,
71                'thisMonth' => $thisMonthStats,
72                'evolution' => $evolution,
73            ],
74        ]);
75    }
76}