fix flows
This commit is contained in:
parent
7b6d58be33
commit
e26c8b3469
7 changed files with 52 additions and 35 deletions
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -225,9 +225,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "bson"
|
||||
version = "2.11.0"
|
||||
version = "2.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8a88e82b9106923b5c4d6edfca9e7db958d4e98a478ec115022e81b9b38e2c8"
|
||||
checksum = "80cf6f7806607bd58ad490bab34bf60e25455ea4aaf995f897a13324d41ea580"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"base64 0.13.1",
|
||||
|
@ -1281,8 +1281,8 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "mongod"
|
||||
version = "0.2.1"
|
||||
source = "git+https://git.hydrar.de/jmarya/mongod#949340a7f28810768e408f4039dc1f1dc9cc1a82"
|
||||
version = "0.2.2"
|
||||
source = "git+https://git.hydrar.de/jmarya/mongod#b0bae64efcfdc833c3b9e8508bf228c9299c1d21"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
|
@ -1298,7 +1298,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "mongod_derive"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.hydrar.de/jmarya/mongod#949340a7f28810768e408f4039dc1f1dc9cc1a82"
|
||||
source = "git+https://git.hydrar.de/jmarya/mongod#b0bae64efcfdc833c3b9e8508bf228c9299c1d21"
|
||||
dependencies = [
|
||||
"case",
|
||||
"proc-macro2",
|
||||
|
@ -1658,9 +1658,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.3"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
|
||||
checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853"
|
||||
dependencies = [
|
||||
"bitflags 2.6.0",
|
||||
]
|
||||
|
@ -1929,9 +1929,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.36"
|
||||
version = "0.38.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36"
|
||||
checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
|
||||
dependencies = [
|
||||
"bitflags 2.6.0",
|
||||
"errno",
|
||||
|
|
51
src/flow.rs
51
src/flow.rs
|
@ -45,6 +45,8 @@ use serde::{Deserialize, Serialize};
|
|||
use serde_json::json;
|
||||
|
||||
use crate::item::Item;
|
||||
use crate::routes::item::{item_does_not_exist_error, variant_does_not_exist_error};
|
||||
use crate::routes::{api_error, ApiError};
|
||||
use crate::transaction::{Price, Transaction};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
|
@ -133,30 +135,39 @@ impl Flow {
|
|||
pub async fn end_with_produce(
|
||||
self,
|
||||
produced: &[HashMap<String, u64>],
|
||||
) -> HashMap<String, Vec<String>> {
|
||||
) -> Result<HashMap<String, Vec<String>>, ApiError> {
|
||||
let mut ret = HashMap::new();
|
||||
let mut t_create = Vec::new();
|
||||
let mut produced_ref = Vec::with_capacity(ret.len());
|
||||
|
||||
for prod in produced {
|
||||
for (item_variant, amount) in prod {
|
||||
let (item, variant) = item_variant.split_once("::").unwrap();
|
||||
for _ in 0..*amount {
|
||||
let t = Item::get(item)
|
||||
.await
|
||||
.unwrap()
|
||||
.variant(variant)
|
||||
.unwrap()
|
||||
.supply(
|
||||
Price::zero(),
|
||||
Some(&format!("flow::{}::{}", self.kind.id(), self._id)),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
ret.entry(item_variant.clone())
|
||||
.or_insert(Vec::new())
|
||||
.push(t._id.clone());
|
||||
produced_ref.push(t.reference());
|
||||
}
|
||||
let (item, variant) = item_variant
|
||||
.split_once("::")
|
||||
.ok_or_else(|| api_error("Bad Request"))?;
|
||||
let t = Item::get(item)
|
||||
.await
|
||||
.ok_or_else(item_does_not_exist_error)?
|
||||
.variant(variant)
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
t_create.push((t, amount));
|
||||
}
|
||||
}
|
||||
|
||||
for (item, amount) in t_create {
|
||||
for _ in 0..*amount {
|
||||
let t = item
|
||||
.supply(
|
||||
Price::zero(),
|
||||
Some(&format!("flow::{}::{}", self.kind.id(), self._id)),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
ret.entry(item.item_variant_id().clone())
|
||||
.or_insert(Vec::new())
|
||||
.push(t._id.clone());
|
||||
produced_ref.push(t.reference());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,7 +181,7 @@ impl Flow {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
ret
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
pub async fn continue_next(self, next_flow: &Flow) -> Self {
|
||||
|
|
|
@ -86,7 +86,10 @@ async fn rocket() -> _ {
|
|||
routes::flow::flows_list,
|
||||
routes::item::expired_items_route,
|
||||
routes::item::min_items_route,
|
||||
routes::item::variant_price_history_by_origin
|
||||
routes::item::variant_price_history_by_origin,
|
||||
routes::flow::end_flow_route,
|
||||
routes::flow::continue_flow_route,
|
||||
routes::flow::create_flow_route
|
||||
],
|
||||
)
|
||||
.manage(itemdb)
|
||||
|
|
|
@ -123,8 +123,7 @@ pub async fn end_flow_route(id: &str, form: Json<EndFlow>) -> FallibleApiRespons
|
|||
.ok_or_else(|| api_error("Flow not found"))?;
|
||||
|
||||
if let Some(produced) = &form.produced {
|
||||
// todo : add transactions
|
||||
let prod = flow.end_with_produce(produced).await;
|
||||
let prod = flow.end_with_produce(produced).await?;
|
||||
Ok(json!({"produced": prod}))
|
||||
} else {
|
||||
flow.end().await;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
mod demand;
|
||||
mod error;
|
||||
pub mod error;
|
||||
mod location;
|
||||
mod stat;
|
||||
mod supply;
|
||||
|
|
|
@ -6,10 +6,10 @@ use serde_json::json;
|
|||
pub mod flow;
|
||||
pub mod item;
|
||||
|
||||
type ApiError = BadRequest<serde_json::Value>;
|
||||
pub type ApiError = BadRequest<serde_json::Value>;
|
||||
type FallibleApiResponse = Result<serde_json::Value, ApiError>;
|
||||
|
||||
fn api_error(msg: &str) -> ApiError {
|
||||
pub fn api_error(msg: &str) -> ApiError {
|
||||
BadRequest(json!({
|
||||
"error": msg
|
||||
}))
|
||||
|
|
|
@ -76,6 +76,10 @@ impl Variant {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn item_variant_id(&self) -> String {
|
||||
format!("{}::{}", self.item, self.variant)
|
||||
}
|
||||
|
||||
/// Returns the IDs of Transactions from this Item Variant.
|
||||
pub async fn supply_log(&self) -> Vec<String> {
|
||||
let filter = doc! {
|
||||
|
|
Loading…
Reference in a new issue