Skip to content

Commit

Permalink
Merge pull request #22 from savannahghi/get-services-pagination
Browse files Browse the repository at this point in the history
fix: add pagination when fetching services
  • Loading branch information
Muchogoc committed Oct 18, 2023
2 parents c6e9437 + 788ece9 commit 0c1a00b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
38 changes: 18 additions & 20 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,45 +142,43 @@ 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) {
func (h *HealthCRMLib) GetFacilityServices(ctx context.Context, facilityID string, pagination *Pagination) (*FacilityServicePage, error) {
path := "/v1/facilities/services/"

var resp *http.Response
queryParams := make(map[string]string)

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

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
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
if err != nil {
return nil, err
}

respBytes, err := io.ReadAll(resp.Body)
defer response.Body.Close()

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

if resp.StatusCode != http.StatusOK {
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}

var facilityServicePage *FacilityServicePage

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

return facilityServicePage, nil
return &facilityServicePage, nil
}

// GetFacilitiesOfferingAService fetches the facilities that offer a particular service
Expand Down
13 changes: 11 additions & 2 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) {
type args struct {
ctx context.Context
facilityID string
pagination *Pagination
}
tests := []struct {
name string
Expand All @@ -508,6 +509,10 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) {
name: "Happy case: get all services",
args: args{
ctx: context.Background(),
pagination: &Pagination{
Page: "2",
PageSize: "5",
},
},
wantErr: false,
},
Expand All @@ -516,6 +521,10 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) {
args: args{
ctx: context.Background(),
facilityID: "1b5baf1a-1aec-48bd-951c-01896e5fe5a8",
pagination: &Pagination{
Page: "2",
PageSize: "5",
},
},
wantErr: false,
},
Expand Down Expand Up @@ -562,7 +571,7 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) {
})
}
if tt.name == "Happy case: get services in a facility" {
path := fmt.Sprintf("%s/v1/facilities/services/?facility=1b5baf1a-1aec-48bd-951c-01896e5fe5a8", BaseURL)
path := fmt.Sprintf("%s/v1/facilities/services/?facility=1b5baf1a-1aec-48bd-951c-01896e5fe5a8&page=2&page_size=5", BaseURL)
httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) {
resp := &FacilityServicePage{
Results: []FacilityService{
Expand Down Expand Up @@ -610,7 +619,7 @@ func TestHealthCRMLib_GetFacilityServices(t *testing.T) {
t.Errorf("unable to initialize sdk: %v", err)
}

_, err = h.GetFacilityServices(tt.args.ctx, tt.args.facilityID)
_, err = h.GetFacilityServices(tt.args.ctx, tt.args.facilityID, tt.args.pagination)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.GetFacilityServices() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
6 changes: 6 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ type BusinessHours struct {
OpeningTime string `json:"opening_time"`
ClosingTime string `json:"closing_time"`
}

// Pagination is used to hold pagination values
type Pagination struct {
Page string `json:"page"`
PageSize string `json:"page_size"`
}

0 comments on commit 0c1a00b

Please sign in to comment.