Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data. Co-authored-by: Cursor <cursoragent@cursor.com>
47 lines
1008 B
Go
47 lines
1008 B
Go
package auth
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
"unicode"
|
||
)
|
||
|
||
func onlyDigits(s string) string {
|
||
var b strings.Builder
|
||
for _, r := range s {
|
||
if unicode.IsDigit(r) {
|
||
b.WriteRune(r)
|
||
}
|
||
}
|
||
return b.String()
|
||
}
|
||
|
||
// NormalizePhone strips a leading +993 / 993 country code, then checks
|
||
// that the remaining local number is 8 digits with prefix 61–65 or 71–72.
|
||
// It returns +993 XX XX XX XX.
|
||
func NormalizePhone(raw string) (string, error) {
|
||
digits := onlyDigits(raw)
|
||
if strings.HasPrefix(digits, "993") {
|
||
digits = digits[3:]
|
||
} else if len(digits) == 9 && digits[0] == '8' {
|
||
digits = digits[1:]
|
||
}
|
||
|
||
if len(digits) != 8 {
|
||
return "", ErrInvalidPhone
|
||
}
|
||
if !validMobilePrefix(digits[:2]) {
|
||
return "", ErrInvalidPhone
|
||
}
|
||
return fmt.Sprintf("+993 %s %s %s %s", digits[0:2], digits[2:4], digits[4:6], digits[6:8]), nil
|
||
}
|
||
|
||
func validMobilePrefix(prefix string) bool {
|
||
n, err := strconv.Atoi(prefix)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
return (n >= 61 && n <= 65) || (n >= 71 && n <= 72)
|
||
}
|