Skip to content

Commit

Permalink
Merge pull request #19 from savannahghi/get-facilities-offering-a-ser…
Browse files Browse the repository at this point in the history
…vice

feat: get facilities offering a service
  • Loading branch information
Muchogoc authored Oct 17, 2023
2 parents b4e678f + 3717f60 commit 2d376d1
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
30 changes: 30 additions & 0 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,33 @@ func (h *HealthCRMLib) GetFacilityServices(ctx context.Context, facilityID strin

return facilityServicePage, nil
}

// GetFacilitiesOfferingAService fetches the facilities that offer a particular service
func (h *HealthCRMLib) GetFacilitiesOfferingAService(ctx context.Context, serviceID string) (*FacilityPage, error) {
path := "/v1/facilities/facilities/"

queryParams := make(map[string]string)
queryParams["service"] = serviceID
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
if err != nil {
return nil, err
}

if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to get facilities offering service with status code: %v", response.StatusCode)
}

respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}

var output *FacilityPage

err = json.Unmarshal(respBytes, &output)
if err != nil {
return nil, err
}

return output, nil
}
102 changes: 102 additions & 0 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,105 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) {
})
}
}

func TestHealthCRMLib_GetFacilitiesOfferingAService(t *testing.T) {
type args struct {
ctx context.Context
serviceID string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: get facilities offering a service",
args: args{
ctx: context.Background(),
serviceID: "227305a7-b9a5-4ca7-a211-71210d68206c",
},
wantErr: false,
},
{
name: "Sad case: unable to get facilities offering a service",
args: args{
ctx: context.Background(),
},
wantErr: true,
},
{
name: "Sad case: unable to make request",
args: args{
ctx: context.Background(),
serviceID: gofakeit.UUID(),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Happy case: get facilities offering a service" {
path := fmt.Sprintf("%s/v1/facilities/facilities/", BaseURL)
httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) {
resp := &FacilityOutput{
ID: gofakeit.UUID(),
Name: gofakeit.BeerName(),
Description: gofakeit.HipsterSentence(50),
FacilityType: "HOSPITAL",
County: "Baringo",
Country: "KE",
Address: "",
Coordinates: CoordinatesOutput{
Latitude: 30.4556,
Longitude: 4.54556,
},
Contacts: []ContactsOutput{},
Identifiers: []IdentifiersOutput{},
BusinessHours: []BusinessHoursOutput{
{
ID: gofakeit.UUID(),
Day: "MONDAY",
OpeningTime: "08:00:01",
ClosingTime: "18:00:01",
FacilityID: gofakeit.UUID(),
},
},
}
return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}
if tt.name == "Sad case: unable to get facilities offering a service" {
path := fmt.Sprintf("%s/v1/facilities/facilities/", 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{
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.GetFacilitiesOfferingAService(tt.args.ctx, tt.args.serviceID)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.GetFacilitiesOfferingAService() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

0 comments on commit 2d376d1

Please sign in to comment.