Added additional transaction operations.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct AmountCents(u64);
|
||||
|
||||
impl AmountCents {
|
||||
pub fn get(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for AmountCents {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i64> for AmountCents {
|
||||
type Error = AmountCentsConversionError;
|
||||
|
||||
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
||||
u64::try_from(value)
|
||||
.map(Self)
|
||||
.map_err(|_| AmountCentsConversionError(value.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<AmountCents> for i64 {
|
||||
type Error = AmountCentsConversionError;
|
||||
|
||||
fn try_from(value: AmountCents) -> Result<Self, Self::Error> {
|
||||
i64::try_from(value.get()).map_err(|_| AmountCentsConversionError(value.get().into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
pub struct AmountCentsConversionError(pub i128);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{AmountCents, AmountCentsConversionError};
|
||||
|
||||
#[test]
|
||||
fn converts_non_negative_database_values() {
|
||||
assert_eq!(AmountCents::try_from(4250_i64).unwrap().get(), 4250);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_negative_database_values() {
|
||||
assert!(matches!(
|
||||
AmountCents::try_from(-1_i64),
|
||||
Err(AmountCentsConversionError(-1_i128))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_values_too_large_for_the_database() {
|
||||
assert!(matches!(
|
||||
i64::try_from(AmountCents::from(i64::MAX as u64 + 1)),
|
||||
Err(AmountCentsConversionError(value)) if value == i64::MAX as i128 + 1
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,10 @@ use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::dtos::recurring_transaction::RecurringTransactionRow,
|
||||
routes::dtos::direction::{Direction, DirectionConversionError},
|
||||
routes::dtos::{
|
||||
amount_cents::{AmountCents, AmountCentsConversionError},
|
||||
direction::{Direction, DirectionConversionError},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -36,9 +39,7 @@ impl TryFrom<RecurringTransactionRow> for RecurringTransaction {
|
||||
id: row.id,
|
||||
data: RecurringTransactionData {
|
||||
name: row.name,
|
||||
amount_cents: row.amount_cents.try_into().map_err(|_| {
|
||||
RecurringTransactionConversionError::AmountCents(row.amount_cents)
|
||||
})?,
|
||||
amount_cents: AmountCents::try_from(row.amount_cents)?,
|
||||
direction: Direction::try_from(row.direction)
|
||||
.map_err(RecurringTransactionConversionError::Direction)?,
|
||||
start: row.starts_at,
|
||||
@@ -68,7 +69,7 @@ pub enum ScheduledTransactionData {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RecurringTransactionData {
|
||||
pub name: String,
|
||||
pub amount_cents: u64,
|
||||
pub amount_cents: AmountCents,
|
||||
pub direction: Direction,
|
||||
pub start: DateTime<Utc>,
|
||||
pub end: Option<DateTime<Utc>>,
|
||||
@@ -148,8 +149,8 @@ impl TryFrom<String> for WeekendAdjustment {
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RecurringTransactionConversionError {
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
AmountCents(i64),
|
||||
#[error(transparent)]
|
||||
AmountCents(#[from] AmountCentsConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
@@ -178,7 +179,7 @@ mod tests {
|
||||
fn transaction(amount_cents: u64) -> RecurringTransactionData {
|
||||
RecurringTransactionData {
|
||||
name: String::new(),
|
||||
amount_cents,
|
||||
amount_cents: amount_cents.into(),
|
||||
direction: Direction::Out,
|
||||
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
|
||||
end: None,
|
||||
@@ -204,7 +205,7 @@ mod tests {
|
||||
|
||||
let request: ScheduledTransactionData = serde_json::from_value(response).unwrap();
|
||||
let ScheduledTransactionData::Recurring(data) = request;
|
||||
assert_eq!(data.amount_cents, 1234);
|
||||
assert_eq!(data.amount_cents.get(), 1234);
|
||||
assert_eq!(data.recurrence.every, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,50 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::dtos::transaction::TransactionRow;
|
||||
use crate::routes::dtos::direction::{Direction, DirectionConversionError};
|
||||
use crate::routes::dtos::{
|
||||
amount_cents::{AmountCents, AmountCentsConversionError},
|
||||
direction::{Direction, DirectionConversionError},
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TransactionResponse {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Transaction {
|
||||
pub id: Uuid,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub data: TransactionData,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TransactionData {
|
||||
pub description: String,
|
||||
pub amount_cents: u64,
|
||||
pub amount_cents: AmountCents,
|
||||
pub direction: Direction,
|
||||
pub date: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl TryFrom<TransactionRow> for TransactionResponse {
|
||||
impl TryFrom<TransactionRow> for Transaction {
|
||||
type Error = TransactionConversionError;
|
||||
|
||||
fn try_from(row: TransactionRow) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
id: row.id,
|
||||
description: row.description,
|
||||
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,
|
||||
data: TransactionData {
|
||||
description: row.description,
|
||||
amount_cents: AmountCents::try_from(row.amount_cents)?,
|
||||
direction: Direction::try_from(row.direction)
|
||||
.map_err(TransactionConversionError::Direction)?,
|
||||
date: row.date,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TransactionConversionError {
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
AmountCents(i64),
|
||||
#[error(transparent)]
|
||||
AmountCents(#[from] AmountCentsConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
@@ -49,7 +58,7 @@ mod tests {
|
||||
fn preserves_cents_in_response() {
|
||||
let id = Uuid::new_v4();
|
||||
let date = Utc::now();
|
||||
let response = TransactionResponse::try_from(TransactionRow {
|
||||
let response = Transaction::try_from(TransactionRow {
|
||||
id,
|
||||
description: "Groceries".to_string(),
|
||||
amount_cents: 4250,
|
||||
@@ -60,8 +69,30 @@ mod tests {
|
||||
let response = response.expect("valid direction");
|
||||
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.description, "Groceries");
|
||||
assert_eq!(response.amount_cents, 4250);
|
||||
assert_eq!(response.date, date);
|
||||
assert_eq!(response.data.description, "Groceries");
|
||||
assert_eq!(response.data.amount_cents.get(), 4250);
|
||||
assert_eq!(response.data.date, date);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_transaction_json_flat() {
|
||||
let response = Transaction {
|
||||
id: Uuid::nil(),
|
||||
data: TransactionData {
|
||||
description: "Groceries".to_string(),
|
||||
amount_cents: 4250.into(),
|
||||
direction: Direction::Out,
|
||||
date: Utc::now(),
|
||||
},
|
||||
};
|
||||
|
||||
let response = serde_json::to_value(response).unwrap();
|
||||
assert_eq!(response["id"], Uuid::nil().to_string());
|
||||
assert_eq!(response["amount_cents"], 4250);
|
||||
assert!(response.get("data").is_none());
|
||||
|
||||
let request: TransactionData = serde_json::from_value(response).unwrap();
|
||||
assert_eq!(request.description, "Groceries");
|
||||
assert_eq!(request.amount_cents.get(), 4250);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user