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
+12 -4
View File
@@ -1,6 +1,9 @@
package notification
import "tm/pkg/response"
import (
"time"
"tm/pkg/response"
)
// CreateRequest represents the request to create a notification
type NotificationRequest struct {
@@ -34,8 +37,9 @@ type NotificationResponse struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Title string `json:"title"`
Description string `json:"description"`
Message string `json:"message"`
Link *string `json:"link"`
Image *string `json:"image"`
Priority string `json:"priority"`
Type string `json:"type"`
EventType string `json:"event_type"`
@@ -43,6 +47,10 @@ type NotificationResponse struct {
Methods map[string]string `json:"methods"`
Metadata map[string]any `json:"metadata"`
ScheduleAt int64 `json:"schedule_at"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Seen bool `json:"seen"`
SeenAt int64 `json:"seen_at"`
IsScheduled bool `json:"is_scheduled"`
ScheduledAt int64 `json:"scheduled_at"`
}
+118 -2
View File
@@ -47,7 +47,7 @@ func (h *NotificationHandler) Send(c echo.Context) error {
}
// Send notification
go h.service.Send(c.Request().Context(), &req)
h.service.Send(c.Request().Context(), &req)
return response.Created(c, nil, "Notification sent successfully")
}
@@ -148,6 +148,65 @@ func (h *NotificationHandler) AdminNotifications(c echo.Context) error {
return response.Success(c, notifications, "Notifications retrieved successfully")
}
// MarkSeen marks a notification as seen
// @Summary Mark a notification as seen
// @Description Mark a notification as seen
// @Tags Admin-Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Success 200 {object} response.APIResponse
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Security BearerAuth
// @Router /admin/v1/notifications/mark-seen/{id} [get]
func (h *NotificationHandler) MarkSeen(c echo.Context) error {
notificationID := c.Param("id")
userID, err := user.GetUserIDFromContext(c)
if err != nil {
return response.BadRequest(c, "User ID required", "User must be associated with a user")
}
// Mark notification as seen
err = h.service.MarkSeen(c.Request().Context(), notificationID, userID)
if err != nil {
return response.InternalServerError(c, "Failed to mark notification as seen")
}
return response.Success(c, nil, "Notification marked as seen successfully")
}
// ViewNotification gets a single notification by ID
// @Summary Get notification details
// @Description Get detailed information about a specific notification
// @Tags Admin-Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Success 200 {object} response.APIResponse{data=NotificationResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Security BearerAuth
// @Router /admin/v1/notifications/view/{id} [get]
func (h *NotificationHandler) ViewNotification(c echo.Context) error {
notificationID := c.Param("id")
if notificationID == "" {
return response.BadRequest(c, "notification ID is required", "Notification ID must be provided")
}
// Get notification details
notification, err := h.service.GetNotification(c.Request().Context(), notificationID)
if err != nil {
return response.InternalServerError(c, "Failed to get notification details")
}
return response.Success(c, notification, "Notification details retrieved successfully")
}
// **************************** PUBLIC ENDPOINTS ****************************
// MyNotifications gets notifications for the user
// @Summary Get notifications for the user
// @Description Get notifications for the user
@@ -169,7 +228,7 @@ func (h *NotificationHandler) AdminNotifications(c echo.Context) error {
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Security BearerAuth
// @Router /api/v1/notifications/my [get]
// @Router /api/v1/notifications [get]
func (h *NotificationHandler) CustomerNotifications(c echo.Context) error {
userID, err := user.GetUserIDFromContext(c)
if err != nil {
@@ -199,3 +258,60 @@ func (h *NotificationHandler) CustomerNotifications(c echo.Context) error {
return response.Success(c, notifications, "Notifications retrieved successfully")
}
// MarkSeen marks a notification as seen
// @Summary Mark a notification as seen
// @Description Mark a notification as seen
// @Tags Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Success 200 {object} response.APIResponse
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Security BearerAuth
// @Router /api/v1/notifications/mark-seen/{id} [get]
func (h *NotificationHandler) PublicMarkSeen(c echo.Context) error {
notificationID := c.Param("id")
userID, err := user.GetUserIDFromContext(c)
if err != nil {
return response.BadRequest(c, "User ID required", "User must be associated with a user")
}
// Mark notification as seen
err = h.service.MarkSeen(c.Request().Context(), notificationID, userID)
if err != nil {
return response.InternalServerError(c, "Failed to mark notification as seen")
}
return response.Success(c, nil, "Notification marked as seen successfully")
}
// ViewNotification gets a single notification by ID
// @Summary Get notification details
// @Description Get detailed information about a specific notification
// @Tags Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Success 200 {object} response.APIResponse{data=DetailedNotificationResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Security BearerAuth
// @Router /api/v1/notifications/view/{id} [get]
func (h *NotificationHandler) PublicViewNotification(c echo.Context) error {
notificationID := c.Param("id")
if notificationID == "" {
return response.BadRequest(c, "notification ID is required", "Notification ID must be provided")
}
// Get notification details
notification, err := h.service.GetNotification(c.Request().Context(), notificationID)
if err != nil {
return response.InternalServerError(c, "Failed to get notification details")
}
return response.Success(c, notification, "Notification details retrieved successfully")
}
+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