Skip to content

Commit

Permalink
fix: enable get facilities by service ids
Browse files Browse the repository at this point in the history
  • Loading branch information
allansifuna committed Nov 28, 2023
1 parent e183094 commit 80e2260
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 28 deletions.
9 changes: 2 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newClient() (*client, error) {
}

// MakeRequest performs a HTTP request to the provided path and parameters
func (cl *client) MakeRequest(ctx context.Context, method, path string, queryParams map[string]string, body interface{}) (*http.Response, error) {
func (cl *client) MakeRequest(ctx context.Context, method, path string, queryParams url.Values, body interface{}) (*http.Response, error) {
oauthResponse, err := cl.authClient.Authenticate()
if err != nil {
return nil, err
Expand Down Expand Up @@ -90,13 +90,8 @@ func (cl *client) MakeRequest(ctx context.Context, method, path string, queryPar
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", oauthResponse.AccessToken))

if queryParams != nil {
q := url.Values{}

for key, value := range queryParams {
q.Add(key, value)
}

request.URL.RawQuery = q.Encode()
request.URL.RawQuery = queryParams.Encode()
}

return cl.httpClient.Do(request)
Expand Down
7 changes: 5 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"net/url"
"testing"

"github.com/jarcoal/httpmock"
Expand All @@ -22,20 +23,22 @@ func (m *MockAuthUtilsLib) Authenticate() (*authutils.OAUTHResponse, error) {

func TestMakeRequest(t *testing.T) {
ctx := context.Background()
queryParam := url.Values{}
queryParam.Add("param1", "value1")

tests := []struct {
name string
method string
path string
queryParams map[string]string
queryParams url.Values
body interface{}
want int
}{
{
name: "Happy case: GET Request",
method: http.MethodGet,
path: "/v1/facilities/facilities/",
queryParams: map[string]string{"param1": "value1"},
queryParams: queryParam,
body: &Facility{
Name: "Test Facility",
Description: "A test facility",
Expand Down
37 changes: 18 additions & 19 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"

"github.com/savannahghi/serverutils"
)
Expand Down Expand Up @@ -125,14 +126,14 @@ func (h *HealthCRMLib) UpdateFacility(ctx context.Context, id string, updatePayl
func (h *HealthCRMLib) GetServices(ctx context.Context, pagination *Pagination) (*FacilityServicePage, error) {
path := "/v1/facilities/services/"

queryParams := make(map[string]string)
queryParams := url.Values{}

if pagination != nil {
queryParams["page_size"] = pagination.PageSize
queryParams["page"] = pagination.Page
queryParams.Add("page_size", pagination.PageSize)
queryParams.Add("page", pagination.Page)
}

queryParams["crm_service_code"] = crmServiceCode
queryParams.Add("crm_service_code", crmServiceCode)

response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
if err != nil {
Expand Down Expand Up @@ -163,12 +164,12 @@ func (h *HealthCRMLib) GetServices(ctx context.Context, pagination *Pagination)
func (h *HealthCRMLib) GetFacilitiesOfferingAService(ctx context.Context, serviceID string, pagination *Pagination) (*FacilityPage, error) {
path := "/v1/facilities/facilities/"

queryParams := make(map[string]string)
queryParams["service"] = serviceID
queryParams := url.Values{}
queryParams.Add("service", serviceID)

if pagination != nil {
queryParams["page_size"] = pagination.PageSize
queryParams["page"] = pagination.Page
queryParams.Add("page_size", pagination.PageSize)
queryParams.Add("page", pagination.Page)
}

response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
Expand Down Expand Up @@ -277,11 +278,11 @@ func (h *HealthCRMLib) LinkServiceToFacility(ctx context.Context, facilityID str
//
// Example 3: Retrieve all facilities without specifying location or services:
func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates, serviceIDs []string, searchParameter string, pagination *Pagination) (*FacilityPage, error) {
queryParams := make(map[string]string)
queryParams := url.Values{}

if pagination != nil {
queryParams["page_size"] = pagination.PageSize
queryParams["page"] = pagination.Page
queryParams.Add("page_size", pagination.PageSize)
queryParams.Add("page", pagination.Page)
}

if location != nil {
Expand All @@ -290,10 +291,10 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates,
return nil, err
}

queryParams["ref_location"] = coordinateString
queryParams.Add("ref_location", coordinateString)

if location.Radius != "" {
queryParams["distance"] = location.Radius
queryParams.Add("distance", location.Radius)
}
}

Expand All @@ -303,15 +304,15 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates,

if len(serviceIDs) > 0 {
for _, id := range serviceIDs {
queryParams["service"] = id
queryParams.Add("service", id)
}
}

if searchParameter != "" {
queryParams["search"] = searchParameter
queryParams.Add("search", searchParameter)
}

queryParams["crm_service_code"] = crmServiceCode
queryParams.Add("crm_service_code", crmServiceCode)

response, err := h.client.MakeRequest(ctx, http.MethodGet, facilitiesPath, queryParams, nil)
if err != nil {
Expand Down Expand Up @@ -342,9 +343,7 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates,
func (h *HealthCRMLib) GetService(ctx context.Context, serviceID string) (*FacilityService, error) {
path := fmt.Sprintf("/v1/facilities/services/%s", serviceID)

queryParams := make(map[string]string)

response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
Expand Down
76 changes: 76 additions & 0 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ func TestHealthCRMLib_GetFacilities(t *testing.T) {
},
wantErr: false,
},
{
name: "Happy case: fetch facilities",
args: args{
ctx: context.Background(),
location: &Coordinates{
Latitude: "-1.29",
Longitude: "36.79",
},
serviceIDs: []string{"1234", "4567"},
pagination: &Pagination{
Page: "1",
PageSize: "10",
},
},
wantErr: false,
},
{
name: "Happy case: search facility by service name",
args: args{
Expand Down Expand Up @@ -275,6 +291,66 @@ func TestHealthCRMLib_GetFacilities(t *testing.T) {
return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}
if tt.name == "Happy case: fetch facilities" {
path := fmt.Sprintf("%s/v1/facilities/facilities/", BaseURL)
httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) {

service1 := &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(),
},
},
}
service2 := &FacilityOutput{
ID: gofakeit.UUID(),
Name: gofakeit.BeerName(),
Description: gofakeit.HipsterSentence(50),
FacilityType: "HOSPITAL",
County: "Nairobi",
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(),
},
},
}

resp := &FacilityPage{
Results: []FacilityOutput{*service1, *service2},
}

return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}

if tt.name == "Happy case: search facility by service name" {
path := fmt.Sprintf("%s/v1/facilities/facilities/", BaseURL)
Expand Down

0 comments on commit 80e2260

Please sign in to comment.