Add ability to subtract allowances (#108)
All checks were successful
Backend Build and Test / build (push) Successful in 3m38s
Backend Deploy / build (push) Successful in 1m53s

Closes #104

Reviewed-on: #108
This commit was merged in pull request #108.
This commit is contained in:
2025-05-27 17:02:14 +02:00
parent 2714f550a4
commit a0d0c37fdb
2 changed files with 111 additions and 24 deletions

View File

@@ -562,11 +562,39 @@ func (db *Db) AddAllowanceAmount(userId int, allowanceId int, request AddAllowan
}
if allowanceId == 0 {
if remainingAmount < 0 {
var userBalance int
err = tx.Query("select balance from users where id = ?").
Bind(userId).ScanSingle(&userBalance)
if err != nil {
return err
}
if remainingAmount > userBalance {
return fmt.Errorf("cannot remove more than the current balance: %d", userBalance)
}
}
err = tx.Query("update users set balance = balance + ? where id = ?").
Bind(remainingAmount, userId).Exec()
if err != nil {
return err
}
} else if remainingAmount < 0 {
var progress int
err = tx.Query("select balance from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&progress)
if err != nil {
return err
}
if remainingAmount > progress {
return fmt.Errorf("cannot remove more than the current allowance balance: %d", progress)
}
err = tx.Query("update allowances set balance = balance + ? where id = ? and user_id = ?").
Bind(remainingAmount, allowanceId, userId).Exec()
if err != nil {
return err
}
} else {
// Fetch the target and progress of the specified allowance
var target, progress int