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
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,
},