diff --git a/.sqlx/query-1d60e3018eabd870c8a76b9b43ab3472b9e2b398d67f3a7478eba83be439807d.json b/.sqlx/query-1d60e3018eabd870c8a76b9b43ab3472b9e2b398d67f3a7478eba83be439807d.json deleted file mode 100644 index 6c7825d..0000000 --- a/.sqlx/query-1d60e3018eabd870c8a76b9b43ab3472b9e2b398d67f3a7478eba83be439807d.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT\n COUNT(*) as quantity\n FROM items\n WHERE\n items.status = $1 AND\n items.piece_kind = $2 AND\n items.order_id IS NULL\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "quantity", - "type_info": "Int8" - } - ], - "parameters": { - "Left": [ - { - "Custom": { - "name": "item_status", - "kind": { - "Enum": [ - "pending", - "in_transit", - "in_stock", - "delivered", - "consumed" - ] - } - } - }, - { - "Custom": { - "name": "piece_kind", - "kind": { - "Enum": [ - "P1", - "P2", - "P3", - "P4", - "P5", - "P6", - "P7", - "P8", - "P9" - ] - } - } - } - ] - }, - "nullable": [ - null - ] - }, - "hash": "1d60e3018eabd870c8a76b9b43ab3472b9e2b398d67f3a7478eba83be439807d" -} diff --git a/.sqlx/query-2de978768ada5a8faf344845a628ee65adc43f25d0d8c37dc4345e1b2325f629.json b/.sqlx/query-2de978768ada5a8faf344845a628ee65adc43f25d0d8c37dc4345e1b2325f629.json deleted file mode 100644 index de78ed8..0000000 --- a/.sqlx/query-2de978768ada5a8faf344845a628ee65adc43f25d0d8c37dc4345e1b2325f629.json +++ /dev/null @@ -1,63 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT\n id,\n raw_material_kind as \"raw_material_kind: RawMaterial\",\n min_order_quantity,\n unit_price,\n delivery_time\n FROM suppliers\n WHERE id = $1\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int8" - }, - { - "ordinal": 1, - "name": "raw_material_kind: RawMaterial", - "type_info": { - "Custom": { - "name": "piece_kind", - "kind": { - "Enum": [ - "P1", - "P2", - "P3", - "P4", - "P5", - "P6", - "P7", - "P8", - "P9" - ] - } - } - } - }, - { - "ordinal": 2, - "name": "min_order_quantity", - "type_info": "Int4" - }, - { - "ordinal": 3, - "name": "unit_price", - "type_info": "Money" - }, - { - "ordinal": 4, - "name": "delivery_time", - "type_info": "Int4" - } - ], - "parameters": { - "Left": [ - "Int8" - ] - }, - "nullable": [ - false, - false, - false, - false, - false - ] - }, - "hash": "2de978768ada5a8faf344845a628ee65adc43f25d0d8c37dc4345e1b2325f629" -} diff --git a/.sqlx/query-3da0897145af0f747232930e2d0b18bb555ee823fd040af81e65160b7cabc84a.json b/.sqlx/query-3da0897145af0f747232930e2d0b18bb555ee823fd040af81e65160b7cabc84a.json new file mode 100644 index 0000000..e1e4b0d --- /dev/null +++ b/.sqlx/query-3da0897145af0f747232930e2d0b18bb555ee823fd040af81e65160b7cabc84a.json @@ -0,0 +1,20 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT simulation_date FROM epoch_table", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "simulation_date", + "type_info": "Int4" + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false + ] + }, + "hash": "3da0897145af0f747232930e2d0b18bb555ee823fd040af81e65160b7cabc84a" +} diff --git a/.sqlx/query-070290a2c348195bf3a04582b93301dbe7cc4dfb980be968022048974e50894e.json b/.sqlx/query-46451228d67da5e779aed034ed89a8e2095a57f381f31fd02cc9a887b7dfc676.json similarity index 68% rename from .sqlx/query-070290a2c348195bf3a04582b93301dbe7cc4dfb980be968022048974e50894e.json rename to .sqlx/query-46451228d67da5e779aed034ed89a8e2095a57f381f31fd02cc9a887b7dfc676.json index 0c6a8df..4e86b79 100644 --- a/.sqlx/query-070290a2c348195bf3a04582b93301dbe7cc4dfb980be968022048974e50894e.json +++ b/.sqlx/query-46451228d67da5e779aed034ed89a8e2095a57f381f31fd02cc9a887b7dfc676.json @@ -1,13 +1,12 @@ { "db_name": "PostgreSQL", - "query": "UPDATE items\n SET\n order_id = $1,\n warehouse = $2,\n production_line = $3,\n status = $4,\n acc_cost = $5\n WHERE id = $6", + "query": "UPDATE items\n SET\n order_id = $1,\n warehouse = $2,\n status = $3,\n acc_cost = $4\n WHERE id = $5", "describe": { "columns": [], "parameters": { "Left": [ "Uuid", "Bpchar", - "Bpchar", { "Custom": { "name": "item_status", @@ -28,5 +27,5 @@ }, "nullable": [] }, - "hash": "070290a2c348195bf3a04582b93301dbe7cc4dfb980be968022048974e50894e" + "hash": "46451228d67da5e779aed034ed89a8e2095a57f381f31fd02cc9a887b7dfc676" } diff --git a/.sqlx/query-b4b786efab4ecdb6c23db4cd1178ad8685b602f60ffbe6a2c59fc1233f4462e6.json b/.sqlx/query-61ebbc64fdc1972f2f68acc91856d6d5d3a78cbf7e0956b84c03caa087574201.json similarity index 81% rename from .sqlx/query-b4b786efab4ecdb6c23db4cd1178ad8685b602f60ffbe6a2c59fc1233f4462e6.json rename to .sqlx/query-61ebbc64fdc1972f2f68acc91856d6d5d3a78cbf7e0956b84c03caa087574201.json index 618cea6..ffca0e2 100644 --- a/.sqlx/query-b4b786efab4ecdb6c23db4cd1178ad8685b602f60ffbe6a2c59fc1233f4462e6.json +++ b/.sqlx/query-61ebbc64fdc1972f2f68acc91856d6d5d3a78cbf7e0956b84c03caa087574201.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT\n id,\n piece_kind as \"piece_kind: PieceKind\",\n order_id,\n warehouse,\n production_line,\n status as \"status: ItemStatus\",\n acc_cost\n FROM items WHERE id = $1", + "query": "SELECT\n id,\n piece_kind as \"piece_kind: PieceKind\",\n order_id,\n warehouse,\n status as \"status: ItemStatus\",\n acc_cost\n FROM items WHERE id = $1", "describe": { "columns": [ { @@ -42,11 +42,6 @@ }, { "ordinal": 4, - "name": "production_line", - "type_info": "Bpchar" - }, - { - "ordinal": 5, "name": "status: ItemStatus", "type_info": { "Custom": { @@ -64,7 +59,7 @@ } }, { - "ordinal": 6, + "ordinal": 5, "name": "acc_cost", "type_info": "Money" } @@ -79,10 +74,9 @@ false, true, true, - true, false, false ] }, - "hash": "b4b786efab4ecdb6c23db4cd1178ad8685b602f60ffbe6a2c59fc1233f4462e6" + "hash": "61ebbc64fdc1972f2f68acc91856d6d5d3a78cbf7e0956b84c03caa087574201" } diff --git a/.sqlx/query-3772df638548e40b36e639c895304250a72beb399757f46e4cbf33e6d279bc33.json b/.sqlx/query-63b3734a2a7c37c0100e9596b78d8272c6fecbcc61611974869f39c626535e14.json similarity index 82% rename from .sqlx/query-3772df638548e40b36e639c895304250a72beb399757f46e4cbf33e6d279bc33.json rename to .sqlx/query-63b3734a2a7c37c0100e9596b78d8272c6fecbcc61611974869f39c626535e14.json index f7c9800..ecfd9d1 100644 --- a/.sqlx/query-3772df638548e40b36e639c895304250a72beb399757f46e4cbf33e6d279bc33.json +++ b/.sqlx/query-63b3734a2a7c37c0100e9596b78d8272c6fecbcc61611974869f39c626535e14.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "INSERT INTO\n items (id, piece_kind, order_id, warehouse, production_line, status, acc_cost)\n VALUES ($1, $2, $3, $4, $5, $6, $7)", + "query": "INSERT INTO\n items (id, piece_kind, order_id, warehouse, status, acc_cost)\n VALUES ($1, $2, $3, $4, $5, $6)", "describe": { "columns": [], "parameters": { @@ -26,7 +26,6 @@ }, "Uuid", "Bpchar", - "Bpchar", { "Custom": { "name": "item_status", @@ -46,5 +45,5 @@ }, "nullable": [] }, - "hash": "3772df638548e40b36e639c895304250a72beb399757f46e4cbf33e6d279bc33" + "hash": "63b3734a2a7c37c0100e9596b78d8272c6fecbcc61611974869f39c626535e14" } diff --git a/.sqlx/query-6b45e37be7df715fbf077f260bcc91b703e368e634a3645501fe9e89c8ba839c.json b/.sqlx/query-6b45e37be7df715fbf077f260bcc91b703e368e634a3645501fe9e89c8ba839c.json deleted file mode 100644 index fadc6d4..0000000 --- a/.sqlx/query-6b45e37be7df715fbf077f260bcc91b703e368e634a3645501fe9e89c8ba839c.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT id FROM orders WHERE due_date = $1", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Uuid" - } - ], - "parameters": { - "Left": [ - "Int4" - ] - }, - "nullable": [ - false - ] - }, - "hash": "6b45e37be7df715fbf077f260bcc91b703e368e634a3645501fe9e89c8ba839c" -} diff --git a/.sqlx/query-eb0827f18105e24c0ad7e117708b0093f52fc6979a2809290eb077afc3fee077.json b/.sqlx/query-b8690bd4b6224ad7c33e74b82e3f8b11f928390c79ac00018bc016fc7766c26f.json similarity index 55% rename from .sqlx/query-eb0827f18105e24c0ad7e117708b0093f52fc6979a2809290eb077afc3fee077.json rename to .sqlx/query-b8690bd4b6224ad7c33e74b82e3f8b11f928390c79ac00018bc016fc7766c26f.json index 1f8a0e5..7c0f1cd 100644 --- a/.sqlx/query-eb0827f18105e24c0ad7e117708b0093f52fc6979a2809290eb077afc3fee077.json +++ b/.sqlx/query-b8690bd4b6224ad7c33e74b82e3f8b11f928390c79ac00018bc016fc7766c26f.json @@ -1,15 +1,17 @@ { "db_name": "PostgreSQL", - "query": "UPDATE transformations\n SET status = 'completed', date = $1\n WHERE id = $2", + "query": "UPDATE transformations\n SET status = 'completed', date = $1, line = $2, machine = $3\n WHERE id = $4", "describe": { "columns": [], "parameters": { "Left": [ "Int4", + "Bpchar", + "Bpchar", "Int8" ] }, "nullable": [] }, - "hash": "eb0827f18105e24c0ad7e117708b0093f52fc6979a2809290eb077afc3fee077" + "hash": "b8690bd4b6224ad7c33e74b82e3f8b11f928390c79ac00018bc016fc7766c26f" } diff --git a/.sqlx/query-bfb0d1eaa38621ede9c6353b840d850bcf8d39eabeb3db62f1e38ea6909d162c.json b/.sqlx/query-bfb0d1eaa38621ede9c6353b840d850bcf8d39eabeb3db62f1e38ea6909d162c.json deleted file mode 100644 index 8c58000..0000000 --- a/.sqlx/query-bfb0d1eaa38621ede9c6353b840d850bcf8d39eabeb3db62f1e38ea6909d162c.json +++ /dev/null @@ -1,81 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT\n id,\n raw_material_kind as \"raw_material_kind: RawMaterial\",\n min_order_quantity,\n unit_price,\n delivery_time\n FROM suppliers\n WHERE raw_material_kind = $1 AND delivery_time <= $2\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int8" - }, - { - "ordinal": 1, - "name": "raw_material_kind: RawMaterial", - "type_info": { - "Custom": { - "name": "piece_kind", - "kind": { - "Enum": [ - "P1", - "P2", - "P3", - "P4", - "P5", - "P6", - "P7", - "P8", - "P9" - ] - } - } - } - }, - { - "ordinal": 2, - "name": "min_order_quantity", - "type_info": "Int4" - }, - { - "ordinal": 3, - "name": "unit_price", - "type_info": "Money" - }, - { - "ordinal": 4, - "name": "delivery_time", - "type_info": "Int4" - } - ], - "parameters": { - "Left": [ - { - "Custom": { - "name": "piece_kind", - "kind": { - "Enum": [ - "P1", - "P2", - "P3", - "P4", - "P5", - "P6", - "P7", - "P8", - "P9" - ] - } - } - }, - "Int4" - ] - }, - "nullable": [ - false, - false, - false, - false, - false - ] - }, - "hash": "bfb0d1eaa38621ede9c6353b840d850bcf8d39eabeb3db62f1e38ea6909d162c" -} diff --git a/.sqlx/query-c291cf0ff866509962e6ccb41b835f17f9c482292d1688d5c7e8779280300c45.json b/.sqlx/query-c291cf0ff866509962e6ccb41b835f17f9c482292d1688d5c7e8779280300c45.json new file mode 100644 index 0000000..0e5c6ad --- /dev/null +++ b/.sqlx/query-c291cf0ff866509962e6ccb41b835f17f9c482292d1688d5c7e8779280300c45.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE epoch_table SET simulation_date = $1", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [] + }, + "hash": "c291cf0ff866509962e6ccb41b835f17f9c482292d1688d5c7e8779280300c45" +} diff --git a/migrations/005_items.sql b/migrations/005_items.sql index e01eba6..4c084d1 100644 --- a/migrations/005_items.sql +++ b/migrations/005_items.sql @@ -11,7 +11,6 @@ CREATE TABLE IF NOT EXISTS items ( piece_kind piece_kind NOT NULL REFERENCES pieces(code), order_id uuid REFERENCES orders(id), warehouse char(2) REFERENCES warehouses(code), - production_line char(2) REFERENCES production_lines(code), status item_status NOT NULL DEFAULT 'pending', acc_cost money NOT NULL DEFAULT 0, diff --git a/migrations/010_transformations.sql b/migrations/010_transformations.sql index 1ce49bd..4240458 100644 --- a/migrations/010_transformations.sql +++ b/migrations/010_transformations.sql @@ -6,5 +6,9 @@ CREATE TABLE IF NOT EXISTS transformations( product_id uuid NOT NULL REFERENCES items(id), recipe_id bigint NOT NULL REFERENCES recipes(id), status transformation_status NOT NULL DEFAULT 'pending', - date int + + -- Metadata for vistualization purposes + date int, + line char(2) REFERENCES production_lines(code), + machine char(2) REFERENCES machines(code) ); diff --git a/migrations/015_metada.sql b/migrations/015_metada.sql index f6a215c..9d6d87c 100644 --- a/migrations/015_metada.sql +++ b/migrations/015_metada.sql @@ -5,3 +5,5 @@ simulation_date INT CHECK(simulation_date >= 0) PRIMARY KEY ); + +INSERT INTO epoch_table(simulation_date) VALUES(1); diff --git a/src/db_api/items.rs b/src/db_api/items.rs index 3338cb6..5e8029e 100644 --- a/src/db_api/items.rs +++ b/src/db_api/items.rs @@ -31,7 +31,6 @@ pub struct Item { piece_kind: PieceKind, order_id: Option, warehouse: Option, - production_line: Option, status: ItemStatus, acc_cost: PgMoney, } @@ -43,7 +42,6 @@ impl Item { piece_kind, order_id: None, warehouse: None, - production_line: None, status: ItemStatus::Pending, acc_cost: PgMoney(0), } @@ -54,11 +52,7 @@ impl Item { self } - pub fn produce( - mut self, - cost: PgMoney, - production_line: impl ToString, - ) -> anyhow::Result { + pub fn produce(mut self, cost: PgMoney) -> anyhow::Result { if self.status != ItemStatus::Pending { anyhow::bail!(format!( "Item {} is {}, cannot produce", @@ -67,15 +61,11 @@ impl Item { } self.status = ItemStatus::InTransit; - self.production_line = Some(production_line.to_string()); self.acc_cost = cost; Ok(self) } - pub fn consume( - mut self, - production_line: impl ToString, - ) -> anyhow::Result { + pub fn consume(mut self) -> anyhow::Result { if self.status != ItemStatus::InTransit { anyhow::bail!(format!( "Item {} is {}, cannot consume", @@ -85,7 +75,6 @@ impl Item { self.status = ItemStatus::Consumed; self.warehouse = None; - self.production_line = Some(production_line.to_string()); Ok(self) } @@ -102,14 +91,10 @@ impl Item { self.status = ItemStatus::InStock; self.warehouse = Some(warehouse.to_string()); - self.production_line = None; Ok(self) } - pub fn exit_warehouse( - mut self, - production_line: impl ToString, - ) -> anyhow::Result { + pub fn exit_warehouse(mut self) -> anyhow::Result { if self.status != ItemStatus::InStock { anyhow::bail!(format!( "Item {} is {}, cannot exit warehouse", @@ -119,7 +104,6 @@ impl Item { self.status = ItemStatus::InTransit; self.warehouse = None; - self.production_line = Some(production_line.to_string()); Ok(self) } @@ -133,13 +117,12 @@ impl Item { ) -> sqlx::Result { sqlx::query!( "INSERT INTO - items (id, piece_kind, order_id, warehouse, production_line, status, acc_cost) - VALUES ($1, $2, $3, $4, $5, $6, $7)", + items (id, piece_kind, order_id, warehouse, status, acc_cost) + VALUES ($1, $2, $3, $4, $5, $6)", self.id, self.piece_kind as PieceKind, self.order_id, self.warehouse, - self.production_line, self.status as ItemStatus, self.acc_cost ) @@ -158,7 +141,6 @@ impl Item { piece_kind as "piece_kind: PieceKind", order_id, warehouse, - production_line, status as "status: ItemStatus", acc_cost FROM items WHERE id = $1"#, @@ -201,13 +183,11 @@ impl Item { SET order_id = $1, warehouse = $2, - production_line = $3, - status = $4, - acc_cost = $5 - WHERE id = $6"#, + status = $3, + acc_cost = $4 + WHERE id = $5"#, self.order_id, self.warehouse, - self.production_line, self.status as ItemStatus, self.acc_cost, self.id diff --git a/src/db_api/mod.rs b/src/db_api/mod.rs index bff1ebd..a6aa922 100644 --- a/src/db_api/mod.rs +++ b/src/db_api/mod.rs @@ -22,7 +22,7 @@ use sqlx::PgConnection; pub async fn get_date(con: &mut PgConnection) -> sqlx::Result { Ok( - sqlx::query_scalar!("SELECT simulation_date FROM epoch_table LIMIT 1") + sqlx::query_scalar!("SELECT simulation_date FROM epoch_table") .fetch_one(con) .await? as u32, ) diff --git a/src/db_api/transformations.rs b/src/db_api/transformations.rs index 2adbfb0..3908c14 100644 --- a/src/db_api/transformations.rs +++ b/src/db_api/transformations.rs @@ -39,7 +39,7 @@ impl Transformation { self.material_id, self.product_id, self.recipe_id, - self.date + self.date, ) .fetch_one(con) .await? @@ -52,13 +52,17 @@ impl Transformation { pub async fn complete( &self, completion_date: u32, + line: &str, + machine: &str, con: &mut PgConnection, ) -> sqlx::Result<()> { sqlx::query!( r#"UPDATE transformations - SET status = 'completed', date = $1 - WHERE id = $2"#, + SET status = 'completed', date = $1, line = $2, machine = $3 + WHERE id = $4"#, completion_date as i32, + line, + machine, self.id ) .execute(con) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 6462152..76b80fe 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -213,6 +213,7 @@ struct TransfCompletionFrom { material_id: Uuid, product_id: Uuid, line_id: String, + machine_id: String, time_taken: i64, } @@ -247,9 +248,10 @@ pub async fn post_transformation_completion( (Err(e), _) | (_, Err(e)) => return internal_server_error(e), }; + // form.line_id.clone() let new_cost = material.get_cost() + PgMoney(form.time_taken * 100); - let p_action_result = product.produce(new_cost, form.line_id.clone()); - let m_action_result = material.consume(form.line_id.clone()); + let p_action_result = product.produce(new_cost); + let m_action_result = material.consume(); let (product, material) = match (p_action_result, m_action_result) { (Ok(p), Ok(m)) => (p, m), (Err(e), _) | (_, Err(e)) => return bad_request(e), @@ -260,7 +262,9 @@ pub async fn post_transformation_completion( Err(e) => return internal_server_error(e), }; - let tf_result = transf.complete(current_date, &mut tx).await; + let tf_result = transf + .complete(current_date, &form.line_id, &form.machine_id, &mut tx) + .await; let m_result = material.update(&mut tx).await; let p_result = product.update(&mut tx).await; let tx_result = match (m_result, p_result, tf_result) { @@ -282,7 +286,7 @@ pub async fn post_transformation_completion( #[serde(rename_all = "lowercase")] enum WarehouseAction { Entry(String), - Exit(String), + Exit, } #[derive(Debug, Deserialize)] @@ -312,9 +316,7 @@ pub async fn post_warehouse_action( WarehouseAction::Entry(warehouse_code) => { item.enter_warehouse(warehouse_code) } - WarehouseAction::Exit(production_line_code) => { - item.exit_warehouse(production_line_code) - } + WarehouseAction::Exit => item.exit_warehouse(), }; let item = match item_action_result {