use chrono::{DateTime, Utc};
use serde::Deserialize;
use super::common::OrderType;
#[derive(Debug, Clone, Deserialize)]
pub struct Transaction {
pub id: String,
#[serde(rename = "type")]
pub order_type: OrderType,
pub platinum: u32,
pub quantity: u32,
#[serde(rename = "itemId")]
pub item_id: String,
#[serde(rename = "createdAt")]
pub created_at: DateTime<Utc>,
}
impl Transaction {
pub fn total_value(&self) -> u64 {
self.platinum as u64 * self.quantity as u64
}
pub fn is_sale(&self) -> bool {
matches!(self.order_type, OrderType::Sell)
}
pub fn is_purchase(&self) -> bool {
matches!(self.order_type, OrderType::Buy)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_transaction_total_value() {
let tx = Transaction {
id: "tx-1".to_string(),
order_type: OrderType::Sell,
platinum: 50,
quantity: 3,
item_id: "item-1".to_string(),
created_at: Utc::now(),
};
assert_eq!(tx.total_value(), 150);
assert!(tx.is_sale());
assert!(!tx.is_purchase());
}
}