summaryrefslogtreecommitdiff
path: root/backend/importers/actualbudget/src/models
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2024-10-22 07:46:43 +0100
committerJoe Carstairs <me@joeac.net>2024-10-22 07:46:43 +0100
commit5824c353f978a5b9ab9696451ee3014c6a614ffc (patch)
treebc39754ca417e84eb00b0aa9a1ae2330da0bf282 /backend/importers/actualbudget/src/models
parent7adaace61a4fe983eba5ded3aaaf624ce6fb9014 (diff)
Refactors importer to use core stuff
Diffstat (limited to 'backend/importers/actualbudget/src/models')
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget/account.rs15
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget/category.rs15
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget/date.rs69
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget/mod.rs5
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget/transaction.rs43
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget/zero_budget.rs30
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget_account.rs8
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget_category.rs8
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget_date.rs75
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget_transaction.rs18
-rw-r--r--backend/importers/actualbudget/src/models/actualbudget_zero_budget.rs13
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/DateTimeUtc.rs57
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/account.rs8
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/budget_update.rs10
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/category.rs4
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/category_transfer.rs7
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/date_time_utc.rs20
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/mod.rs7
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/transaction.rs11
-rw-r--r--backend/importers/actualbudget/src/models/budgeting_app/transaction_categorisation.rs7
-rw-r--r--backend/importers/actualbudget/src/models/mod.rs7
21 files changed, 127 insertions, 310 deletions
diff --git a/backend/importers/actualbudget/src/models/actualbudget/account.rs b/backend/importers/actualbudget/src/models/actualbudget/account.rs
deleted file mode 100644
index 727c082..0000000
--- a/backend/importers/actualbudget/src/models/actualbudget/account.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-use sqlite::Statement;
-
-pub struct Account {
- pub id: String,
- pub name: String,
-}
-
-impl Account {
- pub fn from_row(statement: &Statement) -> Account {
- Account {
- id: statement.read("id").unwrap(),
- name: statement.read("name").unwrap(),
- }
- }
-}
diff --git a/backend/importers/actualbudget/src/models/actualbudget/category.rs b/backend/importers/actualbudget/src/models/actualbudget/category.rs
deleted file mode 100644
index 21fdf80..0000000
--- a/backend/importers/actualbudget/src/models/actualbudget/category.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-use sqlite::Statement;
-
-pub struct Category {
- pub id: String,
- pub name: String,
-}
-
-impl Category {
- pub fn from_row(statement: &Statement) -> Category {
- Category {
- id: statement.read("id").unwrap(),
- name: statement.read("name").unwrap(),
- }
- }
-}
diff --git a/backend/importers/actualbudget/src/models/actualbudget/date.rs b/backend/importers/actualbudget/src/models/actualbudget/date.rs
deleted file mode 100644
index bd37c46..0000000
--- a/backend/importers/actualbudget/src/models/actualbudget/date.rs
+++ /dev/null
@@ -1,69 +0,0 @@
-use sqlite::Statement;
-
-#[derive(Clone, PartialEq, Eq)]
-pub struct Date {
- value: i64,
- last_date_part: DatePart,
-}
-
-#[derive(Clone, PartialOrd, PartialEq, Eq)]
-enum DatePart {
- MonthOfYear,
- DayOfMonth,
-}
-
-// actualbudget represents dates as numbers which "look like" the corresponding
-// ISO string when written in decimal. E.g. 20240901 represents 1 Sep 2024. It
-// also uses month specifiers, e.g. 202409 to represent Sep 2024. In this case,
-// we interpret this as the first day of the month, e.g. 2024-09-01.
-
-impl Date {
- pub fn from_date_row(statement: &Statement, column: &str) -> Date {
- Date {
- value: statement.read::<i64, _>(column).unwrap(),
- last_date_part: DatePart::DayOfMonth,
- }
- }
-
- pub fn from_month_row(statement: &Statement, column: &str) -> Date {
- Date {
- value: statement.read::<i64, _>(column).unwrap() * 100 + 1,
- last_date_part: DatePart::MonthOfYear,
- }
- }
-
- pub fn to_iso(&self) -> String {
- format!("{0}-{1}-{2}", self.year(), self.month(), self.day())
- }
-
- pub fn year(&self) -> String {
- self.value.to_string()[0..4].to_string()
- }
-
- pub fn month(&self) -> String {
- self.value.to_string()[4..6].to_string()
- }
-
- pub fn day(&self) -> String {
- self.dayified_value().to_string()[6..8].to_string()
- }
-
- fn dayified_value(&self) -> i64 {
- match self.last_date_part {
- DatePart::DayOfMonth => self.value,
- DatePart::MonthOfYear => self.value * 100 + 1,
- }
- }
-}
-
-impl Ord for Date {
- fn cmp(&self, other: &Self) -> std::cmp::Ordering {
- self.dayified_value().cmp(&other.dayified_value())
- }
-}
-
-impl PartialOrd for Date {
- fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
- Some(self.cmp(other))
- }
-}
diff --git a/backend/importers/actualbudget/src/models/actualbudget/mod.rs b/backend/importers/actualbudget/src/models/actualbudget/mod.rs
deleted file mode 100644
index 5ca472f..0000000
--- a/backend/importers/actualbudget/src/models/actualbudget/mod.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-pub mod account;
-pub mod category;
-pub mod date;
-pub mod transaction;
-pub mod zero_budget;
diff --git a/backend/importers/actualbudget/src/models/actualbudget/transaction.rs b/backend/importers/actualbudget/src/models/actualbudget/transaction.rs
deleted file mode 100644
index 0a21cfe..0000000
--- a/backend/importers/actualbudget/src/models/actualbudget/transaction.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-use sqlite::Statement;
-
-use super::date::Date;
-
-#[derive(Clone)]
-pub struct Transaction {
- pub id: String,
- pub is_parent: bool,
- pub is_child: bool,
- pub account_id: String,
- pub category_id: String,
- pub amount: i64,
- pub payee: String,
- pub notes: String,
- pub date: Date,
- pub parent_id: Option<String>,
-}
-
-impl Transaction {
- pub fn from_row(statement: &Statement) -> Transaction {
- Transaction {
- id: statement.read("id").unwrap(),
- is_parent: statement.read::<i64, &str>("isParent").unwrap() == 1,
- is_child: statement.read::<i64, &str>("isChild").unwrap() == 1,
- account_id: statement.read("acct").unwrap(),
- category_id: statement
- .read::<Option<String>, &str>("category")
- .map(Option::<String>::unwrap_or_default)
- .unwrap(),
- amount: statement.read("amount").unwrap(),
- payee: statement
- .read::<Option<String>, &str>("description")
- .map(Option::<String>::unwrap_or_default)
- .unwrap(),
- notes: statement
- .read::<Option<String>, &str>("notes")
- .map(Option::<String>::unwrap_or_default)
- .unwrap(),
- date: Date::from_date_row(statement, "date"),
- parent_id: statement.read("parent_id").unwrap(),
- }
- }
-}
diff --git a/backend/importers/actualbudget/src/models/actualbudget/zero_budget.rs b/backend/importers/actualbudget/src/models/actualbudget/zero_budget.rs
deleted file mode 100644
index 87c88e7..0000000
--- a/backend/importers/actualbudget/src/models/actualbudget/zero_budget.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-use sqlite::Statement;
-
-use super::date::Date;
-
-#[derive(Clone)]
-pub struct ZeroBudget {
- pub id: String,
- pub month: Date,
- pub category_id: String,
- pub amount: i64,
- pub do_carry_over: bool,
-}
-
-impl ZeroBudget {
- pub fn from_row(statement: &Statement) -> ZeroBudget {
- ZeroBudget {
- id: statement.read("id").unwrap(),
- month: Date::from_month_row(statement, "month"),
- category_id: statement.read("category").unwrap(),
- amount: statement.read("amount").unwrap(),
- do_carry_over: match statement.read::<i64, &str>("carryover").unwrap() {
- 0 => false,
- 1 => true,
- carryover => {
- panic!("carryover column was neither 0 nor 1, but instead {carryover}")
- }
- },
- }
- }
-}
diff --git a/backend/importers/actualbudget/src/models/actualbudget_account.rs b/backend/importers/actualbudget/src/models/actualbudget_account.rs
new file mode 100644
index 0000000..b3d00ac
--- /dev/null
+++ b/backend/importers/actualbudget/src/models/actualbudget_account.rs
@@ -0,0 +1,8 @@
+use diesel::prelude::{Queryable, Selectable};
+
+#[derive(Queryable, Selectable)]
+#[diesel(table_name = crate::schema::accounts)]
+pub struct ActualbudgetAccount {
+ pub id: String,
+ pub name: String,
+}
diff --git a/backend/importers/actualbudget/src/models/actualbudget_category.rs b/backend/importers/actualbudget/src/models/actualbudget_category.rs
new file mode 100644
index 0000000..cbea99b
--- /dev/null
+++ b/backend/importers/actualbudget/src/models/actualbudget_category.rs
@@ -0,0 +1,8 @@
+use diesel::{prelude::Queryable, Selectable};
+
+#[derive(Queryable, Selectable)]
+#[diesel(table_name = crate::schema::categories)]
+pub struct ActualbudgetCategory {
+ pub id: String,
+ pub name: String,
+}
diff --git a/backend/importers/actualbudget/src/models/actualbudget_date.rs b/backend/importers/actualbudget/src/models/actualbudget_date.rs
new file mode 100644
index 0000000..bef160c
--- /dev/null
+++ b/backend/importers/actualbudget/src/models/actualbudget_date.rs
@@ -0,0 +1,75 @@
+use diesel::{backend::Backend, deserialize::{FromSql, FromSqlRow}, expression::AsExpression, sql_types::Integer, sqlite::Sqlite};
+
+#[derive(Clone, PartialEq, Eq, Debug, AsExpression, FromSqlRow)]
+#[diesel(sql_type=Integer)]
+pub struct ActualbudgetDate(i32);
+
+#[derive(Clone, PartialOrd, PartialEq, Eq)]
+enum DatePart {
+ MonthOfYear,
+ DayOfMonth,
+}
+
+// actualbudget represents dates as numbers which "look like" the corresponding
+// ISO string when written in decimal. E.g. 20240901 represents 1 Sep 2024. It
+// also uses month specifiers, e.g. 202409 to represent Sep 2024. In this case,
+// we interpret this as the first day of the month, e.g. 2024-09-01.
+
+impl ActualbudgetDate {
+ pub fn to_iso(&self) -> String {
+ format!("{0}-{1}-{2}", self.year(), self.month(), self.day())
+ }
+
+ pub fn year(&self) -> String {
+ self.0.to_string()[0..4].to_string()
+ }
+
+ pub fn month(&self) -> String {
+ self.0.to_string()[4..6].to_string()
+ }
+
+ pub fn day(&self) -> String {
+ match self.date_part() {
+ DatePart::DayOfMonth => self.0.to_string()[6..8].to_string(),
+ DatePart::MonthOfYear => String::from("01"),
+ }
+ }
+
+ pub fn from_i32(i: i32) -> Self {
+ ActualbudgetDate(i)
+ }
+
+ fn date_part(&self) -> DatePart {
+ if self.0 < 10000000 {
+ DatePart::MonthOfYear
+ } else {
+ DatePart::DayOfMonth
+ }
+ }
+
+ fn dayified_value(&self) -> i32 {
+ match self.date_part() {
+ DatePart::DayOfMonth => self.0,
+ DatePart::MonthOfYear => self.0 * 100 + 1,
+ }
+ }
+}
+
+impl Ord for ActualbudgetDate {
+ fn cmp(&self, other: &Self) -> std::cmp::Ordering {
+ self.dayified_value().cmp(&other.dayified_value())
+ }
+}
+
+impl PartialOrd for ActualbudgetDate {
+ fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl FromSql<Integer, Sqlite> for ActualbudgetDate {
+ fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> diesel::deserialize::Result<Self> {
+ let int: i32 = FromSql::<Integer, Sqlite>::from_sql(bytes)?;
+ Ok(ActualbudgetDate(int))
+ }
+}
diff --git a/backend/importers/actualbudget/src/models/actualbudget_transaction.rs b/backend/importers/actualbudget/src/models/actualbudget_transaction.rs
new file mode 100644
index 0000000..be2ca1a
--- /dev/null
+++ b/backend/importers/actualbudget/src/models/actualbudget_transaction.rs
@@ -0,0 +1,18 @@
+use diesel::prelude::{Identifiable, Queryable, QueryableByName, Selectable};
+
+use crate::models::actualbudget_date::ActualbudgetDate;
+
+#[derive(Identifiable, QueryableByName, Queryable, Selectable, Clone)]
+#[diesel(table_name = crate::schema::transactions)]
+pub struct ActualbudgetTransaction {
+ pub id: String,
+ pub is_parent: bool,
+ pub is_child: bool,
+ pub account_id: String,
+ pub category_id: String,
+ pub amount: i32,
+ pub payee: String,
+ pub notes: String,
+ pub date: ActualbudgetDate,
+ pub parent_id: Option<String>,
+}
diff --git a/backend/importers/actualbudget/src/models/actualbudget_zero_budget.rs b/backend/importers/actualbudget/src/models/actualbudget_zero_budget.rs
new file mode 100644
index 0000000..6478d34
--- /dev/null
+++ b/backend/importers/actualbudget/src/models/actualbudget_zero_budget.rs
@@ -0,0 +1,13 @@
+use crate::models::actualbudget_date::ActualbudgetDate;
+use diesel::{prelude::{Identifiable, Queryable, QueryableByName, Selectable}, sqlite::Sqlite};
+
+#[derive(Identifiable, QueryableByName, Queryable, Selectable, Clone)]
+#[diesel(table_name = crate::schema::zero_budgets)]
+#[diesel(check_for_backend(Sqlite))]
+pub struct ActualbudgetZeroBudget {
+ pub id: String,
+ pub month: ActualbudgetDate,
+ pub category_id: String,
+ pub amount: i32,
+ pub do_carry_over: i32,
+}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/DateTimeUtc.rs b/backend/importers/actualbudget/src/models/budgeting_app/DateTimeUtc.rs
deleted file mode 100644
index e44fa8c..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/DateTimeUtc.rs
+++ /dev/null
@@ -1,57 +0,0 @@
-use chrono::{DateTime, Datelike, Utc};
-use diesel::{
- deserialize::{FromSql, FromSqlRow},
- expression::AsExpression,
- serialize::ToSql,
- sql_types::Text,
- sqlite::Sqlite,
-};
-use serde::{Deserialize, Serialize};
-
-#[derive(
- Eq,
- PartialEq,
- Ord,
- PartialOrd,
- Serialize,
- Deserialize,
- Copy,
- Clone,
- Debug,
- AsExpression,
- FromSqlRow,
-)]
-#[diesel(sql_type=Text)]
-pub struct DateTimeUtc(DateTime<Utc>);
-
-impl DateTimeUtc {
- pub fn day_diff(&self, other: &DateTimeUtc) -> u32 {
- self.0
- .num_days_from_ce()
- .abs_diff(other.0.num_days_from_ce())
- }
-
- pub fn new(date_time_utc: DateTime<Utc>) -> Self {
- Self(date_time_utc)
- }
-}
-
-impl ToSql<Text, Sqlite> for DateTimeUtc {
- fn to_sql<'b>(
- &'b self,
- out: &mut diesel::serialize::Output<'b, '_, Sqlite>,
- ) -> diesel::serialize::Result {
- out.set_value((*self).0.to_rfc3339());
- Ok(diesel::serialize::IsNull::No)
- }
-}
-
-impl FromSql<Text, Sqlite> for DateTimeUtc {
- fn from_sql(
- bytes: <Sqlite as diesel::backend::Backend>::RawValue<'_>,
- ) -> diesel::deserialize::Result<Self> {
- let str: String = FromSql::<Text, Sqlite>::from_sql(bytes)?;
- let date_time = str.as_str().parse::<DateTime<Utc>>()?;
- Ok(DateTimeUtc(date_time))
- }
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/account.rs b/backend/importers/actualbudget/src/models/budgeting_app/account.rs
deleted file mode 100644
index 165b55c..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/account.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-use super::date_time_utc::DateTimeUtc;
-
-pub struct Account {
- pub id: i32,
- pub name: String,
- pub opening_balance: i32,
- pub opening_date: DateTimeUtc,
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/budget_update.rs b/backend/importers/actualbudget/src/models/budgeting_app/budget_update.rs
deleted file mode 100644
index 9958446..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/budget_update.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-use super::date_time_utc::DateTimeUtc;
-
-#[derive(Clone)]
-pub struct BudgetUpdate {
- pub id: i32,
- pub category_id: i32,
- pub date: DateTimeUtc,
- pub new_budget: i32,
- pub new_period: i32,
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/category.rs b/backend/importers/actualbudget/src/models/budgeting_app/category.rs
deleted file mode 100644
index 3f51dfd..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/category.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-pub struct Category {
- pub id: i32,
- pub name: String,
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/category_transfer.rs b/backend/importers/actualbudget/src/models/budgeting_app/category_transfer.rs
deleted file mode 100644
index 0097429..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/category_transfer.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-pub struct CategoryTransfer {
- pub id: i32,
- pub description: String,
- pub quantity: i32,
- pub from_category_id: i32,
- pub to_category_id: i32,
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/date_time_utc.rs b/backend/importers/actualbudget/src/models/budgeting_app/date_time_utc.rs
deleted file mode 100644
index 69cdfe2..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/date_time_utc.rs
+++ /dev/null
@@ -1,20 +0,0 @@
-use std::{fmt::Display, str::FromStr};
-
-use chrono::{DateTime, Utc};
-
-#[derive(Clone)]
-pub struct DateTimeUtc(DateTime<Utc>);
-
-impl DateTimeUtc {
- pub fn new(date: &str) -> Self {
- let mut date_time = String::from(date);
- date_time.push_str("T00:00:00.000Z");
- Self(DateTime::<Utc>::from_str(&date_time).unwrap())
- }
-}
-
-impl Display for DateTimeUtc {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- self.0.fmt(f)
- }
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/mod.rs b/backend/importers/actualbudget/src/models/budgeting_app/mod.rs
deleted file mode 100644
index bca0f99..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/mod.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-pub mod account;
-pub mod budget_update;
-pub mod category;
-pub mod category_transfer;
-pub mod date_time_utc;
-pub mod transaction;
-pub mod transaction_categorisation;
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/transaction.rs b/backend/importers/actualbudget/src/models/budgeting_app/transaction.rs
deleted file mode 100644
index e679e1e..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/transaction.rs
+++ /dev/null
@@ -1,11 +0,0 @@
-use super::date_time_utc::DateTimeUtc;
-
-#[derive(Clone)]
-pub struct Transaction {
- pub id: i32,
- pub description: String,
- pub payee: String,
- pub quantity: i32,
- pub date: DateTimeUtc,
- pub account_id: i32,
-}
diff --git a/backend/importers/actualbudget/src/models/budgeting_app/transaction_categorisation.rs b/backend/importers/actualbudget/src/models/budgeting_app/transaction_categorisation.rs
deleted file mode 100644
index ca7e7f5..0000000
--- a/backend/importers/actualbudget/src/models/budgeting_app/transaction_categorisation.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-pub struct TransactionCategorisation {
- pub id: i32,
- pub description: String,
- pub quantity: i32,
- pub transaction_id: i32,
- pub category_id: i32,
-}
diff --git a/backend/importers/actualbudget/src/models/mod.rs b/backend/importers/actualbudget/src/models/mod.rs
index cf1b71a..1c97c65 100644
--- a/backend/importers/actualbudget/src/models/mod.rs
+++ b/backend/importers/actualbudget/src/models/mod.rs
@@ -1,2 +1,5 @@
-pub mod actualbudget;
-pub mod budgeting_app;
+pub mod actualbudget_account;
+pub mod actualbudget_category;
+pub mod actualbudget_date;
+pub mod actualbudget_transaction;
+pub mod actualbudget_zero_budget;