Enhance Notification System with New API Endpoints and Response Structures

- Added new endpoints for marking notifications as seen and retrieving detailed notification information for both admin and user contexts, improving the flexibility of the notification system.
- Implemented the MarkSeen and ViewNotification methods in the NotificationHandler, ensuring proper handling of notification visibility and details retrieval.
- Updated the NotificationService interface to include methods for getting notification details and marking notifications as seen, enhancing service capabilities.
- Enhanced the notification response structures to include additional fields such as Image, Seen, and ScheduledAt, providing richer data in API responses.
- Updated API documentation with Swagger comments for the new endpoints and response formats, ensuring clarity for API consumers.
This commit is contained in:
n.nakhostin
2025-09-21 11:26:54 +03:30
parent 0f981880b5
commit 5906904caf
12 changed files with 1200 additions and 45 deletions
+102 -3
View File
@@ -3,6 +3,8 @@ package notification
import (
"context"
"errors"
"strings"
"time"
"tm/internal/customer"
"tm/internal/user"
@@ -15,6 +17,8 @@ import (
type Service interface {
Send(ctx context.Context, req *NotificationRequest) error
GetNotifications(ctx context.Context, req *SearchForm, pagination *response.Pagination) (*NotificationListResponse, error)
GetNotification(ctx context.Context, notificationID string) (*NotificationResponse, error)
MarkSeen(ctx context.Context, notificationID, userID string) error
getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error)
getCustomers(ctx context.Context, values []string, target string) ([]notificationRecipient, error)
}
@@ -65,7 +69,7 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Priority: string(req.Priority),
EventType: notification.EventTypePush,
Metadata: map[string]any{
"tender": req.Tender,
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Push: v,
@@ -84,7 +88,7 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Priority: string(req.Priority),
EventType: notification.EventTypeEmail,
Metadata: map[string]any{
"tender": req.Tender,
"tender": strings.TrimSpace(req.Tender),
},
Methods: notification.NotificationMethods{
Email: recipient.Email,
@@ -111,6 +115,9 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Push: v,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
}
@@ -126,6 +133,9 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
Email: recipient.Email,
},
ScheduledAt: req.ScheduleAt,
Metadata: map[string]any{
"tender": strings.TrimSpace(req.Tender),
},
})
}
}
@@ -168,11 +178,19 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
notificationsResponse := make([]*NotificationResponse, 0)
for _, notification := range notifications.Data {
createdAt, err := time.Parse(time.RFC3339, notification.CreatedAt)
if err != nil {
return nil, err
}
updatedAt, err := time.Parse(time.RFC3339, notification.UpdatedAt)
if err != nil {
return nil, err
}
notificationsResponse = append(notificationsResponse, &NotificationResponse{
ID: notification.ID,
UserID: notification.UserID,
Title: notification.Title,
Description: notification.Message,
Message: notification.Message,
Link: &notification.Link,
Priority: notification.Priority,
Type: notification.Type,
@@ -181,6 +199,13 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
Methods: notification.Methods,
Metadata: notification.Metadata,
ScheduleAt: notification.ScheduledAt,
IsScheduled: notification.IsScheduled,
Image: &notification.Image,
Seen: notification.Seen,
SeenAt: notification.SeenAt,
ScheduledAt: notification.ScheduledAt,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
})
}
@@ -196,6 +221,80 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
}, nil
}
func (s *notificationService) GetNotification(ctx context.Context, notificationID string) (*NotificationResponse, error) {
s.logger.Info("Getting notification", map[string]interface{}{
"notification_id": notificationID,
})
notification, err := s.sdk.GetNotification(ctx, notificationID)
if err != nil {
s.logger.Error("Failed to get notification", map[string]interface{}{
"error": err.Error(),
"notification_id": notificationID,
})
return nil, err
}
// Convert to detailed response format
var linkPtr *string
if notification.Data.Link != "" {
linkPtr = &notification.Data.Link
}
var imagePtr *string
if notification.Data.Image != "" {
imagePtr = &notification.Data.Image
}
detailedResponse := &NotificationResponse{
ID: notification.Data.ID,
UserID: notification.Data.UserID,
Title: notification.Data.Title,
Message: notification.Data.Message,
Link: linkPtr,
Image: imagePtr,
Type: notification.Data.Type,
Priority: notification.Data.Priority,
EventType: notification.Data.EventType,
CreatedAt: notification.Data.CreatedAt,
UpdatedAt: notification.Data.UpdatedAt,
Methods: notification.Data.Methods,
Metadata: notification.Data.Metadata,
Status: notification.Data.Status,
Seen: notification.Data.Seen,
SeenAt: notification.Data.SeenAt,
ScheduledAt: notification.Data.ScheduledAt,
IsScheduled: notification.Data.IsScheduled,
}
return detailedResponse, nil
}
func (s *notificationService) MarkSeen(ctx context.Context, notificationID, userID string) error {
s.logger.Info("Marking notification as seen", map[string]interface{}{
"notification_id": notificationID,
})
notif, err := s.sdk.GetNotification(ctx, notificationID)
if err != nil {
s.logger.Error("Failed to get notification", map[string]interface{}{
"error": err.Error(),
"notification_id": notificationID,
})
return err
}
if notif.Data.UserID != userID {
return errors.New("notification not found")
}
_, err = s.sdk.MarkSeen(ctx, notificationID)
if err != nil {
return err
}
return nil
}
func (s *notificationService) getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error) {
recipients := make([]notificationRecipient, 0)
offset := 0