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:
@@ -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,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user