From 217ddbbf44f821c3a8042a2d3a334a15a03da5ec Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 3 Feb 2024 00:12:55 +0100 Subject: [PATCH] Fix: Update amount of paired transactions when updating one transaction to keep budget properly in-sync Signed-off-by: Knut Ahlers --- pkg/database/database.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/database/database.go b/pkg/database/database.go index b7506b7..08ad640 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -480,7 +480,26 @@ func (c *Client) UpdateTransaction(txID uuid.UUID, tx Transaction) (err error) { return fmt.Errorf("validating transaction: %w", err) } - return db.Save(&tx).Error + if err = db.Save(&tx).Error; err != nil { + return fmt.Errorf("saving transaction: %w", err) + } + + if !oldTX.PairKey.Valid || tx.Amount == oldTX.Amount { + // is not a paired transaction or amount did not change: skip rest + return nil + } + + // transaction is paired and amount changed, we need to update the + // paired transaction too or it will cause trouble + + if err = db.Model(&Transaction{}). + Where("pair_key = ?", oldTX.PairKey.UUID). + Update("amount", tx.Amount). + Error; err != nil { + return fmt.Errorf("updating amount for paired transaction: %w", err) + } + + return nil }); err != nil { return fmt.Errorf("updating transaction: %w", err) }