Add event for updating things

This commit is contained in:
Mo Tarbin 2025-02-10 02:03:37 -05:00
parent 80afab3bc0
commit b1036730c4
2 changed files with 32 additions and 11 deletions

View file

@ -31,6 +31,7 @@ const (
EventTypeTaskCompleted EventType = "task.completed" EventTypeTaskCompleted EventType = "task.completed"
EventTypeTaskReassigned EventType = "task.reassigned" EventTypeTaskReassigned EventType = "task.reassigned"
EventTypeTaskSkipped EventType = "task.skipped" EventTypeTaskSkipped EventType = "task.skipped"
EventTypeThingChanged EventType = "thing.changed"
) )
type Event struct { type Event struct {
@ -162,3 +163,12 @@ func (p *EventsProducer) NotificationEvent(ctx context.Context, url string, even
Data: event, Data: event,
}) })
} }
func (p *EventsProducer) ThingsUpdated(ctx context.Context, url string, data interface{}) {
p.publishEvent(Event{
URL: url,
Type: EventTypeThingChanged,
Timestamp: time.Now(),
Data: data,
})
}

View file

@ -7,6 +7,7 @@ import (
auth "donetick.com/core/internal/authorization" auth "donetick.com/core/internal/authorization"
chRepo "donetick.com/core/internal/chore/repo" chRepo "donetick.com/core/internal/chore/repo"
cRepo "donetick.com/core/internal/circle/repo" cRepo "donetick.com/core/internal/circle/repo"
"donetick.com/core/internal/events"
nRepo "donetick.com/core/internal/notifier/repo" nRepo "donetick.com/core/internal/notifier/repo"
nps "donetick.com/core/internal/notifier/service" nps "donetick.com/core/internal/notifier/service"
tModel "donetick.com/core/internal/thing/model" tModel "donetick.com/core/internal/thing/model"
@ -17,11 +18,12 @@ import (
) )
type Handler struct { type Handler struct {
choreRepo *chRepo.ChoreRepository choreRepo *chRepo.ChoreRepository
circleRepo *cRepo.CircleRepository circleRepo *cRepo.CircleRepository
nPlanner *nps.NotificationPlanner nPlanner *nps.NotificationPlanner
nRepo *nRepo.NotificationRepository nRepo *nRepo.NotificationRepository
tRepo *tRepo.ThingRepository tRepo *tRepo.ThingRepository
eventsProducer *events.EventsProducer
} }
type ThingRequest struct { type ThingRequest struct {
@ -32,13 +34,14 @@ type ThingRequest struct {
} }
func NewHandler(cr *chRepo.ChoreRepository, circleRepo *cRepo.CircleRepository, func NewHandler(cr *chRepo.ChoreRepository, circleRepo *cRepo.CircleRepository,
np *nps.NotificationPlanner, nRepo *nRepo.NotificationRepository, tRepo *tRepo.ThingRepository) *Handler { np *nps.NotificationPlanner, nRepo *nRepo.NotificationRepository, tRepo *tRepo.ThingRepository, eventsProducer *events.EventsProducer) *Handler {
return &Handler{ return &Handler{
choreRepo: cr, choreRepo: cr,
circleRepo: circleRepo, circleRepo: circleRepo,
nPlanner: np, nPlanner: np,
nRepo: nRepo, nRepo: nRepo,
tRepo: tRepo, tRepo: tRepo,
eventsProducer: eventsProducer,
} }
} }
@ -95,6 +98,7 @@ func (h *Handler) UpdateThingState(c *gin.Context) {
return return
} }
thing, err := h.tRepo.GetThingByID(c, thingID) thing, err := h.tRepo.GetThingByID(c, thingID)
old_state := thing.State
if thing.UserID != currentUser.ID { if thing.UserID != currentUser.ID {
c.JSON(403, gin.H{"error": "Forbidden"}) c.JSON(403, gin.H{"error": "Forbidden"})
return return
@ -118,6 +122,13 @@ func (h *Handler) UpdateThingState(c *gin.Context) {
if shouldReturn { if shouldReturn {
return return
} }
h.eventsProducer.ThingsUpdated(c.Request.Context(), *currentUser.WebhookURL, map[string]interface{}{
"id": thing.ID,
"name": thing.Name,
"type": thing.Type,
"from_state": old_state,
"to_state": val,
})
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"res": thing, "res": thing,