Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| AdminRolePlaySettingsController | |
100.00% |
35 / 35 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| show | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| update | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| serialize | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2\Admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Models\CompanyRolePlaySettings; |
| 7 | use Illuminate\Http\JsonResponse; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Validation\Rule; |
| 10 | |
| 11 | /** |
| 12 | * Company-level RolePlay defaults editor. |
| 13 | * |
| 14 | * Unlike the scorecard controller, this surface covers the "soft" defaults |
| 15 | * a company can tune: default call type shown on the personas grid, default |
| 16 | * difficulty slider position, voice preference, and a bag of feature |
| 17 | * toggles (e.g. hide-roleplay-tab, disable-direct-calls-globally). |
| 18 | * |
| 19 | * One document per company; `PUT` is an upsert. |
| 20 | */ |
| 21 | class AdminRolePlaySettingsController extends Controller |
| 22 | { |
| 23 | /** |
| 24 | * Fetch the company's current settings. Returns an empty-defaults |
| 25 | * payload when none has been persisted yet so the form can render. |
| 26 | */ |
| 27 | public function show(Request $request): JsonResponse |
| 28 | { |
| 29 | $row = CompanyRolePlaySettings::forCompany($request->user()->company_id)->first(); |
| 30 | |
| 31 | return response()->json([ |
| 32 | 'status' => 'success', |
| 33 | 'data' => $row ? $this->serialize($row) : [ |
| 34 | 'company_id' => $request->user()->company_id, |
| 35 | 'default_call_type' => null, |
| 36 | 'default_difficulty' => null, |
| 37 | 'default_voice' => null, |
| 38 | 'feature_toggles' => [], |
| 39 | ], |
| 40 | ]); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Upsert the company's settings. |
| 45 | */ |
| 46 | public function update(Request $request): JsonResponse |
| 47 | { |
| 48 | $validated = $request->validate([ |
| 49 | 'default_call_type' => ['sometimes', 'nullable', 'string', Rule::in(['cold-call', 'discovery-call'])], |
| 50 | 'default_difficulty' => 'sometimes|nullable|integer|min:1|max:5', |
| 51 | 'default_voice' => 'sometimes|nullable|string|max:255', |
| 52 | 'feature_toggles' => 'sometimes|nullable|array', |
| 53 | ]); |
| 54 | |
| 55 | $row = CompanyRolePlaySettings::updateOrCreate( |
| 56 | ['company_id' => $request->user()->company_id], |
| 57 | $validated, |
| 58 | ); |
| 59 | |
| 60 | return response()->json([ |
| 61 | 'status' => 'success', |
| 62 | 'data' => $this->serialize($row->fresh()), |
| 63 | ]); |
| 64 | } |
| 65 | |
| 66 | private function serialize(CompanyRolePlaySettings $row): array |
| 67 | { |
| 68 | return [ |
| 69 | 'id' => (string) $row->_id, |
| 70 | 'company_id' => $row->company_id, |
| 71 | 'default_call_type' => $row->default_call_type, |
| 72 | 'default_difficulty' => $row->default_difficulty, |
| 73 | 'default_voice' => $row->default_voice, |
| 74 | 'feature_toggles' => $row->feature_toggles ?? [], |
| 75 | 'created_at' => $row->created_at?->timestamp, |
| 76 | 'updated_at' => $row->updated_at?->timestamp, |
| 77 | ]; |
| 78 | } |
| 79 | } |