Updating UserCircle points when completing a chore

This commit is contained in:
Mo Tarbin 2024-12-31 02:19:02 -05:00
parent 13d6cfd7e7
commit 33801b5004
2 changed files with 17 additions and 7 deletions

View file

@ -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,

View file

@ -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 {