Enhance Notification Management with New Endpoints and Response Structures
- Added new endpoints for retrieving notifications for both admins and users, improving the flexibility of the notification system. - Implemented query parameters for filtering notifications by status, method, event type, type, and recipient, enhancing usability. - Introduced new response structures for notifications, including pagination information, to provide better data handling in API responses. - Updated API documentation with Swagger comments for the new endpoints and response formats, ensuring clarity for API consumers. - Refactored notification handling logic to support the new features, promoting a more robust notification management system.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package notification
|
||||
|
||||
import "tm/pkg/response"
|
||||
|
||||
// CreateRequest represents the request to create a notification
|
||||
type NotificationRequest struct {
|
||||
Recipient []string `json:"recipient"`
|
||||
@@ -16,11 +18,31 @@ type NotificationRequest struct {
|
||||
|
||||
// SearchForm represents the form for searching notifications
|
||||
type SearchForm struct {
|
||||
Search *string `query:"q" valid:"optional"`
|
||||
Status NotificationStatus `query:"status" valid:"optional"`
|
||||
|
||||
SortBy *string `query:"sort_by" valid:"optional"`
|
||||
SortOrder *string `query:"sort_order" valid:"optional"`
|
||||
Limit *int `query:"limit" valid:"optional"`
|
||||
Offset *int `query:"offset" valid:"optional"`
|
||||
Status string `query:"status" valid:"optional"`
|
||||
Method string `query:"method" valid:"optional"`
|
||||
EventType string `query:"event_type" valid:"optional"`
|
||||
Type string `query:"type" valid:"optional"`
|
||||
Recipient string `query:"recipient" valid:"optional"`
|
||||
}
|
||||
|
||||
type NotificationListResponse struct {
|
||||
Notifications []*NotificationResponse `json:"notifications"`
|
||||
Meta *response.Meta `json:"meta"`
|
||||
}
|
||||
|
||||
type NotificationResponse struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Link *string `json:"link"`
|
||||
Priority string `json:"priority"`
|
||||
Type string `json:"type"`
|
||||
EventType string `json:"event_type"`
|
||||
Status string `json:"status"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/asaskevich/govalidator"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"tm/internal/user"
|
||||
"tm/pkg/logger"
|
||||
"tm/pkg/response"
|
||||
)
|
||||
@@ -32,6 +33,7 @@ func NewHandler(service Service, logger logger.Logger) *NotificationHandler {
|
||||
// @Success 201 {object} response.APIResponse "Notification created successfully"
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Security BearerAuth
|
||||
// @Router /admin/v1/notifications [post]
|
||||
func (h *NotificationHandler) Send(c echo.Context) error {
|
||||
var req NotificationRequest
|
||||
@@ -49,3 +51,151 @@ func (h *NotificationHandler) Send(c echo.Context) error {
|
||||
|
||||
return response.Created(c, nil, "Notification sent successfully")
|
||||
}
|
||||
|
||||
// GetNotifications gets notifications
|
||||
// @Summary Get notifications
|
||||
// @Description Get notifications
|
||||
// @Tags Admin-Notification
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param status query string false "Status filter"
|
||||
// @Param method query string false "Method filter"
|
||||
// @Param event_type query string false "Event type filter"
|
||||
// @Param type query string false "Type filter"
|
||||
// @Param recipient query string false "Recipient filter"
|
||||
// @Param limit query int false "Limit results"
|
||||
// @Param offset query int false "Offset results"
|
||||
// @Param sort_by query string false "Sort field"
|
||||
// @Param sort_order query string false "Sort order"
|
||||
// @Success 200 {object} response.APIResponse{data=NotificationListResponse}
|
||||
// @Failure 401 {object} response.APIResponse
|
||||
// @Failure 403 {object} response.APIResponse
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Security BearerAuth
|
||||
// @Router /admin/v1/notifications [get]
|
||||
func (h *NotificationHandler) GetNotifications(c echo.Context) error {
|
||||
form, err := response.Parse[SearchForm](c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", err.Error())
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if _, err := govalidator.ValidateStruct(form); err != nil {
|
||||
return response.ValidationError(c, err.Error(), "")
|
||||
}
|
||||
|
||||
pagination := response.NewPagination(c)
|
||||
|
||||
// Call service
|
||||
notifications, err := h.service.GetNotifications(c.Request().Context(), form, pagination)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to get notifications")
|
||||
}
|
||||
|
||||
return response.Success(c, notifications, "Notifications retrieved successfully")
|
||||
}
|
||||
|
||||
// MyAdminNotifications gets notifications for the admin
|
||||
// @Summary Get notifications for the admin
|
||||
// @Description Get notifications for the admin
|
||||
// @Tags Admin-Notification
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param status query string false "Status filter"
|
||||
// @Param method query string false "Method filter"
|
||||
// @Param event_type query string false "Event type filter"
|
||||
// @Param type query string false "Type filter"
|
||||
// @Param recipient query string false "Recipient filter"
|
||||
// @Param limit query int false "Limit results"
|
||||
// @Param offset query int false "Offset results"
|
||||
// @Param sort_by query string false "Sort field"
|
||||
// @Param sort_order query string false "Sort order"
|
||||
// @Success 200 {object} response.APIResponse{data=NotificationListResponse}
|
||||
// @Failure 401 {object} response.APIResponse
|
||||
// @Failure 403 {object} response.APIResponse
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Security BearerAuth
|
||||
// @Router /admin/v1/notifications/my [get]
|
||||
func (h *NotificationHandler) AdminNotifications(c echo.Context) error {
|
||||
userID, err := user.GetUserIDFromContext(c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "User ID required", "User must be associated with a user")
|
||||
}
|
||||
|
||||
form, err := response.Parse[SearchForm](c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", err.Error())
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if _, err := govalidator.ValidateStruct(form); err != nil {
|
||||
return response.ValidationError(c, err.Error(), "")
|
||||
}
|
||||
|
||||
form.Recipient = userID
|
||||
form.Status = "sent"
|
||||
|
||||
pagination := response.NewPagination(c)
|
||||
|
||||
// Call service
|
||||
notifications, err := h.service.GetNotifications(c.Request().Context(), form, pagination)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to get notifications")
|
||||
}
|
||||
|
||||
return response.Success(c, notifications, "Notifications retrieved successfully")
|
||||
}
|
||||
|
||||
// MyNotifications gets notifications for the user
|
||||
// @Summary Get notifications for the user
|
||||
// @Description Get notifications for the user
|
||||
// @Tags Notification
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param status query string false "Status filter"
|
||||
// @Param method query string false "Method filter"
|
||||
// @Param event_type query string false "Event type filter"
|
||||
// @Param type query string false "Type filter"
|
||||
// @Param recipient query string false "Recipient filter"
|
||||
// @Param limit query int false "Limit results"
|
||||
// @Param offset query int false "Offset results"
|
||||
// @Param sort_by query string false "Sort field"
|
||||
// @Param sort_order query string false "Sort order"
|
||||
// @Success 200 {object} response.APIResponse{data=NotificationListResponse}
|
||||
// @Failure 401 {object} response.APIResponse
|
||||
// @Failure 403 {object} response.APIResponse
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Security BearerAuth
|
||||
// @Router /api/v1/notifications/my [get]
|
||||
func (h *NotificationHandler) CustomerNotifications(c echo.Context) error {
|
||||
userID, err := user.GetUserIDFromContext(c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "User ID required", "User must be associated with a user")
|
||||
}
|
||||
|
||||
form, err := response.Parse[SearchForm](c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", err.Error())
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if _, err := govalidator.ValidateStruct(form); err != nil {
|
||||
return response.ValidationError(c, err.Error(), "")
|
||||
}
|
||||
|
||||
form.Recipient = userID
|
||||
form.Status = "sent"
|
||||
|
||||
pagination := response.NewPagination(c)
|
||||
|
||||
// Call service
|
||||
notifications, err := h.service.GetNotifications(c.Request().Context(), form, pagination)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to get notifications")
|
||||
}
|
||||
|
||||
return response.Success(c, notifications, "Notifications retrieved successfully")
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
// NotificationService defines the interface for notification business logic
|
||||
type Service interface {
|
||||
Send(ctx context.Context, req *NotificationRequest) error
|
||||
GetNotifications(ctx context.Context, req *SearchForm, pagination *response.Pagination) (*NotificationListResponse, error)
|
||||
getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error)
|
||||
getCustomers(ctx context.Context, values []string, target string) ([]notificationRecipient, error)
|
||||
}
|
||||
@@ -138,6 +139,63 @@ func (s *notificationService) Send(ctx context.Context, req *NotificationRequest
|
||||
return nil
|
||||
}
|
||||
|
||||
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,
|
||||
"method": req.Method,
|
||||
"event_type": req.EventType,
|
||||
"type": req.Type,
|
||||
"recipient": req.Recipient,
|
||||
"limit": pagination.Limit,
|
||||
"offset": pagination.Offset,
|
||||
})
|
||||
|
||||
notifications, err := s.sdk.GetNotifications(ctx, ¬ification.GetNotificationsRequest{
|
||||
Status: req.Status,
|
||||
Method: req.Method,
|
||||
EventType: req.EventType,
|
||||
Type: req.Type,
|
||||
UserID: req.Recipient,
|
||||
PerPage: pagination.Limit,
|
||||
Page: pagination.Offset / pagination.Limit,
|
||||
})
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get notifications", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
notificationsResponse := make([]*NotificationResponse, 0)
|
||||
for _, notification := range notifications.Data {
|
||||
notificationsResponse = append(notificationsResponse, &NotificationResponse{
|
||||
ID: notification.ID,
|
||||
UserID: notification.UserID,
|
||||
Title: notification.Title,
|
||||
Description: notification.Message,
|
||||
Link: ¬ification.Link,
|
||||
Priority: notification.Priority,
|
||||
Type: notification.Type,
|
||||
EventType: notification.EventType,
|
||||
Status: notification.Status,
|
||||
Methods: notification.Methods,
|
||||
Metadata: notification.Metadata,
|
||||
ScheduleAt: notification.ScheduledAt,
|
||||
})
|
||||
}
|
||||
|
||||
return &NotificationListResponse{
|
||||
Notifications: notificationsResponse,
|
||||
Meta: &response.Meta{
|
||||
Total: notifications.Pagination.Total,
|
||||
Limit: notifications.Pagination.PerPage,
|
||||
Offset: pagination.Offset,
|
||||
Page: notifications.Pagination.CurrentPage,
|
||||
Pages: notifications.Pagination.TotalPages,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *notificationService) getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error) {
|
||||
recipients := make([]notificationRecipient, 0)
|
||||
offset := 0
|
||||
|
||||
Reference in New Issue
Block a user