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 PrintedCheckId(pub Uuid);
impl PrintedCheckId {
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 PrintedCheckId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for PrintedCheckId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for PrintedCheckId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<PrintedCheckId> for Uuid {
fn from(id: PrintedCheckId) -> Self { id.0 }
}
impl AsRef<Uuid> for PrintedCheckId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for PrintedCheckId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct PrintedCheck {
pub id: Uuid,
pub bank_account_id: Uuid,
pub payment_id: Uuid,
pub payment_number: Option<String>,
pub check_number: String,
pub amount: Decimal,
pub payee_name: Option<String>,
pub status: String,
pub printed_at: DateTime<Utc>,
pub printed_by: Option<Uuid>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub metadata: serde_json::Value,
}
impl PrintedCheck {
pub fn builder() -> PrintedCheckBuilder {
<PrintedCheckBuilder as Default>::default()
}
pub fn new(bank_account_id: Uuid, payment_id: Uuid, check_number: String, amount: Decimal, status: String, printed_at: DateTime<Utc>, metadata: serde_json::Value) -> Self {
Self {
id: Uuid::new_v4(),
bank_account_id,
payment_id,
payment_number: None,
check_number,
amount,
payee_name: None,
status,
printed_at,
printed_by: None,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata,
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> PrintedCheckId {
PrintedCheckId(self.id)
}
pub fn created_at(&self) -> &DateTime<Utc> {
&self.created_at
}
pub fn updated_at(&self) -> &DateTime<Utc> {
&self.updated_at
}
pub fn status(&self) -> &String {
&self.status
}
pub fn with_payment_number(mut self, value: String) -> Self {
self.payment_number = Some(value);
self
}
pub fn with_payee_name(mut self, value: String) -> Self {
self.payee_name = Some(value);
self
}
pub fn with_printed_by(mut self, value: Uuid) -> Self {
self.printed_by = 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() {
"bank_account_id" => {
if let Ok(v) = serde_json::from_value(value) { self.bank_account_id = v; }
}
"payment_id" => {
if let Ok(v) = serde_json::from_value(value) { self.payment_id = v; }
}
"payment_number" => {
if let Ok(v) = serde_json::from_value(value) { self.payment_number = v; }
}
"check_number" => {
if let Ok(v) = serde_json::from_value(value) { self.check_number = v; }
}
"amount" => {
if let Ok(v) = serde_json::from_value(value) { self.amount = v; }
}
"payee_name" => {
if let Ok(v) = serde_json::from_value(value) { self.payee_name = v; }
}
"status" => {
if let Ok(v) = serde_json::from_value(value) { self.status = v; }
}
"printed_at" => {
if let Ok(v) = serde_json::from_value(value) { self.printed_at = v; }
}
"printed_by" => {
if let Ok(v) = serde_json::from_value(value) { self.printed_by = v; }
}
"metadata" => {
if let Ok(v) = serde_json::from_value(value) { self.metadata = v; }
}
_ => {} }
}
}
}
impl super::Entity for PrintedCheck {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"PrintedCheck"
}
}
impl backbone_core::PersistentEntity for PrintedCheck {
fn write_protected_fields() -> &'static [&'static str] {
&["created_at"]
}
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 PrintedCheck {
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("bank_account_id".to_string(), "uuid".to_string());
m.insert("payment_id".to_string(), "uuid".to_string());
m.insert("printed_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] {
&["check_number", "status"]
}
}
#[derive(Debug, Clone, Default)]
pub struct PrintedCheckBuilder {
bank_account_id: Option<Uuid>,
payment_id: Option<Uuid>,
payment_number: Option<String>,
check_number: Option<String>,
amount: Option<Decimal>,
payee_name: Option<String>,
status: Option<String>,
printed_at: Option<DateTime<Utc>>,
printed_by: Option<Uuid>,
metadata: Option<serde_json::Value>,
}
impl PrintedCheckBuilder {
pub fn bank_account_id(mut self, value: Uuid) -> Self {
self.bank_account_id = Some(value);
self
}
pub fn payment_id(mut self, value: Uuid) -> Self {
self.payment_id = Some(value);
self
}
pub fn payment_number(mut self, value: String) -> Self {
self.payment_number = Some(value);
self
}
pub fn check_number(mut self, value: String) -> Self {
self.check_number = Some(value);
self
}
pub fn amount(mut self, value: Decimal) -> Self {
self.amount = Some(value);
self
}
pub fn payee_name(mut self, value: String) -> Self {
self.payee_name = Some(value);
self
}
pub fn status(mut self, value: String) -> Self {
self.status = Some(value);
self
}
pub fn printed_at(mut self, value: DateTime<Utc>) -> Self {
self.printed_at = Some(value);
self
}
pub fn printed_by(mut self, value: Uuid) -> Self {
self.printed_by = Some(value);
self
}
pub fn metadata(mut self, value: serde_json::Value) -> Self {
self.metadata = Some(value);
self
}
pub fn build(self) -> Result<PrintedCheck, String> {
let bank_account_id = self.bank_account_id.ok_or_else(|| "bank_account_id is required".to_string())?;
let payment_id = self.payment_id.ok_or_else(|| "payment_id is required".to_string())?;
let check_number = self.check_number.ok_or_else(|| "check_number is required".to_string())?;
let amount = self.amount.ok_or_else(|| "amount is required".to_string())?;
Ok(PrintedCheck {
id: Uuid::new_v4(),
bank_account_id,
payment_id,
payment_number: self.payment_number,
check_number,
amount,
payee_name: self.payee_name,
status: self.status.unwrap_or_default(),
printed_at: self.printed_at.unwrap_or(Utc::now()),
printed_by: self.printed_by,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: self.metadata.unwrap_or(serde_json::json!({})),
})
}
}