From 3e0b68bbffa275ad2b9484619a878c27dc2527a7 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sat, 18 Jan 2025 13:11:11 -0500 Subject: [PATCH] Update CompleteChore to deduct point properly --- internal/chore/repo/repository.go | 36 +++++++++++++++++-------------- internal/circle/handler.go | 2 +- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/internal/chore/repo/repository.go b/internal/chore/repo/repository.go index 014674e..205af9a 100644 --- a/internal/chore/repo/repository.go +++ b/internal/chore/repo/repository.go @@ -97,6 +97,18 @@ func (r *ChoreRepository) IsChoreOwner(c context.Context, choreID int, userID in func (r *ChoreRepository) CompleteChore(c context.Context, chore *chModel.Chore, note *string, userID int, dueDate *time.Time, completedDate *time.Time, nextAssignedTo int, applyPoints bool) error { err := r.db.WithContext(c).Transaction(func(tx *gorm.DB) error { + + choreUpdates := map[string]interface{}{} + choreUpdates["next_due_date"] = dueDate + choreUpdates["status"] = chModel.ChoreStatusNoStatus + + if dueDate != nil { + choreUpdates["assigned_to"] = nextAssignedTo + } else { + // one time task + choreUpdates["is_active"] = false + } + // Create a new chore history record. ch := &chModel.ChoreHistory{ ChoreID: chore.ID, CompletedAt: completedDate, @@ -105,30 +117,22 @@ func (r *ChoreRepository) CompleteChore(c context.Context, chore *chModel.Chore, DueDate: chore.NextDueDate, Note: note, } - if err := tx.Create(ch).Error; err != nil { - return err - } - updates := map[string]interface{}{} - updates["next_due_date"] = dueDate - updates["status"] = chModel.ChoreStatusNoStatus - if dueDate != nil { - updates["assigned_to"] = nextAssignedTo - } else { - // one time task - updates["is_active"] = false - } - // Perform the update operation once, using the prepared updates map. - if err := tx.Model(&chModel.Chore{}).Where("id = ?", chore.ID).Updates(updates).Error; err != nil { - return err - } // Update UserCirclee Points : if applyPoints && chore.Points != nil && *chore.Points > 0 { + ch.Points = chore.Points if err := tx.Model(&cModel.UserCircle{}).Where("user_id = ? AND circle_id = ?", userID, chore.CircleID).Update("points", gorm.Expr("points + ?", chore.Points)).Error; err != nil { return err } } + // Perform the update operation once, using the prepared updates map. + if err := tx.Model(&chModel.Chore{}).Where("id = ?", chore.ID).Updates(choreUpdates).Error; err != nil { + return err + } + if err := tx.Create(ch).Error; err != nil { + return err + } return nil }) return err diff --git a/internal/circle/handler.go b/internal/circle/handler.go index c878971..cd8d346 100644 --- a/internal/circle/handler.go +++ b/internal/circle/handler.go @@ -519,7 +519,7 @@ func (h *Handler) RedeemPoints(c *gin.Context) { return } - err = h.circleRepo.RedeemPoints(c, currentUser.CircleID, currentUser.ID, redeemReq.Points, currentUser.ID) + err = h.circleRepo.RedeemPoints(c, currentUser.CircleID, redeemReq.UserID, redeemReq.Points, currentUser.ID) if err != nil { log.Error("Error redeeming points:", err) c.JSON(500, gin.H{