Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| RolePlayCallTypeSettingsController | |
100.00% |
35 / 35 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolved | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| upsert | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2\RolePlay; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Models\RolePlayCallTypeSettings; |
| 7 | use App\Http\Requests\v2\RolePlay\UpsertCallTypeSettingsRequest; |
| 8 | use App\Http\Services\RolePlay\RolePlayCallTypeSettingsService; |
| 9 | use Illuminate\Http\JsonResponse; |
| 10 | use Illuminate\Http\Request; |
| 11 | |
| 12 | /** |
| 13 | * User-scoped controller for the RolePlay call-type target duration settings. |
| 14 | * |
| 15 | * Returns the fully resolved (user → company → system → default) target for |
| 16 | * the authenticated user, and allows the user to upsert their own per-user |
| 17 | * row — subject to the `allow_user_override` flag on their company row. |
| 18 | */ |
| 19 | class RolePlayCallTypeSettingsController extends Controller |
| 20 | { |
| 21 | public function __construct( |
| 22 | private readonly RolePlayCallTypeSettingsService $settingsService, |
| 23 | ) {} |
| 24 | |
| 25 | /** |
| 26 | * Return resolved target duration for both call types for the current |
| 27 | * user, along with the source (user|company|system|default) and whether |
| 28 | * the user is allowed to override each setting. |
| 29 | */ |
| 30 | public function resolved(Request $request): JsonResponse |
| 31 | { |
| 32 | $user = $request->user(); |
| 33 | |
| 34 | return response()->json([ |
| 35 | 'status' => 'success', |
| 36 | 'data' => $this->settingsService->resolveAll($user), |
| 37 | ]); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Upsert the authenticated user's personal target for one call type. |
| 42 | * |
| 43 | * If the user's company has disabled overrides for this call type, the |
| 44 | * write is rejected with a 403 so the UI can render the locked state. |
| 45 | */ |
| 46 | public function upsert(UpsertCallTypeSettingsRequest $request): JsonResponse |
| 47 | { |
| 48 | if (! $request->isSupportedCallType()) { |
| 49 | return response()->json([ |
| 50 | 'status' => 'error', |
| 51 | 'message' => 'Unsupported call type.', |
| 52 | ], 422); |
| 53 | } |
| 54 | |
| 55 | $user = $request->user(); |
| 56 | $callType = $request->callType(); |
| 57 | $targetSeconds = (int) $request->input('target_duration_seconds'); |
| 58 | |
| 59 | $resolved = $this->settingsService->resolve($user, $callType); |
| 60 | if (! $resolved['can_user_override']) { |
| 61 | return response()->json([ |
| 62 | 'status' => 'error', |
| 63 | 'message' => 'Your company administrator has locked this setting.', |
| 64 | ], 403); |
| 65 | } |
| 66 | |
| 67 | $row = $this->settingsService->upsert( |
| 68 | RolePlayCallTypeSettings::SCOPE_USER, |
| 69 | (string) $user->id, |
| 70 | $callType, |
| 71 | $targetSeconds, |
| 72 | ); |
| 73 | |
| 74 | return response()->json([ |
| 75 | 'status' => 'success', |
| 76 | 'data' => [ |
| 77 | 'call_type' => $row->call_type, |
| 78 | 'target_duration_seconds' => (int) $row->target_duration_seconds, |
| 79 | 'source' => RolePlayCallTypeSettings::SCOPE_USER, |
| 80 | 'can_user_override' => true, |
| 81 | ], |
| 82 | ]); |
| 83 | } |
| 84 | } |