Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RolePlaySessionStartRequest
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 authorize
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
 rules
100.00% covered (success)
100.00%
6 / 6
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 RolePlaySessionStartRequest 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('created_at', '>=', new UTCDateTime(strtotime(now()->startOfMonth()->toDateString()) * 1000))
36            ->count() ?? 0;
37
38        $usedRolePlayTimeCredits = RolePlayConversations::where('user_id', $user->id)
39            ->where('created_at', '>=', new UTCDateTime(strtotime(now()->startOfMonth()->toDateString()) * 1000))
40            ->sum('duration') ?? 0;
41
42        return $usedRolePlayCredits < $monthly_roleplay_credits && $usedRolePlayTimeCredits < $monthly_total_time_credits;
43    }
44
45    /**
46     * Get the validation rules that apply to the request.
47     *
48     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
49     */
50    public function rules(): array
51    {
52        return [
53            'project_id' => 'required|exists:role_play_projects,id',
54            'icp' => 'required|json',
55            'agent' => 'required|json',
56            'prompt' => 'required|string'
57        ];
58    }
59}