fix produce flows

This commit is contained in:
JMARyA 2024-09-25 22:37:48 +02:00
parent 860508fa29
commit e3128d7214
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 21 additions and 28 deletions

View file

@ -47,7 +47,7 @@ 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::item::{item_does_not_exist_error, variant_does_not_exist_error, SupplyForm};
use crate::routes::{api_error, ApiError}; use crate::routes::{api_error, ApiError};
use crate::transaction::{Price, Transaction}; use crate::transaction::{Price, Transaction};
@ -156,42 +156,35 @@ impl Flow {
pub async fn end_with_produce( pub async fn end_with_produce(
self, self,
produced: &[HashMap<String, u64>], produced: &[SupplyForm],
) -> Result<HashMap<String, Vec<String>>, ApiError> { ) -> Result<HashMap<String, Vec<String>>, ApiError> {
let mut ret = HashMap::new(); let mut ret = HashMap::new();
let mut t_create = Vec::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 { let t = Item::get(&prod.item)
let (item, variant) = item_variant .await
.split_once("::") .ok_or_else(item_does_not_exist_error)?
.ok_or_else(|| api_error("Bad Request"))?; .variant(&prod.variant)
let t = Item::get(item) .ok_or_else(variant_does_not_exist_error)?;
.await
.ok_or_else(item_does_not_exist_error)?
.variant(variant)
.ok_or_else(variant_does_not_exist_error)?;
t_create.push((t, amount)); t_create.push((t, prod));
} }
}
for (item, amount) in t_create { for (item, info) in t_create {
for _ in 0..*amount {
let t = item 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, info.location.as_ref().map(|x| x.as_str()),
None, info.note.as_ref().map(|x| x.as_str()),
) )
.await; .await;
ret.entry(item.item_variant_id().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()

View file

@ -15,7 +15,7 @@ use crate::{
transaction::{Price, Transaction}, transaction::{Price, Transaction},
}; };
use super::ApiError; use super::{item::SupplyForm, ApiError};
#[get("/flow/<id>/info")] #[get("/flow/<id>/info")]
pub async fn flow_info( pub async fn flow_info(
@ -129,7 +129,7 @@ pub struct CreateFlow {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndFlow { pub struct EndFlow {
pub produced: Option<Vec<HashMap<String, u64>>>, pub produced: Option<Vec<SupplyForm>>,
} }
#[post("/flow/<id>", data = "<form>")] #[post("/flow/<id>", data = "<form>")]

View file

@ -1,7 +1,7 @@
use mongod::ToAPI; use mongod::ToAPI;
use rocket::serde::json::Json; use rocket::serde::json::Json;
use rocket::{get, post, State}; use rocket::{get, post, State};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use crate::check_auth; use crate::check_auth;
@ -14,14 +14,14 @@ use crate::{
use super::{item_does_not_exist_error, variant_does_not_exist_error}; use super::{item_does_not_exist_error, variant_does_not_exist_error};
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug, Clone, Serialize)]
pub struct SupplyForm { pub struct SupplyForm {
item: String, pub item: String,
variant: String, pub variant: String,
price: String, pub price: String,
origin: Option<String>, pub origin: Option<String>,
location: Option<String>, pub location: Option<String>,
note: Option<String>, pub note: Option<String>,
} }
/// Route for supply action. Creates a new Transaction for the specified Item Variant. /// Route for supply action. Creates a new Transaction for the specified Item Variant.