assign #104

Merged
Sayli.Parab merged 1 commits from sayali into main 2025-04-28 11:08:38 +00:00

View File

@@ -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 {