fix: canonicalize phone numbers as E.164
This commit is contained in:
@@ -144,15 +144,37 @@ func backfillUserPhones(ctx context.Context, conn *sqlx.Conn, scoped bool) error
|
||||
if scoped {
|
||||
prefix = "tenant_id,"
|
||||
}
|
||||
if err = conn.SelectContext(ctx, &existing, "SELECT "+prefix+"id AS user_id,phone AS value FROM im_users WHERE phone IS NOT NULL AND phone<>''"); err != nil {
|
||||
orderBy := "id"
|
||||
if scoped {
|
||||
orderBy = "tenant_id,id"
|
||||
}
|
||||
if err = conn.SelectContext(ctx, &existing, "SELECT "+prefix+"id AS user_id,phone AS value FROM im_users WHERE phone IS NOT NULL AND phone<>'' ORDER BY "+orderBy); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, item := range existing {
|
||||
seen[identityUniqueKey(item.TenantID, item.Value, scoped)] = true
|
||||
canonical, err := normalizeUserPhone(item.Value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("normalize existing phone for user %d: %w", item.UserID, err)
|
||||
}
|
||||
if canonical != item.Value {
|
||||
query := "UPDATE im_users SET phone=? WHERE id=?"
|
||||
args := []any{canonical, item.UserID}
|
||||
if scoped {
|
||||
query = "UPDATE im_users SET phone=? WHERE tenant_id=? AND id=?"
|
||||
args = []any{canonical, item.TenantID, item.UserID}
|
||||
}
|
||||
if _, err = conn.ExecContext(ctx, query, args...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
seen[identityUniqueKey(item.TenantID, canonical, scoped)] = true
|
||||
}
|
||||
updatedUsers := make(map[string]bool)
|
||||
for _, item := range candidates {
|
||||
item.Value = strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(item.Value, "tel:"), "basic:"))
|
||||
item.Value, err = normalizeUserPhone(item.Value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("normalize legacy phone for user %d: %w", item.UserID, err)
|
||||
}
|
||||
userKey := identityUserKey(item.TenantID, item.UserID, scoped)
|
||||
valueKey := identityUniqueKey(item.TenantID, item.Value, scoped)
|
||||
if item.Value == "" || updatedUsers[userKey] || seen[valueKey] {
|
||||
|
||||
Reference in New Issue
Block a user