Implemented amount_cents for transactions.
This commit is contained in:
@@ -36,7 +36,9 @@ impl TryFrom<RecurringTransactionRow> for RecurringTransaction {
|
||||
id: row.id,
|
||||
data: RecurringTransactionData {
|
||||
name: row.name,
|
||||
amount: row.amount_cents as f64 / 100.0,
|
||||
amount_cents: row.amount_cents.try_into().map_err(|_| {
|
||||
RecurringTransactionConversionError::AmountCents(row.amount_cents)
|
||||
})?,
|
||||
direction: Direction::try_from(row.direction)
|
||||
.map_err(RecurringTransactionConversionError::Direction)?,
|
||||
start: row.starts_at,
|
||||
@@ -66,7 +68,7 @@ pub enum ScheduledTransactionData {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RecurringTransactionData {
|
||||
pub name: String,
|
||||
pub amount: f64,
|
||||
pub amount_cents: u64,
|
||||
pub direction: Direction,
|
||||
pub start: DateTime<Utc>,
|
||||
pub end: Option<DateTime<Utc>>,
|
||||
@@ -146,6 +148,7 @@ impl TryFrom<String> for WeekendAdjustment {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RecurringTransactionConversionError {
|
||||
AmountCents(i64),
|
||||
Direction(DirectionConversionError),
|
||||
RecurrenceEvery(i32),
|
||||
RecurrenceUnit(String),
|
||||
@@ -157,6 +160,9 @@ impl std::error::Error for RecurringTransactionConversionError {}
|
||||
impl std::fmt::Display for RecurringTransactionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountCents(value) => {
|
||||
write!(formatter, "invalid amount_cents value: {value}")
|
||||
}
|
||||
Self::Direction(error) => error.fmt(formatter),
|
||||
Self::RecurrenceEvery(value) => {
|
||||
write!(formatter, "invalid recurrence_every value: {value}")
|
||||
@@ -182,10 +188,10 @@ mod tests {
|
||||
};
|
||||
use crate::routes::dtos::direction::Direction;
|
||||
|
||||
fn transaction(amount: f64) -> RecurringTransactionData {
|
||||
fn transaction(amount_cents: u64) -> RecurringTransactionData {
|
||||
RecurringTransactionData {
|
||||
name: String::new(),
|
||||
amount,
|
||||
amount_cents,
|
||||
direction: Direction::Out,
|
||||
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
|
||||
end: None,
|
||||
@@ -201,17 +207,17 @@ mod tests {
|
||||
fn keeps_scheduled_transaction_json_flat() {
|
||||
let response = ScheduledTransaction::Recurring(RecurringTransaction {
|
||||
id: Uuid::nil(),
|
||||
data: transaction(12.34),
|
||||
data: transaction(1234),
|
||||
});
|
||||
let response = serde_json::to_value(response).unwrap();
|
||||
assert_eq!(response["type"], "recurring");
|
||||
assert_eq!(response["id"], Uuid::nil().to_string());
|
||||
assert_eq!(response["amount"], 12.34);
|
||||
assert_eq!(response["amount_cents"], 1234);
|
||||
assert!(response.get("data").is_none());
|
||||
|
||||
let request: ScheduledTransactionData = serde_json::from_value(response).unwrap();
|
||||
let ScheduledTransactionData::Recurring(data) = request;
|
||||
assert_eq!(data.amount, 12.34);
|
||||
assert_eq!(data.amount_cents, 1234);
|
||||
assert_eq!(data.recurrence.every, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,31 +9,54 @@ use crate::routes::dtos::direction::{Direction, DirectionConversionError};
|
||||
pub struct TransactionResponse {
|
||||
pub id: Uuid,
|
||||
pub description: String,
|
||||
pub amount: f64,
|
||||
pub amount_cents: u64,
|
||||
pub direction: Direction,
|
||||
pub date: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl TryFrom<TransactionRow> for TransactionResponse {
|
||||
type Error = DirectionConversionError;
|
||||
type Error = TransactionConversionError;
|
||||
|
||||
fn try_from(row: TransactionRow) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
id: row.id,
|
||||
description: row.description,
|
||||
amount: row.amount_cents as f64 / 100.0,
|
||||
direction: Direction::try_from(row.direction)?,
|
||||
amount_cents: row
|
||||
.amount_cents
|
||||
.try_into()
|
||||
.map_err(|_| TransactionConversionError::AmountCents(row.amount_cents))?,
|
||||
direction: Direction::try_from(row.direction)
|
||||
.map_err(TransactionConversionError::Direction)?,
|
||||
date: row.date,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TransactionConversionError {
|
||||
AmountCents(i64),
|
||||
Direction(DirectionConversionError),
|
||||
}
|
||||
|
||||
impl std::error::Error for TransactionConversionError {}
|
||||
|
||||
impl std::fmt::Display for TransactionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountCents(value) => {
|
||||
write!(formatter, "invalid amount_cents value: {value}")
|
||||
}
|
||||
Self::Direction(error) => error.fmt(formatter),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn converts_cents_to_major_currency_units() {
|
||||
fn preserves_cents_in_response() {
|
||||
let id = Uuid::new_v4();
|
||||
let date = Utc::now();
|
||||
let response = TransactionResponse::try_from(TransactionRow {
|
||||
@@ -48,7 +71,7 @@ mod tests {
|
||||
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.description, "Groceries");
|
||||
assert_eq!(response.amount, 42.5);
|
||||
assert_eq!(response.amount_cents, 4250);
|
||||
assert_eq!(response.date, date);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user