From 9d671ed516e01a2c32c08da8906c108376e547cc Mon Sep 17 00:00:00 2001 From: sayaliparab Date: Mon, 28 Apr 2025 16:38:10 +0530 Subject: [PATCH] assign --- .../APIS/AdminApi/AssetadmintController.php | 65 ++++++++++++++++--- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php index b4f671b..b1637c9 100644 --- a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php +++ b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php @@ -381,6 +381,52 @@ class AssetadmintController extends Controller // } // } + // public function assignAssetToUser(Request $request) + // { + // try { + // $request->validate([ + // 'user_id' => 'required|exists:users,id', + // 'asset_id' => 'required|uuid', + // 'active' => 'required|boolean' + // ]); + + // $userId = $request->user_id; + // $assetId = $request->asset_id; + // $activeStatus = $request->active; + + // $userAssetLink = UserAssetLink::where('user_id', $userId) + // ->where('asset_id', $assetId) + // ->first(); + + // if (!$userAssetLink) { + // $userAssetLink = new UserAssetLink(); + // $userAssetLink->user_id = $userId; + // $userAssetLink->asset_id = $assetId; + // } + + // $userAssetLink->active = $activeStatus; + // $userAssetLink->save(); + + // $message = $activeStatus + // ? 'Asset assigned to user successfully.' + // : 'Asset unassigned from user.'; + + // Log::info($message, ['userId' => $userId, 'assetId' => $assetId]); + + // return response()->json([ + // 'success' => true, + // 'message' => $message, + // 'data' => $userAssetLink + // ], 200); + // } catch (Exception $e) { + // Log::error("Error assigning/unassigning asset: " . $e->getMessage()); + // return response()->json([ + // 'success' => false, + // 'message' => 'Failed to assign or unassign asset to user', + // 'error' => $e->getMessage() + // ], 500); + // } + // } public function assignAssetToUser(Request $request) { try { @@ -394,16 +440,18 @@ class AssetadmintController extends Controller $assetId = $request->asset_id; $activeStatus = $request->active; - $userAssetLink = UserAssetLink::where('user_id', $userId) - ->where('asset_id', $assetId) - ->first(); - - if (!$userAssetLink) { - $userAssetLink = new UserAssetLink(); - $userAssetLink->user_id = $userId; - $userAssetLink->asset_id = $assetId; + if ($activeStatus) { + // If assigning asset, first unassign from any other users + UserAssetLink::where('asset_id', $assetId) + ->update(['active' => 0]); } + // Now either update existing record or create a new one for this user and asset + $userAssetLink = UserAssetLink::firstOrNew([ + 'user_id' => $userId, + 'asset_id' => $assetId + ]); + $userAssetLink->active = $activeStatus; $userAssetLink->save(); @@ -429,6 +477,7 @@ class AssetadmintController extends Controller } + public function customerList() { try { -- 2.34.1