From 39c614fb981b83d18cfd996da1986ea4646293bc Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Mon, 21 May 2018 18:47:13 +0200 Subject: [PATCH] Upstream sync --- config.dev.toml | 5 +- config.prd.toml | 3 ++ data/datamysql/zipcode.go | 2 +- infra/config/config.go | 8 +++ server/router/usersroute/controller.go | 70 +++++++++++++++++++++----- 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/config.dev.toml b/config.dev.toml index c431538..77cbd91 100644 --- a/config.dev.toml +++ b/config.dev.toml @@ -80,4 +80,7 @@ secret = "hK4Yc9jtBrxaZTr4UU2VsfX9" [blue365] url = "https://api-cloud.bcbs.com/blue365deals-stg/v1/validatePrefix/" key = "jcn5xjxvarc96rtjxp25dctj" -secret = "6XdEusG2w2PWWXsfXVyweQnY" \ No newline at end of file +secret = "6XdEusG2w2PWWXsfXVyweQnY" + +[eligibility] +url = "https://portal-api.dev.bcbsinstitute.com/v1/nemt/eligibility" \ No newline at end of file diff --git a/config.prd.toml b/config.prd.toml index cf9324c..dacdcc8 100644 --- a/config.prd.toml +++ b/config.prd.toml @@ -81,3 +81,6 @@ secret = "vFhNeWE8JdJpbDZQtcm6AHjX" url = "https://api-cloud.bcbs.com/blue365deals-stg/v1/validatePrefix/" key = "jcn5xjxvarc96rtjxp25dctj" secret = "6XdEusG2w2PWWXsfXVyweQnY" + +[eligibility] +url = "https://portal-api.bcbsinstitute.com/v1/nemt/eligibility" \ No newline at end of file diff --git a/data/datamysql/zipcode.go b/data/datamysql/zipcode.go index a228dad..872e2e2 100644 --- a/data/datamysql/zipcode.go +++ b/data/datamysql/zipcode.go @@ -24,7 +24,7 @@ func (c *zipcodeRepo) getQuery() string { a.participating_zip_code_id, a.participating_zip_code_uuid, a.zipcode, - a.participating + (IFNULL(a.participating, b'0') = b'1') participating FROM tab_participating_zip_code a ` ) diff --git a/infra/config/config.go b/infra/config/config.go index d3b1f98..d17cdb8 100644 --- a/infra/config/config.go +++ b/infra/config/config.go @@ -25,6 +25,7 @@ type Config struct { Blue365 Blue365Config Email EmailConfig GoogleShortener GoogleShortenerConfig + Eligibility EligibilityConfig } // AppConfig represents the configuration values about the application. @@ -137,6 +138,10 @@ type GoogleShortenerConfig struct { SecretKey string } +type EligibilityConfig struct { + Url string +} + // Read returns the configuration values, // based on the configuration files and environment variables. func Read() (*Config, error) { @@ -238,6 +243,9 @@ func Read() (*Config, error) { ClientID: viper.GetString("google-shortener.client-id"), SecretKey: viper.GetString("google-shortener.secret-key"), }, + Eligibility : EligibilityConfig{ + Url : viper.GetString("eligibility.url"), + }, }, nil } diff --git a/server/router/usersroute/controller.go b/server/router/usersroute/controller.go index b06d0d6..f6bad4d 100644 --- a/server/router/usersroute/controller.go +++ b/server/router/usersroute/controller.go @@ -6,6 +6,10 @@ import ( "math/rand" "sync" "time" + "net/http" + "encoding/json" + "bytes" + "strings" "bitbucket.org/nemt/nemt-portal-api/application/applicationservice" "bitbucket.org/nemt/nemt-portal-api/application/third/eligibility/bcbsi" @@ -22,6 +26,10 @@ import ( "googlemaps.github.io/maps" ) +const ( + zipcodeTrimLength = 5 +) + var ( instance *controller once sync.Once @@ -395,15 +403,52 @@ func (c *controller) handleMember(ctx echo.Context) error { eligibility.ServiceInfo.DateOfService = time.Now() eligibility.ServiceInfo.ServiceTypeCodes = []string{"30"} + /* resp, err := c.bcbsi.BXE.Get271(eligibility) if err != nil { fmt.Println("Eligibility Not Found or Error: ", err.Error()) return routeutils.ResponseAPINotEligibleError(ctx) } + */ + + //This part is emulating eligibility check for testing purposes + client := &http.Client{} + eligibilityJson, err := json.Marshal(eligibility) + if err != nil { + return routeutils.HandleAPIError(ctx, err) + } + req, _ := http.NewRequest("POST", c.cfg.Eligibility.Url, bytes.NewBuffer(eligibilityJson)) + req.Header.Add("App", c.cfg.HTTP.Auth.AppKey) + req.Header.Add("Token", ctx.Request().Header.Get("Token")) + req.Header.Add("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return routeutils.HandleAPIError(ctx, err) + } + defer resp.Body.Close() + + if resp.StatusCode < 200 || resp.StatusCode > 300 { + return routeutils.ResponseAPINotEligibleWithMessageError(ctx, "Cannot check eligibility") + } + + eligibilityResponse := viewmodel.Interchange271{} + decoder := json.NewDecoder(resp.Body) + err = decoder.Decode(&eligibilityResponse) + if err != nil { + return routeutils.ResponseAPINotEligibleWithMessageError(ctx, "Cannot check eligibility") + } + //================================================================ + + if len(eligibilityResponse.Division.HealthCareEligibilityResponse.LoopHL0030) < 1 { + return routeutils.ResponseAPINotEligibleWithMessageError(ctx, "Cannot check eligibility") + } + address := viewmodel.Address{} - header := resp.Division.HealthCareEligibilityResponse.LoopHL0030[0].HL_0460[0].HL_0890[0].NM1_0920[0].N3_0950 - body := resp.Division.HealthCareEligibilityResponse.LoopHL0030[0].HL_0460[0].HL_0890[0].NM1_0920[0].N4_0960 + //header := resp.Division.HealthCareEligibilityResponse.LoopHL0030[0].HL_0460[0].HL_0890[0].NM1_0920[0].N3_0950 + //body := resp.Division.HealthCareEligibilityResponse.LoopHL0030[0].HL_0460[0].HL_0890[0].NM1_0920[0].N4_0960 + header := eligibilityResponse.Division.HealthCareEligibilityResponse.LoopHL0030[0].HL_0460[0].HL_0890[0].NM1_0920[0].N3_0950 + body := eligibilityResponse.Division.HealthCareEligibilityResponse.LoopHL0030[0].HL_0460[0].HL_0890[0].NM1_0920[0].N4_0960 address.AddressTypeName = "Home" address.AddressType = "home" @@ -412,21 +457,22 @@ func (c *controller) handleMember(ctx echo.Context) error { address.CreatedUserUUID = authUser.ID address.User = user - zipCodeFromAddress := body.N403 - zipCodeObj, err := c.svc.Zipcodes.GetByParticipatingZipcode(zipCodeFromAddress) + cleanZipcode := strings.TrimSpace(body.N403) + trimmedZipcode := cleanZipcode + if len(cleanZipcode) > zipcodeTrimLength { + trimmedZipcode = cleanZipcode[:zipcodeTrimLength] + } + + _, err = c.svc.Zipcodes.GetByParticipatingZipcode(trimmedZipcode) if err != nil{ - return routeutils.HandleAPIError(ctx, err) + return routeutils.ResponseAPINotEligibleWithMessageError(ctx, "Member's Home zipcode, " + trimmedZipcode + ", is not currently eligible for participation in this program") } - if !zipCodeObj.Participating { - return routeutils.ResponseAPINotEligibleWithMessageError(ctx, "Member's Home zipcode, " + zipCodeFromAddress + ", is not currently eligible for participation in this program") - } - googleMapsAPI, err := maps.NewClient(maps.WithClientIDAndSignature("gme-bluecrossandblue1", "msqgD-jdqCyR0M_1u5C1HION5iI=")) if err != nil { fmt.Println("Error to instantiate googles api: ", err.Error()) - return routeutils.ResponseAPINotEligibleError(ctx) + return routeutils.HandleAPIError(ctx,err) } r := &maps.GeocodingRequest{ @@ -436,7 +482,7 @@ func (c *controller) handleMember(ctx echo.Context) error { result, err := googleMapsAPI.Geocode(context.Background(), r) if err != nil { fmt.Println("Error to instantiate googles api: ", err.Error()) - return routeutils.ResponseAPINotEligibleError(ctx) + return routeutils.HandleAPIError(ctx,err) } if len(result) > 0 { @@ -446,7 +492,7 @@ func (c *controller) handleMember(ctx echo.Context) error { _, err := c.svc.Users.SaveAddress(address) if err != nil { fmt.Println("Error to save address: ", err.Error()) - return routeutils.ResponseAPINotEligibleError(ctx) + return routeutils.HandleAPIError(ctx,err) } }