Skip to content

Commit

Permalink
feat: add multiple facilityIDs search (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmuhia committed Jun 10, 2024
1 parent cf499f5 commit 273b983
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
49 changes: 49 additions & 0 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,52 @@ func (h *HealthCRMLib) GetMultipleServices(ctx context.Context, servicesIDs []st

return services.Results, nil
}

// GetMultipleFacilities is used to fetch multiple facilities
//
// Parameters:
// - facilityIDs: A parameter that is a list of IDs specifying one or more
// facility IDs. Facility identifiers, contacts, services and business hours linked to a facility will be
// included in the results. You can **ONLY** pass a single or multiple facility
// IDs which should be of type **UUID** (e.g., GetMultipleFacilities(ctx, []string{"0fee2792-dffc-40d3-a744-2a70732b1053",
// "56c62083-c7b4-4055-8d44-6cc7446ac1d0", "8474ea55-8ede-4bc6-aa67-f53ed5456a03"})).
func (h *HealthCRMLib) GetMultipleFacilities(ctx context.Context, facilityIDs []string) ([]*FacilityOutput, error) {
if len(facilityIDs) < 1 || facilityIDs == nil {
return nil, fmt.Errorf("no facility IDs provided")
}

searchParameter := facilityIDs[0]
for idx, id := range facilityIDs {
if idx == 0 {
continue
}

searchParameter += fmt.Sprintf(",%s", id)
}

path := fmt.Sprintf("/v1/facilities/facilities?facility_ids=%s", searchParameter)

response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}

defer response.Body.Close()

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

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

var facilities *FacilityOutputs
err = json.Unmarshal(respBytes, &facilities)
if err != nil {
return nil, err
}

return facilities.Results, nil
}
126 changes: 126 additions & 0 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1472,3 +1472,129 @@ func TestHealthCRMLib_GetMultipleServices(t *testing.T) {
})
}
}

func TestHealthCRMLib_GetMultipleFacilities(t *testing.T) {
type args struct {
ctx context.Context
facilityIDs []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: get list of facilities",
args: args{
ctx: context.Background(),
facilityIDs: []string{
"556a1dd9-fbb5-40c2-a623-dde9a2335597",
"7f59c528-8d9e-4a97-a9e5-bea7d7938c0e",
"b8246d32-b9e7-422c-b3bb-a1066dec8561",
},
},
wantErr: false,
},
{
name: "Sad case: no facility IDs provided(empty list)",
args: args{
ctx: context.Background(),
facilityIDs: []string{},
},
wantErr: true,
},
{
name: "Sad case: no facility IDs provided(nil)",
args: args{
ctx: context.Background(),
facilityIDs: nil,
},
wantErr: true,
},
{
name: "Sad case: unable to get services",
args: args{
ctx: context.Background(),
facilityIDs: []string{
"556a1dd9-fbb5-40c2-a623-dde9a2335597",
"7f59c528-8d9e-4a97-a9e5-bea7d7938c0e",
"b8246d32-b9e7-422c-b3bb-a1066dec8561",
},
},
wantErr: true,
},
{
name: "Sad case: unable to make request",
args: args{
ctx: context.Background(),
facilityIDs: []string{
"556a1dd9-fbb5-40c2-a623-dde9a2335597",
"7f59c528-8d9e-4a97-a9e5-bea7d7938c0e",
"b8246d32-b9e7-422c-b3bb-a1066dec8561",
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := fmt.Sprintf("%s/v1/facilities/facilities?facility_ids=556a1dd9-fbb5-40c2-a623-dde9a2335597,7f59c528-8d9e-4a97-a9e5-bea7d7938c0e,b8246d32-b9e7-422c-b3bb-a1066dec8561", BaseURL)

if tt.name == "Happy case: get list of facilities" {
httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) {
resp := FacilityOutputs{
Results: []*FacilityOutput{
{
ID: gofakeit.UUID(),
Name: "Oxygen",
Description: "158211",
},
{
ID: gofakeit.UUID(),
Name: "Oxygen",
Description: "158211",
},
},
}
return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}

if tt.name == "Sad case: unable to get services" {
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.GetMultipleFacilities(tt.args.ctx, tt.args.facilityIDs)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.GetMultipleFacilities() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
7 changes: 6 additions & 1 deletion output.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ type ProfileOutput struct {
SladeCode string `json:"slade_code"`
}

// FacilityServices is used to get a list of Facility Services
// FacilityServices is used to get a list of facility Services
type FacilityServices struct {
Results []*FacilityService `json:"results"`
}

// FacilityOutputs is used to get a list of facilities
type FacilityOutputs struct {
Results []*FacilityOutput `json:"results"`
}

0 comments on commit 273b983

Please sign in to comment.