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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user