diff --git a/healthcrm.go b/healthcrm.go index 42ef53d..d9c7af4 100644 --- a/healthcrm.go +++ b/healthcrm.go @@ -143,18 +143,31 @@ func (h *HealthCRMLib) UpdateFacility(ctx context.Context, id string, updatePayl // GetFacilityServices fetches services associated with facility func (h *HealthCRMLib) GetFacilityServices(ctx context.Context, facilityID string) (*FacilityServicePage, error) { path := "/v1/facilities/services/" - queryParams := make(map[string]string) - queryParams["facility"] = facilityID - response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil) - if err != nil { - return nil, err + + var resp *http.Response + if facilityID != "" { + queryParams := make(map[string]string) + queryParams["facility"] = facilityID + response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil) + if err != nil { + return nil, err + } + + resp = response + } else { + response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil) + if err != nil { + return nil, err + } + + resp = response } - if response.StatusCode != http.StatusOK { - return nil, fmt.Errorf("unable to get facility services with status code: %v", response.StatusCode) + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unable to get facility services with status code: %v", resp.StatusCode) } - respBytes, err := io.ReadAll(response.Body) + respBytes, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) } diff --git a/healthcrm_test.go b/healthcrm_test.go index 1c905c8..735c894 100644 --- a/healthcrm_test.go +++ b/healthcrm_test.go @@ -505,13 +505,27 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) { wantErr bool }{ { - name: "Happy case: get facility services", + name: "Happy case: get all services", + args: args{ + ctx: context.Background(), + }, + wantErr: false, + }, + { + name: "Happy case: get services in a facility", args: args{ ctx: context.Background(), - facilityID: gofakeit.UUID(), + facilityID: "1b5baf1a-1aec-48bd-951c-01896e5fe5a8", }, wantErr: false, }, + { + name: "Sad case: unable to get all services", + args: args{ + ctx: context.Background(), + }, + wantErr: true, + }, { name: "Sad case: unable to get facility services", args: args{ @@ -531,7 +545,7 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.name == "Happy case: get facility services" { + if tt.name == "Happy case: get all services" { path := fmt.Sprintf("%s/v1/facilities/services/", BaseURL) httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) { resp := &FacilityServicePage{ @@ -547,12 +561,34 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) { return httpmock.NewJsonResponse(http.StatusOK, resp) }) } - if tt.name == "Sad case: unable to get facility services" { + if tt.name == "Happy case: get services in a facility" { + path := fmt.Sprintf("%s/v1/facilities/services/?facility=1b5baf1a-1aec-48bd-951c-01896e5fe5a8", BaseURL) + httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) { + resp := &FacilityServicePage{ + Results: []FacilityService{ + { + ID: gofakeit.UUID(), + Name: gofakeit.BeerName(), + Description: gofakeit.HipsterSentence(56), + Identifiers: []*ServiceIdentifier{}, + }, + }, + } + return httpmock.NewJsonResponse(http.StatusOK, resp) + }) + } + if tt.name == "Sad case: unable to get all services" { path := fmt.Sprintf("%s/v1/facilities/services/", BaseURL) 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 get facility services" { + path := fmt.Sprintf("%s/v1/facilities/services/?facility=1b5baf1a-1aec-48bd-951c-01896e5fe5a8", BaseURL) + 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{