This commit is contained in:
JMARyA 2024-05-10 11:59:05 +02:00
parent 985296d366
commit 9d5ec6d1b3
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 135 additions and 7 deletions

View file

@ -2,7 +2,7 @@ use mongodb::bson::doc;
use crate::{
cache::InventoryCache,
cdb_col, get_mongo,
cdb_col, collect_results, get_mongo,
transaction::{BatchTransaction, Price, Transaction},
};
@ -14,13 +14,18 @@ use crate::{
/// configurations.
#[derive(Debug, Clone)]
pub struct Variant {
/// Associated Item
pub item: String,
/// Variant Name
pub variant: String,
/// Amount of items this variant represents
pub amount: u64,
/// Dependencies for the item variant.
pub depends: Vec<String>,
}
impl Variant {
/// Create variant from itemdb yaml
pub fn from_yml(json: &serde_yaml::Value, variant: &str, item: &str) -> Self {
Self {
item: item.to_string(),
@ -46,7 +51,55 @@ impl Variant {
}
}
pub async fn demand(&self, uuid: &str, price: Price, destination: String) -> Option<String> {
pub async fn supply_log(&self) -> Vec<String> {
let db = get_mongo!();
let supply = cdb_col!(db, "supply");
let filter = doc! {
"item": &self.item,
"variant": &self.variant
};
let mut db_res = supply.find(filter, None).await.unwrap();
let result: Vec<mongodb::bson::Document> = collect_results!(db_res);
let mut ret = Vec::new();
for doc in result {
ret.push(doc.get("_id").unwrap().as_str().unwrap().to_string());
}
ret
}
pub async fn demand_log(&self) -> Vec<String> {
// todo : add referenced supply
let db = get_mongo!();
let demand = cdb_col!(db, "demand");
let filter = doc! {
"item": &self.item,
"variant": &self.variant
};
let mut db_res = demand.find(filter, None).await.unwrap();
let result: Vec<mongodb::bson::Document> = collect_results!(db_res);
let mut ret = Vec::new();
for doc in result {
ret.push(doc.get("_id").unwrap().as_str().unwrap().to_string());
}
ret
}
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<String> {
let db = get_mongo!();
// check if supply transaction exists
@ -76,6 +129,9 @@ impl Variant {
.await
.unwrap();
// update cache
InventoryCache::remove(uuid).await;
Some(uuid.to_string())
}