Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get service by id #29

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,34 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates,

return facilityPage, nil
}

// GetService is used to fetch a single service given its ID
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)
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 service FacilityService
err = json.Unmarshal(respBytes, &service)
if err != nil {
return nil, err
}

return &service, nil
}
92 changes: 92 additions & 0 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,3 +1031,95 @@ func TestHealthCRMLib_LinkServiceToFacility(t *testing.T) {
})
}
}

func TestHealthCRMLib_GetService(t *testing.T) {
type args struct {
ctx context.Context
serviceID string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: get service",
args: args{
ctx: context.Background(),
serviceID: "b7142d0f-88a0-436b-976d-4ecc86482107",
},
wantErr: false,
},
{
name: "Sad case: unable to get a service",
args: args{
ctx: context.Background(),
serviceID: "b7142d0f-88a0-436b-976d-4ecc86482107",
},
wantErr: true,
},
{
name: "Sad case: unable to make request",
args: args{
ctx: context.Background(),
serviceID: "b7142d0f-88a0-436b-976d-4ecc86482107",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Happy case: get service" {
path := fmt.Sprintf("%s/v1/facilities/services/b7142d0f-88a0-436b-976d-4ecc86482107", BaseURL)
httpmock.RegisterResponder(http.MethodGet, path, func(r *http.Request) (*http.Response, error) {
resp := &FacilityService{
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 service" {
path := fmt.Sprintf("%s/v1/facilities/services/b7142d0f-88a0-436b-976d-4ecc86482107", BaseURL)
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.GetService(tt.args.ctx, tt.args.serviceID)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.GetService() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}