Skip to content

Commit

Permalink
Merge pull request #17 from savannahghi/fetch-services-offered-in-a-f…
Browse files Browse the repository at this point in the history
…acility

feat: fetch services offered in a facility
  • Loading branch information
Muchogoc authored Oct 17, 2023
2 parents 5ee301a + 0d93d3e commit 881a95f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
29 changes: 21 additions & 8 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
44 changes: 40 additions & 4 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 881a95f

Please sign in to comment.