Refactor notification service to support multiple delivery channels

- Updated the `sendToRecipient` method to iterate over the channels specified in the notification request, allowing notifications to be persisted for each channel.
- Modified the `persistNotification` method to accept a channel parameter, enhancing the logic for handling different delivery methods (email, push, in-app).
- Improved error logging to include the channel information when persisting notifications fails, providing better context for debugging.

This update enhances the flexibility of the notification service by enabling it to handle multiple delivery channels more effectively, improving overall notification management.
This commit is contained in:
Mazyar
2026-07-11 15:49:04 +03:30
parent 932b0cf24e
commit 9d8ce12816
+27 -13
View File
@@ -99,11 +99,14 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
}
func (s *notificationService) sendToRecipient(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string) {
if err := s.persistNotification(ctx, recipient, req, link, image); err != nil {
s.logger.Error("Failed to persist in-app notification", map[string]interface{}{
"error": err.Error(),
"user_id": recipient.UserID,
})
for _, channel := range req.Channels {
if err := s.persistNotification(ctx, recipient, req, link, image, channel); err != nil {
s.logger.Error("Failed to persist notification", map[string]interface{}{
"error": err.Error(),
"user_id": recipient.UserID,
"channel": channel,
})
}
}
metadata := map[string]any{
@@ -149,15 +152,26 @@ func (s *notificationService) sendToRecipient(ctx context.Context, recipient not
}
}
func (s *notificationService) persistNotification(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string) error {
methods := map[string]string{
"in_app": "true",
}
if slices.Contains(req.Channels, DeliveryChannelEmail) && recipient.Email != "" {
func (s *notificationService) persistNotification(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string, channel DeliveryChannel) error {
methods := map[string]string{}
eventType := string(channel)
switch channel {
case DeliveryChannelEmail:
if recipient.Email == "" {
return nil
}
methods["email"] = recipient.Email
}
if slices.Contains(req.Channels, DeliveryChannelPush) && len(recipient.DeviceTokens) > 0 {
case DeliveryChannelPush:
if len(recipient.DeviceTokens) == 0 {
return nil
}
methods["push"] = recipient.DeviceTokens[0]
case DeliveryChannelInApp:
eventType = EventTypeInApp
methods["in_app"] = "true"
default:
return nil
}
status := string(DeliveryStatusSent)
@@ -173,7 +187,7 @@ func (s *notificationService) persistNotification(ctx context.Context, recipient
Image: image,
Priority: string(req.Priority),
Type: string(req.Type),
EventType: EventTypeInApp,
EventType: eventType,
Status: status,
Methods: methods,
Metadata: map[string]any{