Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RolePlaySessionEndRequest
95.65% covered (success)
95.65%
22 / 23
50.00% covered (danger)
50.00%
1 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 authorize
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
5
 rules
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Requests\v2\RolePlay;
4
5use App\Http\Models\RolePlayConversations;
6use App\Http\Models\UserAddOns;
7use Illuminate\Foundation\Http\FormRequest;
8use MongoDB\BSON\UTCDateTime;
9
10class RolePlaySessionEndRequest extends FormRequest
11{
12    /**
13     * Determine if the user is authorized to make this request.
14     */
15    public function authorize(): bool
16    {
17        $user = $this->user();
18
19        $rolePlayAddOn = UserAddOns::where('user_id', $user->id)
20            ->where('product', 'RolePlay')
21            ->where('status', 'active')
22            ->orderBy('created_at', 'desc')
23            ->first();
24
25        if (! $rolePlayAddOn) {
26            return false;
27        }
28
29        $rolePlayAddOn->load('addOn');
30
31        $monthly_total_time_credits = $rolePlayAddOn->addOn->features['monthly_total_time_credits'] ?? 0;
32        $monthly_roleplay_credits = $rolePlayAddOn->addOn->features['monthly_roleplay_credits'] ?? 0;
33
34        $usedRolePlayCredits = RolePlayConversations::where('user_id', $user->id)
35            ->where('status', '!=', 'created')
36            ->where('created_at', '>=', new UTCDateTime(strtotime(now()->startOfMonth()->toDateString()) * 1000))
37            ->count() ?? 0;
38
39        $usedRolePlayTimeCredits = RolePlayConversations::where('user_id', $user->id)
40            ->where('status', '!=', 'created')
41            ->where('created_at', '>=', new UTCDateTime(strtotime(now()->startOfMonth()->toDateString()) * 1000))
42            ->sum('duration') ?? 0;
43
44        return ($monthly_roleplay_credits === -1 || $usedRolePlayCredits < $monthly_roleplay_credits) && ($monthly_total_time_credits === -1 || $usedRolePlayTimeCredits < $monthly_total_time_credits);
45    }
46
47    /**
48     * Get the validation rules that apply to the request.
49     *
50     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
51     */
52    public function rules(): array
53    {
54        return [
55            'vapi_call_id' => 'sometimes|nullable|string|max:255',
56        ];
57    }
58}