Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RolePlaySessionEndRequest | |
95.24% |
20 / 21 |
|
50.00% |
1 / 2 |
6 | |
0.00% |
0 / 1 |
| authorize | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
5 | |||
| rules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Requests\v2\RolePlay; |
| 4 | |
| 5 | use App\Http\Models\RolePlayConversations; |
| 6 | use App\Http\Models\UserAddOns; |
| 7 | use Illuminate\Foundation\Http\FormRequest; |
| 8 | use MongoDB\BSON\UTCDateTime; |
| 9 | |
| 10 | class 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 ($monthly_roleplay_credits === -1 || $usedRolePlayCredits < $monthly_roleplay_credits) && ($monthly_total_time_credits === -1 || $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 | } |