Refactor Notification Management and Enhance Customer Features
- Updated the notification handling logic to utilize a new SDK for sending notifications, improving the flexibility and scalability of the notification system. - Introduced new methods in the notification service for sending notifications to users and customers based on various target audience types, enhancing the notification delivery capabilities. - Added a new endpoint to assign companies to a customer, improving customer management functionalities. - Refactored the customer entity and forms to replace 'CompanyIDs' with 'Companies', ensuring consistency across the data model. - Enhanced API documentation with Swagger comments for the new endpoint and updated notification structures, ensuring clarity for API consumers.
This commit is contained in:
@@ -10,12 +10,12 @@ import (
|
||||
|
||||
// NotificationHandler handles HTTP requests for notification operations
|
||||
type NotificationHandler struct {
|
||||
service NotificationService
|
||||
service Service
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
// NewNotificationHandler creates a new notification handler
|
||||
func NewHandler(service NotificationService, logger logger.Logger) *NotificationHandler {
|
||||
func NewHandler(service Service, logger logger.Logger) *NotificationHandler {
|
||||
return &NotificationHandler{
|
||||
service: service,
|
||||
logger: logger,
|
||||
@@ -33,7 +33,7 @@ func NewHandler(service NotificationService, logger logger.Logger) *Notification
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Router /admin/v1/notifications [post]
|
||||
func (h *NotificationHandler) Create(c echo.Context) error {
|
||||
func (h *NotificationHandler) Send(c echo.Context) error {
|
||||
var req NotificationRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", err.Error())
|
||||
@@ -44,145 +44,10 @@ func (h *NotificationHandler) Create(c echo.Context) error {
|
||||
return response.ValidationError(c, "Validation failed", err.Error())
|
||||
}
|
||||
|
||||
// Create notification
|
||||
resp, err := h.service.Create(c.Request().Context(), &req)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to create notification")
|
||||
// Send notification
|
||||
if err := h.service.Send(c.Request().Context(), &req); err != nil {
|
||||
return response.InternalServerError(c, "Failed to send notification")
|
||||
}
|
||||
|
||||
return response.Created(c, resp, "Notification created successfully")
|
||||
}
|
||||
|
||||
// UpdateNotification updates a notification
|
||||
// @Summary Update notification
|
||||
// @Description Update an existing notification
|
||||
// @Tags Admin-Notification
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Notification ID"
|
||||
// @Param notification body NotificationRequest true "Updated notification information"
|
||||
// @Success 200 {object} response.APIResponse "Notification updated successfully"
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 404 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Router /admin/v1/notifications/{id} [put]
|
||||
func (h *NotificationHandler) UpdateNotification(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
return response.BadRequest(c, "Notification ID is required", "")
|
||||
}
|
||||
|
||||
var req NotificationRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", err.Error())
|
||||
}
|
||||
|
||||
// Update notification
|
||||
_, err := h.service.Update(c.Request().Context(), id, &req)
|
||||
if err != nil {
|
||||
if err.Error() == "notification not found" {
|
||||
return response.NotFound(c, "Notification not found")
|
||||
}
|
||||
return response.BadRequest(c, "Failed to update notification", err.Error())
|
||||
}
|
||||
|
||||
return response.Success(c, nil, "Notification updated successfully")
|
||||
}
|
||||
|
||||
// GetNotifications retrieves notifications for a user
|
||||
// @Summary Get user notifications
|
||||
// @Description Retrieve notifications for a specific user with filtering and pagination
|
||||
// @Tags Admin-Notification
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param recipient query string true "Recipient"
|
||||
// @Param type query string false "Notification type (info, warning, alert, reject)"
|
||||
// @Param priority query string false "Notification priority (low, medium, important)"
|
||||
// @Param status query string false "Delivery status (pending, sent, failed)"
|
||||
// @Param is_read query bool false "Read status"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(20)
|
||||
// @Param sort_by query string false "Sort field" default(created_at)
|
||||
// @Param sort_order query string false "Sort order (asc, desc)" default(desc)
|
||||
// @Success 200 {object} response.APIResponse{data=NotificationsResponse}
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Router /admin/v1/notifications [get]
|
||||
func (h *NotificationHandler) Search(c echo.Context) error {
|
||||
req, err := response.Parse[SearchForm](c)
|
||||
if err != nil {
|
||||
return response.ValidationError(c, "Invalid request data", err.Error())
|
||||
}
|
||||
|
||||
pagination := response.NewPagination(c)
|
||||
|
||||
// Validate request
|
||||
if valid, err := govalidator.ValidateStruct(req); !valid {
|
||||
return response.ValidationError(c, "Validation failed", err.Error())
|
||||
}
|
||||
|
||||
// Get notifications
|
||||
resp, err := h.service.Search(c.Request().Context(), req, pagination)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to get notifications")
|
||||
}
|
||||
|
||||
return response.Success(c, resp, "Notifications retrieved successfully")
|
||||
}
|
||||
|
||||
// Get retrieves a notification by ID
|
||||
// @Summary Get notification by ID
|
||||
// @Description Retrieve a specific notification by its ID
|
||||
// @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
|
||||
// @Router /admin/v1/notifications/{id} [get]
|
||||
func (h *NotificationHandler) Get(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
return response.BadRequest(c, "Notification ID is required", "")
|
||||
}
|
||||
|
||||
// Get notification
|
||||
resp, err := h.service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
if err.Error() == "notification not found" {
|
||||
return response.NotFound(c, "Notification not found")
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to get notification")
|
||||
}
|
||||
|
||||
return response.Success(c, resp, "Notification retrieved successfully")
|
||||
}
|
||||
|
||||
// DeleteNotification deletes a notification
|
||||
// @Summary Delete notification
|
||||
// @Description Delete a specific notification
|
||||
// @Tags Admin-Notification
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Notification ID"
|
||||
// @Success 200 {object} response.APIResponse "Notification deleted successfully"
|
||||
// @Failure 400 {object} response.APIResponse
|
||||
// @Failure 404 {object} response.APIResponse
|
||||
// @Failure 500 {object} response.APIResponse
|
||||
// @Router /admin/v1/notifications/{id} [delete]
|
||||
func (h *NotificationHandler) DeleteNotification(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
return response.BadRequest(c, "Notification ID is required", "")
|
||||
}
|
||||
|
||||
// Delete notification
|
||||
err := h.service.Delete(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
return response.InternalServerError(c, "Failed to delete notification")
|
||||
}
|
||||
|
||||
return response.Success(c, nil, "Notification deleted successfully")
|
||||
return response.Created(c, nil, "Notification sent successfully")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user