use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use rust_decimal::Decimal;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FullReconcileId(pub Uuid);
impl FullReconcileId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for FullReconcileId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for FullReconcileId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for FullReconcileId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<FullReconcileId> for Uuid {
fn from(id: FullReconcileId) -> Self { id.0 }
}
impl AsRef<Uuid> for FullReconcileId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for FullReconcileId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct FullReconcile {
pub id: Uuid,
pub exchange_total: Decimal,
pub reconciled_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub metadata: Option<serde_json::Value>,
}
impl FullReconcile {
pub fn builder() -> FullReconcileBuilder {
<FullReconcileBuilder as Default>::default()
}
pub fn new(exchange_total: Decimal, reconciled_at: DateTime<Utc>) -> Self {
Self {
id: Uuid::new_v4(),
exchange_total,
reconciled_at,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: None,
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> FullReconcileId {
FullReconcileId(self.id)
}
pub fn created_at(&self) -> &DateTime<Utc> {
&self.created_at
}
pub fn updated_at(&self) -> &DateTime<Utc> {
&self.updated_at
}
pub fn with_metadata(mut self, value: serde_json::Value) -> Self {
self.metadata = Some(value);
self
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"exchange_total" => {
if let Ok(v) = serde_json::from_value(value) { self.exchange_total = v; }
}
"reconciled_at" => {
if let Ok(v) = serde_json::from_value(value) { self.reconciled_at = v; }
}
"metadata" => {
if let Ok(v) = serde_json::from_value(value) { self.metadata = v; }
}
_ => {} }
}
}
}
impl super::Entity for FullReconcile {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"FullReconcile"
}
}
impl backbone_core::PersistentEntity for FullReconcile {
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
Some(self.created_at)
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.created_at = ts;
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
Some(self.updated_at)
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.updated_at = ts;
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
None
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
let _ = ts;
}
}
impl backbone_orm::EntityRepoMeta for FullReconcile {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("reconciled_at".to_string(), "timestamptz".to_string());
m.insert("created_at".to_string(), "timestamptz".to_string());
m.insert("updated_at".to_string(), "timestamptz".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&[]
}
}
#[derive(Debug, Clone, Default)]
pub struct FullReconcileBuilder {
exchange_total: Option<Decimal>,
reconciled_at: Option<DateTime<Utc>>,
metadata: Option<serde_json::Value>,
}
impl FullReconcileBuilder {
pub fn exchange_total(mut self, value: Decimal) -> Self {
self.exchange_total = Some(value);
self
}
pub fn reconciled_at(mut self, value: DateTime<Utc>) -> Self {
self.reconciled_at = Some(value);
self
}
pub fn metadata(mut self, value: serde_json::Value) -> Self {
self.metadata = Some(value);
self
}
pub fn build(self) -> Result<FullReconcile, String> {
Ok(FullReconcile {
id: Uuid::new_v4(),
exchange_total: self.exchange_total.unwrap_or(Decimal::from(0)),
reconciled_at: self.reconciled_at.unwrap_or(Utc::now()),
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: self.metadata,
})
}
}