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:
parent
91d2447728
commit
217ddbbf44
1 changed files with 20 additions and 1 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue