247 lines
7.6 KiB
Rust
247 lines
7.6 KiB
Rust
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::{domain, routes::dtos::direction::Direction};
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
#[serde(tag = "type")]
|
|
pub enum ScheduledTransaction {
|
|
Recurring(RecurringTransaction),
|
|
}
|
|
|
|
impl From<RecurringTransaction> for ScheduledTransaction {
|
|
fn from(transaction: RecurringTransaction) -> Self {
|
|
Self::Recurring(transaction)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct RecurringTransaction {
|
|
pub id: Uuid,
|
|
|
|
#[serde(flatten)]
|
|
pub data: RecurringTransactionData,
|
|
|
|
/// The next scheduled occurrence of the transaction.
|
|
pub next: Option<DateTime<Utc>>,
|
|
}
|
|
|
|
impl RecurringTransaction {
|
|
pub fn new(transaction: domain::RecurringTransaction, next: Option<DateTime<Utc>>) -> Self {
|
|
Self {
|
|
id: transaction.id,
|
|
next,
|
|
data: RecurringTransactionData {
|
|
name: transaction.name,
|
|
amount_cents: transaction.amount.get(),
|
|
direction: transaction.direction.into(),
|
|
start: transaction.schedule.start(),
|
|
end: transaction.schedule.end(),
|
|
recurrence: transaction.schedule.recurrence().into(),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
#[serde(tag = "type")]
|
|
pub enum ScheduledTransactionData {
|
|
Recurring(RecurringTransactionData),
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct RecurringTransactionData {
|
|
pub name: String,
|
|
pub amount_cents: u64,
|
|
pub direction: Direction,
|
|
pub start: DateTime<Utc>,
|
|
pub end: Option<DateTime<Utc>>,
|
|
pub recurrence: Recurrence,
|
|
}
|
|
|
|
impl RecurringTransactionData {
|
|
pub fn into_domain(
|
|
self,
|
|
id: Uuid,
|
|
) -> Result<domain::RecurringTransaction, domain::ValidationError> {
|
|
let recurrence = domain::Recurrence {
|
|
every: domain::RecurrenceInterval::try_from(self.recurrence.every)?,
|
|
unit: self.recurrence.unit.into(),
|
|
weekend_adjustment: self.recurrence.weekend_adjustment.map(Into::into),
|
|
};
|
|
|
|
Ok(domain::RecurringTransaction {
|
|
id,
|
|
name: self.name,
|
|
amount: domain::Cents::try_from(self.amount_cents)?,
|
|
direction: self.direction.into(),
|
|
schedule: domain::Schedule::new(self.start, self.end, recurrence)?,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Recurrence {
|
|
pub every: u32,
|
|
pub unit: RecurrenceUnit,
|
|
pub weekend_adjustment: Option<WeekendAdjustment>,
|
|
}
|
|
|
|
impl From<domain::Recurrence> for Recurrence {
|
|
fn from(recurrence: domain::Recurrence) -> Self {
|
|
Self {
|
|
every: recurrence.every.get(),
|
|
unit: recurrence.unit.into(),
|
|
weekend_adjustment: recurrence.weekend_adjustment.map(Into::into),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum RecurrenceUnit {
|
|
Day,
|
|
Week,
|
|
Month,
|
|
Year,
|
|
}
|
|
|
|
impl From<RecurrenceUnit> for domain::RecurrenceUnit {
|
|
fn from(unit: RecurrenceUnit) -> Self {
|
|
match unit {
|
|
RecurrenceUnit::Day => Self::Day,
|
|
RecurrenceUnit::Week => Self::Week,
|
|
RecurrenceUnit::Month => Self::Month,
|
|
RecurrenceUnit::Year => Self::Year,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<domain::RecurrenceUnit> for RecurrenceUnit {
|
|
fn from(unit: domain::RecurrenceUnit) -> Self {
|
|
match unit {
|
|
domain::RecurrenceUnit::Day => Self::Day,
|
|
domain::RecurrenceUnit::Week => Self::Week,
|
|
domain::RecurrenceUnit::Month => Self::Month,
|
|
domain::RecurrenceUnit::Year => Self::Year,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum WeekendAdjustment {
|
|
PreviousBusinessDay,
|
|
NextBusinessDay,
|
|
}
|
|
|
|
impl From<WeekendAdjustment> for domain::WeekendAdjustment {
|
|
fn from(adjustment: WeekendAdjustment) -> Self {
|
|
match adjustment {
|
|
WeekendAdjustment::PreviousBusinessDay => Self::PreviousBusinessDay,
|
|
WeekendAdjustment::NextBusinessDay => Self::NextBusinessDay,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<domain::WeekendAdjustment> for WeekendAdjustment {
|
|
fn from(adjustment: domain::WeekendAdjustment) -> Self {
|
|
match adjustment {
|
|
domain::WeekendAdjustment::PreviousBusinessDay => Self::PreviousBusinessDay,
|
|
domain::WeekendAdjustment::NextBusinessDay => Self::NextBusinessDay,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use chrono::{TimeDelta, TimeZone, Utc};
|
|
use uuid::Uuid;
|
|
|
|
use super::{
|
|
Recurrence, RecurrenceUnit, RecurringTransaction, RecurringTransactionData,
|
|
ScheduledTransaction, ScheduledTransactionData, WeekendAdjustment,
|
|
};
|
|
use crate::{domain, routes::dtos::direction::Direction};
|
|
|
|
fn data(amount_cents: u64) -> RecurringTransactionData {
|
|
RecurringTransactionData {
|
|
name: String::new(),
|
|
amount_cents,
|
|
direction: Direction::Out,
|
|
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
|
|
end: None,
|
|
recurrence: Recurrence {
|
|
every: 2,
|
|
unit: RecurrenceUnit::Month,
|
|
weekend_adjustment: Some(WeekendAdjustment::NextBusinessDay),
|
|
},
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn keeps_scheduled_transaction_json_flat() {
|
|
let transaction = data(1234).into_domain(Uuid::nil()).unwrap();
|
|
let response = ScheduledTransaction::from(RecurringTransaction::new(transaction, None));
|
|
|
|
let response = serde_json::to_value(response).unwrap();
|
|
assert_eq!(response["type"], "recurring");
|
|
assert_eq!(response["id"], Uuid::nil().to_string());
|
|
assert!(response["next"].is_null());
|
|
assert_eq!(response["amount_cents"], 1234);
|
|
assert_eq!(response["recurrence"]["unit"], "month");
|
|
assert_eq!(
|
|
response["recurrence"]["weekend_adjustment"],
|
|
"next_business_day"
|
|
);
|
|
assert!(response.get("data").is_none());
|
|
|
|
let request: ScheduledTransactionData = serde_json::from_value(response).unwrap();
|
|
let ScheduledTransactionData::Recurring(data) = request;
|
|
assert_eq!(data.amount_cents, 1234);
|
|
assert_eq!(data.recurrence.every, 2);
|
|
}
|
|
|
|
#[test]
|
|
fn converts_to_domain() {
|
|
let id = Uuid::new_v4();
|
|
let transaction = data(1234).into_domain(id).unwrap();
|
|
let recurrence = transaction.schedule.recurrence();
|
|
|
|
assert_eq!(transaction.id, id);
|
|
assert_eq!(transaction.amount.get(), 1234);
|
|
assert_eq!(transaction.direction, domain::Direction::Out);
|
|
assert_eq!(recurrence.every.get(), 2);
|
|
assert_eq!(recurrence.unit, domain::RecurrenceUnit::Month);
|
|
assert_eq!(
|
|
recurrence.weekend_adjustment,
|
|
Some(domain::WeekendAdjustment::NextBusinessDay)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_invalid_requests() {
|
|
assert!(matches!(
|
|
data(i64::MAX as u64 + 1).into_domain(Uuid::nil()),
|
|
Err(domain::ValidationError::Amount(_))
|
|
));
|
|
|
|
let mut every = data(100);
|
|
every.recurrence.every = 0;
|
|
assert!(matches!(
|
|
every.into_domain(Uuid::nil()),
|
|
Err(domain::ValidationError::RecurrenceInterval(_))
|
|
));
|
|
|
|
let mut end = data(100);
|
|
end.end = Some(end.start - TimeDelta::seconds(1));
|
|
assert!(matches!(
|
|
end.into_domain(Uuid::nil()),
|
|
Err(domain::ValidationError::EndBeforeStart)
|
|
));
|
|
}
|
|
}
|