Skip to content

Commit

Permalink
feat: search facility
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
  • Loading branch information
KathurimaKimathi committed Oct 8, 2023
1 parent ac6d1c3 commit 0739aa4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
30 changes: 30 additions & 0 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,33 @@ func (h *HealthCRMLib) UpdateFacility(ctx context.Context, id string, updatePayl

return facilityOutput, nil
}

// SearchFacility is used to search for facilities
func (h *HealthCRMLib) SearchFacility(ctx context.Context, searchTerm string) (*FacilityPage, error) {
path := "/v1/facilities/facilities/"
queryParams := make(map[string]string)
queryParams["search"] = searchTerm

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 search facility in the registry 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 page *FacilityPage

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

return page, nil
}
96 changes: 96 additions & 0 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,99 @@ func TestHealthCRMLib_UpdateFacility(t *testing.T) {
})
}
}

func TestHealthCRMLib_SearchFacility(t *testing.T) {
type args struct {
ctx context.Context
searchTerm string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: search facility",
args: args{
ctx: context.Background(),
searchTerm: "karen",
},
wantErr: false,
},
{
name: "Sad case: unable to search for a facility",
args: args{
ctx: context.Background(),
searchTerm: "karen",
},
wantErr: true,
},
{
name: "Sad case: unable to make request",
args: args{
ctx: context.Background(),
searchTerm: "karen",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Happy case: search facility" {
path := "/v1/facilities/facilities/?search=karen"
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: []any{},
}
return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}
if tt.name == "Sad case: unable to search for a facility" {
path := "/v1/facilities/facilities/?search=karen"
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.SearchFacility(tt.args.ctx, tt.args.searchTerm)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.SearchFacility() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

0 comments on commit 0739aa4

Please sign in to comment.