Added recategorization from UI

This commit is contained in:
Manuel Forcén Muñoz 2024-05-16 23:08:56 +02:00
parent 128cd85a9b
commit 1ad1e66470
4 changed files with 25 additions and 12 deletions

View file

@ -63,15 +63,9 @@ impl Account {
Ok(res) Ok(res)
} }
pub async fn recategorize_transactions( pub async fn recategorize_transactions(&self, pool: &SqlitePool) -> Result<()> {
&self,
pool: &SqlitePool,
from: Option<DateTime<Utc>>,
to: Option<DateTime<Utc>>,
) -> Result<()> {
let rules = Rule::list_by_user(pool, self.user).await?; let rules = Rule::list_by_user(pool, self.user).await?;
let mut tx_list = let mut tx_list = Transaction::list_uncategorized(pool, self.account_id).await?;
Transaction::list_by_date(pool, self.account_id, from, to, None, true).await?;
for tx in tx_list.iter_mut() { for tx in tx_list.iter_mut() {
println!("Checking {}", tx.get_description()); println!("Checking {}", tx.get_description());
if tx.recategorize(pool, &rules).await? { if tx.recategorize(pool, &rules).await? {

View file

@ -157,6 +157,20 @@ impl Transaction {
Ok(res) Ok(res)
} }
pub async fn list_uncategorized(pool: &SqlitePool, account: i32) -> Result<Vec<Self>> {
let mut query = sqlx::QueryBuilder::new("SELECT * FROM TRANSACTIONS WHERE account=");
query.push_bind(account);
query.push(" AND category IS NULL");
let rows = query.build().fetch_all(pool).await?;
let mut ret = Vec::new();
for r in &rows {
ret.push(Transaction::from_row(r)?);
}
Ok(ret)
}
pub async fn group_by_date( pub async fn group_by_date(
pool: &SqlitePool, pool: &SqlitePool,
account: i32, account: i32,

View file

@ -22,6 +22,7 @@
</div> </div>
<div class="mb-2"> <div class="mb-2">
<h2>Last transactions</h2> <h2>Last transactions</h2>
<button class="ars-button" onclick="onRecategorize()">Recategorize</button>
<table width="100%"> <table width="100%">
<thead> <thead>
<tr> <tr>
@ -75,6 +76,13 @@
<script src="https://cdn.jsdelivr.net/npm/litepicker/dist/litepicker.js"></script> <script src="https://cdn.jsdelivr.net/npm/litepicker/dist/litepicker.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script> <script>
function onRecategorize() {
fetch(
'/api/v1/accounts/id/{{account.account_id}}/recategorize',
{method: 'POST'}
).then(e=>console.log(e));
}
function onDateChange(e) { function onDateChange(e) {
let date_val = document.getElementById('amount-date-range').value.split(' - '); let date_val = document.getElementById('amount-date-range').value.split(' - ');

View file

@ -61,10 +61,7 @@ pub async fn recategorize(
return (StatusCode::UNAUTHORIZED, String::new()); return (StatusCode::UNAUTHORIZED, String::new());
} }
match account match account.recategorize_transactions(db.as_ref()).await {
.recategorize_transactions(db.as_ref(), None, None)
.await
{
Ok(_) => (StatusCode::OK, String::new()), Ok(_) => (StatusCode::OK, String::new()),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")), Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")),
} }