Skip to content

Commit

Permalink
manifest draft
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Jan 12, 2025
1 parent 96371e5 commit a7ceb33
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 7 deletions.
23 changes: 23 additions & 0 deletions bruno/Change Order.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
meta {
name: Change Order
type: http
seq: 7
}

post {
url: http://127.0.0.1/api/manifest/change/order
body: json
auth: none
}

body:json {
{
"id": 2,
"name": "Chicken Fingers",
"count": 44,
"unit_cost": 2.43,
"store_in": "MEB 2350",
"team": "Software",
"reason": "Machine learning... maybe"
}
}
17 changes: 17 additions & 0 deletions bruno/Delete Order.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
meta {
name: Delete Order
type: http
seq: 8
}

delete {
url: http://127.0.0.1/api/manifest/del/order
body: json
auth: none
}

body:json {
{
"id": 3
}
}
2 changes: 1 addition & 1 deletion bruno/New Order.bru
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ post {

body:json {
{
"name": "RTX 5090",
"name": "Chicken",
"count": 40,
"unit_cost": 1000.43,
"store_in": "MEB 2350",
Expand Down
18 changes: 18 additions & 0 deletions bruno/Update Order.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
meta {
name: Update Order
type: http
seq: 9
}

post {
url: http://127.0.0.1/api/manifest/update/order
body: json
auth: none
}

body:json {
{
"id": 1,
"status": "InStorage"
}
}
193 changes: 187 additions & 6 deletions usr-backend/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ use std::
use axum::{
extract::State,
http::StatusCode,
routing::post,
routing::{delete, post},
Json, Router,
};
use discord_webhook2::message::Message;
use sea_orm::{
prelude::Decimal,
sea_query::Table,
sqlx::types::chrono::Local,
ActiveModelTrait, ActiveValue, ConnectionTrait, DatabaseConnection, Schema,
prelude::Decimal, sea_query::Table, sqlx::types::chrono::Local, ActiveModelTrait, ActiveValue, ConnectionTrait, DatabaseConnection, EntityTrait, Schema
};
use serde::Deserialize;
use tracing::error;
Expand Down Expand Up @@ -74,8 +71,192 @@ async fn new_order(
}
}

#[derive(Deserialize)]
pub struct ChangeOrder {
pub id: u32,
pub name: String,
pub count: u32,
pub unit_cost: Decimal,
pub store_in: Option<String>,
pub team: scheduler::Team,
pub reason: String
}

#[axum::debug_handler]
async fn change_order(
State(state): State<Arc<UsrState>>,
Json(change_order): Json<ChangeOrder>,
) -> (StatusCode, &'static str) {
match order::Entity::find_by_id(change_order.id).one(&state.db).await {
Ok(Some(model)) => {
if model.status != order::Status::New {
return (StatusCode::BAD_REQUEST, "Order has already been processed");
}
}
Ok(None) => {
return (StatusCode::BAD_REQUEST, "Order not found");
}
Err(e) => {
error!("Failed to find order: {e}");
return (StatusCode::INTERNAL_SERVER_ERROR, "");
}
}
let webhook_msg = format!(
">>> ***Order Changed***\n**Name:** {}\n**Count:** {}\n**Unit Cost:** ${}\n**Subtotal:** ${}\n**Team:** {}\n**Reason:** {}",
change_order.name,
change_order.count,
change_order.unit_cost,
Decimal::from(change_order.count) * change_order.unit_cost,
change_order.team,
change_order.reason
);
let active_model = order::ActiveModel {
id: ActiveValue::Unchanged(change_order.id),
name: ActiveValue::Set(change_order.name),
date: ActiveValue::NotSet,
status: ActiveValue::NotSet,
count: ActiveValue::Set(change_order.count),
unit_cost: ActiveValue::Set(change_order.unit_cost),
store_in: ActiveValue::Set(change_order.store_in),
team: ActiveValue::Set(change_order.team),
reason: ActiveValue::Set(change_order.reason),
};
if let Err(e) = active_model.update(&state.db).await {
error!("Failed to change order: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, "")
} else {
tokio::spawn(async move {
if let Err(e) = state
.new_orders_webhook
.send(&Message::new(|message| message.content(webhook_msg)))
.await
{
error!("Failed to trigger new-order webhook: {e}");
}
});
(StatusCode::OK, "")
}
}

#[derive(Deserialize)]
struct DeleteOrder {
id: u32
}

#[axum::debug_handler]
async fn cancel_order(
State(state): State<Arc<UsrState>>,
Json(DeleteOrder { id }): Json<DeleteOrder>,
) -> (StatusCode, &'static str) {
let webhook_msg;
match order::Entity::find_by_id(id).one(&state.db).await {
Ok(Some(model)) => {
if model.status != order::Status::New {
return (StatusCode::BAD_REQUEST, "Order has already been processed");
}
webhook_msg = format!(
">>> ***Order Cancelled***\n**Name:** {}\n**Count:** {}\n**Team:** {}",
model.name,
model.count,
model.team,
);
}
Ok(None) => {
return (StatusCode::BAD_REQUEST, "Order not found");
}
Err(e) => {
error!("Failed to find order: {e}");
return (StatusCode::INTERNAL_SERVER_ERROR, "");
}
}

if let Err(e) = order::Entity::delete_by_id(id).exec(&state.db).await {
error!("Failed to delete order: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, "")
} else {
tokio::spawn(async move {
if let Err(e) = state
.new_orders_webhook
.send(&Message::new(|message| message.content(webhook_msg)))
.await
{
error!("Failed to trigger new-order webhook: {e}");
}
});
(StatusCode::OK, "")
}
}

#[derive(Deserialize)]
pub struct UpdateOrder {
pub id: u32,
pub status: order::Status
}

#[axum::debug_handler]
async fn update_order(
State(state): State<Arc<UsrState>>,
Json(update_order): Json<UpdateOrder>,
) -> (StatusCode, &'static str) {
let webhook_msg;
match order::Entity::find_by_id(update_order.id).one(&state.db).await {
Ok(Some(model)) => {
if model.status == order::Status::InStorage {
return (StatusCode::BAD_REQUEST, "Order is already in storage");
}
if model.status == update_order.status {
return (StatusCode::BAD_REQUEST, "Order is already in that state");
}
webhook_msg = format!(
">>> **Order Update!**\n**Name:** {}\n**Team:** {}\n**Status:** {}",
model.name,
model.team,
update_order.status
);
}
Ok(None) => {
return (StatusCode::BAD_REQUEST, "Order not found");
}
Err(e) => {
error!("Failed to find order: {e}");
return (StatusCode::INTERNAL_SERVER_ERROR, "");
}
}

let active_model = order::ActiveModel {
id: ActiveValue::Unchanged(update_order.id),
name: ActiveValue::NotSet,
date: ActiveValue::NotSet,
status: ActiveValue::Set(update_order.status),
count: ActiveValue::NotSet,
unit_cost: ActiveValue::NotSet,
store_in: ActiveValue::NotSet,
team: ActiveValue::NotSet,
reason: ActiveValue::NotSet,
};
if let Err(e) = active_model.update(&state.db).await {
error!("Failed to update order: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, "")
} else {
tokio::spawn(async move {
if let Err(e) = state
.order_updates_webhook
.send(&Message::new(|message| message.content(webhook_msg)))
.await
{
error!("Failed to trigger order-updates webhook: {e}");
}
});
(StatusCode::OK, "")
}
}

pub fn router() -> Router<Arc<UsrState>> {
Router::new().route("/new/order", post(new_order))
Router::new()
.route("/new/order", post(new_order))
.route("/change/order", post(change_order))
.route("/del/order", delete(cancel_order))
.route("/update/order", post(update_order))
}

pub async fn reset_tables(db: &DatabaseConnection) -> Result<(), sea_orm::DbErr> {
Expand Down
12 changes: 12 additions & 0 deletions usr-backend/src/manifest/order.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -38,4 +40,14 @@ pub enum Status {
Delivered,
#[sea_orm(string_value = "I")]
InStorage,
}

impl Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self == &Self::InStorage {
write!(f, "In Storage")
} else {
write!(f, "{:?}", self)
}
}
}

0 comments on commit a7ceb33

Please sign in to comment.