debt settling with transaction

This commit is contained in:
2025-09-08 14:23:24 +01:00
parent 4c44252a5f
commit 1e8cd044b4
5 changed files with 50 additions and 18 deletions
+3 -6
View File
@@ -1,8 +1,5 @@
# DOING
Refactor how the models access the database (move the Tx creation to the models)
Refactor the Makefiles
Refactor the App start function
Add NIF columns to stores and to users
Implement scanning the invoice QR code to insert stuff into the tables
# TODO
- [ ] On the categories page, add a column of how many times a given category is
@@ -10,10 +7,10 @@ used
- [ ] Multiple categories per expense
- [X] Make all the other items update/create page be like expenses
- [ ] Ability to attach a document to an expense
- [ ] Debts table
~~- [ ] Debts table~~
- [ ] Ability to export data in multiple formats and with a selection of columns
- [ ] Migrate every column named "ExpenseSomething" to "ExpSomething"
- [ ] Make the models just return errors and not do any handling themselves
- [X] Make the models just return errors and not do any handling themselves
- [X] Reduce the size of the font file
- [X] Add some migration tool
- [X] Ownership on the image for the expenses webapp dir need to be changed
+9 -7
View File
@@ -3,8 +3,9 @@ package expenses
import (
"context"
mod "expenses/expenses/models"
dec "github.com/shopspring/decimal"
"sort"
dec "github.com/shopspring/decimal"
)
type UserTab struct {
@@ -142,18 +143,19 @@ func CalculateDebts(e *mod.Expense) (mod.Debts, error) {
return debts, nil
}
// settleDebt updates a debt settlement on the database
// it takes away the payed debt by payment from the creditor payments
// TODO
// This won't work if the payment was made with multiple payments - but for my
// use case that won't be a thing. Right now every expense I introduce has been
// fully payed in one go
func (a *ExpensesApp) settleDebt(payment mod.Payment,
credit mod.Payment, eId int32) error {
ctx := context.Background()
credit.PayedAmount = credit.PayedAmount.Sub(payment.PayedAmount)
// TODO
// This needs to run as a transaction
err := a.ExpRepo.InsertPayment(ctx, eId, payment)
err = a.ExpRepo.UpdatePayment(ctx, credit)
return err
return a.ExpRepo.SettleDebt(ctx, eId, payment, credit)
}
func (a *ExpensesApp) ProcessDebt(expID int32, debt mod.Debt) error {
+4
View File
@@ -32,5 +32,9 @@ type ExpenseRepository interface {
InsertPayments(ctx context.Context, eId int32, pm mod.Payments) error
DeletePayment(ctx context.Context, id int32) error
// Debts
SettleDebt(ctx context.Context, eId int32,
payment mod.Payment, credit mod.Payment) error
Close()
}
+25
View File
@@ -0,0 +1,25 @@
package repo
import (
"context"
mod "expenses/expenses/models"
"expenses/expenses/repo/pgdb/pgsqlc"
)
func (p PgExpRepo) SettleDebt(
ctx context.Context, eId int32, payment mod.Payment, credit mod.Payment) error {
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
err := p.insertPayment(ctx, q, eId, payment)
if err != nil {
return err
}
err = p.updatePayment(ctx, q, credit)
if err != nil {
return err
}
return nil // yeah we could just return the last op, but I like this way
})
}
+9 -5
View File
@@ -26,14 +26,18 @@ func (p PgExpRepo) Close() {
p.DB.Close()
}
func (r *PgExpRepo) withTx(ctx context.Context, fn func(q *pgsqlc.Queries) error) error {
tx, err := r.DB.BeginTx(ctx, pgx.TxOptions{})
func withTx(
pgPool *pgxpool.Pool,
ctx context.Context,
fn func(q *pgsqlc.Queries) error) error {
tx, err := pgPool.BeginTx(ctx, pgx.TxOptions{})
if err != nil {
return err
}
defer tx.Rollback(ctx)
q := pgsqlc.New(tx) // bind sqlc to this tx
q := pgsqlc.New(tx)
if err := fn(q); err != nil {
return err
@@ -79,7 +83,7 @@ func (p PgExpRepo) Update(
ctx context.Context,
exp mod.Expense) error {
return p.withTx(ctx, func(q *pgsqlc.Queries) error {
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
value, err := decimalToNumeric(exp.Value)
if err != nil {
return err
@@ -157,7 +161,7 @@ func (p PgExpRepo) Insert(
ctx context.Context,
exp mod.Expense) error {
return p.withTx(ctx, func(q *pgsqlc.Queries) error {
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
value, err := decimalToNumeric(exp.Value)
if err != nil {
return err