update updateAssignee to retrieve the current user and use their ID as the UpdatedBy field.

This commit is contained in:
Mo Tarbin 2024-08-10 14:28:24 -04:00
parent bddb80b134
commit 187078ffe8
2 changed files with 12 additions and 6 deletions

View file

@ -594,6 +594,13 @@ func (h *Handler) deleteChore(c *gin.Context) {
// } // }
func (h *Handler) updateAssignee(c *gin.Context) { func (h *Handler) updateAssignee(c *gin.Context) {
currentUser, ok := auth.CurrentUser(c)
if !ok {
c.JSON(500, gin.H{
"error": "Error getting current user",
})
return
}
rawID := c.Param("id") rawID := c.Param("id")
id, err := strconv.Atoi(rawID) id, err := strconv.Atoi(rawID)
if err != nil { if err != nil {
@ -603,8 +610,7 @@ func (h *Handler) updateAssignee(c *gin.Context) {
return return
} }
type AssigneeReq struct { type AssigneeReq struct {
AssignedTo int `json:"assignedTo" binding:"required"` Assignee int `json:"assignee" binding:"required"`
UpdatedBy int `json:"updatedBy" binding:"required"`
} }
var assigneeReq AssigneeReq var assigneeReq AssigneeReq
@ -626,7 +632,7 @@ func (h *Handler) updateAssignee(c *gin.Context) {
assigneeFound := false assigneeFound := false
for _, assignee := range chore.Assignees { for _, assignee := range chore.Assignees {
if assignee.UserID == assigneeReq.AssignedTo { if assignee.UserID == assigneeReq.Assignee {
assigneeFound = true assigneeFound = true
break break
} }
@ -638,8 +644,8 @@ func (h *Handler) updateAssignee(c *gin.Context) {
return return
} }
chore.UpdatedBy = assigneeReq.UpdatedBy chore.UpdatedBy = currentUser.ID
chore.AssignedTo = assigneeReq.AssignedTo chore.AssignedTo = assigneeReq.Assignee
if err := h.choreRepo.UpsertChore(c, chore); err != nil { if err := h.choreRepo.UpsertChore(c, chore); err != nil {
c.JSON(500, gin.H{ c.JSON(500, gin.H{
"error": "Error updating assignee", "error": "Error updating assignee",

View file

@ -102,7 +102,7 @@ func (h *Handler) signUp(c *gin.Context) {
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
}); err != nil { }); err != nil {
c.JSON(500, gin.H{ c.JSON(500, gin.H{
"error": "Error creating user", "error": "Error creating user, email already exists or username is taken",
}) })
return return
} }