Skip to content

Commit

Permalink
feat: add dedicated struct for event data and use struct for goal update
Browse files Browse the repository at this point in the history
  • Loading branch information
HamoonZamiri committed Aug 28, 2024
1 parent d5be8c5 commit 253d57c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions backend/goals/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 7 additions & 16 deletions backend/users/service/events.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package service

import (
"goalify/entities"
"goalify/utils/events"
"log/slog"
)
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions backend/utils/events/event_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package events

import "goalify/entities"

type GoalUpdatedData struct {
OldGoal *entities.Goal
NewGoal *entities.Goal
Xp int
}

0 comments on commit 253d57c

Please sign in to comment.