Add support for completion window in chore creation and editing

This commit is contained in:
Mo Tarbin 2024-12-31 02:18:42 -05:00
parent 27a47a0c1f
commit 13d6cfd7e7
2 changed files with 15 additions and 1 deletions

View file

@ -54,6 +54,7 @@ type ChoreReq struct {
LabelsV2 *[]LabelReq `json:"labelsV2"` LabelsV2 *[]LabelReq `json:"labelsV2"`
ThingTrigger *ThingTrigger `json:"thingTrigger"` ThingTrigger *ThingTrigger `json:"thingTrigger"`
Points *int `json:"points"` Points *int `json:"points"`
CompletionWindow *int `json:"completionWindow"`
} }
type Handler struct { type Handler struct {
choreRepo *chRepo.ChoreRepository choreRepo *chRepo.ChoreRepository
@ -279,6 +280,7 @@ func (h *Handler) createChore(c *gin.Context) {
CreatedAt: time.Now().UTC(), CreatedAt: time.Now().UTC(),
CircleID: currentUser.CircleID, CircleID: currentUser.CircleID,
Points: choreReq.Points, Points: choreReq.Points,
CompletionWindow: choreReq.CompletionWindow,
} }
id, err := h.choreRepo.CreateChore(c, createdChore) id, err := h.choreRepo.CreateChore(c, createdChore)
createdChore.ID = id createdChore.ID = id
@ -539,6 +541,7 @@ func (h *Handler) editChore(c *gin.Context) {
CreatedBy: oldChore.CreatedBy, CreatedBy: oldChore.CreatedBy,
CreatedAt: oldChore.CreatedAt, CreatedAt: oldChore.CreatedAt,
Points: choreReq.Points, Points: choreReq.Points,
CompletionWindow: choreReq.CompletionWindow,
} }
if err := h.choreRepo.UpsertChore(c, updatedChore); err != nil { if err := h.choreRepo.UpsertChore(c, updatedChore); err != nil {
c.JSON(500, gin.H{ c.JSON(500, gin.H{
@ -963,6 +966,16 @@ func (h *Handler) completeChore(c *gin.Context) {
}) })
return return
} }
// confirm that the chore in completion window:
if chore.CompletionWindow != nil {
if completedDate.After(chore.NextDueDate.Add(time.Hour * time.Duration(*chore.CompletionWindow))) {
c.JSON(400, gin.H{
"error": "Chore is out of completion window",
})
return
}
}
var nextDueDate *time.Time var nextDueDate *time.Time
if chore.FrequencyType == "adaptive" { if chore.FrequencyType == "adaptive" {
history, err := h.choreRepo.GetChoreHistoryWithLimit(c, chore.ID, 5) history, err := h.choreRepo.GetChoreHistoryWithLimit(c, chore.ID, 5)
@ -1227,7 +1240,7 @@ func (h *Handler) getChoresHistory(c *gin.Context) {
}) })
return return
} }
includeCircleRaw := c.Query("all") includeCircleRaw := c.Query("members")
includeCircle := false includeCircle := false
if includeCircleRaw == "true" { if includeCircleRaw == "true" {
includeCircle = true includeCircle = true

View file

@ -109,6 +109,7 @@ type ChoreDetail struct {
Priority int `json:"priority" gorm:"column:priority"` Priority int `json:"priority" gorm:"column:priority"`
Notes *string `json:"notes" gorm:"column:notes"` Notes *string `json:"notes" gorm:"column:notes"`
CreatedBy int `json:"createdBy" gorm:"column:created_by"` CreatedBy int `json:"createdBy" gorm:"column:created_by"`
CompletionWindow *int `json:"completionWindow,omitempty" gorm:"column:completion_window"`
} }
type Label struct { type Label struct {