Add support for adding allowance to ID=0 (#106)
Closes #102 Reviewed-on: #106
This commit is contained in:
parent
344f7a7eef
commit
2714f550a4
@ -764,7 +764,6 @@ func TestAddAllowanceSimple(t *testing.T) {
|
|||||||
createTestAllowance(e, "Test Allowance 1", 1000, 1)
|
createTestAllowance(e, "Test Allowance 1", 1000, 1)
|
||||||
|
|
||||||
request := map[string]interface{}{
|
request := map[string]interface{}{
|
||||||
"id": 1,
|
|
||||||
"amount": 10,
|
"amount": 10,
|
||||||
"description": "Added to allowance 1",
|
"description": "Added to allowance 1",
|
||||||
}
|
}
|
||||||
@ -783,6 +782,30 @@ func TestAddAllowanceSimple(t *testing.T) {
|
|||||||
history.Value(0).Object().Value("description").String().IsEqual("Added to allowance 1")
|
history.Value(0).Object().Value("description").String().IsEqual("Added to allowance 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddAllowanceIdZero(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
|
||||||
|
createTestAllowance(e, "Test Allowance 1", 1000, 1)
|
||||||
|
|
||||||
|
request := map[string]interface{}{
|
||||||
|
"amount": 10,
|
||||||
|
"description": "Added to allowance 1",
|
||||||
|
}
|
||||||
|
e.POST("/user/1/allowance/0/add").WithJSON(request).Expect().Status(200)
|
||||||
|
|
||||||
|
// Verify the allowance is updated
|
||||||
|
allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
|
||||||
|
allowances.Value(0).Object().Value("id").Number().IsEqual(0)
|
||||||
|
allowances.Value(0).Object().Value("progress").Number().InDelta(10.0, 0.01)
|
||||||
|
|
||||||
|
// Verify the history is updated
|
||||||
|
history := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
|
||||||
|
history.Length().IsEqual(1)
|
||||||
|
history.Value(0).Object().Value("allowance").Number().InDelta(10.0, 0.01)
|
||||||
|
history.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
||||||
|
history.Value(0).Object().Value("description").String().IsEqual("Added to allowance 1")
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddAllowanceWithSpillage(t *testing.T) {
|
func TestAddAllowanceWithSpillage(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
|
|
||||||
@ -791,7 +814,6 @@ func TestAddAllowanceWithSpillage(t *testing.T) {
|
|||||||
e.PUT("/user/1/allowance/0").WithJSON(UpdateAllowanceRequest{Weight: 1}).Expect().Status(200)
|
e.PUT("/user/1/allowance/0").WithJSON(UpdateAllowanceRequest{Weight: 1}).Expect().Status(200)
|
||||||
|
|
||||||
request := map[string]interface{}{
|
request := map[string]interface{}{
|
||||||
"id": 1,
|
|
||||||
"amount": 10,
|
"amount": 10,
|
||||||
"description": "Added to allowance 1",
|
"description": "Added to allowance 1",
|
||||||
}
|
}
|
||||||
|
@ -561,6 +561,13 @@ func (db *Db) AddAllowanceAmount(userId int, allowanceId int, request AddAllowan
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if allowanceId == 0 {
|
||||||
|
err = tx.Query("update users set balance = balance + ? where id = ?").
|
||||||
|
Bind(remainingAmount, userId).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// Fetch the target and progress of the specified allowance
|
// Fetch the target and progress of the specified allowance
|
||||||
var target, progress int
|
var target, progress int
|
||||||
err = tx.Query("select target, balance from allowances where id = ? and user_id = ?").
|
err = tx.Query("select target, balance from allowances where id = ? and user_id = ?").
|
||||||
@ -592,6 +599,7 @@ func (db *Db) AddAllowanceAmount(userId int, allowanceId int, request AddAllowan
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user