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 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::transaction::{Price, Transaction};
@ -156,42 +156,35 @@ impl Flow {
pub async fn end_with_produce(
self,
produced: &[HashMap<String, u64>],
produced: &[SupplyForm],
) -> 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("::")
.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)?;
let t = Item::get(&prod.item)
.await
.ok_or_else(item_does_not_exist_error)?
.variant(&prod.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 _ in 0..*amount {
for (item, info) in t_create {
let t = item
.supply(
Price::zero(),
Some(&format!("flow::{}::{}", self.kind.id(), self._id)),
None,
None,
info.location.as_ref().map(|x| x.as_str()),
info.note.as_ref().map(|x| x.as_str()),
)
.await;
ret.entry(item.item_variant_id().clone())
.or_insert(Vec::new())
.push(t._id.clone());
produced_ref.push(t.reference());
}
}
self.change()

View file

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

View file

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