Skip to content

Commit

Permalink
feat: add ability to fetch multiple services (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmuhia committed Jun 10, 2024
1 parent 2fb3566 commit cf499f5
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
49 changes: 49 additions & 0 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,52 @@ func (h *HealthCRMLib) CreateProfile(ctx context.Context, profile *ProfileInput)

return profileResponse, nil
}

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

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

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

path := fmt.Sprintf("/v1/facilities/services?service_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 services *FacilityServices
err = json.Unmarshal(respBytes, &services)
if err != nil {
return nil, err
}

return services.Results, nil
}
142 changes: 142 additions & 0 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,3 +1330,145 @@ func TestHealthCRMLib_CreateProfile(t *testing.T) {
})
}
}

func TestHealthCRMLib_GetMultipleServices(t *testing.T) {
type args struct {
ctx context.Context
servicesIDs []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: get list of services",
args: args{
ctx: context.Background(),
servicesIDs: []string{
"0fee2792-dffc-40d3-a744-2a70732b1053",
"56c62083-c7b4-4055-8d44-6cc7446ac1d0",
"8474ea55-8ede-4bc6-aa67-f53ed5456a03",
},
},
wantErr: false,
},
{
name: "Sad case: no service IDs provided(empty list)",
args: args{
ctx: context.Background(),
servicesIDs: []string{},
},
wantErr: true,
},
{
name: "Sad case: no service IDs provided(nil)",
args: args{
ctx: context.Background(),
servicesIDs: nil,
},
wantErr: true,
},
{
name: "Sad case: unable to get services",
args: args{
ctx: context.Background(),
servicesIDs: []string{
"0fee2792-dffc-40d3-a744-2a70732b1053",
"56c62083-c7b4-4055-8d44-6cc7446ac1d0",
"8474ea55-8ede-4bc6-aa67-f53ed5456a03",
},
},
wantErr: true,
},
{
name: "Sad case: unable to make request",
args: args{
ctx: context.Background(),
servicesIDs: []string{
"0fee2792-dffc-40d3-a744-2a70732b1053",
"56c62083-c7b4-4055-8d44-6cc7446ac1d0",
"8474ea55-8ede-4bc6-aa67-f53ed5456a03",
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := fmt.Sprintf("%s/v1/facilities/services?service_ids=0fee2792-dffc-40d3-a744-2a70732b1053,56c62083-c7b4-4055-8d44-6cc7446ac1d0,8474ea55-8ede-4bc6-aa67-f53ed5456a03", BaseURL)

if tt.name == "Happy case: get list of services" {
httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) {
resp := FacilityServices{
Results: []*FacilityService{
{
ID: gofakeit.UUID(),
Name: "Oxygen",
Description: "158211",
Identifiers: []*ServiceIdentifier{
{
ID: gofakeit.UUID(),
IdentifierType: "CIEL",
IdentifierValue: "158211",
ServiceID: gofakeit.UUID(),
},
},
},
{
ID: gofakeit.UUID(),
Name: "Oxygen",
Description: "158211",
Identifiers: []*ServiceIdentifier{
{
ID: gofakeit.UUID(),
IdentifierType: "CIEL",
IdentifierValue: "158211",
ServiceID: gofakeit.UUID(),
},
},
},
},
}
return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}

if tt.name == "Sad case: unable to get a 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.GetMultipleServices(tt.args.ctx, tt.args.servicesIDs)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.GetMultipleServices() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
5 changes: 5 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,8 @@ type ProfileOutput struct {
HealthID string `json:"health_id,omitempty"`
SladeCode string `json:"slade_code"`
}

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

0 comments on commit cf499f5

Please sign in to comment.