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.
This commit is contained in:
n.nakhostin
2025-10-20 09:38:13 +03:30
parent 01d3826003
commit 729c73a8af
+20 -10
View File
@@ -50,12 +50,22 @@ func NewService(
// Send sends a multiple notification // Send sends a multiple notification
func (s *notificationService) Send(ctx context.Context, req *NotificationRequest) error { 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{}{ s.logger.Info("Sending notification", map[string]interface{}{
"recipient": req.Recipient, "recipient": req.Recipient,
"type": req.Type, "type": req.Type,
"priority": req.Priority, "priority": req.Priority,
"link": req.Link, "link": link,
"image": req.Image, "image": image,
}) })
if req.Target == TargetAudienceAllUsers || req.Target == TargetAudienceSpecificUsers { if req.Target == TargetAudienceAllUsers || req.Target == TargetAudienceSpecificUsers {
@@ -73,8 +83,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Type: string(req.Type), Type: string(req.Type),
Priority: string(req.Priority), Priority: string(req.Priority),
EventType: notification.EventTypePush, EventType: notification.EventTypePush,
Link: *req.Link, Link: link,
Image: *req.Image, Image: image,
Metadata: map[string]any{ Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender), "tender": strings.TrimSpace(req.Tender),
}, },
@@ -96,8 +106,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Type: string(req.Type), Type: string(req.Type),
Priority: string(req.Priority), Priority: string(req.Priority),
EventType: notification.EventTypeEmail, EventType: notification.EventTypeEmail,
Link: *req.Link, Link: link,
Image: *req.Image, Image: image,
Metadata: map[string]any{ Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender), "tender": strings.TrimSpace(req.Tender),
}, },
@@ -124,8 +134,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Type: string(req.Type), Type: string(req.Type),
Priority: string(req.Priority), Priority: string(req.Priority),
EventType: notification.EventTypePush, EventType: notification.EventTypePush,
Link: *req.Link, Link: link,
Image: *req.Image, Image: image,
Methods: notification.NotificationMethods{ Methods: notification.NotificationMethods{
Push: v, Push: v,
}, },
@@ -146,8 +156,8 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Type: string(req.Type), Type: string(req.Type),
Priority: string(req.Priority), Priority: string(req.Priority),
EventType: notification.EventTypeEmail, EventType: notification.EventTypeEmail,
Link: *req.Link, Link: link,
Image: *req.Image, Image: image,
Methods: notification.NotificationMethods{ Methods: notification.NotificationMethods{
Email: recipient.Email, Email: recipient.Email,
}, },