fix flows
This commit is contained in:
parent
7b6d58be33
commit
e26c8b3469
7 changed files with 52 additions and 35 deletions
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…
Add table
Add a link
Reference in a new issue