41 lines
1.0 KiB
Rust
41 lines
1.0 KiB
Rust
use chrono::{DateTime, FixedOffset};
|
|
use serde::Serialize;
|
|
use tracing::error;
|
|
|
|
use crate::firefly::TransactionRead;
|
|
|
|
#[derive(Serialize)]
|
|
pub struct Transaction {
|
|
pub id: String,
|
|
pub amount: f64,
|
|
pub description: String,
|
|
pub date: DateTime<FixedOffset>,
|
|
}
|
|
|
|
impl TryFrom<TransactionRead> for Transaction {
|
|
type Error = &'static str;
|
|
|
|
fn try_from(t: TransactionRead) -> Result<Self, Self::Error> {
|
|
let first_split = match t.attributes.transactions.first() {
|
|
Some(t) => t,
|
|
None => {
|
|
error!("Invalid transaction {}: no splits", t.id);
|
|
return Err("Transaction has no splits");
|
|
},
|
|
};
|
|
let date = first_split.date;
|
|
let amount = t.amount();
|
|
let description = if let Some(title) = &t.attributes.group_title {
|
|
title.into()
|
|
} else {
|
|
first_split.description.clone()
|
|
};
|
|
Ok(Self {
|
|
id: t.id,
|
|
amount,
|
|
description,
|
|
date,
|
|
})
|
|
}
|
|
}
|