From 33801b5004b70b6ba9a925e45ad6b273594a3f1d Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Tue, 31 Dec 2024 02:19:02 -0500 Subject: [PATCH] Updating UserCircle points when completing a chore --- internal/chore/repo/repository.go | 8 ++++++++ internal/circle/model/model.go | 16 +++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/chore/repo/repository.go b/internal/chore/repo/repository.go index 3cc28c0..1643f84 100644 --- a/internal/chore/repo/repository.go +++ b/internal/chore/repo/repository.go @@ -8,6 +8,7 @@ import ( config "donetick.com/core/config" chModel "donetick.com/core/internal/chore/model" + cModel "donetick.com/core/internal/circle/model" "gorm.io/gorm" ) @@ -118,6 +119,12 @@ func (r *ChoreRepository) CompleteChore(c context.Context, chore *chModel.Chore, if err := tx.Model(&chModel.Chore{}).Where("id = ?", chore.ID).Updates(updates).Error; err != nil { return err } + // Update UserCirclee Points : + if chore.Points != nil && *chore.Points > 0 { + if err := tx.Debug().Model(&cModel.UserCircle{}).Where("user_id = ? AND circle_id = ?", userID, chore.CircleID).Update("points", gorm.Expr("points + ?", chore.Points)).Error; err != nil { + return err + } + } return nil }) @@ -270,6 +277,7 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir chores.assigned_to, chores.created_by, chores.priority, + chores.completion_window, recent_history.last_completed_date, recent_history.notes, recent_history.last_assigned_to as last_completed_by, diff --git a/internal/circle/model/model.go b/internal/circle/model/model.go index e731f1e..284c412 100644 --- a/internal/circle/model/model.go +++ b/internal/circle/model/model.go @@ -22,13 +22,15 @@ type CircleDetail struct { } type UserCircle struct { - ID int `json:"id" gorm:"primary_key"` // Unique identifier - UserID int `json:"userId" gorm:"column:user_id"` // User ID - CircleID int `json:"circleId" gorm:"column:circle_id"` // Circle ID - Role string `json:"role" gorm:"column:role"` // Role - IsActive bool `json:"isActive" gorm:"column:is_active;default:false"` - CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"` // Created at - UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at"` // Updated at + ID int `json:"id" gorm:"primary_key"` // Unique identifier + UserID int `json:"userId" gorm:"column:user_id"` // User ID + CircleID int `json:"circleId" gorm:"column:circle_id"` // Circle ID + Role string `json:"role" gorm:"column:role"` // Role + IsActive bool `json:"isActive" gorm:"column:is_active;default:false"` + CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"` // Created at + UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at"` // Updated at + Points int `json:"points" gorm:"column:points;default:0;not null"` // Points + PointsRedeemed int `json:"pointsRedeemed" gorm:"column:points_redeemed;default:0;not null"` // Points Redeemed } type UserCircleDetail struct {