From 7cd8cab3e4f46e0879ed2943caaeaefe03ad08d5 Mon Sep 17 00:00:00 2001 From: lahirulakruwan Date: Thu, 30 May 2024 18:38:06 +0530 Subject: [PATCH 1/4] Asset Add inventories api function backend development changes added --- api/Dependencies.toml | 2 +- api/inventory_data.bal | 6 +- api/main.bal | 165 ++++++++++++++++++++++++++++++++--------- api/types.bal | 6 +- 4 files changed, 139 insertions(+), 40 deletions(-) diff --git a/api/Dependencies.toml b/api/Dependencies.toml index 4cef7dc..2baa3f2 100644 --- a/api/Dependencies.toml +++ b/api/Dependencies.toml @@ -106,7 +106,7 @@ modules = [ [[package]] org = "ballerina" name = "http" -version = "2.8.6" +version = "2.8.7" dependencies = [ {org = "ballerina", name = "auth"}, {org = "ballerina", name = "cache"}, diff --git a/api/inventory_data.bal b/api/inventory_data.bal index bae7064..537e76a 100644 --- a/api/inventory_data.bal +++ b/api/inventory_data.bal @@ -89,19 +89,19 @@ public isolated service class InventoryData{ return new PersonData((),id); } - isolated resource function get quantity() returns int?|error { + isolated resource function get quantity() returns decimal?|error { lock { return self.inventory.quantity; } } - isolated resource function get quantity_in() returns int?|error { + isolated resource function get quantity_in() returns decimal?|error { lock { return self.inventory.quantity_in; } } - isolated resource function get quantity_out() returns int?|error { + isolated resource function get quantity_out() returns decimal?|error { lock { return self.inventory.quantity_out; } diff --git a/api/main.bal b/api/main.bal index 8fae651..8b93253 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4723,39 +4723,67 @@ lock { stream inventory_data; // first check if inventory data for date are already have - 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, - C.name, C.description, C.manufacturer - FROM inventory I - 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};` + int|error? check_inventory_data_for_date = check db_client->queryRow( + `SELECT + COUNT(*) AS data_exists + FROM + inventory I + 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}; + ` ); - 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, - C.name,C.description,C.manufacturer - FROM inventory I - 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; - ` - ); - - if(inventory_data.next()== ()){ - - inventory_data = db_client->query( + if (check_inventory_data_for_date == 0){ + + int|error? check_least_updated_inventory_data_for_date = check db_client->queryRow( + `SELECT + CASE + WHEN COUNT(*) > 0 THEN 1 + ELSE 0 + END AS has_rows + FROM ( + 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, + C.name, + C.description, + C.manufacturer + FROM + inventory I + 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 + ) AS subquery;`); + + + if(check_least_updated_inventory_data_for_date == 0){ + + 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, @@ -4764,17 +4792,42 @@ lock { RIGHT JOIN consumable C ON I.consumable_id = C.id Left JOIN resource_property RP ON C.id = RP.consumable_id; `); - } - } + }else{ - } + 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, + C.name,C.description,C.manufacturer + FROM inventory I + 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; + `); + } + + }else{ + 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, + C.name, C.description, C.manufacturer + FROM inventory I + 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};`); + } InventoryData[] inventoryDatas = []; check from Inventory inventory in inventory_data do { InventoryData|error inventoryData = new InventoryData(0,inventory); + if !(inventoryData is error) { inventoryDatas.push(inventoryData); } @@ -4783,6 +4836,52 @@ lock { check inventory_data.close(); return inventoryDatas; } + + remote function add_inventories(Inventory[] inventories) returns int|error? { + + int count = 0; + + //access the 0 index inventory object + Inventory inventory_object = inventories[0]; + + sql:ExecutionResult res = check db_client->execute( + `DELETE FROM inventory + WHERE organization_id = ${inventory_object.organization_id} AND DATE(updated) = ${inventory_object.updated};` + ); + + int? delete_id = res.affectedRowCount; + if (delete_id < 0) { + return error("Unable to delete with org id : " + inventory_object.organization_id.toString()); + } + + foreach Inventory inventory in inventories { + sql:ExecutionResult response = check db_client->execute( + `INSERT INTO inventory ( + avinya_type_id, + consumable_id, + organization_id, + person_id, + quantity, + quantity_in + ) VALUES ( + ${inventory.avinya_type_id}, + ${inventory.consumable_id}, + ${inventory.organization_id}, + ${inventory.person_id}, + ${inventory.quantity}, + ${inventory.quantity_in} + );` + ); + + int|string? insert_id = response.lastInsertId; + if !(insert_id is int) { + return error("Unable to insert inventories"); + } else { + count += 1; + } + } + return count; + } } isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int { diff --git a/api/types.bal b/api/types.bal index d6c3c39..3e1e5d6 100644 --- a/api/types.bal +++ b/api/types.bal @@ -613,9 +613,9 @@ public type Inventory record {| string? manufacturer; int? organization_id; int? person_id; - int? quantity; - int? quantity_in; - int? quantity_out; + decimal? quantity; + decimal? quantity_in; + decimal? quantity_out; int? resource_property_id; string? resource_property_value; string? created; From ccd8d3146fa2e9c135d3876c8b9886c574d3d4e4 Mon Sep 17 00:00:00 2001 From: lahirulakruwan Date: Tue, 4 Jun 2024 20:53:33 +0530 Subject: [PATCH 2/4] Consumable replenishment api changes added --- api/main.bal | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/api/main.bal b/api/main.bal index 8b93253..0c239ac 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4837,23 +4837,26 @@ lock { return inventoryDatas; } - remote function add_inventories(Inventory[] inventories) returns int|error? { + remote function consumable_replenishment(Inventory[] inventories) returns InventoryData[]|error? { - int count = 0; - //access the 0 index inventory object - Inventory inventory_object = inventories[0]; + foreach Inventory inventory_object in inventories { + sql:ExecutionResult res = check db_client->execute( + `DELETE FROM inventory + WHERE organization_id = ${inventory_object.organization_id} + AND consumable_id = ${inventory_object.consumable_id} + AND DATE(updated) = ${inventory_object.updated};` + ); - sql:ExecutionResult res = check db_client->execute( - `DELETE FROM inventory - WHERE organization_id = ${inventory_object.organization_id} AND DATE(updated) = ${inventory_object.updated};` - ); + int? delete_id = res.affectedRowCount; + if (delete_id < 0) { + return error("Unable to delete with org id : " + inventory_object.organization_id.toString()); + } - int? delete_id = res.affectedRowCount; - if (delete_id < 0) { - return error("Unable to delete with org id : " + inventory_object.organization_id.toString()); } + InventoryData[] newlyAddedInventoryDatas = []; + foreach Inventory inventory in inventories { sql:ExecutionResult response = check db_client->execute( `INSERT INTO inventory ( @@ -4862,14 +4865,18 @@ lock { organization_id, person_id, quantity, - quantity_in + quantity_in, + created, + updated ) VALUES ( ${inventory.avinya_type_id}, ${inventory.consumable_id}, ${inventory.organization_id}, ${inventory.person_id}, ${inventory.quantity}, - ${inventory.quantity_in} + ${inventory.quantity_in}, + ${inventory.updated}, + ${inventory.updated} );` ); @@ -4877,10 +4884,13 @@ lock { if !(insert_id is int) { return error("Unable to insert inventories"); } else { - count += 1; + InventoryData|error newlyAddedInventoryData = new InventoryData(insert_id); + if !(newlyAddedInventoryData is error) { + newlyAddedInventoryDatas.push(newlyAddedInventoryData); + } } } - return count; + return newlyAddedInventoryDatas; } } From 63d36eae2498d7326a939ba8c825b43aa2c954ee Mon Sep 17 00:00:00 2001 From: lahirulakruwan Date: Wed, 5 Jun 2024 20:04:23 +0530 Subject: [PATCH 3/4] Consumable depletion backend api function changes added --- api/main.bal | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/api/main.bal b/api/main.bal index 0c239ac..3badbc8 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4892,6 +4892,48 @@ lock { } return newlyAddedInventoryDatas; } + + remote function consumable_depletion(Inventory[] inventories) returns InventoryData[]|error? { + + InventoryData[] inventoryDatas = []; + + foreach Inventory inventory in inventories { + + int id = inventory.id ?: 0; + + if (id == 0) { + return error("Unable to update inventory Data"); + } + + Inventory|error? inventoryRaw = db_client->queryRow( + `SELECT * + FROM inventory + WHERE id = ${inventory.id};`); + + if !(inventoryRaw is Inventory) { + return error("Inventory Data does not exist"); + } + + sql:ExecutionResult|error response = db_client->execute( + `UPDATE inventory SET + quantity_out = ${inventory.quantity_out}, + updated = ${inventory.updated} + WHERE id = ${id};`); + + if (response is sql:ExecutionResult) { + InventoryData|error inventoryData = new InventoryData(id); + + if !(inventoryData is error) { + + inventoryDatas.push(inventoryData); + } + } else{ + return error("Unable to update inventory Data"); + } + + } + return inventoryDatas; + } } isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int { From 8f258335451895bebf9b5d4ab342f5b1abc9f049 Mon Sep 17 00:00:00 2001 From: lahirulakruwan Date: Tue, 11 Jun 2024 09:35:22 +0530 Subject: [PATCH 4/4] Consumable depletion api changes added --- api/main.bal | 302 +++++++++++++++++++++++++++++---------------------- 1 file changed, 175 insertions(+), 127 deletions(-) diff --git a/api/main.bal b/api/main.bal index 3badbc8..7768367 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4725,102 +4725,168 @@ lock { // first check if inventory data for date are already have int|error? check_inventory_data_for_date = check db_client->queryRow( `SELECT - COUNT(*) AS data_exists - FROM - inventory I - 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}; - ` - ); + CASE + WHEN ( + SELECT COUNT(DISTINCT I.consumable_id) + FROM inventory I + WHERE I.organization_id = ${organization_id} + AND DATE(I.updated) = ${date} + ) = ( + SELECT COUNT(*) + FROM consumable + ) THEN 1 + ELSE 0 + END AS all_consumables_present;`); if (check_inventory_data_for_date == 0){ int|error? check_least_updated_inventory_data_for_date = check db_client->queryRow( `SELECT - CASE - WHEN COUNT(*) > 0 THEN 1 - ELSE 0 - END AS has_rows - FROM ( - 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, - C.name, - C.description, - C.manufacturer - FROM - inventory I - 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 - ) AS subquery;`); + CASE + WHEN ( + SELECT COUNT(DISTINCT I.consumable_id) + FROM inventory I + 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 + ) = ( + SELECT COUNT(*) + FROM consumable + ) THEN 1 + ELSE 0 + END AS all_consumables_present;`); if(check_least_updated_inventory_data_for_date == 0){ + 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; - `); + `SELECT + I.id, + I.avinya_type_id, + C.id AS consumable_id, + I.organization_id, + I.person_id, + COALESCE(I.quantity, 0.0) AS quantity, + COALESCE(I.quantity_in, 0.0) AS quantity_in, + COALESCE(I.quantity_out, 0.0) AS quantity_out, + RP.id AS resource_property_id, + RP.value AS resource_property_value, + C.name, + C.description, + C.manufacturer + FROM + consumable C + LEFT JOIN + (SELECT * FROM inventory WHERE organization_id = ${organization_id}) I + ON C.id = I.consumable_id AND I.updated = ( + SELECT MAX(I2.updated) + FROM inventory I2 + WHERE I2.consumable_id = C.id AND I2.organization_id = ${organization_id} + ) + LEFT JOIN + resource_property RP + ON C.id = RP.consumable_id;`); }else{ - 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, - C.name,C.description,C.manufacturer - FROM inventory I - 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; - `); + 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, + C.name, + C.description, + C.manufacturer + FROM + consumable C + INNER JOIN + ( + SELECT + I1.* + FROM + inventory I1 + INNER JOIN + ( + SELECT + consumable_id, + MAX(updated) AS max_updated_at + FROM + inventory + WHERE + organization_id = ${organization_id} + GROUP BY + consumable_id + ) latest_inventory + ON I1.consumable_id = latest_inventory.consumable_id + AND I1.updated = latest_inventory.max_updated_at + ) I + ON C.id = I.consumable_id + LEFT JOIN + resource_property RP + ON C.id = RP.consumable_id;`); } }else{ + + 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, - C.name, C.description, C.manufacturer - FROM inventory I - 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};`); - } + `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, + C.name, + C.description, + C.manufacturer + FROM + inventory I + 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 latest_update + FROM + inventory + WHERE + organization_id = ${organization_id} + AND DATE(updated) = ${date} + GROUP BY + consumable_id + ) Latest + ON I.consumable_id = Latest.consumable_id + AND I.updated = Latest.latest_update + WHERE + I.organization_id = ${organization_id} + AND DATE(I.updated) = ${date}; + `); + } InventoryData[] inventoryDatas = []; @@ -4839,22 +4905,6 @@ lock { remote function consumable_replenishment(Inventory[] inventories) returns InventoryData[]|error? { - - foreach Inventory inventory_object in inventories { - sql:ExecutionResult res = check db_client->execute( - `DELETE FROM inventory - WHERE organization_id = ${inventory_object.organization_id} - AND consumable_id = ${inventory_object.consumable_id} - AND DATE(updated) = ${inventory_object.updated};` - ); - - int? delete_id = res.affectedRowCount; - if (delete_id < 0) { - return error("Unable to delete with org id : " + inventory_object.organization_id.toString()); - } - - } - InventoryData[] newlyAddedInventoryDatas = []; foreach Inventory inventory in inventories { @@ -4895,44 +4945,42 @@ lock { remote function consumable_depletion(Inventory[] inventories) returns InventoryData[]|error? { - InventoryData[] inventoryDatas = []; + InventoryData[] newlyAddedInventoryDepletionDatas = []; foreach Inventory inventory in inventories { + sql:ExecutionResult response = check db_client->execute( + `INSERT INTO inventory ( + avinya_type_id, + consumable_id, + organization_id, + person_id, + quantity, + quantity_out, + created, + updated + ) VALUES ( + ${inventory.avinya_type_id}, + ${inventory.consumable_id}, + ${inventory.organization_id}, + ${inventory.person_id}, + ${inventory.quantity}, + ${inventory.quantity_out}, + ${inventory.updated}, + ${inventory.updated} + );` + ); - int id = inventory.id ?: 0; - - if (id == 0) { - return error("Unable to update inventory Data"); - } - - Inventory|error? inventoryRaw = db_client->queryRow( - `SELECT * - FROM inventory - WHERE id = ${inventory.id};`); - - if !(inventoryRaw is Inventory) { - return error("Inventory Data does not exist"); + int|string? insert_id = response.lastInsertId; + if !(insert_id is int) { + return error("Unable to insert inventory depletion data"); + } else { + InventoryData|error newlyAddedInventoryDepletionData = new InventoryData(insert_id); + if !(newlyAddedInventoryDepletionData is error) { + newlyAddedInventoryDepletionDatas.push(newlyAddedInventoryDepletionData); + } } - - sql:ExecutionResult|error response = db_client->execute( - `UPDATE inventory SET - quantity_out = ${inventory.quantity_out}, - updated = ${inventory.updated} - WHERE id = ${id};`); - - if (response is sql:ExecutionResult) { - InventoryData|error inventoryData = new InventoryData(id); - - if !(inventoryData is error) { - - inventoryDatas.push(inventoryData); - } - } else{ - return error("Unable to update inventory Data"); - } - } - return inventoryDatas; + return newlyAddedInventoryDepletionDatas; } }