Add inquiry status transition error handling and validation improvements
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced InvalidStatusTransitionError to handle invalid status transitions for inquiries, providing clearer error messages. - Updated UpdateInquiryStatusForm to make the Reason field optional and added logic to set a default reason if not provided. - Enhanced form validation tests to cover new status transition error scenarios and validation messages. - Refactored handler methods to utilize new error handling functions for improved response management. This update improves the robustness of inquiry status management by ensuring proper error handling and validation, enhancing user experience during status updates.
This commit is contained in:
+18
-21
@@ -42,14 +42,13 @@ func NewHandler(service Service, logger logger.Logger, hcaptchaVerifier hcaptcha
|
||||
// @Failure 500 {object} response.APIResponse "Internal server error"
|
||||
// @Router /api/v1/inquiries [post]
|
||||
func (h *Handler) CreateInquiry(c echo.Context) error {
|
||||
form, err := response.Parse[CreateInquiryForm](c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", "")
|
||||
var form CreateInquiryForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return handleBindError(c, err)
|
||||
}
|
||||
|
||||
// Validate and sanitize form
|
||||
if err := form.ValidateAndSanitize(h.xssPolicy); err != nil {
|
||||
return response.ValidationError(c, err.Error(), "")
|
||||
return handleValidationError(c, err)
|
||||
}
|
||||
|
||||
// Verify hCaptcha token
|
||||
@@ -64,7 +63,7 @@ func (h *Handler) CreateInquiry(c echo.Context) error {
|
||||
}
|
||||
|
||||
// Call service
|
||||
inquiry, err := h.service.Create(c.Request().Context(), form)
|
||||
inquiry, err := h.service.Create(c.Request().Context(), &form)
|
||||
if err != nil {
|
||||
// Check if it's a duplicate inquiry error
|
||||
if duplicateErr, ok := err.(*DuplicateInquiryError); ok {
|
||||
@@ -110,7 +109,7 @@ func (h *Handler) GetInquiryByID(c echo.Context) error {
|
||||
|
||||
inquiry, err := h.service.GetByID(c.Request().Context(), idStr)
|
||||
if err != nil {
|
||||
return response.NotFound(c, "Inquiry not found")
|
||||
return handleServiceError(c, err, "Failed to retrieve inquiry")
|
||||
}
|
||||
|
||||
return response.Success(c, inquiry, "Inquiry retrieved successfully")
|
||||
@@ -133,14 +132,13 @@ func (h *Handler) GetInquiryByID(c echo.Context) error {
|
||||
func (h *Handler) UpdateInquiryStatus(c echo.Context) error {
|
||||
idStr := c.Param("id")
|
||||
|
||||
form, err := response.Parse[UpdateInquiryStatusForm](c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", "")
|
||||
var form UpdateInquiryStatusForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return handleBindError(c, err)
|
||||
}
|
||||
|
||||
// Validate and sanitize form
|
||||
if err := form.ValidateAndSanitize(h.xssPolicy); err != nil {
|
||||
return response.ValidationError(c, err.Error(), "")
|
||||
return handleValidationError(c, err)
|
||||
}
|
||||
|
||||
// Get user ID from context (assuming it's set by auth middleware)
|
||||
@@ -148,9 +146,9 @@ func (h *Handler) UpdateInquiryStatus(c echo.Context) error {
|
||||
// changedBy := c.Get("user_id").(string)
|
||||
|
||||
// Call service
|
||||
inquiry, err := h.service.UpdateStatus(c.Request().Context(), idStr, form, changedBy)
|
||||
inquiry, err := h.service.UpdateStatus(c.Request().Context(), idStr, &form, changedBy)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, err.Error(), "")
|
||||
return handleServiceError(c, err, "Failed to update inquiry status")
|
||||
}
|
||||
|
||||
return response.Success(c, inquiry, "Inquiry status updated successfully")
|
||||
@@ -174,14 +172,13 @@ func (h *Handler) UpdateInquiryStatus(c echo.Context) error {
|
||||
// @Failure 500 {object} response.APIResponse "Internal server error"
|
||||
// @Router /admin/v1/inquiries [get]
|
||||
func (h *Handler) SearchInquiries(c echo.Context) error {
|
||||
form, err := response.Parse[SearchInquiriesForm](c)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, "Invalid request format", "")
|
||||
var form SearchInquiriesForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
return handleBindError(c, err)
|
||||
}
|
||||
|
||||
// Validate and sanitize form
|
||||
if err := form.ValidateAndSanitize(h.xssPolicy); err != nil {
|
||||
return response.ValidationError(c, err.Error(), "")
|
||||
return handleValidationError(c, err)
|
||||
}
|
||||
|
||||
// Call service
|
||||
@@ -190,7 +187,7 @@ func (h *Handler) SearchInquiries(c echo.Context) error {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
|
||||
inquiries, err := h.service.Search(c.Request().Context(), form, pagination)
|
||||
inquiries, err := h.service.Search(c.Request().Context(), &form, pagination)
|
||||
if err != nil {
|
||||
if response.IsListPaginationError(err) {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
@@ -220,7 +217,7 @@ func (h *Handler) DeleteInquiry(c echo.Context) error {
|
||||
|
||||
err := h.service.Delete(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
return response.BadRequest(c, err.Error(), "")
|
||||
return handleServiceError(c, err, "Failed to delete inquiry")
|
||||
}
|
||||
|
||||
return response.Success(c, map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user