Add history entry and fix bug when completing task (#54)
The reward wasn't properly being distributed to all users Reviewed-on: #54
This commit is contained in:
parent
b5aae3be3d
commit
9cb71d53cf
@ -567,7 +567,8 @@ func TestCompleteTask(t *testing.T) {
|
|||||||
|
|
||||||
// Verify the task is marked as completed
|
// Verify the task is marked as completed
|
||||||
e.GET("/task/" + strconv.Itoa(taskId)).Expect().Status(404)
|
e.GET("/task/" + strconv.Itoa(taskId)).Expect().Status(404)
|
||||||
// Verify the allowances are updated
|
|
||||||
|
// Verify the allowances are updated for user 1
|
||||||
allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
|
allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
|
||||||
allowances.Length().IsEqual(3)
|
allowances.Length().IsEqual(3)
|
||||||
allowances.Value(0).Object().Value("id").Number().IsEqual(0)
|
allowances.Value(0).Object().Value("id").Number().IsEqual(0)
|
||||||
@ -576,6 +577,21 @@ func TestCompleteTask(t *testing.T) {
|
|||||||
allowances.Value(1).Object().Value("progress").Number().IsEqual(50)
|
allowances.Value(1).Object().Value("progress").Number().IsEqual(50)
|
||||||
allowances.Value(2).Object().Value("id").Number().IsEqual(2)
|
allowances.Value(2).Object().Value("id").Number().IsEqual(2)
|
||||||
allowances.Value(2).Object().Value("progress").Number().IsEqual(25)
|
allowances.Value(2).Object().Value("progress").Number().IsEqual(25)
|
||||||
|
|
||||||
|
// And also for user 2
|
||||||
|
allowances = e.GET("/user/2/allowance").Expect().Status(200).JSON().Array()
|
||||||
|
allowances.Length().IsEqual(1)
|
||||||
|
allowances.Value(0).Object().Value("id").Number().IsEqual(0)
|
||||||
|
allowances.Value(0).Object().Value("progress").Number().IsEqual(101)
|
||||||
|
|
||||||
|
for userId := 1; userId <= 2; userId++ {
|
||||||
|
userIdStr := strconv.Itoa(userId)
|
||||||
|
// Ensure the history got updated
|
||||||
|
history := e.GET("/user/" + userIdStr + "/history").Expect().Status(200).JSON().Array()
|
||||||
|
history.Length().IsEqual(1)
|
||||||
|
history.Value(0).Object().Value("allowance").Number().IsEqual(101)
|
||||||
|
history.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
|
func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
|
||||||
|
@ -341,10 +341,22 @@ func (db *Db) CompleteTask(taskId int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the history entry
|
||||||
|
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
|
||||||
|
Bind(userId, time.Now().Unix(), reward).
|
||||||
|
Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the sums of all weights
|
||||||
var sumOfWeights float64
|
var sumOfWeights float64
|
||||||
err = tx.Query("select sum(weight) from allowances where user_id = ? and weight > 0").Bind(userId).ScanSingle(&sumOfWeights)
|
err = tx.Query("select sum(weight) from allowances where user_id = ? and weight > 0").Bind(userId).ScanSingle(&sumOfWeights)
|
||||||
sumOfWeights += userWeight
|
sumOfWeights += userWeight
|
||||||
|
|
||||||
|
remainingReward := reward
|
||||||
|
|
||||||
|
// Distribute the reward to the allowances
|
||||||
for allowanceRow := range tx.Query("select id, weight from allowances where user_id = ? and weight > 0").Bind(userId).Range(&err) {
|
for allowanceRow := range tx.Query("select id, weight from allowances where user_id = ? and weight > 0").Bind(userId).Range(&err) {
|
||||||
var allowanceId int
|
var allowanceId int
|
||||||
var allowanceWeight float64
|
var allowanceWeight float64
|
||||||
@ -354,19 +366,19 @@ func (db *Db) CompleteTask(taskId int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the amount to add to the allowance
|
// Calculate the amount to add to the allowance
|
||||||
amount := int((allowanceWeight / sumOfWeights) * float64(reward))
|
amount := int((allowanceWeight / sumOfWeights) * float64(remainingReward))
|
||||||
sumOfWeights -= allowanceWeight
|
sumOfWeights -= allowanceWeight
|
||||||
err = tx.Query("update allowances set balance = balance + ? where id = ? and user_id = ?").
|
err = tx.Query("update allowances set balance = balance + ? where id = ? and user_id = ?").
|
||||||
Bind(amount, allowanceId, userId).Exec()
|
Bind(amount, allowanceId, userId).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reward -= amount
|
remainingReward -= amount
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the remaining reward to the user
|
// Add the remaining reward to the user
|
||||||
err = tx.Query("update users set balance = balance + ? where id = ?").
|
err = tx.Query("update users set balance = balance + ? where id = ?").
|
||||||
Bind(reward, userId).Exec()
|
Bind(remainingReward, userId).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user