Added recategorization from UI
This commit is contained in:
parent
128cd85a9b
commit
1ad1e66470
4 changed files with 25 additions and 12 deletions
|
|
@ -63,15 +63,9 @@ impl Account {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
pub async fn recategorize_transactions(
|
||||
&self,
|
||||
pool: &SqlitePool,
|
||||
from: Option<DateTime<Utc>>,
|
||||
to: Option<DateTime<Utc>>,
|
||||
) -> Result<()> {
|
||||
pub async fn recategorize_transactions(&self, pool: &SqlitePool) -> Result<()> {
|
||||
let rules = Rule::list_by_user(pool, self.user).await?;
|
||||
let mut tx_list =
|
||||
Transaction::list_by_date(pool, self.account_id, from, to, None, true).await?;
|
||||
let mut tx_list = Transaction::list_uncategorized(pool, self.account_id).await?;
|
||||
for tx in tx_list.iter_mut() {
|
||||
println!("Checking {}", tx.get_description());
|
||||
if tx.recategorize(pool, &rules).await? {
|
||||
|
|
|
|||
|
|
@ -157,6 +157,20 @@ impl Transaction {
|
|||
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(
|
||||
pool: &SqlitePool,
|
||||
account: i32,
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
</div>
|
||||
<div class="mb-2">
|
||||
<h2>Last transactions</h2>
|
||||
<button class="ars-button" onclick="onRecategorize()">Recategorize</button>
|
||||
<table width="100%">
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
@ -75,6 +76,13 @@
|
|||
<script src="https://cdn.jsdelivr.net/npm/litepicker/dist/litepicker.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<script>
|
||||
function onRecategorize() {
|
||||
fetch(
|
||||
'/api/v1/accounts/id/{{account.account_id}}/recategorize',
|
||||
{method: 'POST'}
|
||||
).then(e=>console.log(e));
|
||||
}
|
||||
|
||||
function onDateChange(e) {
|
||||
let date_val = document.getElementById('amount-date-range').value.split(' - ');
|
||||
|
||||
|
|
|
|||
|
|
@ -61,10 +61,7 @@ pub async fn recategorize(
|
|||
return (StatusCode::UNAUTHORIZED, String::new());
|
||||
}
|
||||
|
||||
match account
|
||||
.recategorize_transactions(db.as_ref(), None, None)
|
||||
.await
|
||||
{
|
||||
match account.recategorize_transactions(db.as_ref()).await {
|
||||
Ok(_) => (StatusCode::OK, String::new()),
|
||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue