From 9cb71d53cf02873464ad41a3f25da90e808c4992 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sun, 18 May 2025 08:31:39 +0200 Subject: [PATCH] Add history entry and fix bug when completing task (#54) The reward wasn't properly being distributed to all users Reviewed-on: https://gitea.seeseepuff.be/seeseemelk/allowance_planner_2000/pulls/54 --- backend/api_test.go | 18 +++++++++++++++++- backend/db.go | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/backend/api_test.go b/backend/api_test.go index 86abe14..33e7082 100644 --- a/backend/api_test.go +++ b/backend/api_test.go @@ -567,7 +567,8 @@ func TestCompleteTask(t *testing.T) { // Verify the task is marked as completed 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.Length().IsEqual(3) 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(2).Object().Value("id").Number().IsEqual(2) 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) { diff --git a/backend/db.go b/backend/db.go index dbf8436..7f9feff 100644 --- a/backend/db.go +++ b/backend/db.go @@ -341,10 +341,22 @@ func (db *Db) CompleteTask(taskId int) error { 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 err = tx.Query("select sum(weight) from allowances where user_id = ? and weight > 0").Bind(userId).ScanSingle(&sumOfWeights) 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) { var allowanceId int var allowanceWeight float64 @@ -354,19 +366,19 @@ func (db *Db) CompleteTask(taskId int) error { } // Calculate the amount to add to the allowance - amount := int((allowanceWeight / sumOfWeights) * float64(reward)) + amount := int((allowanceWeight / sumOfWeights) * float64(remainingReward)) sumOfWeights -= allowanceWeight err = tx.Query("update allowances set balance = balance + ? where id = ? and user_id = ?"). Bind(amount, allowanceId, userId).Exec() if err != nil { return err } - reward -= amount + remainingReward -= amount } // Add the remaining reward to the user err = tx.Query("update users set balance = balance + ? where id = ?"). - Bind(reward, userId).Exec() + Bind(remainingReward, userId).Exec() if err != nil { return err }