json([ 'data' => $assignments->listProducts(), ]); } public function index(string $publicId, PlatformUserResolver $users, AdminHostingAssignmentService $assignments): JsonResponse { $owner = $users->resolveOrCreate($publicId); if (! $owner) { return response()->json(['data' => []]); } $accounts = HostingAccount::query() ->where('user_id', $owner->id) ->with('product') ->latest() ->get() ->map(fn (HostingAccount $account) => $assignments->serializeAccount($account)) ->values() ->all(); return response()->json(['data' => $accounts]); } public function assign( Request $request, string $publicId, PlatformUserResolver $users, AdminHostingAssignmentService $assignments, ): JsonResponse { $validated = $request->validate([ 'hosting_product_id' => ['required', 'integer', 'exists:hosting_products,id'], 'duration_months' => ['required', 'integer', 'min:1', 'max:120'], 'primary_domain' => ['nullable', 'string', 'max:255'], 'username' => ['nullable', 'string', 'max:32'], 'notes' => ['nullable', 'string', 'max:1000'], 'owner_email' => ['required', 'email', 'max:255'], 'owner_name' => ['nullable', 'string', 'max:255'], 'assigned_by_admin' => ['nullable', 'integer'], ]); $owner = $users->resolveOrCreate( $publicId, (string) $validated['owner_email'], (string) ($validated['owner_name'] ?? ''), ); if (! $owner) { return response()->json(['error' => 'User not found.'], 404); } $result = $assignments->assign($owner, $validated); return response()->json(['data' => $result], 201); } public function updateDuration( Request $request, int $account, AdminHostingAssignmentService $assignments, ): JsonResponse { $validated = $request->validate([ 'duration_months' => ['required', 'integer', 'min:1', 'max:120'], 'admin_id' => ['nullable', 'integer'], ]); $hostingAccount = HostingAccount::query()->findOrFail($account); $data = $assignments->updateDuration( $hostingAccount, (int) $validated['duration_months'], isset($validated['admin_id']) ? (int) $validated['admin_id'] : null, ); return response()->json(['data' => $data]); } public function renew( Request $request, int $account, AdminHostingAssignmentService $assignments, ): JsonResponse { $validated = $request->validate([ 'admin_id' => ['nullable', 'integer'], ]); $hostingAccount = HostingAccount::query()->findOrFail($account); $data = $assignments->renew( $hostingAccount, isset($validated['admin_id']) ? (int) $validated['admin_id'] : null, ); return response()->json(['data' => $data]); } public function unsuspend( int $account, AdminHostingAssignmentService $assignments, HostingResourcePolicyService $resourcePolicy, ): JsonResponse { $hostingAccount = HostingAccount::query()->findOrFail($account); $assignments->unsuspend($hostingAccount, $resourcePolicy); return response()->json(['data' => ['ok' => true]]); } }