diff options
author | rtkay123 <dev@kanjala.com> | 2025-08-09 11:45:07 +0200 |
---|---|---|
committer | rtkay123 <dev@kanjala.com> | 2025-08-09 11:45:18 +0200 |
commit | d50185df5a5870498b6c7e6b51e0368d051743ca (patch) | |
tree | 2820c340d86650efdc2e231b4190bddcd54c9f73 | |
parent | 9e3e07220ef10d2b8e5251c4871d54f9c5a1e9d0 (diff) | |
download | warden-d50185df5a5870498b6c7e6b51e0368d051743ca.tar.bz2 warden-d50185df5a5870498b6c7e6b51e0368d051743ca.zip |
feat: money type
-rw-r--r-- | crates/warden/src/server/routes/processor/pacs008.rs | 40 | ||||
-rw-r--r-- | lib/warden-core/build.rs | 3 | ||||
-rw-r--r-- | lib/warden-core/src/google/parser.rs | 2 | ||||
-rw-r--r-- | lib/warden-core/src/google/parser/money.rs | 110 |
4 files changed, 42 insertions, 113 deletions
diff --git a/crates/warden/src/server/routes/processor/pacs008.rs b/crates/warden/src/server/routes/processor/pacs008.rs index e241d38..87f1fee 100644 --- a/crates/warden/src/server/routes/processor/pacs008.rs +++ b/crates/warden/src/server/routes/processor/pacs008.rs @@ -1,6 +1,8 @@ use axum::{extract::State, response::IntoResponse}; -use tracing::{error, trace, warn}; +use stack_up::tracing_opentelemetry::OpenTelemetrySpanExt; +use tracing::{debug, error, trace, warn}; use warden_core::{ + google::r#type::Money, iso20022::{TransactionType, pacs008::Pacs008Document}, message::DataCache, }; @@ -35,7 +37,8 @@ pub(super) async fn post_pacs008( State(state): State<AppHandle>, axum::Json(transaction): axum::Json<Pacs008Document>, ) -> Result<impl IntoResponse, AppError> { - let tx_tp = TransactionType::PACS008; + let tx_tp = TransactionType::PACS008.to_string(); + tracing::Span::current().record("tx_tp", &tx_tp); let data_cache = build_data_cache(&transaction)?; let tx_count = transaction.f_i_to_f_i_cstmr_cdt_trf.cdt_trf_tx_inf.len(); @@ -63,6 +66,39 @@ pub(super) async fn post_pacs008( .map(|value| value.pmt_id.end_to_end_id.as_str()) .ok_or_else(|| anyhow::anyhow!("missing end_to_end_id id"))?; + let ccy = + cdt_trf_tx_inf.and_then(|value| value.instd_amt.as_ref().map(|value| value.ccy.as_str())); + + let end_to_end_id = cdt_trf_tx_inf + .as_ref() + .map(|value| value.pmt_id.end_to_end_id.as_str()) + .ok_or_else(|| { + error!("missing end_to_end_id"); + anyhow::anyhow!("missing end_to_end_id id") + })?; + + tracing::Span::current().record("end_to_end_id", end_to_end_id); + let end_to_end_id = String::from(end_to_end_id); + + let msg_id = &transaction.f_i_to_f_i_cstmr_cdt_trf.grp_hdr.msg_id; + tracing::Span::current().record("msg_id", msg_id); + + let pmt_inf_id = cdt_trf_tx_inf + .and_then(|value| value.pmt_id.instr_id.as_ref()) + .ok_or_else(|| { + error!("missing pmt_inf_id"); + anyhow::anyhow!("missing pmt_inf_id id") + })?; + + debug!(%msg_id, %end_to_end_id, "extracted transaction identifiers"); + + let money = if let (Some(amt), Some(ccy)) = (amount, ccy) { + Some(Money::try_from((amt, ccy)).map_err(|_e| anyhow::anyhow!("invalid currency"))?) + } else { + trace!(msg_id, "transaction has no amount or currency"); + None + }; + Ok(String::default()) } diff --git a/lib/warden-core/build.rs b/lib/warden-core/build.rs index 1998137..46793ed 100644 --- a/lib/warden-core/build.rs +++ b/lib/warden-core/build.rs @@ -14,6 +14,7 @@ impl Entity { vec![ "proto/iso20022/pacs_008_001_12.proto", "proto/iso20022/pacs_002_001_12.proto", + "proto/googleapis/google/type/money.proto", "proto/warden_message.proto", ] } @@ -84,7 +85,7 @@ fn add_serde(config: tonic_prost_build::Builder) -> tonic_prost_build::Builder { "#[derive(serde::Serialize, serde::Deserialize)] #[serde(rename_all = \"snake_case\")]", ); - #[cfg(feature = "time")] + #[cfg(feature = "serde-time")] let config = config.type_attribute( ".google.protobuf.Timestamp", "#[serde(try_from = \"time::OffsetDateTime\")] #[serde(into = \"String\")]", diff --git a/lib/warden-core/src/google/parser.rs b/lib/warden-core/src/google/parser.rs index 7405077..f2fe5bc 100644 --- a/lib/warden-core/src/google/parser.rs +++ b/lib/warden-core/src/google/parser.rs @@ -1,4 +1,4 @@ #[cfg(feature = "time")] mod dt; -//mod money; +mod money; diff --git a/lib/warden-core/src/google/parser/money.rs b/lib/warden-core/src/google/parser/money.rs index 54ed5fa..a703a4a 100644 --- a/lib/warden-core/src/google/parser/money.rs +++ b/lib/warden-core/src/google/parser/money.rs @@ -1,4 +1,4 @@ -use crate::google::r#type::Date; +use crate::google::r#type::Money; /// If money cannot be created #[derive(Debug, PartialEq)] @@ -183,111 +183,3 @@ mod tests { ); } } - -#[cfg(feature = "time")] -impl From<time::OffsetDateTime> for Date { - fn from(dt: time::OffsetDateTime) -> Self { - Self { - year: dt.year(), - month: dt.month() as i32, - day: dt.day() as i32, - } - } -} - -#[cfg(feature = "time")] -impl From<time::Date> for Date { - fn from(value: time::Date) -> Self { - Self { - year: value.year(), - month: value.month() as i32, - day: value.day() as i32, - } - } -} - -#[cfg(feature = "time")] -impl TryFrom<Date> for time::Date { - type Error = time::Error; - - fn try_from(value: Date) -> Result<Self, Self::Error> { - Ok(Self::from_calendar_date( - value.year, - time::Month::try_from(value.month as u8)?, - value.day as u8, - )?) - } -} - -#[cfg(feature = "time")] -impl std::str::FromStr for Date { - type Err = time::Error; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - let date = time::OffsetDateTime::parse(&s, &time::format_description::well_known::Rfc3339) - .map(Date::from); - - match date { - Ok(dt) => Ok(dt), - Err(_e) => { - let my_format = time::macros::format_description!("[year]-[month]-[day]"); - let date = time::Date::parse(&s, &my_format)?; - Ok(Date::from(date)) - } - } - } -} - -#[cfg(feature = "time")] -impl TryFrom<String> for Date { - type Error = time::Error; - - fn try_from(value: String) -> Result<Self, Self::Error> { - <Date as std::str::FromStr>::from_str(&value) - } -} - -#[cfg(feature = "time")] -impl TryFrom<super::helpers::time_util::DateItem> for Date { - type Error = time::Error; - - fn try_from(value: super::helpers::time_util::DateItem) -> Result<Self, Self::Error> { - match value { - super::helpers::time_util::DateItem::String(ref string) => { - <Date as std::str::FromStr>::from_str(string) - } - #[cfg(feature = "iso20022")] - super::helpers::time_util::DateItem::Date { year, month, day } => { - Ok(Date { year, month, day }) - } - super::helpers::time_util::DateItem::Timestamp { seconds, nanos } => { - let odt = time::OffsetDateTime::try_from(crate::google::protobuf::Timestamp { - seconds, - nanos, - })?; - Ok(Self { - year: odt.year(), - month: odt.month() as i32, - day: odt.day() as i32, - }) - } - } - } -} - -impl From<Date> for String { - fn from(value: Date) -> Self { - let prepend = |value: i32| -> String { - match value.lt(&10) { - true => format!("0{}", value), - false => value.to_string(), - } - }; - format!( - "{}-{}-{}", - value.year, - prepend(value.month), - prepend(value.day), - ) - } -} |