Fix: Update amount of paired transactions

when updating one transaction to keep budget properly in-sync

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-02-03 00:12:55 +01:00
parent 91d2447728
commit 217ddbbf44
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E

View file

@ -480,7 +480,26 @@ func (c *Client) UpdateTransaction(txID uuid.UUID, tx Transaction) (err error) {
return fmt.Errorf("validating transaction: %w", err) 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 { }); err != nil {
return fmt.Errorf("updating transaction: %w", err) return fmt.Errorf("updating transaction: %w", err)
} }