53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package regionimgroup
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (s *Service) resolveUserRegion(ctx context.Context, sysOrigin string, userID int64) (string, error) {
|
|
var user model.UserBaseInfo
|
|
err := s.db.WithContext(ctx).Where("id = ?", userID).First(&user).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return "", NewAppError(http.StatusBadRequest, "voice_room_region_missing", "user region is missing")
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
countryCode := strings.ToUpper(strings.TrimSpace(user.CountryCode))
|
|
if countryCode == "" {
|
|
return "", NewAppError(http.StatusBadRequest, "voice_room_region_missing", "user country code is missing")
|
|
}
|
|
|
|
if s != nil && s.regionResolver != nil {
|
|
regionCode, err := s.regionResolver.ResolveRegionCodeByCountryCode(ctx, sysOrigin, countryCode)
|
|
if err == nil {
|
|
if normalized := normalizeRegionCode(regionCode); normalized != "" {
|
|
return normalized, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
var mapping model.VoiceRoomRegionCountry
|
|
err = s.db.WithContext(ctx).
|
|
Where("sys_origin = ? AND country_code = ? AND enabled = ?", sysOrigin, countryCode, true).
|
|
First(&mapping).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return countryCode, nil
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
regionCode := strings.ToUpper(strings.TrimSpace(mapping.RegionCode))
|
|
if regionCode == "" {
|
|
return countryCode, nil
|
|
}
|
|
return regionCode, nil
|
|
}
|