From 1e8cd044b42cec76fe1e48828b6f304937bf3115 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 8 Sep 2025 14:23:24 +0100 Subject: [PATCH] debt settling with transaction --- src/TODO.md | 9 +++------ src/expenses/expenseDebts.go | 16 +++++++++------- src/expenses/repo/expenseRepo.go.go | 4 ++++ src/expenses/repo/pg_expenseDebtRepo.go | 25 +++++++++++++++++++++++++ src/expenses/repo/pg_expenseRepo.go | 14 +++++++++----- 5 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 src/expenses/repo/pg_expenseDebtRepo.go diff --git a/src/TODO.md b/src/TODO.md index a59a86f..7745020 100644 --- a/src/TODO.md +++ b/src/TODO.md @@ -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 diff --git a/src/expenses/expenseDebts.go b/src/expenses/expenseDebts.go index 31a844d..463a644 100644 --- a/src/expenses/expenseDebts.go +++ b/src/expenses/expenseDebts.go @@ -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 { diff --git a/src/expenses/repo/expenseRepo.go.go b/src/expenses/repo/expenseRepo.go.go index 6c883ec..0664954 100644 --- a/src/expenses/repo/expenseRepo.go.go +++ b/src/expenses/repo/expenseRepo.go.go @@ -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() } diff --git a/src/expenses/repo/pg_expenseDebtRepo.go b/src/expenses/repo/pg_expenseDebtRepo.go new file mode 100644 index 0000000..8fa9eda --- /dev/null +++ b/src/expenses/repo/pg_expenseDebtRepo.go @@ -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 + }) +} diff --git a/src/expenses/repo/pg_expenseRepo.go b/src/expenses/repo/pg_expenseRepo.go index 4a56cc6..1c6a023 100644 --- a/src/expenses/repo/pg_expenseRepo.go +++ b/src/expenses/repo/pg_expenseRepo.go @@ -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