chore: Update chore detail query to include notes in result
This commit is contained in:
parent
c92cdb427d
commit
010db330a4
|
@ -802,8 +802,16 @@ func (h *Handler) completeChore(c *gin.Context) {
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var nextDueDate *time.Time
|
||||||
nextDueDate, err := scheduleNextDueDate(chore, completedDate)
|
if chore.FrequencyType == "adaptive" {
|
||||||
|
history, err := h.choreRepo.GetChoreHistoryWithLimit(c, chore.ID, 5)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, gin.H{
|
||||||
|
"error": "Error getting chore history",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nextDueDate, err = scheduleAdaptiveNextDueDate(chore, completedDate, history)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error scheduling next due date: %s", err)
|
log.Printf("Error scheduling next due date: %s", err)
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
|
@ -811,6 +819,17 @@ func (h *Handler) completeChore(c *gin.Context) {
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
nextDueDate, err = scheduleNextDueDate(chore, completedDate)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error scheduling next due date: %s", err)
|
||||||
|
c.JSON(500, gin.H{
|
||||||
|
"error": "Error scheduling next due date",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
choreHistory, err := h.choreRepo.GetChoreHistory(c, chore.ID)
|
choreHistory, err := h.choreRepo.GetChoreHistory(c, chore.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
|
|
|
@ -80,5 +80,6 @@ type ChoreDetail struct {
|
||||||
LastCompletedDate *time.Time `json:"lastCompletedDate" gorm:"column:last_completed_date"`
|
LastCompletedDate *time.Time `json:"lastCompletedDate" gorm:"column:last_completed_date"`
|
||||||
LastCompletedBy *int `json:"lastCompletedBy" gorm:"column:last_completed_by"`
|
LastCompletedBy *int `json:"lastCompletedBy" gorm:"column:last_completed_by"`
|
||||||
TotalCompletedCount int `json:"totalCompletedCount" gorm:"column:total_completed"`
|
TotalCompletedCount int `json:"totalCompletedCount" gorm:"column:total_completed"`
|
||||||
|
Notes *string `json:"notes" gorm:"column:notes"`
|
||||||
CreatedBy int `json:"createdBy" gorm:"column:created_by"`
|
CreatedBy int `json:"createdBy" gorm:"column:created_by"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -234,6 +234,7 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir
|
||||||
chores.assigned_to,
|
chores.assigned_to,
|
||||||
chores.created_by,
|
chores.created_by,
|
||||||
recent_history.last_completed_date,
|
recent_history.last_completed_date,
|
||||||
|
recent_history.notes,
|
||||||
recent_history.last_assigned_to as last_completed_by,
|
recent_history.last_assigned_to as last_completed_by,
|
||||||
COUNT(chore_histories.id) as total_completed`).
|
COUNT(chore_histories.id) as total_completed`).
|
||||||
Joins("LEFT JOIN chore_histories ON chores.id = chore_histories.chore_id").
|
Joins("LEFT JOIN chore_histories ON chores.id = chore_histories.chore_id").
|
||||||
|
@ -241,7 +242,9 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir
|
||||||
SELECT
|
SELECT
|
||||||
chore_id,
|
chore_id,
|
||||||
assigned_to AS last_assigned_to,
|
assigned_to AS last_assigned_to,
|
||||||
completed_at AS last_completed_date
|
completed_at AS last_completed_date,
|
||||||
|
notes
|
||||||
|
|
||||||
FROM chore_histories
|
FROM chore_histories
|
||||||
WHERE (chore_id, completed_at) IN (
|
WHERE (chore_id, completed_at) IN (
|
||||||
SELECT chore_id, MAX(completed_at)
|
SELECT chore_id, MAX(completed_at)
|
||||||
|
@ -250,7 +253,7 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir
|
||||||
)
|
)
|
||||||
) AS recent_history ON chores.id = recent_history.chore_id`).
|
) AS recent_history ON chores.id = recent_history.chore_id`).
|
||||||
Where("chores.id = ? and chores.circle_id = ?", choreID, circleID).
|
Where("chores.id = ? and chores.circle_id = ?", choreID, circleID).
|
||||||
Group("chores.id, recent_history.last_completed_date, recent_history.last_assigned_to").
|
Group("chores.id, recent_history.last_completed_date, recent_history.last_assigned_to, recent_history.notes").
|
||||||
First(&choreDetail).Error; err != nil {
|
First(&choreDetail).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ func scheduleNextDueDate(chore *chModel.Chore, completedDate time.Time) (*time.T
|
||||||
} else if chore.FrequencyType == "yearly" {
|
} else if chore.FrequencyType == "yearly" {
|
||||||
nextDueDate = baseDate.AddDate(1, 0, 0)
|
nextDueDate = baseDate.AddDate(1, 0, 0)
|
||||||
} else if chore.FrequencyType == "adaptive" {
|
} else if chore.FrequencyType == "adaptive" {
|
||||||
|
|
||||||
// TODO: calculate next due date based on the history of the chore
|
// TODO: calculate next due date based on the history of the chore
|
||||||
// calculate the difference between the due date and now in days:
|
// calculate the difference between the due date and now in days:
|
||||||
diff := completedDate.UTC().Sub(chore.NextDueDate.UTC())
|
diff := completedDate.UTC().Sub(chore.NextDueDate.UTC())
|
||||||
|
@ -129,6 +130,33 @@ func scheduleNextDueDate(chore *chModel.Chore, completedDate time.Time) (*time.T
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func scheduleAdaptiveNextDueDate(chore *chModel.Chore, completedDate time.Time, history []*chModel.ChoreHistory) (*time.Time, error) {
|
||||||
|
// will generate due date base on history and the different between the completed date and the due date
|
||||||
|
// the more recent the higher weight
|
||||||
|
if len(history) <= 1 {
|
||||||
|
if chore.NextDueDate != nil {
|
||||||
|
diff := completedDate.UTC().Sub(chore.NextDueDate.UTC())
|
||||||
|
nextDueDate := completedDate.UTC().Add(diff)
|
||||||
|
return &nextDueDate, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var weight float64
|
||||||
|
var totalWeight float64
|
||||||
|
var nextDueDate time.Time
|
||||||
|
for i := 0; i < len(history)-1; i++ {
|
||||||
|
delay := history[i].CompletedAt.UTC().Sub(history[i+1].CompletedAt.UTC()).Seconds()
|
||||||
|
weight = delay * float64(len(history)-i)
|
||||||
|
totalWeight += weight
|
||||||
|
}
|
||||||
|
// calculate the average delay
|
||||||
|
averageDelay := totalWeight / float64(len(history)-1)
|
||||||
|
// calculate the difference between the completed date and the due date
|
||||||
|
nextDueDate = completedDate.UTC().Add(time.Duration(averageDelay) * time.Second)
|
||||||
|
|
||||||
|
return &nextDueDate, nil
|
||||||
|
}
|
||||||
|
|
||||||
func RemoveAssigneeAndReassign(chore *chModel.Chore, userID int) {
|
func RemoveAssigneeAndReassign(chore *chModel.Chore, userID int) {
|
||||||
for i, assignee := range chore.Assignees {
|
for i, assignee := range chore.Assignees {
|
||||||
if assignee.UserID == userID {
|
if assignee.UserID == userID {
|
||||||
|
|
Loading…
Reference in New Issue