Ver Fonte

Customer Phone Number model

- added
Alexey Kim há 2 anos atrás
pai
commit
be80b8475d
3 ficheiros alterados com 53 adições e 2 exclusões
  1. 2 1
      go.mod
  2. 4 1
      go.sum
  3. 47 0
      model/phone.go

+ 2 - 1
go.mod

@@ -7,6 +7,8 @@ require (
 	github.com/Nerzal/gocloak/v11 v11.2.0
 	github.com/golang-jwt/jwt/v4 v4.4.1
 	github.com/gshopify/service-wrapper v0.0.1-alpha
+	github.com/mitchellh/mapstructure v1.5.0
+	github.com/nyaruka/phonenumbers v1.1.2
 	github.com/spf13/pflag v1.0.5
 	github.com/vektah/gqlparser/v2 v2.5.1
 )
@@ -24,7 +26,6 @@ require (
 	github.com/gorilla/mux v1.8.0 // indirect
 	github.com/gorilla/websocket v1.5.0 // indirect
 	github.com/hashicorp/golang-lru v0.5.4 // indirect
-	github.com/mitchellh/mapstructure v1.3.1 // indirect
 	github.com/opentracing/opentracing-go v1.2.0 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/russross/blackfriday/v2 v2.1.0 // indirect

+ 4 - 1
go.sum

@@ -126,14 +126,17 @@ github.com/matryer/moq v0.2.7/go.mod h1:kITsx543GOENm48TUAQyJ9+SAvFSr7iGQXPoth/V
 github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
-github.com/mitchellh/mapstructure v1.3.1 h1:cCBH2gTD2K0OtLlv/Y5H01VQCqmlDxz30kS5Y5bqfLA=
 github.com/mitchellh/mapstructure v1.3.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
+github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
+github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
 github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
 github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
+github.com/nyaruka/phonenumbers v1.1.2 h1:MIDljnA08HCUzgNOrkCYja7CJ5U9ylZ+U3Sge8RWW14=
+github.com/nyaruka/phonenumbers v1.1.2/go.mod h1:cGaEsOrLjIL0iKGqJR5Rfywy86dSkbApEpXuM9KySNA=
 github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
 github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

+ 47 - 0
model/phone.go

@@ -0,0 +1,47 @@
+package model
+
+import (
+	"fmt"
+	"github.com/gshopify/service-wrapper/model"
+	"github.com/mitchellh/mapstructure"
+	"github.com/nyaruka/phonenumbers"
+)
+
+type Phone struct {
+	number   *phonenumbers.PhoneNumber
+	Verified bool `mapstructure:"phone_verified"`
+
+	PhoneNumber string `mapstructure:"phone_number"`
+	PhoneRegion string `mapstructure:"phone_region"`
+}
+
+func ParsePhoneNumber(m map[string]any) (*Phone, error) {
+	var (
+		p   = Phone{}
+		err error
+	)
+
+	if err := mapstructure.Decode(m, &p); err != nil {
+		return nil, err
+	}
+
+	if p.PhoneNumber == "" || p.PhoneRegion == "" {
+		return nil, nil
+	}
+
+	region := model.CountryCode(p.PhoneRegion)
+	if !region.IsValid() {
+		return nil, fmt.Errorf("illegal country code: `%s`", p.PhoneRegion)
+	}
+
+	p.number, err = phonenumbers.Parse(p.PhoneNumber, p.PhoneRegion)
+	if err != nil {
+		return nil, err
+	}
+
+	return &p, nil
+}
+
+func (p *Phone) String() string {
+	return phonenumbers.Format(p.number, phonenumbers.INTERNATIONAL)
+}