Return account details.

This commit is contained in:
2026-09-27 11:50:59 +02:00
parent 26a370aa31
commit 406f30ef80
7 changed files with 239 additions and 33 deletions
+88 -13
View File
@@ -30,8 +30,8 @@ pub struct RecurringTransactionRequest {
pub name: String,
pub amount_cents: u64,
pub direction: Direction,
pub source_account_id: Option<Uuid>,
pub destination_account_id: Option<Uuid>,
pub source_account: Option<SourceAccountRequest>,
pub destination_account: Option<DestinationAccountRequest>,
pub schedule: ScheduleRequest,
}
@@ -50,8 +50,8 @@ impl RecurringTransactionRequest {
name: self.name,
amount: domain::Cents::try_from(self.amount_cents)?,
direction: self.direction.into(),
source_account_id: self.source_account_id,
destination_account_id: self.destination_account_id,
source_account_id: self.source_account.as_ref().map(|account| account.id),
destination_account_id: self.destination_account.as_ref().map(|account| account.id),
schedule: domain::Schedule::new(
self.schedule.start,
self.schedule.end,
@@ -68,25 +68,58 @@ pub struct RecurringTransactionResponse {
pub name: String,
pub amount_cents: u64,
pub direction: Direction,
pub source_account_id: Option<Uuid>,
pub destination_account_id: Option<Uuid>,
pub source_account: Option<SourceAccountResponse>,
pub destination_account: Option<DestinationAccountResponse>,
pub schedule: ScheduleResponse,
}
impl RecurringTransactionResponse {
pub fn new(transaction: domain::RecurringTransaction, next: Option<DateTime<Utc>>) -> Self {
pub fn new(
transaction: domain::RecurringTransaction,
source_account: Option<domain::Account>,
destination_account: Option<domain::Account>,
schedule_next: Option<DateTime<Utc>>,
) -> Self {
Self {
id: transaction.id,
name: transaction.name,
amount_cents: transaction.amount.get(),
direction: transaction.direction.into(),
source_account_id: transaction.source_account_id,
destination_account_id: transaction.destination_account_id,
schedule: ScheduleResponse::new(transaction.schedule, next),
source_account: source_account.map(|account| SourceAccountResponse {
id: account.id,
name: account.name,
}),
destination_account: destination_account.map(|account| DestinationAccountResponse {
id: account.id,
name: account.name,
}),
schedule: ScheduleResponse::new(transaction.schedule, schedule_next),
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct SourceAccountRequest {
pub id: Uuid,
}
#[derive(Debug, Clone, Serialize)]
pub struct SourceAccountResponse {
pub id: Uuid,
pub name: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct DestinationAccountRequest {
pub id: Uuid,
}
#[derive(Debug, Clone, Serialize)]
pub struct DestinationAccountResponse {
pub id: Uuid,
pub name: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ScheduleRequest {
pub start: DateTime<Utc>,
@@ -207,8 +240,8 @@ mod tests {
name: String::new(),
amount_cents,
direction: Direction::Out,
source_account_id: None,
destination_account_id: None,
source_account: None,
destination_account: None,
schedule: ScheduleRequest {
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
end: None,
@@ -224,15 +257,22 @@ mod tests {
#[test]
fn serializes_scheduled_transaction_with_nested_schedule() {
let transaction = request(1234).into_domain(Uuid::nil()).unwrap();
let source_account = None;
let destination_account = None;
let schedule_next = None;
let response = ScheduledTransactionResponse::from(RecurringTransactionResponse::new(
transaction,
None,
source_account,
destination_account,
schedule_next,
));
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_cents"], 1234);
assert!(response["source_account"].is_null());
assert!(response["destination_account"].is_null());
assert_eq!(response["schedule"]["recurrence"]["unit"], "month");
assert_eq!(
response["schedule"]["weekend_adjustment"],
@@ -248,6 +288,41 @@ mod tests {
assert_eq!(request.schedule.recurrence.every, 2);
}
#[test]
fn serializes_source_and_destination_accounts() {
let source_id = Uuid::new_v4();
let destination_id = Uuid::new_v4();
let mut transaction = request(1234);
transaction.source_account = Some(super::SourceAccountRequest { id: source_id });
transaction.destination_account =
Some(super::DestinationAccountRequest { id: destination_id });
let transaction = transaction.into_domain(Uuid::nil()).unwrap();
let source_account = Some(domain::Account {
id: source_id,
name: "Checking".to_owned(),
});
let destination_account = Some(domain::Account {
id: destination_id,
name: "Savings".to_owned(),
});
let response = RecurringTransactionResponse::new(
transaction,
source_account,
destination_account,
None,
);
let response = serde_json::to_value(response).unwrap();
assert_eq!(response["source_account"]["id"], source_id.to_string());
assert_eq!(response["source_account"]["name"], "Checking");
assert_eq!(
response["destination_account"]["id"],
destination_id.to_string()
);
assert_eq!(response["destination_account"]["name"], "Savings");
}
#[test]
fn converts_to_domain() {
let id = Uuid::new_v4();