add item mutation events

This commit is contained in:
Hayden 2023-08-02 11:24:46 -05:00
parent 1ad392c00c
commit 79b6c66e06
No known key found for this signature in database
GPG key ID: 17CF79474E257545
5 changed files with 55 additions and 34 deletions

View file

@ -1,6 +1,7 @@
package v1 package v1
import ( import (
"fmt"
"net/http" "net/http"
"github.com/google/uuid" "github.com/google/uuid"
@ -126,41 +127,31 @@ func (ctrl *V1Controller) HandleCacheWS() errchain.HandlerFunc {
s.Set("gid", auth.GID) s.Set("gid", auth.GID)
}) })
ctrl.bus.Subscribe(eventbus.EventLabelMutation, func(data any) { factory := func(e string) func(data any) {
eventData, ok := data.(eventbus.GroupMutationEvent) return func(data any) {
if !ok { eventData, ok := data.(eventbus.GroupMutationEvent)
log.Log().Msgf("invalid event data: %v", data)
return
}
_ = m.BroadcastFilter([]byte(`{"event": "label.mutation"}`), func(s *melody.Session) bool {
gidStr, ok := s.Get("gid")
if !ok { if !ok {
return false log.Log().Msgf("invalid event data: %v", data)
return
} }
gid := gidStr.(uuid.UUID) jsonStr := fmt.Sprintf(`{"event": "%s"}`, e)
return gid == eventData.GID
})
})
ctrl.bus.Subscribe(eventbus.EventLocationMutation, func(data any) { _ = m.BroadcastFilter([]byte(jsonStr), func(s *melody.Session) bool {
eventData, ok := data.(eventbus.GroupMutationEvent) groupIDStr, ok := s.Get("gid")
if !ok { if !ok {
log.Log().Msgf("invalid event data: %v", data) return false
return }
GID := groupIDStr.(uuid.UUID)
return GID == eventData.GID
})
} }
}
_ = m.BroadcastFilter([]byte(`{"event": "location.mutation"}`), func(s *melody.Session) bool { ctrl.bus.Subscribe(eventbus.EventLabelMutation, factory("label.mutation"))
gidStr, ok := s.Get("gid") ctrl.bus.Subscribe(eventbus.EventLocationMutation, factory("location.mutation"))
if !ok { ctrl.bus.Subscribe(eventbus.EventItemMutation, factory("item.mutation"))
return false
}
gid := gidStr.(uuid.UUID)
return gid == eventData.GID
})
})
return func(w http.ResponseWriter, r *http.Request) error { return func(w http.ResponseWriter, r *http.Request) error {
return m.HandleRequest(w, r) return m.HandleRequest(w, r)

View file

@ -12,6 +12,7 @@ type Event string
const ( const (
EventLabelMutation Event = "label.mutation" EventLabelMutation Event = "label.mutation"
EventLocationMutation Event = "location.mutation" EventLocationMutation Event = "location.mutation"
EventItemMutation Event = "item.mutation"
) )
type GroupMutationEvent struct { type GroupMutationEvent struct {
@ -37,6 +38,7 @@ func New() *EventBus {
subscribers: map[Event][]func(any){ subscribers: map[Event][]func(any){
EventLabelMutation: {}, EventLabelMutation: {},
EventLocationMutation: {}, EventLocationMutation: {},
EventItemMutation: {},
}, },
} }
} }

View file

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
"github.com/hay-kot/homebox/backend/internal/data/ent" "github.com/hay-kot/homebox/backend/internal/data/ent"
"github.com/hay-kot/homebox/backend/internal/data/ent/group" "github.com/hay-kot/homebox/backend/internal/data/ent/group"
"github.com/hay-kot/homebox/backend/internal/data/ent/item" "github.com/hay-kot/homebox/backend/internal/data/ent/item"
@ -17,7 +18,8 @@ import (
) )
type ItemsRepository struct { type ItemsRepository struct {
db *ent.Client db *ent.Client
bus *eventbus.EventBus
} }
type ( type (
@ -266,6 +268,12 @@ func mapItemOut(item *ent.Item) ItemOut {
} }
} }
func (r *ItemsRepository) publishMutationEvent(GID uuid.UUID) {
if r.bus != nil {
r.bus.Publish(eventbus.EventItemMutation, eventbus.GroupMutationEvent{GID: GID})
}
}
func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (ItemOut, error) { func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (ItemOut, error) {
q := e.db.Item.Query().Where(where...) q := e.db.Item.Query().Where(where...)
@ -520,11 +528,18 @@ func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCr
return ItemOut{}, err return ItemOut{}, err
} }
e.publishMutationEvent(gid)
return e.GetOne(ctx, result.ID) return e.GetOne(ctx, result.ID)
} }
func (e *ItemsRepository) Delete(ctx context.Context, id uuid.UUID) error { func (e *ItemsRepository) Delete(ctx context.Context, id uuid.UUID) error {
return e.db.Item.DeleteOneID(id).Exec(ctx) err := e.db.Item.DeleteOneID(id).Exec(ctx)
if err != nil {
return err
}
e.publishMutationEvent(id)
return nil
} }
func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID) error { func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID) error {
@ -534,6 +549,12 @@ func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID)
item.ID(id), item.ID(id),
item.HasGroupWith(group.ID(gid)), item.HasGroupWith(group.ID(gid)),
).Exec(ctx) ).Exec(ctx)
if err != nil {
return err
}
e.publishMutationEvent(gid)
return err return err
} }
@ -649,6 +670,7 @@ func (e *ItemsRepository) UpdateByGroup(ctx context.Context, GID uuid.UUID, data
} }
} }
e.publishMutationEvent(GID)
return e.GetOne(ctx, data.ID) return e.GetOne(ctx, data.ID)
} }
@ -687,6 +709,7 @@ func (e *ItemsRepository) Patch(ctx context.Context, GID, ID uuid.UUID, data Ite
q.SetQuantity(*data.Quantity) q.SetQuantity(*data.Quantity)
} }
e.publishMutationEvent(GID)
return q.Exec(ctx) return q.Exec(ctx)
} }

View file

@ -26,7 +26,7 @@ func New(db *ent.Client, bus *eventbus.EventBus, root string) *AllRepos {
Groups: NewGroupRepository(db), Groups: NewGroupRepository(db),
Locations: &LocationRepository{db, bus}, Locations: &LocationRepository{db, bus},
Labels: &LabelRepository{db, bus}, Labels: &LabelRepository{db, bus},
Items: &ItemsRepository{db}, Items: &ItemsRepository{db, bus},
Docs: &DocumentRepository{db, root}, Docs: &DocumentRepository{db, root},
Attachments: &AttachmentRepo{db}, Attachments: &AttachmentRepo{db},
MaintEntry: &MaintenanceEntryRepository{db}, MaintEntry: &MaintenanceEntryRepository{db},

View file

@ -186,13 +186,18 @@
const ws = new WebSocket(`ws://${window.location.host}/api/v1/ws/events`); const ws = new WebSocket(`ws://${window.location.host}/api/v1/ws/events`);
ws.onmessage = event => { ws.onmessage = event => {
const msg: EventMessage = JSON.parse(event.data); const msg: EventMessage = JSON.parse(event.data);
console.debug("recieved event", msg);
switch (msg.event) { switch (msg.event) {
case "label.mutation": case "label.mutation":
console.log("label.mutation");
labelStore.refresh(); labelStore.refresh();
break; break;
case "location.mutation": case "location.mutation":
console.log("location.mutation"); locationStore.refreshChildren();
locationStore.refreshParents();
break;
case "item.mutation":
// item mutations can affect locations counts
// so we need to refresh those as well
locationStore.refreshChildren(); locationStore.refreshChildren();
locationStore.refreshParents(); locationStore.refreshParents();
break; break;