From 06d953cfcb039d0140a9be47c71271f888a9c39a Mon Sep 17 00:00:00 2001 From: lahirulakruwan Date: Mon, 27 May 2024 12:59:28 +0530 Subject: [PATCH] Asset get inventory data api gdapi changes added --- api/inventory_data.bal | 19 ++++++++++++ api/main.bal | 66 ++++++++++++++++-------------------------- api/types.bal | 3 ++ 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/api/inventory_data.bal b/api/inventory_data.bal index 8e40c1a..bae7064 100644 --- a/api/inventory_data.bal +++ b/api/inventory_data.bal @@ -118,8 +118,27 @@ public isolated service class InventoryData{ return new ResourcePropertyData(id); } + + isolated resource function get name() returns string?|error { + lock { + return self.inventory.name; + } + } + isolated resource function get description() returns string?|error { + lock { + return self.inventory.description; + } + } + + + isolated resource function get manufacturer() returns string?|error { + lock { + return self.inventory.manufacturer; + } + } + isolated resource function get created() returns string?|error { lock { return self.inventory.created; diff --git a/api/main.bal b/api/main.bal index 58a4c36..8fae651 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4719,59 +4719,53 @@ lock { } isolated resource function get inventory_data_by_organization(int? organization_id,string? date= null) returns InventoryData[]|error? { + stream inventory_data; // first check if inventory data for date are already have - Inventory|error dateInventoryData = db_client->queryRow( + inventory_data = db_client->query( `SELECT I.id,I.avinya_type_id,I.consumable_id, - I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value + I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value, + C.name, C.description, C.manufacturer FROM inventory I - INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id + INNER JOIN consumable C ON I.consumable_id = C.id + INNER JOIN resource_property RP ON C.id = RP.consumable_id WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date};` ); - // lock { - // inventory_data = db_client->query( - // `SELECT I.id,I.avinya_type_id,I.consumable_id, - // I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value - // FROM inventory I - // INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id - // WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date}; - // ` - // ); - // } - if !(dateInventoryData is Inventory) { - //if(inventory_data.next() == ()){ + if (inventory_data.next() == ()) { lock { inventory_data = db_client->query( `SELECT I.id,I.avinya_type_id,I.consumable_id, - I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value + I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value, + C.name,C.description,C.manufacturer FROM inventory I - INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id + INNER JOIN consumable C ON I.consumable_id = C.id + INNER JOIN resource_property RP ON C.id = RP.consumable_id INNER JOIN ( SELECT consumable_id, MAX(updated) as max_updated_at FROM inventory WHERE organization_id = ${organization_id} GROUP BY consumable_id - ) max_updated ON I.consumable_id = max_updated.consumable_id AND I.updated = max_updated.max_updated_at; + ) max_updated ON I.consumable_id = max_updated.consumable_id AND I.updated = max_updated.max_updated_at; ` ); - } - }else{ - - lock { - inventory_data = db_client->query( - `SELECT I.id,I.avinya_type_id,I.consumable_id, - I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value - FROM inventory I - INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id - WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date}; - ` - ); - } + if(inventory_data.next()== ()){ + + inventory_data = db_client->query( + `SELECT I.id,I.avinya_type_id,I.consumable_id,I.organization_id,I.person_id,COALESCE(I.quantity, 0) AS quantity, + COALESCE(I.quantity_in, 0) AS quantity_in,COALESCE(I.quantity_out, 0) AS quantity_out, + RP.id as resource_property_id,RP.value as resource_property_value, + C.name,C.description,C.manufacturer + FROM inventory I + RIGHT JOIN consumable C ON I.consumable_id = C.id + Left JOIN resource_property RP ON C.id = RP.consumable_id; + `); + } + } } @@ -4785,16 +4779,6 @@ lock { inventoryDatas.push(inventoryData); } }; - - if(inventoryDatas.length() == 0){ - Inventory inventory = {id:(),avinya_type_id: (),consumable_id: (),quantity: 0, - quantity_in: 0 , quantity_out:0,asset_id: 0, - organization_id:0,person_id: 0,created: (), - resource_property_id: () ,resource_property_value: (), - updated: () }; - InventoryData|error inventoryData = new InventoryData(0, inventory); - inventoryDatas.push(check inventoryData); - } check inventory_data.close(); return inventoryDatas; diff --git a/api/types.bal b/api/types.bal index 9fc90ba..d6c3c39 100644 --- a/api/types.bal +++ b/api/types.bal @@ -608,6 +608,9 @@ public type Inventory record {| int? avinya_type_id; int? asset_id; int? consumable_id; + string? name; + string? description; + string? manufacturer; int? organization_id; int? person_id; int? quantity;