|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "trakrlog/internal/model" |
| 9 | +) |
| 10 | + |
| 11 | +// PushServiceInterface defines the interface for push notification operations |
| 12 | +type PushServiceInterface interface { |
| 13 | + SendToUser(ctx context.Context, userID string, payload *model.NotificationPayload, eventID string) error |
| 14 | +} |
| 15 | + |
| 16 | +// ProjectServiceInterface defines the interface for project operations |
| 17 | +type ProjectServiceInterface interface { |
| 18 | + GetProjectByID(ctx context.Context, id string) (*model.Project, error) |
| 19 | +} |
| 20 | + |
| 21 | +// ChannelServiceInterface defines the interface for channel operations |
| 22 | +type ChannelServiceInterface interface { |
| 23 | + GetChannelByID(ctx context.Context, id string) (*model.Channel, error) |
| 24 | +} |
| 25 | + |
| 26 | +// NotificationService handles the business logic for sending notifications based on events |
| 27 | +type NotificationService struct { |
| 28 | + pushService PushServiceInterface |
| 29 | + projectService ProjectServiceInterface |
| 30 | + channelService ChannelServiceInterface |
| 31 | +} |
| 32 | + |
| 33 | +// NewNotificationService creates a new notification service |
| 34 | +func NewNotificationService( |
| 35 | + pushService PushServiceInterface, |
| 36 | + projectService ProjectServiceInterface, |
| 37 | + channelService ChannelServiceInterface, |
| 38 | +) *NotificationService { |
| 39 | + return &NotificationService{ |
| 40 | + pushService: pushService, |
| 41 | + projectService: projectService, |
| 42 | + channelService: channelService, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// ProcessEventNotification processes an event and sends notifications to subscribed users |
| 47 | +// In the MVP, all events trigger notifications for users with active subscriptions |
| 48 | +func (s *NotificationService) ProcessEventNotification(ctx context.Context, event *model.Event) error { |
| 49 | + // Get project details to build notification context |
| 50 | + project, err := s.projectService.GetProjectByID(ctx, event.ProjectID.Hex()) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("failed to get project: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Get channel details |
| 56 | + channel, err := s.channelService.GetChannelByID(ctx, event.ChannelID.Hex()) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to get channel: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Build notification payload |
| 62 | + payload := s.buildNotificationPayload(event, project, channel) |
| 63 | + |
| 64 | + // Send notification to the project owner |
| 65 | + // Note: In MVP, we send to project owner. Future enhancement: support channel subscribers |
| 66 | + userID := project.UserID.Hex() |
| 67 | + |
| 68 | + log.Printf("Processing notification for event %s to user %s", event.ID.Hex(), userID) |
| 69 | + |
| 70 | + if err := s.pushService.SendToUser(ctx, userID, payload, event.ID.Hex()); err != nil { |
| 71 | + // Log the error but don't fail event creation |
| 72 | + log.Printf("Failed to send notification for event %s: %v", event.ID.Hex(), err) |
| 73 | + return fmt.Errorf("failed to send notification: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// buildNotificationPayload creates the notification payload from event data |
| 80 | +func (s *NotificationService) buildNotificationPayload( |
| 81 | + event *model.Event, |
| 82 | + project *model.Project, |
| 83 | + channel *model.Channel, |
| 84 | +) *model.NotificationPayload { |
| 85 | + // Build notification title |
| 86 | + title := fmt.Sprintf("[%s] %s", project.Name, event.Title) |
| 87 | + if len(title) > 80 { |
| 88 | + title = title[:77] + "..." |
| 89 | + } |
| 90 | + |
| 91 | + // Build notification body |
| 92 | + body := event.Description |
| 93 | + if body == "" { |
| 94 | + body = fmt.Sprintf("New event in %s", channel.Name) |
| 95 | + } |
| 96 | + if len(body) > 120 { |
| 97 | + body = body[:117] + "..." |
| 98 | + } |
| 99 | + |
| 100 | + // Use event icon if available, otherwise use project logo or default |
| 101 | + icon := event.Icon |
| 102 | + if icon == "" && project.LogoBase64 != "" { |
| 103 | + icon = project.LogoBase64 |
| 104 | + } |
| 105 | + if icon == "" { |
| 106 | + icon = "/icon.png" // Default app icon |
| 107 | + } |
| 108 | + |
| 109 | + // Build the notification data with event metadata |
| 110 | + data := map[string]interface{}{ |
| 111 | + "eventId": event.ID.Hex(), |
| 112 | + "projectId": project.ID.Hex(), |
| 113 | + "channelId": channel.ID.Hex(), |
| 114 | + "url": fmt.Sprintf("/projects/%s/channels/%s/events/%s", project.ID.Hex(), channel.ID.Hex(), event.ID.Hex()), |
| 115 | + "timestamp": event.CreatedAt.Unix(), |
| 116 | + } |
| 117 | + |
| 118 | + // Add tags if present |
| 119 | + if len(event.Tags) > 0 { |
| 120 | + data["tags"] = event.Tags |
| 121 | + } |
| 122 | + |
| 123 | + return &model.NotificationPayload{ |
| 124 | + Title: title, |
| 125 | + Body: body, |
| 126 | + Icon: icon, |
| 127 | + Badge: "/badge.png", |
| 128 | + Tag: fmt.Sprintf("event-%s", event.ID.Hex()), // Group notifications by event |
| 129 | + Data: data, |
| 130 | + Actions: []model.NotificationAction{ |
| 131 | + { |
| 132 | + Action: "view", |
| 133 | + Title: "View Event", |
| 134 | + }, |
| 135 | + { |
| 136 | + Action: "close", |
| 137 | + Title: "Dismiss", |
| 138 | + }, |
| 139 | + }, |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// SendTestNotification sends a test notification to verify the setup |
| 144 | +func (s *NotificationService) SendTestNotification(ctx context.Context, userID string) error { |
| 145 | + payload := &model.NotificationPayload{ |
| 146 | + Title: "🔔 Test Notification", |
| 147 | + Body: "Your push notifications are working correctly!", |
| 148 | + Icon: "/icon.png", |
| 149 | + Badge: "/badge.png", |
| 150 | + Tag: "test-notification", |
| 151 | + Data: map[string]interface{}{ |
| 152 | + "type": "test", |
| 153 | + "url": "/settings", |
| 154 | + }, |
| 155 | + Actions: []model.NotificationAction{ |
| 156 | + { |
| 157 | + Action: "view", |
| 158 | + Title: "Open Settings", |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + if err := s.pushService.SendToUser(ctx, userID, payload, ""); err != nil { |
| 164 | + return fmt.Errorf("failed to send test notification: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + log.Printf("Test notification sent successfully to user %s", userID) |
| 168 | + return nil |
| 169 | +} |
0 commit comments