From 729c73a8af22b694fec5d7889f81cbfe8f52b37b Mon Sep 17 00:00:00 2001 From: "n.nakhostin" Date: Mon, 20 Oct 2025 09:38:13 +0330 Subject: [PATCH] Refactor Notification Service to Handle Optional Image and Link Fields - Updated the Send method in the notification service to handle optional image and link fields more gracefully by using local variables. - Improved logging to ensure that the correct values are sent in the notification context, enhancing clarity and maintainability of the notification process. - This change ensures that nil values for image and link do not cause dereferencing issues, improving overall robustness of the notification sending logic. --- internal/notification/service.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/internal/notification/service.go b/internal/notification/service.go index 8bb79cd..ed8821a 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -50,12 +50,22 @@ func NewService( // Send sends a multiple notification func (s *notificationService) Send(ctx context.Context, req *NotificationRequest) error { + image := "" + if req.Image != nil { + image = *req.Image + } + + link := "" + if req.Link != nil { + link = *req.Link + } + s.logger.Info("Sending notification", map[string]interface{}{ "recipient": req.Recipient, "type": req.Type, "priority": req.Priority, - "link": req.Link, - "image": req.Image, + "link": link, + "image": image, }) if req.Target == TargetAudienceAllUsers || req.Target == TargetAudienceSpecificUsers { @@ -73,8 +83,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest Type: string(req.Type), Priority: string(req.Priority), EventType: notification.EventTypePush, - Link: *req.Link, - Image: *req.Image, + Link: link, + Image: image, Metadata: map[string]any{ "tender": strings.TrimSpace(req.Tender), }, @@ -96,8 +106,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest Type: string(req.Type), Priority: string(req.Priority), EventType: notification.EventTypeEmail, - Link: *req.Link, - Image: *req.Image, + Link: link, + Image: image, Metadata: map[string]any{ "tender": strings.TrimSpace(req.Tender), }, @@ -124,8 +134,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest Type: string(req.Type), Priority: string(req.Priority), EventType: notification.EventTypePush, - Link: *req.Link, - Image: *req.Image, + Link: link, + Image: image, Methods: notification.NotificationMethods{ Push: v, }, @@ -146,8 +156,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest Type: string(req.Type), Priority: string(req.Priority), EventType: notification.EventTypeEmail, - Link: *req.Link, - Image: *req.Image, + Link: link, + Image: image, Methods: notification.NotificationMethods{ Email: recipient.Email, },