Refactor Notification Sending Logic to Include Channel Checks

- Updated the Send method in the notification service to check for specified delivery channels (Push and Email) before sending notifications, enhancing the flexibility of the notification system.
- Utilized the slices package to streamline the channel checking process, improving code readability and maintainability.
- Ensured that notifications are only sent to recipients based on the selected channels, promoting more efficient notification delivery.
This commit is contained in:
n.nakhostin
2025-09-21 15:05:01 +03:30
parent 29bca402a4
commit 4d7344421b
+73 -64
View File
@@ -3,6 +3,7 @@ package notification
import (
"context"
"errors"
"slices"
"strings"
"time"
@@ -60,41 +61,45 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err
}
for _, recipient := range recipients {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Push: v,
},
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Push: v,
},
ScheduledAt: req.ScheduleAt,
})
ScheduledAt: req.ScheduleAt,
})
}
}
if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
})
if slices.Contains(req.Channels, DeliveryChannelEmail) {
if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
})
}
}
}
} else {
@@ -103,40 +108,44 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err
}
for _, recipient := range recipients {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Methods: notification.NotificationMethods{
Push: v,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Methods: notification.NotificationMethods{
Push: v,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
}
}
if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
if slices.Contains(req.Channels, DeliveryChannelEmail) {
if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
}
}
}
s.logger.Info("Notification sent", map[string]interface{}{