use { crate::PhysicalAddress, chrono::{DateTime, Utc}, derive_more::{Deref, Display}, serde::{Deserialize, Serialize}, std::str::FromStr, uuid::Uuid, }; #[cfg(feature = "client")] const _: () = { use crate::{MuralError, RequestExt, SearchParams, SearchResponse}; impl crate::Client { pub async fn search_counterparties( &self, params: Option>, ) -> Result, MuralError> { maybe_mock!(self, search_counterparties(params)); self.http_post(|base| format!("{base}/api/counterparties/search")) .query(¶ms.map(|p| p.to_query()).unwrap_or_default()) .send_mural() .await } pub async fn get_counterparty( &self, id: CounterpartyId, ) -> Result { maybe_mock!(self, get_counterparty(id)); self.http_get(|base| format!("{base}/api/counterparties/counterparty/{id}")) .send_mural() .await } pub async fn create_counterparty( &self, counterparty: &CreateCounterparty, ) -> Result { #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct Body<'a> { counterparty: &'a CreateCounterparty, } maybe_mock!(self, create_counterparty(counterparty)); let body = Body { counterparty }; self.http_post(|base| format!("{base}/api/counterparties")) .json(&body) .send_mural() .await } pub async fn update_counterparty( &self, id: CounterpartyId, counterparty: &UpdateCounterparty, ) -> Result { #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct Body<'a> { counterparty: &'a UpdateCounterparty, } maybe_mock!(self, update_counterparty(id, counterparty)); let body = Body { counterparty }; self.http_put(|base| format!("{base}/api/counterparties/counterparty/{id}")) .json(&body) .send_mural() .await } } }; #[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash, Deref, Serialize, Deserialize)] #[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] #[display("{}", _0.hyphenated())] pub struct CounterpartyId(pub Uuid); impl FromStr for CounterpartyId { type Err = ::Err; fn from_str(s: &str) -> Result { s.parse::().map(Self) } } impl From for Uuid { fn from(value: CounterpartyId) -> Self { value.0 } } #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] #[serde(rename_all = "camelCase")] pub struct Counterparty { pub id: CounterpartyId, pub created_at: DateTime, pub updated_at: DateTime, pub alias: Option, #[serde(flatten)] pub kind: CounterpartyKind, } #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] #[serde(tag = "type", rename_all = "camelCase")] pub enum CounterpartyKind { #[serde(rename_all = "camelCase")] Individual { first_name: String, last_name: String, email: String, physical_address: PhysicalAddress, }, #[serde(rename_all = "camelCase")] Business { name: String, email: String, physical_address: PhysicalAddress, }, } #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] #[serde(tag = "type", rename_all = "camelCase")] pub enum CreateCounterparty { #[serde(rename_all = "camelCase")] Individual { alias: Option, first_name: String, last_name: String, email: String, physical_address: PhysicalAddress, }, #[serde(rename_all = "camelCase")] Business { alias: Option, name: String, email: String, physical_address: PhysicalAddress, }, } #[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] #[serde(tag = "type", rename_all = "camelCase")] pub enum UpdateCounterparty { #[serde(rename_all = "camelCase")] Individual { alias: Option, first_name: Option, last_name: Option, email: Option, physical_address: Option, }, #[serde(rename_all = "camelCase")] Business { alias: Option, name: Option, email: Option, physical_address: Option, }, }