Files
Mazyar 22487eaef5 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.
2026-06-06 20:34:45 +03:30

43 lines
1.0 KiB
Go

package customer
import (
"errors"
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
}
}