fix lint errors

This commit is contained in:
Ruslan Efanov 2024-12-18 17:27:18 +03:00
parent c72cdebd4b
commit 719d89fafb
2 changed files with 29 additions and 24 deletions

View File

@ -3,19 +3,21 @@ package util
import (
"errors"
"fmt"
phoneiso3166 "github.com/onlinecity/go-phone-iso3166"
pn "github.com/ttacon/libphonenumber"
"regexp"
"slices"
"strconv"
"strings"
phoneiso3166 "github.com/onlinecity/go-phone-iso3166"
pn "github.com/ttacon/libphonenumber"
)
const (
MinPhoneSymbolCount = 5
CountryPhoneCodeDE = 49
CountryPhoneCodeAG = 54
CountryPhoneCodeMX = 52
// CountryPhoneCodeMXWA For Whatsapp
// CountryPhoneCodeMXWA For Whatsapp.
CountryPhoneCodeMXWA = 521
CountryPhoneCodeUS = "1443"
CountryPhoneCodePS = 970
@ -31,7 +33,7 @@ var (
undefinedUSCodes = []string{"1445", "1945", "1840", "1448", "1279", "1839"}
)
// FormatNumberForWA forms a number in E164 format without `+` symbol to send to whatsapp
// FormatNumberForWA forms a number in E164 format without `+` symbol to send to whatsapp.
func FormatNumberForWA(number string) (string, error) {
parsedPhone, err := ParsePhone(number)
@ -55,7 +57,7 @@ func FormatNumberForWA(number string) (string, error) {
}
// FormatNumberForMG forms a number in E164 format without `+` symbol to send to Message Gateway
// TODO Возможно, нет смысла в этих функция, так как в КР и 360 будет своя логика
// TODO Возможно, нет смысла в этих функция, так как в КР и 360 будет своя логика.
func FormatNumberForMG(number string) (string, error) {
parsedPhone, err := ParsePhone(number)
@ -79,10 +81,10 @@ func FormatNumberForMG(number string) (string, error) {
}
// ParsePhone this function parses the number as a string
// Mexican numbers may not have a 1 after the country code 52
// Mexican numbers may not have a 1 after the country code 52.
func ParsePhone(phoneNumber string) (*pn.PhoneNumber, error) {
trimmedPhone := regexp.MustCompile(`\D+`).ReplaceAllString(phoneNumber, "")
if len(trimmedPhone) < 5 {
if len(trimmedPhone) < MinPhoneSymbolCount {
return nil, ErrPhoneTooShort
}
@ -157,7 +159,7 @@ func Add9AGIFNeed(parsedPhone *pn.PhoneNumber) string {
formattedPhone := pn.Format(parsedPhone, pn.E164)
numberWOCountry := fmt.Sprintf("%d", parsedPhone.GetNationalNumber())
if len(numberWOCountry) == 10 {
if len(numberWOCountry) == 10 { // nolint:mnd
formattedPhone = fmt.Sprintf("+%d%s", CountryPhoneCodeAG, "9"+numberWOCountry)
}
@ -166,7 +168,7 @@ func Add9AGIFNeed(parsedPhone *pn.PhoneNumber) string {
// getGermanNationalNumber some German numbers may not be parsed correctly.
// For example, for 491736276098 libphonenumber.PhoneNumber.NationalNumber
// will contain the country code(49). This function fix it and return correct libphonenumber.PhoneNumber
// will contain the country code(49). This function fix it and return correct libphonenumber.PhoneNumber.
func getGermanNationalNumber(phone string, parsedPhone *pn.PhoneNumber) (uint64, error) {
result := parsedPhone.GetNationalNumber()
@ -178,13 +180,13 @@ func getGermanNationalNumber(phone string, parsedPhone *pn.PhoneNumber) (uint64,
return 0, err
}
result = uint64(number)
result = uint64(number) //nolint:gosec
}
return result, nil
}
// For UZ numbers where 8 is deleted after the country code
// For UZ numbers where 8 is deleted after the country code.
func getUzbekistanNationalNumber(phone string, parsedPhone *pn.PhoneNumber) (uint64, error) {
result := parsedPhone.GetNationalNumber()
numberWithEight := fmt.Sprintf("8%d", parsedPhone.GetNationalNumber())
@ -195,7 +197,7 @@ func getUzbekistanNationalNumber(phone string, parsedPhone *pn.PhoneNumber) (uin
return 0, err
}
result = uint64(number)
result = uint64(number) //nolint:gosec
}
return result, nil

View File

@ -1,27 +1,30 @@
package util
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParsePhone(t *testing.T) {
t.Run("russian numers", func(t *testing.T) {
n := "+88002541213"
pn, err := ParsePhone(n)
require.NoError(t, err)
assert.Equal(t, uint64(8002541213), pn.GetNationalNumber())
assert.Equal(t, int32(7), pn.GetCountryCode())
n = "+78002541213"
pn, err = ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, pn)
assert.Equal(t, uint64(8002541213), pn.GetNationalNumber())
assert.Equal(t, int32(7), pn.GetCountryCode())
n = "89521548787"
pn, err = ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(9521548787), pn.GetNationalNumber())
assert.Equal(t, int32(7), pn.GetCountryCode())
})
@ -29,13 +32,13 @@ func TestParsePhone(t *testing.T) {
t.Run("german numbers", func(t *testing.T) {
n := "491736276098"
pn, err := ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(1736276098), pn.GetNationalNumber())
assert.Equal(t, int32(CountryPhoneCodeDE), pn.GetCountryCode())
n = "4915229457499"
pn, err = ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(15229457499), pn.GetNationalNumber())
assert.Equal(t, int32(CountryPhoneCodeDE), pn.GetCountryCode())
})
@ -43,7 +46,7 @@ func TestParsePhone(t *testing.T) {
t.Run("mexican number", func(t *testing.T) {
n := "5219982418333"
pn, err := ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(9982418333), pn.GetNationalNumber())
assert.Equal(t, int32(CountryPhoneCodeMX), pn.GetCountryCode())
})
@ -51,7 +54,7 @@ func TestParsePhone(t *testing.T) {
t.Run("palestine number", func(t *testing.T) {
n := "970567800663"
pn, err := ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(567800663), pn.GetNationalNumber())
assert.Equal(t, int32(CountryPhoneCodePS), pn.GetCountryCode())
})
@ -59,7 +62,7 @@ func TestParsePhone(t *testing.T) {
t.Run("argentine number", func(t *testing.T) {
n := "5491131157821"
pn, err := ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(91131157821), pn.GetNationalNumber())
assert.Equal(t, int32(CountryPhoneCodeAG), pn.GetCountryCode())
})
@ -67,7 +70,7 @@ func TestParsePhone(t *testing.T) {
t.Run("uzbekistan number", func(t *testing.T) {
n := "998882207724"
pn, err := ParsePhone(n)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(882207724), pn.GetNationalNumber())
assert.Equal(t, int32(CountryPhoneCodeUZ), pn.GetCountryCode())
})