Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
replaced GetDailyTransformation logic by GetProduction
Browse files Browse the repository at this point in the history
  • Loading branch information
dvalnn committed Apr 14, 2024
1 parent bab4408 commit c847e3b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 6 deletions.
54 changes: 54 additions & 0 deletions src/db_api/transformations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ impl Transformation {
Ok(())
}

pub async fn get_n_next_raw_mat_transf(
n: i64,
con: &mut PgConnection,
) -> sqlx::Result<Vec<Uuid>> {
Ok(sqlx::query!(
r#"
SELECT t.material_id FROM transformations AS t
JOIN items AS i ON t.material_id = i.id
WHERE
(i.piece_kind = 'P1' OR i.piece_kind = 'P2')
AND i.status = 'in_stock'
AND t.status = 'pending'
ORDER BY date
LIMIT $1;
"#,
n
)
.fetch_all(con)
.await?
.iter()
.map(|row| row.material_id)
.collect())
}

pub async fn get_by_id(
id: i64,
con: &mut PgConnection,
Expand Down Expand Up @@ -146,4 +170,34 @@ impl TransformationDetails {
.fetch_all(con)
.await
}

pub async fn get_by_id(
id: Uuid,
con: &mut PgConnection,
) -> sqlx::Result<Option<Self>> {
sqlx::query_as!(
TransformationDetails,
r#"
SELECT
t.id as transformation_id,
t.material_id,
t.product_id,
recipes.material_kind as "material_kind: PieceKind",
recipes.product_kind as "product_kind: PieceKind",
recipes.tool as "tool: ToolType",
recipes.operation_time
FROM transformations AS t
JOIN recipes ON t.recipe_id = recipes.id
WHERE t.material_id = $1
"#,
id
)
.fetch_optional(con)
.await
}
}
86 changes: 81 additions & 5 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,85 @@ pub async fn post_date(form: Form<DayForm>) -> impl Responder {
match CURRENT_DATE.write() {
Ok(mut date) => {
*date = form.day;
tracing::info!("Date set to {}", form.day);
HttpResponse::Created().finish()
}
Err(e) => internal_server_error(e),
Err(e) => panic!("Date lock was poisoned: {:?}", e),
}
}

#[derive(Debug, Deserialize)]
#[cfg_attr(test, derive(serde::Serialize))]
struct ProductionForm {
max_n_items: u32,
}

#[derive(Debug, Serialize)]
struct Recipe {
steps: Vec<TransformationDetails>,
}

#[get("/production")]
pub async fn get_production(
query: Query<ProductionForm>,
pool: Data<PgPool>,
) -> impl Responder {
let mut tx = match pool.begin().await {
Ok(tx) => tx,
Err(e) => return internal_server_error(e),
};

let n_items = query.max_n_items as i64;
let ids = match Transformation::get_n_next_raw_mat_transf(n_items, &mut tx)
.await
{
Ok(ids) => ids,
Err(e) => return internal_server_error(e),
};

let mut recipes = Vec::new();
for material_id in ids {
let mut steps = Vec::new();
let mut material = material_id;
while let Some(transf) =
match TransformationDetails::get_by_id(material, &mut tx).await {
Ok(t) => t,
Err(e) => return internal_server_error(e),
}
{
material = transf.product_id;
steps.push(transf);
}
recipes.push(Recipe { steps })
}

if recipes.iter().any(|r| r.steps.is_empty()) {
tracing::error!("Some transformations are missing");
return HttpResponse::NotFound().finish();
}

for recipe in &recipes {
//NOTE: all items in a recipe relate to the same order
let transf = &recipe.steps[0];
let order =
match Order::get_by_item_id(transf.product_id, &mut tx).await {
Ok(Some(order)) => order,
Ok(None) => continue,
Err(e) => return internal_server_error(e),
};
if let Err(e) = order.production_start(&mut tx).await {
return internal_server_error(e);
}
tracing::info!("Started production for order {}", order.id());
}

if let Err(e) = tx.commit().await {
return internal_server_error(e);
}

HttpResponse::Ok().json(recipes)
}

#[get("/transformations")]
pub async fn get_daily_transformations(
query: Query<DayForm>,
Expand Down Expand Up @@ -250,12 +323,12 @@ pub async fn post_warehouse_action(

#[derive(Debug, Deserialize, Serialize)]
struct ExpectedShipmentForm {
shipment_id: i64,
shippment_id: i64,
material_type: RawMaterial,
quantity: i32,
}

#[post("/materials/expected")]
#[get("/materials/expected")]
pub async fn get_expected_shippments(
query: Query<DayForm>,
pool: Data<PgPool>,
Expand All @@ -271,7 +344,7 @@ pub async fn get_expected_shippments(
Ok(shipp_vec) => shipp_vec
.iter()
.map(|s| ExpectedShipmentForm {
shipment_id: s.id,
shippment_id: s.id,
material_type: s.material_type,
quantity: s.quantity,
})
Expand All @@ -296,8 +369,11 @@ pub async fn post_material_arrival(
let date = Scheduler::get_date() as i32;

match Shippment::arrived(form.shippment_id, date, &pool).await {
Ok(_) => HttpResponse::Created().finish(),
Err(e) => internal_server_error(e),
Ok(_) => {
tracing::info!("Shippment {} arrived", form.shippment_id);
HttpResponse::Created().finish()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl App {
.service(routes::check_health)
.service(routes::get_date)
.service(routes::post_date)
.service(routes::get_daily_transformations)
.service(routes::get_production)
.service(routes::post_transformation_completion)
.service(routes::post_warehouse_action)
.service(routes::get_expected_shippments)
Expand Down

0 comments on commit c847e3b

Please sign in to comment.