From 1f713b9d2606c0629985403b4951d91ffd7e7a27 Mon Sep 17 00:00:00 2001 From: sayaliparab Date: Tue, 15 Apr 2025 17:05:36 +0530 Subject: [PATCH] validation --- .../APIS/AdminApi/AssetadmintController.php | 56 ++++++++++++++ app/Services/AdminService.php | 73 ++++++++++++++++++- 2 files changed, 126 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php index d6a8a40..3e49fe2 100644 --- a/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php +++ b/app/Http/Controllers/APIS/AdminApi/AssetadmintController.php @@ -129,6 +129,62 @@ 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); + // } + // } + + public function listAssest() diff --git a/app/Services/AdminService.php b/app/Services/AdminService.php index e99a285..12ba33d 100644 --- a/app/Services/AdminService.php +++ b/app/Services/AdminService.php @@ -93,14 +93,47 @@ class AdminService // throw new Exception('Failed to create asset: ' . $response->body()); // } // } - + + // public function createAsset(array $data) + // { + // $token = $this->getToken(); + + // $payload = [ + // 'entityType' => $data['entity_type'], // Always 'ASSET' from backend + // 'createdTime' => $data['createdTime'], // Always from backend + // 'customerId' => [ + // 'id' => $data['customerId'], + // 'entityType' => 'CUSTOMER' + // ], + // 'name' => $data['name'], + // 'type' => $data['type'], + // 'label' => $data['label'], + // 'version' => $data['version'], + // 'additionalInfo' => $data['additionalInfo'], + // ]; + + // $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()); + // } + // } + public function createAsset(array $data) { $token = $this->getToken(); $payload = [ 'entityType' => $data['entity_type'], // Always 'ASSET' from backend - 'createdTime' => $data['createdTime'], // Always from backend + 'createdTime' => $data['createdTime'], 'customerId' => [ 'id' => $data['customerId'], 'entityType' => 'CUSTOMER' @@ -108,7 +141,7 @@ class AdminService 'name' => $data['name'], 'type' => $data['type'], 'label' => $data['label'], - 'version' => $data['version'], + // 'version' => removed to let backend default to 1 'additionalInfo' => $data['additionalInfo'], ]; @@ -127,6 +160,40 @@ 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()); + // } + // } + + + public function createOrUpdateCustomer(array $data) { -- 2.34.1