Update CompleteChore to deduct point properly
This commit is contained in:
commit
b7ce79a21c
2 changed files with 21 additions and 17 deletions
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
Loading…
Add table
Reference in a new issue