@@ -3,42 +3,51 @@ package service
33import (
44 "context"
55 "errors"
6+ "log"
67
78 "trakrlog/internal/model"
89 "trakrlog/internal/repository"
910)
1011
1112type EventService struct {
12- eventRepo repository.EventRepository
13- channelRepo repository.ChannelRepository
14- projectRepo repository.ProjectRepository
13+ eventRepo repository.EventRepository
14+ channelRepo repository.ChannelRepository
15+ projectRepo repository.ProjectRepository
16+ notificationService * NotificationService
1517}
1618
17- func NewEventService (eventRepo repository.EventRepository , channelRepo repository.ChannelRepository , projectRepo repository.ProjectRepository ) * EventService {
19+ func NewEventService (
20+ eventRepo repository.EventRepository ,
21+ channelRepo repository.ChannelRepository ,
22+ projectRepo repository.ProjectRepository ,
23+ notificationService * NotificationService ,
24+ ) * EventService {
1825 return & EventService {
19- eventRepo : eventRepo ,
20- channelRepo : channelRepo ,
21- projectRepo : projectRepo ,
26+ eventRepo : eventRepo ,
27+ channelRepo : channelRepo ,
28+ projectRepo : projectRepo ,
29+ notificationService : notificationService ,
2230 }
2331}
2432
2533// CreateEvent creates a new event in a channel
26- func (s * EventService ) CreateEvent (ctx context.Context , userID , projectName , channelName , title , description , icon string , tags map [string ]string ) (* model.Event , error ) {
34+ // Returns the created event and a channel that will receive the notification result
35+ func (s * EventService ) CreateEvent (ctx context.Context , userID , projectName , channelName , title , description , icon string , tags map [string ]string ) (* model.Event , <- chan error , error ) {
2736 // Validation
2837 if title == "" {
29- return nil , errors .New ("event title required" )
38+ return nil , nil , errors .New ("event title required" )
3039 }
3140
3241 // Find project by user ID and name
3342 project , err := s .projectRepo .FindByUserIDAndName (ctx , userID , projectName )
3443 if err != nil {
35- return nil , errors .New ("project not found" )
44+ return nil , nil , errors .New ("project not found" )
3645 }
3746
3847 // Find channel by project ID and name
3948 channel , err := s .channelRepo .FindByProjectIDAndName (ctx , project .ID .Hex (), channelName )
4049 if err != nil {
41- return nil , errors .New ("channel not found" )
50+ return nil , nil , errors .New ("channel not found" )
4251 }
4352
4453 // Create event
@@ -52,10 +61,28 @@ func (s *EventService) CreateEvent(ctx context.Context, userID, projectName, cha
5261 }
5362
5463 if err := s .eventRepo .Create (ctx , event ); err != nil {
55- return nil , err
56- }
57-
58- return event , nil
64+ return nil , nil , err
65+ }
66+
67+ // Send push notification asynchronously (don't block event creation)
68+ // Return a channel that the caller can use to wait for notification completion
69+ notificationDone := make (chan error , 1 )
70+
71+ go func () {
72+ defer close (notificationDone )
73+ if s .notificationService != nil {
74+ if err := s .notificationService .ProcessEventNotification (context .Background (), event ); err != nil {
75+ // Log error but don't fail the event creation
76+ log .Printf ("Failed to send notification for event %s: %v" , event .ID .Hex (), err )
77+ notificationDone <- err
78+ return
79+ }
80+ log .Printf ("Successfully sent notification for event %s" , event .ID .Hex ())
81+ notificationDone <- nil
82+ }
83+ }()
84+
85+ return event , notificationDone , nil
5986}
6087
6188// GetEventByID retrieves an event by ID
0 commit comments