Files
backend_vib360_laravel/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php

246 lines
8.5 KiB
PHP
Raw Normal View History

2025-03-20 11:52:34 +05:30
<?php
namespace App\Http\Controllers\APIS\AdminApi;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\CreateAssetRequest;
use Illuminate\Support\Facades\Log;
use App\Services\AdminService;
2025-03-21 12:11:18 +05:30
use App\Models\Customer;
2025-03-20 11:52:34 +05:30
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use App\Models\Asset;
use App\Models\UserAssetLink;
use Exception;
class AssetadmintController extends Controller
{
private $adminService;
public function __construct(AdminService $adminService)
{
$this->adminService = $adminService;
}
2025-03-21 12:11:18 +05:30
public function storeAssest(CreateAssetRequest $request)
2025-03-20 11:52:34 +05:30
{
try {
$additionalInfo = $request->has('additional_info') && is_string($request->additional_info)
? json_decode($request->additional_info, true)
: $request->additional_info;
$assetData = [
'entity_type' => $request->entity_type,
'createdTime' => $request->created_time ?? now()->timestamp,
'tenantId' => $request->tenant_id ?? Str::uuid()->toString(),
'customerId' => $request->customer_xid,
'name' => $request->name,
'type' => $request->type,
'label' => $request->label,
'assetProfileId' => $request->asset_profile_id ?? Str::uuid()->toString(),
'externalId' => $request->external_id ?? Str::uuid()->toString(),
'version' => $request->version ?? '1.0',
'additionalInfo' => $additionalInfo ?? ['description' => 'Default asset description'],
];
$response = $this->adminService->createAsset($assetData);
if (!$response) {
throw new Exception('Failed to create asset via admin service');
}
$asset = new Asset();
$asset->id = $response['id']['id'] ?? Str::uuid()->toString();
$asset->entity_type = $response['entityType'] ?? $assetData['entity_type'];
$asset->created_time = $response['createdTime'] ?? $assetData['createdTime'];
$asset->tenant_id = $assetData['tenantId'];
$asset->customer_xid = $assetData['customerId'];
$asset->name = $response['name'] ?? $assetData['name'];
$asset->type = $response['type'] ?? $assetData['type'];
$asset->label = $response['label'] ?? $assetData['label'];
$asset->asset_profile_id = $assetData['assetProfileId'];
$asset->external_id = $assetData['externalId'];
$asset->version = $response['version'] ?? $assetData['version'];
$asset->additional_info = json_encode($response['additionalInfo'] ?? $assetData['additionalInfo']);
$asset->save();
return response()->json([
'message' => 'Asset created successfully!',
'data' => $asset,
'api_response' => $response
], 200);
} catch (Exception $e) {
Log::error('Error in creating asset: ' . $e->getMessage());
return response()->json(['error' => $e->getMessage()], 500);
}
}
public function listAssest()
{
try {
2025-04-08 12:52:01 +05:30
$assets = Asset::with('customer')->get()->map(function ($asset) {
$assetData = $asset->toArray();
unset($assetData['customer']);
2025-04-08 12:52:01 +05:30
$assetData['customer_name'] = $asset->customer?->name;
return $assetData;
});
return jsonResponseWithSuccessMessage('Assets fetched successfully', [
'assests' => $assets
2025-03-20 11:52:34 +05:30
]);
} catch (Exception $e) {
Log::error("An error occurred: " . $e->getMessage());
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
2025-03-28 20:01:51 +05:30
2025-03-28 13:54:21 +05:30
2025-04-08 12:52:01 +05:30
2025-03-28 13:54:21 +05:30
public function deleteAsset(Request $request)
2025-03-20 11:52:34 +05:30
{
2025-03-28 13:54:21 +05:30
$assetId = $request->input('assetId'); // Fetching ID from request body
2025-03-20 11:52:34 +05:30
if (!$assetId) {
return jsonResponseWithErrorMessage('Asset ID is required', 400);
}
2025-03-28 13:54:21 +05:30
2025-03-20 11:52:34 +05:30
$response = $this->adminService->deleteAsset(['assetId' => $assetId]);
Log::info("Response: " . json_encode($response));
2025-03-28 13:54:21 +05:30
2025-03-20 11:52:34 +05:30
if (!is_array($response)) {
Log::error("Unexpected API response format.", ['response' => $response]);
return jsonResponseWithErrorMessage('Unexpected API response format', 500);
}
2025-03-28 13:54:21 +05:30
2025-03-20 11:52:34 +05:30
if (isset($response['status']) && $response['status'] === 400) {
Log::error("Failed to delete asset: " . $response['message']);
return jsonResponseWithErrorMessage($response['message'], 400, $response);
}
2025-03-28 13:54:21 +05:30
2025-03-20 11:52:34 +05:30
if (empty($response)) {
Log::error("API Data is empty, cannot delete asset.");
return jsonResponseWithErrorMessage('Failed to delete asset', 400);
}
2025-03-28 13:54:21 +05:30
// Delete from local database
2025-03-20 11:52:34 +05:30
$asset = Asset::where('id', $assetId)->first();
if ($asset) {
$asset->delete();
}
2025-03-28 13:54:21 +05:30
2025-03-20 11:52:34 +05:30
return jsonResponseWithSuccessMessage('Asset deleted successfully', ['api_response' => $response]);
}
2025-03-28 13:54:21 +05:30
2025-03-20 11:52:34 +05:30
public function assignAssetToUser(Request $request)
{
try {
$request->validate([
'user_id' => 'required|exists:users,id',
'asset_id' => 'required|uuid'
]);
$userId = $request->user_id;
$assetId = $request->asset_id;
$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 = 1;
$userAssetLink->save();
Log::info("Asset successfully assigned to user.", ['userId' => $userId, 'assetId' => $assetId]);
return response()->json([
'success' => true,
'message' => 'Asset assigned to user successfully.',
'data' => $userAssetLink
], 200);
}
if ($userAssetLink->active == 1) {
$userAssetLink->active = 0;
$userAssetLink->save();
return response()->json([
'success' => true,
'message' => 'Asset unassigned from user.',
'data' => $userAssetLink
], 200);
}
$userAssetLink->active = 1;
$userAssetLink->save();
return response()->json([
'success' => true,
'message' => 'Asset reassigned to user successfully.',
'data' => $userAssetLink
], 200);
} catch (Exception $e) {
Log::error("Error assigning asset: " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to assign or unassign asset to user',
'error' => $e->getMessage()
], 500);
}
}
2025-03-21 12:11:18 +05:30
public function customerList()
{
try {
$customers = Customer::all();
return jsonResponseWithSuccessMessage('Customers fetched successfully', [
'customers' => $customers
]);
} catch (Exception $e) {
Log::error("An error occurred: " . $e->getMessage());
return jsonResponseWithErrorMessage($e->getMessage(), 500);
}
}
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'], 404);
}
2025-03-21 12:11:18 +05:30
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);
2025-03-21 12:11:18 +05:30
}
}
2025-03-28 20:01:51 +05:30
2025-04-08 12:52:01 +05:30
2025-03-20 11:52:34 +05:30
}