eeafe2a625
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.
76 lines
2.5 KiB
Go
76 lines
2.5 KiB
Go
package inquiry
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Inquiry-specific errors
|
|
var (
|
|
ErrDuplicateInquiryByEmail = errors.New("duplicate inquiry by email")
|
|
ErrDuplicateInquiryByPhone = errors.New("duplicate inquiry by phone")
|
|
ErrInquiryNotFound = errors.New("inquiry not found")
|
|
ErrInvalidStatusTransition = errors.New("invalid status transition")
|
|
ErrInquiryAlreadyProcessed = errors.New("inquiry already processed")
|
|
)
|
|
|
|
// DuplicateInquiryError represents a duplicate inquiry error with details
|
|
type DuplicateInquiryError struct {
|
|
Type string `json:"type"` // "email" or "phone"
|
|
ExistingID string `json:"existing_id"` // ID of existing inquiry
|
|
Status string `json:"status"` // Status of existing inquiry
|
|
CreatedAt int64 `json:"created_at"` // When existing inquiry was created
|
|
Message string `json:"message"` // User-friendly message
|
|
}
|
|
|
|
func (e *DuplicateInquiryError) Error() string {
|
|
return e.Message
|
|
}
|
|
|
|
// NewDuplicateInquiryError creates a new duplicate inquiry error
|
|
// InvalidStatusTransitionError is returned when an inquiry status change is not allowed.
|
|
type InvalidStatusTransitionError struct {
|
|
CurrentStatus string
|
|
NewStatus string
|
|
Allowed []string
|
|
}
|
|
|
|
func (e *InvalidStatusTransitionError) Error() string {
|
|
if len(e.Allowed) == 0 {
|
|
return fmt.Sprintf("Inquiry status is already %s and cannot be changed", e.CurrentStatus)
|
|
}
|
|
return fmt.Sprintf(
|
|
"Cannot change inquiry status from %s to %s. Allowed next statuses: %s",
|
|
e.CurrentStatus,
|
|
e.NewStatus,
|
|
strings.Join(e.Allowed, ", "),
|
|
)
|
|
}
|
|
|
|
// NewInvalidStatusTransitionError creates a status transition error with allowed next statuses.
|
|
func NewInvalidStatusTransitionError(currentStatus, newStatus string, allowed []string) *InvalidStatusTransitionError {
|
|
return &InvalidStatusTransitionError{
|
|
CurrentStatus: currentStatus,
|
|
NewStatus: newStatus,
|
|
Allowed: allowed,
|
|
}
|
|
}
|
|
|
|
func NewDuplicateInquiryError(inquiryType, existingID, status string, createdAt int64) *DuplicateInquiryError {
|
|
var message string
|
|
if inquiryType == "email" {
|
|
message = "You already have a pending inquiry with this email address. Please wait for it to be processed before submitting a new one."
|
|
} else {
|
|
message = "A pending inquiry already exists with this phone number. Please wait for it to be processed before submitting a new one."
|
|
}
|
|
|
|
return &DuplicateInquiryError{
|
|
Type: inquiryType,
|
|
ExistingID: existingID,
|
|
Status: status,
|
|
CreatedAt: createdAt,
|
|
Message: message,
|
|
}
|
|
}
|