Add validation to deleteThing endpoint to prevent deletion of things with associated tasks

This commit is contained in:
Mo Tarbin 2024-07-02 01:40:09 -04:00
parent e40c2a84cd
commit 6845fd54f1
2 changed files with 13 additions and 0 deletions

View File

@ -48,6 +48,9 @@ func (n *NotificationPlanner) GenerateNotifications(c context.Context, chore *ch
var mt *chModel.NotificationMetadata
if err := json.Unmarshal([]byte(*chore.NotificationMetadata), &mt); err != nil {
log.Error("Error unmarshalling notification metadata", err)
return false
}
if chore.NextDueDate == nil {
return true
}
if mt.DueDate {

View File

@ -261,6 +261,16 @@ func (h *Handler) DeleteThing(c *gin.Context) {
c.JSON(403, gin.H{"error": "Forbidden"})
return
}
// confirm there are no chores associated with the thing:
thingChores, err := h.tRepo.GetThingChoresByThingId(c, thing.ID)
if err != nil {
c.JSON(500, gin.H{"error": "Unable to find tasks linked to this thing"})
return
}
if len(thingChores) > 0 {
c.JSON(405, gin.H{"error": "Unable to delete thing with associated tasks"})
return
}
if err := h.tRepo.DeleteThing(c, thingID); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return