in-app notification bug fix

This commit is contained in:
Mazyar
2026-05-31 03:05:40 +03:30
parent bca94cd69a
commit ef57b302d9
4 changed files with 134 additions and 110 deletions
+102 -97
View File
@@ -76,50 +76,7 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err
}
for _, recipient := range recipients {
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Link: link,
Image: image,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Push: v,
},
ScheduledAt: req.ScheduleAt,
})
}
}
if slices.Contains(req.Channels, DeliveryChannelEmail) {
if recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Link: link,
Image: image,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
})
}
}
s.sendToRecipient(ctx, recipient, req, link, image)
}
} else {
recipients, err := s.getCustomers(ctx, req.Recipient, string(req.Target))
@@ -127,60 +84,104 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
return err
}
for _, recipient := range recipients {
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Link: link,
Image: image,
Methods: notification.NotificationMethods{
Push: v,
},
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, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Link: link,
Image: image,
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
}
}
s.sendToRecipient(ctx, recipient, req, link, image)
}
s.logger.Info("Notification sent", map[string]interface{}{
"recipient": req.Recipient,
"channels": req.Channels,
})
return nil
}
s.logger.Info("Notification sent", map[string]interface{}{
"recipient": req.Recipient,
"channels": req.Channels,
"target": req.Target,
})
return nil
}
func (s *notificationService) sendToRecipient(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string) {
if err := s.persistNotification(ctx, recipient, req, link, image); err != nil {
s.logger.Error("Failed to persist in-app notification", map[string]interface{}{
"error": err.Error(),
"user_id": recipient.UserID,
})
}
metadata := map[string]any{
"tender": strings.TrimSpace(req.Tender),
}
if slices.Contains(req.Channels, DeliveryChannelPush) {
for _, v := range recipient.DeviceTokens {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Link: link,
Image: image,
Metadata: metadata,
Methods: notification.NotificationMethods{
Push: v,
},
ScheduledAt: req.ScheduleAt,
})
}
}
if slices.Contains(req.Channels, DeliveryChannelEmail) && recipient.Email != "" {
s.sdk.SendNotification(ctx, &notification.NotificationRequest{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Type: string(req.Type),
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Link: link,
Image: image,
Metadata: metadata,
Methods: notification.NotificationMethods{
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
})
}
}
func (s *notificationService) persistNotification(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string) error {
methods := make(map[string]string)
if slices.Contains(req.Channels, DeliveryChannelEmail) && recipient.Email != "" {
methods["email"] = recipient.Email
}
if slices.Contains(req.Channels, DeliveryChannelPush) && len(recipient.DeviceTokens) > 0 {
methods["push"] = recipient.DeviceTokens[0]
}
status := string(DeliveryStatusSent)
if req.ScheduleAt > 0 {
status = string(DeliveryStatusPending)
}
return s.repository.Create(ctx, &Notification{
UserID: recipient.UserID,
Title: req.Title,
Message: req.Description,
Link: link,
Image: image,
Priority: string(req.Priority),
Type: string(req.Type),
Status: status,
Methods: methods,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
ScheduleAt: req.ScheduleAt,
ScheduledAt: req.ScheduleAt,
IsScheduled: req.ScheduleAt > 0,
Seen: false,
})
}
func (s *notificationService) GetNotifications(ctx context.Context, req *SearchForm, pagination *response.Pagination) (*NotificationListResponse, error) {
s.logger.Info("Getting notifications", map[string]interface{}{
"status": req.Status,
@@ -227,7 +228,7 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
}
notificationsResponse = append(notificationsResponse, &NotificationResponse{
ID: notification.ID,
ID: notification.GetID(),
UserID: notification.UserID,
Recipient: recipient,
Title: notification.Title,
@@ -292,7 +293,7 @@ func (s *notificationService) GetNotification(ctx context.Context, notificationI
}
detailedResponse := &NotificationResponse{
ID: notification.ID,
ID: notification.GetID(),
UserID: notification.UserID,
Recipient: recipient,
Title: notification.Title,
@@ -378,11 +379,15 @@ func (s *notificationService) getUsers(ctx context.Context, userIDs []string) ([
})
}
if len(recipients) == 0 {
return nil, errors.New("no users found")
if len(userIDs) > 0 || len(users.Users) == 0 {
break
}
}
if len(recipients) == 0 {
return nil, errors.New("no users found")
}
return recipients, nil
}