Skip to content

Commit

Permalink
feat(sma): Add import from SMA Sales
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-ride committed Oct 26, 2024
1 parent a3c493b commit 4cad497
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ fn auth_optional_routes(path: &str) -> axum::Router<state::AppState> {
get(routes::utils::download::download_file),
)
.route("/status", get(routes::utils::status::get_status))
.route("/sma", post(routes::utils::sma::post_update_from_sma))
.nest("/product", routes::product::router()),
)
}
Expand Down
1 change: 1 addition & 0 deletions src/models/response/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod product;
pub mod sma;
27 changes: 27 additions & 0 deletions src/models/response/product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ pub struct ProductResponse {
disabled: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, utoipa::ToSchema)]
#[schema(example = json!({ "id": "1a731f58-18f1-4c95-8de5-611bde07f4f1", "image": "2fa4c8d3-fd93-4066-a7f3-68a35ab72288_water.png", "name": "water", "price": 0.80, "quantity": 27, "creation_time": "2024-10-09T17:55:30.795279Z" }))]
pub struct EditedProductResponse {
pub id: uuid::Uuid,

#[serde(skip_serializing_if = "Option::is_none")]
pub image: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub price: Option<f64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub quantity: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub max_quantity_per_command: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub sma_code: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub disabled: Option<bool>,
}

impl TryFrom<entity::product::Model> for ProductResponse {
type Error = AppError;

Expand Down
36 changes: 36 additions & 0 deletions src/models/response/sma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::{error::AppError, models::utils::sma::SmaChange};

use super::product::{EditedProductResponse, ProductResponse};

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct SmaResponse {
pub unchanged: Vec<uuid::Uuid>,
pub changed: Vec<EditedProductResponse>,
pub created: Vec<ProductResponse>,
}

impl TryFrom<Vec<SmaChange>> for SmaResponse {
type Error = AppError;

fn try_from(value: Vec<SmaChange>) -> Result<Self, Self::Error> {
let iter = value.into_iter();
Ok(Self {
unchanged: iter
.clone()
.filter_map(|x| match x {
SmaChange::Unchanged(x) => Some(x),
_ => None,
})
.map(|x| x.id)
.collect(),
changed: vec![], // TODO: Changed
created: iter
.filter_map(|x| match x {
SmaChange::Created(x) => Some(x),
_ => None,
})
.map(TryInto::<ProductResponse>::try_into)
.collect::<Result<_, AppError>>()?,
})
}
}
1 change: 1 addition & 0 deletions src/models/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod pagination;
pub mod sma;
79 changes: 79 additions & 0 deletions src/models/utils/sma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use entity::product::Model as Product;
use serde::Deserialize;
use serde_json::Value;

use crate::models::response::product::EditedProductResponse;

#[derive(Debug, Clone, PartialEq)]
pub enum SmaChange {
Unchanged(Product),
Edited(EditedProductResponse),
Created(Product),
}

#[derive(Debug, Clone, Copy, PartialEq, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct SmaChangeTypeMatrix {
#[serde(default)]
pub name: bool,
#[serde(default)]
pub price: bool,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct SmaProducts {
pub data: Vec<SmaProduct>,
pub limit: Value,
pub start: Value,
pub total: u64,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct SmaProduct {
pub category: SmCategory,
pub code: String,
pub id: String,
pub image_url: Option<String>,
pub name: String,
pub net_price: String,
pub price: String,
pub slug: String,
pub tax_method: String,
pub tax_rate: SmaTaxRate,
#[serde(rename = "type")]
pub type_field: String,
pub unit: SmaUnit,
pub unit_price: String,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct SmCategory {
pub id: String,
pub code: String,
pub name: String,
pub image: Option<String>,
pub parent_id: String,
pub slug: String,
pub description: String,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct SmaTaxRate {
pub id: String,
pub name: String,
pub code: String,
pub rate: String,
#[serde(rename = "type")]
pub type_field: String,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct SmaUnit {
pub id: String,
pub code: String,
pub name: String,
pub base_unit: Option<String>,
pub operator: Option<String>,
pub unit_value: Option<String>,
pub operation_value: Option<String>,
}
1 change: 1 addition & 0 deletions src/routes/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod download;
pub mod login;
pub mod logout;
pub mod openapi;
pub mod sma;
pub mod status;
pub mod upload;
Loading

0 comments on commit 4cad497

Please sign in to comment.