From f3f1578ca86227f87138bc0728a2f2b10aa29ca0 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 25 Aug 2019 14:49:16 -0400 Subject: also load scheduled transactions --- bin/load | 2 ++ data/schema.sql | 40 ++++++++++++++++++++++++++++++ src/main.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/bin/load b/bin/load index aa24e4c..b8e65e2 100755 --- a/bin/load +++ b/bin/load @@ -18,3 +18,5 @@ psql -U metabase ynab -c 'COPY categories_by_month FROM STDIN' < categories_by_m psql -U metabase ynab -c 'COPY payees FROM STDIN' < payees.tsv psql -U metabase ynab -c 'COPY transactions FROM STDIN' < transactions.tsv psql -U metabase ynab -c 'COPY subtransactions FROM STDIN' < subtransactions.tsv +psql -U metabase ynab -c 'COPY scheduled_transactions FROM STDIN' < scheduled_transactions.tsv +psql -U metabase ynab -c 'COPY scheduled_subtransactions FROM STDIN' < scheduled_subtransactions.tsv diff --git a/data/schema.sql b/data/schema.sql index 7da6862..8c28292 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -1,3 +1,5 @@ +DROP TABLE IF EXISTS scheduled_subtransactions; +DROP TABLE IF EXISTS scheduled_transactions; DROP TABLE IF EXISTS subtransactions; DROP TABLE IF EXISTS transactions; DROP TABLE IF EXISTS payees; @@ -83,3 +85,41 @@ CREATE TABLE subtransactions ( category_id text REFERENCES categories(id), transfer_account_id text REFERENCES accounts(id) ); + +CREATE TYPE frequency_t AS ENUM ( + 'never', + 'daily', + 'weekly', + 'everyOtherWeek', + 'twiceAMonth', + 'every4Weeks', + 'monthly', + 'everyOtherMonth', + 'every3Months', + 'every4Months', + 'twiceAYear', + 'yearly', + 'everyOtherYear' +); +CREATE TABLE scheduled_transactions ( + id text PRIMARY KEY, + date_next date NOT NULL, + frequency frequency_t NOT NULL, + amount bigint NOT NULL, + memo text, + flag_color flag_color_t, + account_id text REFERENCES accounts(id) NOT NULL, + payee_id text REFERENCES payees(id), + category_id text REFERENCES categories(id), + transfer_account_id text REFERENCES accounts(id) +); + +CREATE TABLE scheduled_subtransactions ( + id text PRIMARY KEY, + scheduled_transaction_id text REFERENCES scheduled_transactions(id) NOT NULL, + amount bigint NOT NULL, + memo text, + payee_id text REFERENCES payees(id), + category_id text REFERENCES categories(id), + transfer_account_id text REFERENCES accounts(id) +); diff --git a/src/main.rs b/src/main.rs index 85623cb..3108ebb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ pub fn read_api_key() -> String { key.to_string() } +#[allow(clippy::cognitive_complexity)] fn main() { let key = read_api_key(); let mut ynab_config = ynab_api::apis::configuration::Configuration::new(); @@ -255,4 +256,78 @@ fn main() { } file.sync_all().unwrap(); file2.sync_all().unwrap(); + + let mut file = + std::fs::File::create("scheduled_transactions.tsv").unwrap(); + for scheduled_transaction in budget.scheduled_transactions.unwrap() { + if scheduled_transaction.deleted { + continue; + } + file.write_all( + [ + scheduled_transaction.id.as_ref(), + scheduled_transaction.date_next.as_ref(), + scheduled_transaction.frequency.as_ref(), + format!("{}", scheduled_transaction.amount).as_ref(), + scheduled_transaction + .memo + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + scheduled_transaction.flag_color.as_ref(), + scheduled_transaction.account_id.as_ref(), + scheduled_transaction + .payee_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + if scheduled_transaction.category_id == SPLIT_CATEGORY_ID { + "\\N" + } else { + scheduled_transaction.category_id.as_ref() + }, + scheduled_transaction + .transfer_account_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + ] + .join("\t") + .as_bytes(), + ) + .unwrap(); + file.write_all(b"\n").unwrap(); + } + file.sync_all().unwrap(); + + let mut file = + std::fs::File::create("scheduled_subtransactions.tsv").unwrap(); + for scheduled_subtransaction in budget.scheduled_subtransactions.unwrap() + { + if scheduled_subtransaction.deleted { + continue; + } + file.write_all( + [ + scheduled_subtransaction.id.as_ref(), + scheduled_subtransaction.scheduled_transaction_id.as_ref(), + format!("{}", scheduled_subtransaction.amount).as_ref(), + scheduled_subtransaction + .memo + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + scheduled_subtransaction + .payee_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + scheduled_subtransaction.category_id.as_ref(), + scheduled_subtransaction + .transfer_account_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + ] + .join("\t") + .as_bytes(), + ) + .unwrap(); + file.write_all(b"\n").unwrap(); + } + file.sync_all().unwrap(); } -- cgit v1.2.3