fix(user,customer): return 409 on phone duplicate-key races
Map E11000 from the per-collection phone index to friendly conflict errors and skip redundant GetByPhone when the phone is unchanged. Not blocking: IAT test DB may need a one-off dedupe for index build; phone uniqueness is per collection (users vs customers), not global.
This commit is contained in:
@@ -1,7 +1,42 @@
|
||||
package customer
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
|
||||
var (
|
||||
ErrCustomerNotFound = errors.New("customer not found")
|
||||
orm "tm/pkg/mongo"
|
||||
)
|
||||
|
||||
var ErrCustomerNotFound = errors.New("customer not found")
|
||||
|
||||
func mapCustomerWriteError(err error) error {
|
||||
if err == nil || !orm.IsDuplicateKeyError(err) {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case orm.DuplicateKeyMatchesField(err, "phone"):
|
||||
return errors.New("customer with this phone number already exists")
|
||||
case orm.DuplicateKeyMatchesField(err, "email"):
|
||||
return errors.New("customer with this email already exists")
|
||||
case orm.DuplicateKeyMatchesField(err, "username"):
|
||||
return errors.New("customer with this username already exists")
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func isCustomerConflictError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
switch err.Error() {
|
||||
case "customer with this phone number already exists",
|
||||
"customer with this email already exists",
|
||||
"customer with this username already exists",
|
||||
"company with this name already exists",
|
||||
"company with this registration number already exists",
|
||||
"company with this tax ID already exists":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,12 +53,7 @@ func (h *Handler) CreateCustomer(c echo.Context) error {
|
||||
|
||||
customer, err := h.service.Register(c.Request().Context(), form)
|
||||
if err != nil {
|
||||
if err.Error() == "customer with this email already exists" ||
|
||||
err.Error() == "customer with this username already exists" ||
|
||||
err.Error() == "customer with this phone number already exists" ||
|
||||
err.Error() == "company with this name already exists" ||
|
||||
err.Error() == "company with this registration number already exists" ||
|
||||
err.Error() == "company with this tax ID already exists" {
|
||||
if isCustomerConflictError(err) {
|
||||
return response.Conflict(c, err.Error())
|
||||
}
|
||||
if strings.HasPrefix(err.Error(), "invalid company ID") || err.Error() == "invalid username format" {
|
||||
@@ -127,12 +122,7 @@ func (h *Handler) UpdateCustomer(c echo.Context) error {
|
||||
if err.Error() == "customer not found" {
|
||||
return response.NotFound(c, "Customer not found")
|
||||
}
|
||||
if err.Error() == "customer with this email already exists" ||
|
||||
err.Error() == "customer with this username already exists" ||
|
||||
err.Error() == "customer with this phone number already exists" ||
|
||||
err.Error() == "company with this name already exists" ||
|
||||
err.Error() == "company with this registration number already exists" ||
|
||||
err.Error() == "company with this tax ID already exists" {
|
||||
if isCustomerConflictError(err) {
|
||||
return response.Conflict(c, err.Error())
|
||||
}
|
||||
if strings.HasPrefix(err.Error(), "invalid company ID") {
|
||||
|
||||
@@ -156,6 +156,9 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
||||
// Save to database
|
||||
err = s.repository.Register(ctx, customer)
|
||||
if err != nil {
|
||||
if mapped := mapCustomerWriteError(err); mapped != err {
|
||||
return nil, mapped
|
||||
}
|
||||
s.logger.Error("Failed to create customer", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"email": form.Email,
|
||||
@@ -424,6 +427,9 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
|
||||
// Update in database
|
||||
err = s.repository.Update(ctx, customer)
|
||||
if err != nil {
|
||||
if mapped := mapCustomerWriteError(err); mapped != err {
|
||||
return nil, mapped
|
||||
}
|
||||
s.logger.Error("Failed to update customer", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": id,
|
||||
|
||||
Reference in New Issue
Block a user