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