diff --git a/backend/goals/service/service.go b/backend/goals/service/service.go index b8b4fc6..7675b1b 100644 --- a/backend/goals/service/service.go +++ b/backend/goals/service/service.go @@ -260,12 +260,12 @@ func (gs *goalService) UpdateGoalById(goalId uuid.UUID, updates map[string]inter } cat, err := gs.goalCategoryStore.GetGoalCategoryById(updatedGoal.CategoryId) - event := events.NewEventWithUserId(events.GOAL_UPDATED, map[string]any{ - "oldGoal": goal, - "newGoal": updatedGoal, - "xp": cat.Xp_per_goal, - }, - userId.String()) + eventData := &events.GoalUpdatedData{ + OldGoal: goal, + NewGoal: updatedGoal, + Xp: cat.Xp_per_goal, + } + event := events.NewEventWithUserId(events.GOAL_UPDATED, eventData, userId.String()) gs.eventPublisher.Publish(event) return updatedGoal, nil } diff --git a/backend/users/service/events.go b/backend/users/service/events.go index ed32e0f..fcd8cbd 100644 --- a/backend/users/service/events.go +++ b/backend/users/service/events.go @@ -1,7 +1,6 @@ package service import ( - "goalify/entities" "goalify/utils/events" "log/slog" ) @@ -16,27 +15,19 @@ func (us *userService) HandleEvent(event events.Event) { } func (us *userService) handleGoalUpdatedEvent(event events.Event) { - eventData, err := events.ParseEventData[map[string]any](event) + eventData, err := events.ParseEventData[*events.GoalUpdatedData](event) if err != nil { slog.Error("service.handleGoalUpdatedEvent: events.ParseEventData:", "err", err) return } - oldGoal := eventData["oldGoal"] - newGoal := eventData["newGoal"] - goalXp := eventData["xp"] + oldGoal := eventData.OldGoal + newGoal := eventData.NewGoal + xp := eventData.Xp - assertedOldGoal, ok1 := oldGoal.(*entities.Goal) - assertedNewGoal, ok2 := newGoal.(*entities.Goal) - assertedXp, ok3 := goalXp.(int) - if !ok1 || !ok2 || !ok3 { - slog.Error("service.handleGoalUpdatedEvent: type assertion failed") - return - } - - if assertedOldGoal.Status != assertedNewGoal.Status && assertedNewGoal.Status == "complete" { + if oldGoal.Status != newGoal.Status && newGoal.Status == "complete" { // we need to update the xp of the user - user, err := us.userStore.GetUserById(assertedOldGoal.UserId.String()) + user, err := us.userStore.GetUserById(oldGoal.UserId.String()) if err != nil { slog.Error("service.handleGoalUpdatedEvent: store.GetUserById:", "err", err) return @@ -46,7 +37,7 @@ func (us *userService) handleGoalUpdatedEvent(event events.Event) { slog.Error("service.handleGoalUpdatedEvent: store.GetLevelById:", "err", err) return } - newXp := user.Xp + assertedXp + newXp := user.Xp + xp newLevel := user.LevelId if newXp >= level.LevelUpXp { newXp %= level.LevelUpXp diff --git a/backend/utils/events/event_types.go b/backend/utils/events/event_types.go new file mode 100644 index 0000000..8464d4b --- /dev/null +++ b/backend/utils/events/event_types.go @@ -0,0 +1,9 @@ +package events + +import "goalify/entities" + +type GoalUpdatedData struct { + OldGoal *entities.Goal + NewGoal *entities.Goal + Xp int +}