From 273b98352f26712752e91fec0bd6407826769f8e Mon Sep 17 00:00:00 2001 From: Robin Muhia <109961694+robinmuhia@users.noreply.github.com> Date: Mon, 10 Jun 2024 16:10:11 +0300 Subject: [PATCH] feat: add multiple facilityIDs search (#41) --- healthcrm.go | 49 ++++++++++++++++++ healthcrm_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++ output.go | 7 ++- 3 files changed, 181 insertions(+), 1 deletion(-) diff --git a/healthcrm.go b/healthcrm.go index dd21c1a..1ed4630 100644 --- a/healthcrm.go +++ b/healthcrm.go @@ -458,3 +458,52 @@ func (h *HealthCRMLib) GetMultipleServices(ctx context.Context, servicesIDs []st return services.Results, nil } + +// GetMultipleFacilities is used to fetch multiple facilities +// +// Parameters: +// - facilityIDs: A parameter that is a list of IDs specifying one or more +// facility IDs. Facility identifiers, contacts, services and business hours linked to a facility will be +// included in the results. You can **ONLY** pass a single or multiple facility +// IDs which should be of type **UUID** (e.g., GetMultipleFacilities(ctx, []string{"0fee2792-dffc-40d3-a744-2a70732b1053", +// "56c62083-c7b4-4055-8d44-6cc7446ac1d0", "8474ea55-8ede-4bc6-aa67-f53ed5456a03"})). +func (h *HealthCRMLib) GetMultipleFacilities(ctx context.Context, facilityIDs []string) ([]*FacilityOutput, error) { + if len(facilityIDs) < 1 || facilityIDs == nil { + return nil, fmt.Errorf("no facility IDs provided") + } + + searchParameter := facilityIDs[0] + for idx, id := range facilityIDs { + if idx == 0 { + continue + } + + searchParameter += fmt.Sprintf(",%s", id) + } + + path := fmt.Sprintf("/v1/facilities/facilities?facility_ids=%s", searchParameter) + + response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil) + if err != nil { + return nil, err + } + + defer response.Body.Close() + + respBytes, err := io.ReadAll(response.Body) + if err != nil { + return nil, fmt.Errorf("could not read response: %w", err) + } + + if response.StatusCode != http.StatusOK { + return nil, errors.New(string(respBytes)) + } + + var facilities *FacilityOutputs + err = json.Unmarshal(respBytes, &facilities) + if err != nil { + return nil, err + } + + return facilities.Results, nil +} diff --git a/healthcrm_test.go b/healthcrm_test.go index dede814..2a8c562 100644 --- a/healthcrm_test.go +++ b/healthcrm_test.go @@ -1472,3 +1472,129 @@ func TestHealthCRMLib_GetMultipleServices(t *testing.T) { }) } } + +func TestHealthCRMLib_GetMultipleFacilities(t *testing.T) { + type args struct { + ctx context.Context + facilityIDs []string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy case: get list of facilities", + args: args{ + ctx: context.Background(), + facilityIDs: []string{ + "556a1dd9-fbb5-40c2-a623-dde9a2335597", + "7f59c528-8d9e-4a97-a9e5-bea7d7938c0e", + "b8246d32-b9e7-422c-b3bb-a1066dec8561", + }, + }, + wantErr: false, + }, + { + name: "Sad case: no facility IDs provided(empty list)", + args: args{ + ctx: context.Background(), + facilityIDs: []string{}, + }, + wantErr: true, + }, + { + name: "Sad case: no facility IDs provided(nil)", + args: args{ + ctx: context.Background(), + facilityIDs: nil, + }, + wantErr: true, + }, + { + name: "Sad case: unable to get services", + args: args{ + ctx: context.Background(), + facilityIDs: []string{ + "556a1dd9-fbb5-40c2-a623-dde9a2335597", + "7f59c528-8d9e-4a97-a9e5-bea7d7938c0e", + "b8246d32-b9e7-422c-b3bb-a1066dec8561", + }, + }, + wantErr: true, + }, + { + name: "Sad case: unable to make request", + args: args{ + ctx: context.Background(), + facilityIDs: []string{ + "556a1dd9-fbb5-40c2-a623-dde9a2335597", + "7f59c528-8d9e-4a97-a9e5-bea7d7938c0e", + "b8246d32-b9e7-422c-b3bb-a1066dec8561", + }, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := fmt.Sprintf("%s/v1/facilities/facilities?facility_ids=556a1dd9-fbb5-40c2-a623-dde9a2335597,7f59c528-8d9e-4a97-a9e5-bea7d7938c0e,b8246d32-b9e7-422c-b3bb-a1066dec8561", BaseURL) + + if tt.name == "Happy case: get list of facilities" { + httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) { + resp := FacilityOutputs{ + Results: []*FacilityOutput{ + { + ID: gofakeit.UUID(), + Name: "Oxygen", + Description: "158211", + }, + { + ID: gofakeit.UUID(), + Name: "Oxygen", + Description: "158211", + }, + }, + } + return httpmock.NewJsonResponse(http.StatusOK, resp) + }) + } + + if tt.name == "Sad case: unable to get services" { + httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) { + return httpmock.NewJsonResponse(http.StatusBadGateway, nil) + }) + } + + if tt.name == "Sad case: unable to make request" { + httpmock.RegisterResponder(http.MethodPost, fmt.Sprintf("%s/oauth2/token/", serverutils.MustGetEnvVar("HEALTH_CRM_AUTH_SERVER_ENDPOINT")), func(r *http.Request) (*http.Response, error) { + resp := authutils.OAUTHResponse{ + Scope: "", + ExpiresIn: 3600, + AccessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", + RefreshToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", + TokenType: "Bearer", + } + return httpmock.NewJsonResponse(http.StatusBadRequest, resp) + }) + } + + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + MockAuthenticate() + + h, err := NewHealthCRMLib() + if err != nil { + t.Errorf("unable to initialize sdk: %v", err) + } + + _, err = h.GetMultipleFacilities(tt.args.ctx, tt.args.facilityIDs) + if (err != nil) != tt.wantErr { + t.Errorf("HealthCRMLib.GetMultipleFacilities() error = %v, wantErr %v", err, tt.wantErr) + return + } + }) + } +} diff --git a/output.go b/output.go index b2e2459..da23f84 100644 --- a/output.go +++ b/output.go @@ -106,7 +106,12 @@ type ProfileOutput struct { SladeCode string `json:"slade_code"` } -// FacilityServices is used to get a list of Facility Services +// FacilityServices is used to get a list of facility Services type FacilityServices struct { Results []*FacilityService `json:"results"` } + +// FacilityOutputs is used to get a list of facilities +type FacilityOutputs struct { + Results []*FacilityOutput `json:"results"` +}