Skip to content

Commit

Permalink
Merge pull request #133 from lahirulakruwan/main
Browse files Browse the repository at this point in the history
Asset get inventory data api gdapi changes added
  • Loading branch information
YujithIsura authored May 28, 2024
2 parents e48302b + 06d953c commit c9de6cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
19 changes: 19 additions & 0 deletions api/inventory_data.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 25 additions & 41 deletions api/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4719,59 +4719,53 @@ lock {
}

isolated resource function get inventory_data_by_organization(int? organization_id,string? date= null) returns InventoryData[]|error? {

stream<Inventory, error?> 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;
`);
}
}

}

Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c9de6cf

Please sign in to comment.