From ec67eca87bdf74ad59a593b96e2394d33fc57f41 Mon Sep 17 00:00:00 2001 From: sayaliparab Date: Mon, 28 Apr 2025 12:08:04 +0530 Subject: [PATCH] assignTouser --- .../APIS/AdminApi/AssetadmintController.php | 114 +++++++++++++++++- app/Models/Asset.php | 6 +- 2 files changed, 112 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php index 7f0ae8e..068c82b 100644 --- a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php +++ b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php @@ -136,21 +136,85 @@ class AssetadmintController extends Controller + // public function listAssest() + // { + // try { + // $assets = Asset::with('customer')->get()->map(function ($asset) { + // $assetData = $asset->toArray(); + // unset($assetData['customer']); + // $assetData['customer_name'] = $asset->customer?->name; + // return $assetData; + // }); + + // return jsonResponseWithSuccessMessage('Assets fetched successfully', [ + // 'assests' => $assets + // ]); + // } catch (Exception $e) { + + // Log::error("An error occurred: " . $e->getMessage()); + // return jsonResponseWithErrorMessage($e->getMessage(), 500); + // } + // } + + // public function listAssest() + // { + // try { + // $assets = Asset::with([ + // 'customer:id,name', + // 'userAssetLinks.user:id,name' // eager load user names + // ])->get()->map(function ($asset) { + // $assetData = $asset->toArray(); + + // unset($assetData['customer'], $assetData['user_asset_links']); // remove full objects + + // $assetData['customer_name'] = $asset->customer?->name; + + // // Collect user names from user_asset_links + // $userNames = $asset->userAssetLinks->map(function ($userAssetLink) { + // return optional($userAssetLink->user)->name; + // })->filter()->values(); // remove nulls and reset keys + + // $assetData['user_names'] = $userNames; + + // return $assetData; + // }); + + // return jsonResponseWithSuccessMessage('Assets fetched successfully', [ + // 'assets' => $assets + // ]); + // } catch (Exception $e) { + // Log::error("An error occurred: " . $e->getMessage()); + // return jsonResponseWithErrorMessage($e->getMessage(), 500); + // } + // } + public function listAssest() { try { - $assets = Asset::with('customer')->get()->map(function ($asset) { + $assets = Asset::with([ + 'customer:id,name', + 'userAssetLinks.user:id,first_name' // eager load first_name only + ])->get()->map(function ($asset) { $assetData = $asset->toArray(); - unset($assetData['customer']); + + unset($assetData['customer'], $assetData['user_asset_links']); // remove full objects + $assetData['customer_name'] = $asset->customer?->name; + + // Collect user first names from user_asset_links + $userFirstNames = $asset->userAssetLinks->map(function ($userAssetLink) { + return optional($userAssetLink->user)->first_name; + })->filter()->values(); // remove nulls and reset keys + + $assetData['assign_to_user'] = $userFirstNames; + return $assetData; }); return jsonResponseWithSuccessMessage('Assets fetched successfully', [ - 'assests' => $assets + 'assets' => $assets ]); } catch (Exception $e) { - Log::error("An error occurred: " . $e->getMessage()); return jsonResponseWithErrorMessage($e->getMessage(), 500); } @@ -160,6 +224,8 @@ class AssetadmintController extends Controller + + public function deleteAsset(Request $request) { $assetId = $request->input('assetId'); // Fetching ID from request body @@ -328,17 +394,53 @@ class AssetadmintController extends Controller } + // public function assestlistCustomer($customerId) + // { + // try { + // $assets = Asset::with('customer:id,name') + // ->where('customer_xid', $customerId) + // ->get() + // ->map(function ($asset) { + // return collect($asset) + // ->except('customer') // remove the full customer object + // ->merge([ + // 'customer_name' => optional($asset->customer)->name, + // ]); + // }); + + // if ($assets->isEmpty()) { + // return response()->json(['message' => 'No assets found for this customer ID'], 200); + // } + + // return jsonResponseWithSuccessMessage('Assets fetched successfully', [ + // 'assets' => $assets + // ]); + // } catch (Exception $e) { + // Log::error("An error occurred in asset listing: " . $e->getMessage()); + // return jsonResponseWithErrorMessage($e->getMessage(), 500); + // } + // } + public function assestlistCustomer($customerId) { try { - $assets = Asset::with('customer:id,name') + $assets = Asset::with([ + 'customer:id,name', + 'userAssetLinks.user:id,name' // eager load user through userAssetLink + ]) ->where('customer_xid', $customerId) ->get() ->map(function ($asset) { + // Get user names linked to the asset + $userNames = $asset->userAssetLinks->map(function ($userAssetLink) { + return optional($userAssetLink->user)->name; + })->filter()->values(); // filter nulls and reset keys + return collect($asset) - ->except('customer') // remove the full customer object + ->except('customer', 'userAssetLinks') // remove full objects ->merge([ 'customer_name' => optional($asset->customer)->name, + 'assign_to_user' => $userNames, // attach user names ]); }); diff --git a/app/Models/Asset.php b/app/Models/Asset.php index fd8dc5f..5fb248c 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -57,6 +57,8 @@ class Asset extends Model { return $this->hasMany(Device::class, 'asset_id', 'id'); } - - + public function userAssetLinks() + { + return $this->hasMany(UserAssetLink::class, 'asset_id', 'id'); + } } -- 2.34.1