RIP-Comm / sossoldi

"Sossoldi" is a wealth management / personal finance / Net Worth tracking app, made with Flutter.
MIT License
252 stars 70 forks source link

Accounts list is not updating #140

Closed jackrua closed 5 months ago

jackrua commented 5 months ago

To reproduce the issue go to:

Settings > Your accounts > Add account > (insert any name and value you want) > Create account > Go back and you won't see any new account.

What I have gathered so far

This is a really interesting bug that brought me to discover some of the internal logic of the app, but I can't really get to the bottom of it.

It seems to me that the sossoldi.db is actually updated when you trigger this action, however the accountsList variable (in the _AccountListState object) is not updated, so it doesn't show up in the widget.

When you add an account, the stack goes like this:

  1. accountsList watches accountsProvider which is the AsyncAccountsNotifier() notifier
  2. which in turn uses the BankAccountMethods object to interface with the db. Here everything seems to work (hence why the db is updated when you add the account)

So I would say that the issue lies in 1. . However when you update an account everything works as it should, so in this class (AsyncAccountsNotifier()) updateAccount() works, while addAccount() doesn't.

If anybody, more experienced than me, could point me in the right direction I would love to close this PR.

theperu commented 5 months ago

Hi! If you want I can assign this issue to you while you try to figure out the root cause of the bug. Since the issue might go unnoticed for a bit if you want you can try asking the same question also in the Discord channel since it is a bit more active

jackrua commented 5 months ago

Finally found it! It took me more than I would like to admit. But contrary to my first hunch, the issue was in the SQL query in the selectAll() method in BankAccountMethods. This method launches an SQL query that looks something like this:

SELECT b.*, (b.startingValue +
      SUM(CASE WHEN t.type = 'IN' OR t.type = 'TRSF' AND t.idBankAccountTransfer = b.id THEN t.amount
               ELSE 0 END) -
      SUM(CASE WHEN t.type = 'OUT' OR t.type = 'TRSF' AND t.idBankAccount = b.id THEN t.amount
               ELSE 0 END)
    ) as total
      FROM bankAccount as b
      LEFT JOIN "transaction" as t ON t.idBankAccount = b.id OR t.idBankAccountTransfer = b.id
      WHERE active = 1 AND recurring = 0
      GROUP BY b.id
      ORDER BY createdAt AS

Now, focusing on the LEFT JOIN part of the query, you see that the WHERE clause filters out rows after the join is done, hence filtering out all bank accounts that have a transaction with the recurring field different from 0.
Practically speaking, if I add a new bank account that has no transactions (i.e. I add a row to the bankAccount table) this is correctly saved by the left join but is lost after it meets the WHERE clause.

Hopefully, I will fix it tomorrow!

jackrua commented 5 months ago

This issue has already been fixed in #134, forgot to git pull and have been using an older version of the repo.

mikev-cw commented 5 months ago

Good catch by the way!