From 4d7344421bfc47b80897c95cfad69ca1c7f83096 Mon Sep 17 00:00:00 2001 From: "n.nakhostin" Date: Sun, 21 Sep 2025 15:05:01 +0330 Subject: [PATCH] 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. --- internal/notification/service.go | 137 ++++++++++++++++--------------- 1 file changed, 73 insertions(+), 64 deletions(-) diff --git a/internal/notification/service.go b/internal/notification/service.go index 0625dbf..ab4fc79 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -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, ¬ification.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, ¬ification.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, ¬ification.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, ¬ification.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, ¬ification.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, ¬ification.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, ¬ification.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, ¬ification.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{}{