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 a01cc74
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
12 changes: 11 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/savannahghi/authutils"
Expand Down Expand Up @@ -93,7 +94,16 @@ func (cl *client) MakeRequest(ctx context.Context, method, path string, queryPar
q := url.Values{}

for key, value := range queryParams {
q.Add(key, value)
if key == "service" {
serviceIDs := strings.Split(value, ",")

for _, id := range serviceIDs {
q.Add(key, id)
}
} else {
q.Add(key, value)
}

}

request.URL.RawQuery = q.Encode()
Expand Down
7 changes: 4 additions & 3 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/savannahghi/serverutils"
)
Expand Down Expand Up @@ -302,9 +303,9 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates,
}

if len(serviceIDs) > 0 {
for _, id := range serviceIDs {
queryParams["service"] = id
}
params := []string{}
params = append(params, serviceIDs...)
queryParams["service"] = strings.Join(params, ",")
}

if searchParameter != "" {
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 := &FacilitiesOutput{
Facilities: []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
5 changes: 5 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ type FacilityOutput struct {
Services []FacilityService `json:"services,omitempty"`
}

// FacilitiesOutput is used to display a slice of facility outputs
type FacilitiesOutput struct {
Facilities []FacilityOutput `json:"facilities"`
}

// BusinessHoursOutput models data that show facility's operational hours
type BusinessHoursOutput struct {
ID string `json:"id"`
Expand Down

0 comments on commit a01cc74

Please sign in to comment.