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
+9
View File
@@ -3,6 +3,7 @@ package notification
import ( import (
"context" "context"
"errors" "errors"
"slices"
"strings" "strings"
"time" "time"
@@ -60,6 +61,7 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err return err
} }
for _, recipient := range recipients { for _, recipient := range recipients {
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens { for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID, UserID: recipient.UserID,
@@ -78,7 +80,9 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
ScheduledAt: req.ScheduleAt, ScheduledAt: req.ScheduleAt,
}) })
} }
}
if slices.Contains(req.Channels, DeliveryChannelEmail) {
if recipient.Email != "" { if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID, UserID: recipient.UserID,
@@ -97,12 +101,14 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
}) })
} }
} }
}
} else { } else {
recipients, err := s.getCustomers(ctx, req.Recipient, string(req.Target)) recipients, err := s.getCustomers(ctx, req.Recipient, string(req.Target))
if err != nil { if err != nil {
return err return err
} }
for _, recipient := range recipients { for _, recipient := range recipients {
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens { for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID, UserID: recipient.UserID,
@@ -120,7 +126,9 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
}, },
}) })
} }
}
if slices.Contains(req.Channels, DeliveryChannelEmail) {
if recipient.Email != "" { if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{ s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID, UserID: recipient.UserID,
@@ -139,6 +147,7 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
}) })
} }
} }
}
s.logger.Info("Notification sent", map[string]interface{}{ s.logger.Info("Notification sent", map[string]interface{}{
"recipient": req.Recipient, "recipient": req.Recipient,
"channels": req.Channels, "channels": req.Channels,