refactor
This commit is contained in:
parent
3b0e8c1866
commit
ca364453a7
10 changed files with 341 additions and 604 deletions
120
src/variant.rs
120
src/variant.rs
|
@ -1,11 +1,9 @@
|
|||
use mongod::Model;
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
cache::InventoryCache,
|
||||
cdb_col, collect_results, get_mongo, id_of,
|
||||
transaction::{BatchTransaction, Price, Transaction},
|
||||
};
|
||||
use crate::transaction::{Consumed, Price, Transaction};
|
||||
|
||||
/// Represents a specific instance of an item with potential variations.
|
||||
///
|
||||
|
@ -13,7 +11,7 @@ use crate::{
|
|||
/// in the real world. It may include attributes or properties that deviate from
|
||||
/// the standard definition of the item. For example, different colors, sizes, or
|
||||
/// configurations.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Variant {
|
||||
/// Associated Item
|
||||
pub item: String,
|
||||
|
@ -52,133 +50,66 @@ impl Variant {
|
|||
}
|
||||
|
||||
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 result = Transaction::find_partial(filter, json!({}), None).await.unwrap();
|
||||
|
||||
let mut ret = Vec::new();
|
||||
|
||||
for doc in result {
|
||||
ret.push(doc.get("_id").unwrap().as_str().unwrap().to_string());
|
||||
ret.push(doc._id);
|
||||
}
|
||||
|
||||
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
|
||||
"variant": &self.variant,
|
||||
"consumed": { "$exists": true }
|
||||
};
|
||||
|
||||
let mut db_res = demand.find(filter, None).await.unwrap();
|
||||
|
||||
let result: Vec<mongodb::bson::Document> = collect_results!(db_res);
|
||||
let result = Transaction::find_partial(filter, json!({}), None).await.unwrap();
|
||||
|
||||
let mut ret = Vec::new();
|
||||
|
||||
for doc in result {
|
||||
ret.push(doc.get("_id").unwrap().as_str().unwrap().to_string());
|
||||
ret.push(doc._id);
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<String> {
|
||||
let db = get_mongo!();
|
||||
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<()> {
|
||||
// check if transaction exists
|
||||
let mut t = Transaction::get(uuid).await?;
|
||||
t.update(&json!({
|
||||
"consumed": Consumed{ destination: destination.to_string(), price }
|
||||
})).await.ok()?;
|
||||
|
||||
// check if supply transaction exists
|
||||
let supply_t = cdb_col!(db, "supply");
|
||||
supply_t.find_one(id_of!(uuid), None).await.unwrap()?;
|
||||
|
||||
// todo : demand batch
|
||||
|
||||
// mark as used
|
||||
let demand_t = cdb_col!(db, "demand");
|
||||
demand_t
|
||||
.insert_one(
|
||||
doc! {
|
||||
"_id": uuid,
|
||||
"destination": destination,
|
||||
"price": Into::<mongodb::bson::Bson>::into(price)
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// update cache
|
||||
InventoryCache::remove(uuid).await;
|
||||
|
||||
Some(uuid.to_string())
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Records a supply transaction in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `amount` - The quantity of items supplied.
|
||||
/// * `price` - The price of the supplied items.
|
||||
/// * `origin` - The origin or source of the supplied items.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns a UUID string representing the transaction.
|
||||
pub async fn supply(&self, amount: usize, price: Price, origin: &str) -> String {
|
||||
let db = get_mongo!();
|
||||
pub async fn supply(&self, price: Price, origin: Option<&str>) -> String {
|
||||
let t = Transaction::new(&self.item, &self.variant, price, origin);
|
||||
|
||||
let col: mongodb::Collection<mongodb::bson::Document> =
|
||||
db.database("cdb").collection("supply");
|
||||
t.insert().await.unwrap();
|
||||
|
||||
let mut transactions = vec![];
|
||||
for _ in 0..amount {
|
||||
transactions.push(Transaction::new(
|
||||
&self.item,
|
||||
&self.variant,
|
||||
price.clone(),
|
||||
origin,
|
||||
));
|
||||
}
|
||||
|
||||
for transaction in &transactions {
|
||||
col.insert_one(transaction.as_doc(), None).await.unwrap();
|
||||
|
||||
// update cache
|
||||
InventoryCache::push(&transaction.uuid).await;
|
||||
}
|
||||
|
||||
// batch transaction
|
||||
let ret_uuid = if amount == 1 {
|
||||
transactions.first().unwrap().uuid.clone()
|
||||
} else {
|
||||
let batch =
|
||||
BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect());
|
||||
let col: mongodb::Collection<mongodb::bson::Document> =
|
||||
db.database("cdb").collection("transactions_batch");
|
||||
let batch_uuid = batch.uuid.clone();
|
||||
|
||||
col.insert_one(Into::<mongodb::bson::Document>::into(batch), None)
|
||||
.await
|
||||
.unwrap();
|
||||
batch_uuid
|
||||
};
|
||||
|
||||
ret_uuid
|
||||
t._id
|
||||
}
|
||||
|
||||
pub fn api_json(&self) -> serde_json::Value {
|
||||
|
@ -189,13 +120,4 @@ impl Variant {
|
|||
"depends": self.depends
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_bson(&self) -> mongodb::bson::Document {
|
||||
mongodb::bson::doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant,
|
||||
"amount": self.amount as u32,
|
||||
"depends": &self.depends
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue