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 ( import (
"context" "context"
"errors" "errors"
"slices"
"strings" "strings"
"time" "time"
@@ -60,41 +61,45 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err return err
} }
for _, recipient := range recipients { for _, recipient := range recipients {
for _, v := range recipient.DeviceTokens { if slices.Contains(req.Channels, DeliveryChannelPush) {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ for _, v := range recipient.DeviceTokens {
UserID: recipient.UserID, s.sdk.SendNotification(ctx, &notification.NotificationRequest{
Title: req.Title, UserID: recipient.UserID,
Message: req.Description, Title: req.Title,
Type: string(req.Type), Message: req.Description,
Priority: string(req.Priority), Type: string(req.Type),
EventType: notification.EventTypePush, Priority: string(req.Priority),
Metadata: map[string]any{ EventType: notification.EventTypePush,
"tender": strings.TrimSpace(req.Tender), Metadata: map[string]any{
}, "tender": strings.TrimSpace(req.Tender),
Methods: notification.NotificationMethods{ },
Push: v, Methods: notification.NotificationMethods{
}, Push: v,
},
ScheduledAt: req.ScheduleAt, ScheduledAt: req.ScheduleAt,
}) })
}
} }
if recipient.Email != "" { if slices.Contains(req.Channels, DeliveryChannelEmail) {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ if recipient.Email != "" {
UserID: recipient.UserID, s.sdk.SendNotification(ctx, &notification.NotificationRequest{
Title: req.Title, UserID: recipient.UserID,
Message: req.Description, Title: req.Title,
Type: string(req.Type), Message: req.Description,
Priority: string(req.Priority), Type: string(req.Type),
EventType: notification.EventTypeEmail, Priority: string(req.Priority),
Metadata: map[string]any{ EventType: notification.EventTypeEmail,
"tender": strings.TrimSpace(req.Tender), Metadata: map[string]any{
}, "tender": strings.TrimSpace(req.Tender),
Methods: notification.NotificationMethods{ },
Email: recipient.Email, Methods: notification.NotificationMethods{
}, Email: recipient.Email,
ScheduledAt: req.ScheduleAt, },
}) ScheduledAt: req.ScheduleAt,
})
}
} }
} }
} else { } else {
@@ -103,40 +108,44 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err return err
} }
for _, recipient := range recipients { for _, recipient := range recipients {
for _, v := range recipient.DeviceTokens { if slices.Contains(req.Channels, DeliveryChannelPush) {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ for _, v := range recipient.DeviceTokens {
UserID: recipient.UserID, s.sdk.SendNotification(ctx, &notification.NotificationRequest{
Title: req.Title, UserID: recipient.UserID,
Message: req.Description, Title: req.Title,
Type: string(req.Type), Message: req.Description,
Priority: string(req.Priority), Type: string(req.Type),
EventType: notification.EventTypePush, Priority: string(req.Priority),
Methods: notification.NotificationMethods{ EventType: notification.EventTypePush,
Push: v, Methods: notification.NotificationMethods{
}, Push: v,
ScheduledAt: req.ScheduleAt, },
Metadata: map[string]any{ ScheduledAt: req.ScheduleAt,
"tender": strings.TrimSpace(req.Tender), Metadata: map[string]any{
}, "tender": strings.TrimSpace(req.Tender),
}) },
})
}
} }
if recipient.Email != "" { if slices.Contains(req.Channels, DeliveryChannelEmail) {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ if recipient.Email != "" {
UserID: recipient.UserID, s.sdk.SendNotification(ctx, &notification.NotificationRequest{
Title: req.Title, UserID: recipient.UserID,
Message: req.Description, Title: req.Title,
Type: string(req.Type), Message: req.Description,
Priority: string(req.Priority), Type: string(req.Type),
EventType: notification.EventTypeEmail, Priority: string(req.Priority),
Methods: notification.NotificationMethods{ EventType: notification.EventTypeEmail,
Email: recipient.Email, Methods: notification.NotificationMethods{
}, Email: recipient.Email,
ScheduledAt: req.ScheduleAt, },
Metadata: map[string]any{ ScheduledAt: req.ScheduleAt,
"tender": strings.TrimSpace(req.Tender), Metadata: map[string]any{
}, "tender": strings.TrimSpace(req.Tender),
}) },
})
}
} }
} }
s.logger.Info("Notification sent", map[string]interface{}{ s.logger.Info("Notification sent", map[string]interface{}{