Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateScorecardRequest
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 authorize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rules
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace App\Http\Requests\v2\RolePlay;
4
5use Illuminate\Foundation\Http\FormRequest;
6
7/**
8 * Request for updating a user's custom scorecard configuration for a specific call type.
9 *
10 * Validates the full scorecard structure including sections and criteria.
11 * Enforces that section weights sum to 100 and each section's criteria weights sum to 100.
12 *
13 * @property array<array{name: string, is_default: bool, weight: int, criteria: array<array{name: string, weight: int, description: string}>}> $scorecard
14 *     Array of scorecard section objects. Each section defines a scoring category
15 *     with weighted criteria. Section weights must total 100, and within each section,
16 *     criteria weights must also total 100.
17 */
18class UpdateScorecardRequest extends FormRequest
19{
20    /**
21     * Determine if the user is authorized to make this request.
22     *
23     * @return bool
24     */
25    public function authorize(): bool
26    {
27        return true;
28    }
29
30    /**
31     * Get the validation rules that apply to the request.
32     *
33     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
34     */
35    public function rules(): array
36    {
37        return [
38            'scorecard' => ['required', 'array', 'min:1', function ($attribute, $value, $fail) {
39                // Validate section name uniqueness
40                $names = array_column($value, 'name');
41                if (count($names) !== count(array_unique($names))) {
42                    $fail('Each section name within the scorecard must be unique.');
43                }
44
45                // Validate section weights sum to 100
46                $totalWeight = array_sum(array_column($value, 'weight'));
47                if ($totalWeight !== 100) {
48                    $fail('The total weight of all scorecard sections must equal 100.');
49                }
50            }],
51            'scorecard.*.name'        => 'required|string',
52            'scorecard.*.is_default'  => 'required|boolean',
53            'scorecard.*.weight'      => 'required|integer|min:0|max:100',
54            'scorecard.*.criteria'    => ['required', 'array', 'min:1', function ($attribute, $value, $fail) {
55                // Validate criteria name uniqueness within each section
56                $criteriaNames = array_column($value, 'name');
57                if (count($criteriaNames) !== count(array_unique($criteriaNames))) {
58                    $fail('Each criterion name within a scorecard section must be unique.');
59                }
60
61                // Validate criteria weights sum to 100
62                $totalWeight = array_sum(array_column($value, 'weight'));
63                if ($totalWeight !== 100) {
64                    $fail('The total weight of all criteria within a scorecard section must equal 100.');
65                }
66            }],
67            'scorecard.*.criteria.*.name'        => 'required|string',
68            'scorecard.*.criteria.*.weight'      => 'required|integer|min:0|max:100',
69            'scorecard.*.criteria.*.description' => 'required|string',
70        ];
71    }
72}