Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RolePlaySessionEndRequest
95.24% covered (success)
95.24%
20 / 21
50.00% covered (danger)
50.00%
1 / 2
4
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
3
 rules
100.00% covered (success)
100.00%
1 / 1
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
26        if (!$rolePlayAddOn) {
27            return false;
28        }
29
30        $rolePlayAddOn->load('addOn');
31
32        $monthly_total_time_credits = $rolePlayAddOn->addOn->features['monthly_total_time_credits'] ?? 0;
33        $monthly_roleplay_credits = $rolePlayAddOn->addOn->features['monthly_roleplay_credits'] ?? 0;
34
35        $usedRolePlayCredits = RolePlayConversations::where('user_id', $user->id)
36            ->where('status', '!=', 'created')
37            ->where('created_at', '>=', new UTCDateTime(strtotime(now()->startOfMonth()->toDateString()) * 1000))
38            ->count() ?? 0;
39
40        $usedRolePlayTimeCredits = RolePlayConversations::where('user_id', $user->id)
41            ->where('status', '!=', 'created')
42            ->where('created_at', '>=', new UTCDateTime(strtotime(now()->startOfMonth()->toDateString()) * 1000))
43            ->sum('duration') ?? 0;
44
45        return $usedRolePlayCredits < $monthly_roleplay_credits && $usedRolePlayTimeCredits < $monthly_total_time_credits;
46    }
47
48    /**
49     * Get the validation rules that apply to the request.
50     *
51     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
52     */
53    public function rules(): array
54    {
55        return [];
56    }
57}