Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.08% covered (warning)
77.08%
37 / 48
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompanyRolePlayCallTypeSettingsController
77.08% covered (warning)
77.08%
37 / 48
25.00% covered (danger)
25.00%
1 / 4
11.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
2.00
 upsert
79.31% covered (warning)
79.31%
23 / 29
0.00% covered (danger)
0.00%
0 / 1
4.14
 requireCompanyId
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
4.12
1<?php
2
3namespace App\Http\Controllers\v2\Company;
4
5use App\Http\Controllers\Controller;
6use App\Http\Models\RolePlayCallTypeSettings;
7use App\Http\Requests\v2\RolePlay\UpsertCallTypeSettingsRequest;
8use App\Http\Services\RolePlay\RolePlayCallTypeSettingsService;
9use Illuminate\Http\JsonResponse;
10use Illuminate\Http\Request;
11
12/**
13 * Company-scope controller for RolePlay call-type target duration settings.
14 *
15 * Route authorization is enforced via the `ensure.company.admin` middleware
16 * (global admin or Vengreso admin). Both read and write endpoints operate on
17 * the company the authenticated user belongs to — there is no cross-company
18 * access at this layer; CMC uses the admin controller for that.
19 */
20class CompanyRolePlayCallTypeSettingsController extends Controller
21{
22    public function __construct(
23        private readonly RolePlayCallTypeSettingsService $settingsService,
24    ) {}
25
26    /**
27     * List the current company defaults. Missing rows fall back to the
28     * hard-coded defaults with `allow_user_override = true` so the admin-fe
29     * form has something to render on first use.
30     */
31    public function index(Request $request): JsonResponse
32    {
33        $companyId = $this->requireCompanyId($request);
34        if ($companyId instanceof JsonResponse) {
35            return $companyId;
36        }
37
38        return response()->json([
39            'status' => 'success',
40            'data' => $this->settingsService->listRowsForScope(
41                RolePlayCallTypeSettings::SCOPE_COMPANY,
42                $companyId,
43            ),
44        ]);
45    }
46
47    /**
48     * Upsert the company default for one call type, including the
49     * `allow_user_override` flag that locks/unlocks the per-user overrides.
50     */
51    public function upsert(UpsertCallTypeSettingsRequest $request): JsonResponse
52    {
53        $companyId = $this->requireCompanyId($request);
54        if ($companyId instanceof JsonResponse) {
55            return $companyId;
56        }
57
58        if (! $request->isSupportedCallType()) {
59            return response()->json([
60                'status' => 'error',
61                'message' => 'Unsupported call type.',
62            ], 422);
63        }
64
65        $callType = $request->callType();
66        $targetSeconds = (int) $request->input('target_duration_seconds');
67        $allowUserOverride = $request->has('allow_user_override')
68            ? (bool) $request->input('allow_user_override')
69            : true;
70
71        $row = $this->settingsService->upsert(
72            RolePlayCallTypeSettings::SCOPE_COMPANY,
73            $companyId,
74            $callType,
75            $targetSeconds,
76            $allowUserOverride,
77        );
78
79        return response()->json([
80            'status' => 'success',
81            'data' => [
82                'call_type' => $row->call_type,
83                'target_duration_seconds' => (int) $row->target_duration_seconds,
84                'allow_user_override' => (bool) ($row->allow_user_override ?? true),
85                'scope' => RolePlayCallTypeSettings::SCOPE_COMPANY,
86            ],
87        ]);
88    }
89
90    /**
91     * @return string|JsonResponse Company id string on success, or an error
92     *                             response when the user has no company.
93     */
94    private function requireCompanyId(Request $request)
95    {
96        $user = $request->user();
97        $companyId = $user?->company_id ? (string) $user->company_id : null;
98
99        if (! $companyId) {
100            return response()->json([
101                'status' => 'error',
102                'message' => 'User is not associated with a company.',
103            ], 422);
104        }
105
106        return $companyId;
107    }
108}