Skip to content

Commit

Permalink
fix: removing balance from classifier and passing external category (#…
Browse files Browse the repository at this point in the history
…179)

* schema: saving external category to expenses table

* feat: using optional external category on classifier

* feat: increasing page_size of list transactions from 20 to
100

* sqlx: cargo sqlx prepared for offline queries
  • Loading branch information
nicolasauler authored Dec 18, 2024
1 parent 0ed5ddf commit e46ffe6
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 116 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

2 changes: 2 additions & 0 deletions migrations/20241218025611_external-category.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE expenses
DROP COLUMN external_category;
2 changes: 2 additions & 0 deletions migrations/20241218025611_external-category.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE expenses
ADD COLUMN external_category text;
3 changes: 1 addition & 2 deletions src/client/classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub struct TransactionToCategorize {
pub id: String,
pub description: String,
pub amount: f32,
pub balance: f32,
pub category: String,
pub category: Option<String>,
}

#[derive(Deserialize)]
Expand Down
6 changes: 5 additions & 1 deletion src/client/pluggy/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,13 @@ pub async fn list_transactions(
vec![
("accountId", account_id.to_string()),
("from", last_day.to_string()),
("pageSize", "100".to_owned()),
]
} else {
vec![("accountId", account_id.to_string())]
vec![
("accountId", account_id.to_string()),
("pageSize", "100".to_owned()),
]
};

let client = reqwest::Client::new();
Expand Down
2 changes: 1 addition & 1 deletion src/features/expenses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ pub async fn process_pluggy_expenses(
let params = crate::queries::expenses::CreateParamsFromPluggy {
description: transaction.description,
price: transaction.amount,
category: None,
bank: Some(item.connector_name.clone()),
external_account_id: transaction.account_id,
external_id: transaction.id,
external_created_at: transaction.created_at,
is_essential: false,
date: transaction.date.date(),
external_category: transaction.category,
now: OffsetDateTime::now_utc(),
};
let res =
Expand Down
12 changes: 6 additions & 6 deletions src/queries/expenses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub async fn create(
pub struct CreateParamsFromPluggy {
pub description: String,
pub price: f32,
pub category: Option<ExpenseCategory>,
pub bank: Option<String>,
pub date: Date,
pub now: OffsetDateTime,
pub external_account_id: Uuid,
pub external_id: Uuid,
pub external_created_at: Option<OffsetDateTime>,
pub is_essential: bool,
pub external_category: Option<String>,
}

pub async fn insert_from_pluggy(
Expand All @@ -58,12 +58,11 @@ pub async fn insert_from_pluggy(
sqlx::query!(
r#"
INSERT INTO expenses
(description, price, category, bank_source, external_account_id, external_id, date, uuid, is_essential, user_id, created_at, external_created_at)
VALUES ($1, $2, $3 :: expense_category, $4, $5, $6, $7, $8, $9, $10, $11, $12)
(description, price, bank_source, external_account_id, external_id, date, uuid, is_essential, user_id, created_at, external_created_at, external_category)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
"#,
p.description,
p.price,
p.category as Option<ExpenseCategory>,
p.bank,
p.external_account_id,
p.external_id,
Expand All @@ -73,6 +72,7 @@ pub async fn insert_from_pluggy(
user_id,
p.now,
p.external_created_at,
p.external_category,
)
.execute(conn)
.await
Expand Down Expand Up @@ -269,7 +269,7 @@ pub struct UncategorizedTransactions {
pub id: i32,
pub description: String,
pub price: f32,
pub category: Option<ExpenseCategory>,
pub external_category: Option<String>,
}

pub async fn list_uncategorized(
Expand All @@ -278,7 +278,7 @@ pub async fn list_uncategorized(
sqlx::query_as!(
UncategorizedTransactions,
r#"
SELECT id, description, price, category as "category: ExpenseCategory"
SELECT id, description, price, external_category
FROM expenses
WHERE category IS NULL
"#
Expand Down
4 changes: 1 addition & 3 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ pub fn categorize_transactions_task(
id: row.id.to_string(),
description: row.description.clone(),
amount: row.price,
balance: 0.0,
// FIX: use external category
category: row.category.clone().unwrap_or_default().to_string(),
category: row.external_category.clone(),
})
.collect();

Expand Down

0 comments on commit e46ffe6

Please sign in to comment.