Merge pull request 'sayali' (#100) from sayali into main
Reviewed-on: Nikhil.Kadam/vib360#100
This commit is contained in:
@@ -82,9 +82,9 @@ class AssetadmintController extends Controller
|
||||
public function storeAssest(CreateAssetRequest $request)
|
||||
{
|
||||
try {
|
||||
$additionalInfo = $request->has('additional_info') && is_string($request->additional_info)
|
||||
? json_decode($request->additional_info, true)
|
||||
: $request->additional_info;
|
||||
// $additionalInfo = $request->has('additional_info') && is_string($request->additional_info)
|
||||
// ? json_decode($request->additional_info, true)
|
||||
// : $request->additional_info;
|
||||
$assetData = [
|
||||
'entity_type' => 'ASSET', // Backend defined
|
||||
'createdTime' => now()->timestamp, // Backend defined
|
||||
@@ -96,7 +96,8 @@ class AssetadmintController extends Controller
|
||||
'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'],
|
||||
// 'additionalInfo' => $additionalInfo ?? ['description' => 'Default asset description'],
|
||||
'additionalInfo' => $request->additionalInfo
|
||||
];
|
||||
$response = $this->adminService->createAsset($assetData);
|
||||
if (!$response) {
|
||||
@@ -115,6 +116,7 @@ class AssetadmintController extends Controller
|
||||
$asset->external_id = $assetData['externalId'];
|
||||
$asset->version = $response['version'] ?? $assetData['version'];
|
||||
$asset->additional_info = json_encode($response['additionalInfo'] ?? $assetData['additionalInfo']);
|
||||
// 'additional_info' => json_encode($apiData['additionalInfo'] ?? [])
|
||||
$asset->save();
|
||||
// Fetch only the customer name
|
||||
$customerName = \App\Models\Customer::where('id', $assetData['customerId'])->value('name');
|
||||
@@ -129,60 +131,7 @@ class AssetadmintController extends Controller
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
// public function storeAssest(CreateAssetRequest $request)
|
||||
// {
|
||||
// try {
|
||||
// $additionalInfo = $request->has('additional_info') && is_string($request->additional_info)
|
||||
// ? json_decode($request->additional_info, true)
|
||||
// : (is_array($request->additional_info) ? $request->additional_info : ['description' => 'Default asset description']);
|
||||
|
||||
// $assetData = [
|
||||
// 'entity_type' => 'ASSET',
|
||||
// 'createdTime' => now()->timestamp,
|
||||
// 'tenantId' => $request->tenant_id ?? Str::uuid()->toString(),
|
||||
// 'customerId' => $request->customer_xid,
|
||||
// 'name' => $request->name,
|
||||
// 'type' => $request->type ?? 'Default Type',
|
||||
// 'label' => $request->label ?? '',
|
||||
// 'assetProfileId' => $request->asset_profile_id ?? Str::uuid()->toString(),
|
||||
// 'externalId' => $request->external_id ?? Str::uuid()->toString(),
|
||||
// 'additionalInfo' => $additionalInfo,
|
||||
// ];
|
||||
|
||||
// $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 = $assetData['entity_type'];
|
||||
// $asset->created_time = $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'] ?? 1;
|
||||
// $asset->additional_info = json_encode($response['additionalInfo'] ?? $assetData['additionalInfo']);
|
||||
// $asset->save();
|
||||
|
||||
// $customerName = \App\Models\Customer::where('id', $assetData['customerId'])->value('name');
|
||||
|
||||
// return response()->json([
|
||||
// 'message' => 'Asset created successfully!',
|
||||
// 'data' => $asset,
|
||||
// 'customer_name' => $customerName,
|
||||
// 'api_response' => $response
|
||||
// ], 200);
|
||||
// } catch (\Exception $e) {
|
||||
// Log::error('Error in creating asset: ' . $e->getMessage());
|
||||
// return response()->json(['error' => $e->getMessage()], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@@ -201,6 +150,7 @@ class AssetadmintController extends Controller
|
||||
'assests' => $assets
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
|
||||
Log::error("An error occurred: " . $e->getMessage());
|
||||
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
}
|
||||
@@ -256,18 +206,77 @@ class AssetadmintController extends Controller
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function assignAssetToUser(Request $request)
|
||||
{
|
||||
|
||||
try {
|
||||
$request->validate([
|
||||
'user_id' => 'required|exists:users,id',
|
||||
'asset_id' => 'required|uuid'
|
||||
'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)
|
||||
@@ -277,37 +286,24 @@ class AssetadmintController extends Controller
|
||||
$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->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' => 'Asset reassigned to user successfully.',
|
||||
'message' => $message,
|
||||
'data' => $userAssetLink
|
||||
], 200);
|
||||
} catch (Exception $e) {
|
||||
Log::error("Error assigning asset: " . $e->getMessage());
|
||||
Log::error("Error assigning/unassigning asset: " . $e->getMessage());
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Failed to assign or unassign asset to user',
|
||||
@@ -316,6 +312,7 @@ class AssetadmintController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function customerList()
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -108,7 +108,7 @@ class AlarmControllerCommon extends Controller
|
||||
public function acknowledgeAlarmById($id)
|
||||
{
|
||||
try {
|
||||
$token = app('App\Services\AdminService')->getToken(); // Fetch cached token
|
||||
$token = app('App\Services\AdminService')->getToken(); // Fetch cached token0
|
||||
$url = env('THINGSBOARD_URL') . "/api/alarm/{$id}";
|
||||
|
||||
$getResponse = Http::withHeaders([
|
||||
@@ -149,6 +149,8 @@ class AlarmControllerCommon extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return jsonResponseWithErrorMessage('Failed to acknowledge alarm: ' . $ackResponse->body(), $ackResponse->status());
|
||||
} catch (\Exception $e) {
|
||||
return jsonResponseWithErrorMessage('An error occurred: ' . $e->getMessage(), 500);
|
||||
@@ -200,6 +202,7 @@ class AlarmControllerCommon extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
return jsonResponseWithErrorMessage('Failed to clear alarm: ' . $clearResponse->body(), $clearResponse->status());
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Error clearing alarm: ' . $e->getMessage());
|
||||
|
||||
@@ -93,7 +93,7 @@ class AdminService
|
||||
// throw new Exception('Failed to create asset: ' . $response->body());
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public function createAsset(array $data)
|
||||
// {
|
||||
// $token = $this->getToken();
|
||||
@@ -142,7 +142,8 @@ class AdminService
|
||||
'type' => $data['type'],
|
||||
'label' => $data['label'],
|
||||
// 'version' => removed to let backend default to 1
|
||||
'additionalInfo' => $data['additionalInfo'],
|
||||
// 'additionalInfo' => $data['additionalInfo'],
|
||||
'additionalInfo' => ['description' => $data['additionalInfo'] ?? 'User description'],
|
||||
];
|
||||
|
||||
$response = Http::withHeaders([
|
||||
@@ -160,37 +161,6 @@ class AdminService
|
||||
}
|
||||
}
|
||||
|
||||
// public function createAsset(array $data)
|
||||
// {
|
||||
// $token = $this->getToken();
|
||||
|
||||
// $payload = [
|
||||
// 'entityType' => $data['entity_type'],
|
||||
// 'createdTime' => $data['createdTime'],
|
||||
// 'customerId' => [
|
||||
// 'id' => $data['customerId'],
|
||||
// 'entityType' => 'CUSTOMER'
|
||||
// ],
|
||||
// 'name' => $data['name'],
|
||||
// 'type' => $data['type'],
|
||||
// 'label' => $data['label'],
|
||||
// 'additionalInfo' => $data['additionalInfo'], // must be an array
|
||||
// ];
|
||||
|
||||
// $response = Http::withHeaders([
|
||||
// 'Authorization' => "Bearer $token",
|
||||
// 'Accept' => 'application/json',
|
||||
// 'Content-Type' => 'application/json',
|
||||
// ])->withBody(json_encode($payload), 'application/json')
|
||||
// ->post("{$this->baseUrl}/api/asset");
|
||||
|
||||
// if ($response->successful()) {
|
||||
// return $response->json();
|
||||
// } else {
|
||||
// Log::error('Error in creating asset: ' . $response->body());
|
||||
// throw new Exception('Failed to create asset: ' . $response->body());
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user