Skip to content

Commit

Permalink
feat: update facility
Browse files Browse the repository at this point in the history
- updates facility info

Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
  • Loading branch information
KathurimaKimathi committed Oct 6, 2023
1 parent 5ac5fad commit 5e8406c
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 25 deletions.
19 changes: 19 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/savannahghi/authutils"
"github.com/savannahghi/serverutils"
"moul.io/http2curl"
)

// IAuthUtilsLib holds the method defined in authutils library
Expand Down Expand Up @@ -80,6 +81,21 @@ func (cl *client) MakeRequest(ctx context.Context, method, path string, queryPar

request = req

case http.MethodPatch:
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}

payload := bytes.NewBuffer(encoded)

req, err := http.NewRequestWithContext(ctx, method, urlPath, payload)
if err != nil {
return nil, err
}

request = req

default:
return nil, fmt.Errorf("s.MakeRequest() unsupported http method: %s", method)

Expand All @@ -99,5 +115,8 @@ func (cl *client) MakeRequest(ctx context.Context, method, path string, queryPar
request.URL.RawQuery = q.Encode()
}

command, _ := http2curl.GetCurlCommand(request)
fmt.Println(command)

return cl.httpClient.Do(request)
}
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestMakeRequest(t *testing.T) {
County: "Meru",
Country: "KE",
Address: "1200-Meru",
Coordinates: Coordinates{
Coordinates: &Coordinates{
Latitude: "30.40338",
Longitude: "5.17403",
},
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestMakeRequest(t *testing.T) {
County: "Meru",
Country: "KE",
Address: "1200-Meru",
Coordinates: Coordinates{
Coordinates: &Coordinates{
Latitude: "30.40338",
Longitude: "5.17403",
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/jarcoal/httpmock v1.3.1
github.com/savannahghi/authutils v0.0.10
github.com/savannahghi/serverutils v0.0.7
moul.io/http2curl v1.0.0
)

require (
Expand Down Expand Up @@ -79,5 +80,4 @@ require (
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
moul.io/http2curl v1.0.0 // indirect
)
27 changes: 27 additions & 0 deletions healthcrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,30 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context) (*FacilityPage, error)

return facilityPage, nil
}

// UpdateFacility is used to update facility's data
func (h *HealthCRMLib) UpdateFacility(ctx context.Context, id string, updatePayload *Facility) (*FacilityOutput, error) {
path := fmt.Sprintf("/v1/facilities/facilities/%s/", id)
response, err := h.client.MakeRequest(ctx, http.MethodPatch, path, nil, updatePayload)
if err != nil {
return nil, err
}

if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to update facility in the registry with status code: %v", response.StatusCode)
}

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

var facilityOutput *FacilityOutput

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

return facilityOutput, nil
}
110 changes: 108 additions & 2 deletions healthcrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestHealthCRMLib_CreateFacility(t *testing.T) {
County: "Meru",
Country: "KE",
Address: "1200-Meru",
Coordinates: Coordinates{
Coordinates: &Coordinates{
Latitude: "30.40338",
Longitude: "5.17403",
},
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestHealthCRMLib_CreateFacility(t *testing.T) {
County: "Baringo",
Country: "KE",
Address: "",
Coordinates: Coordinates{},
Coordinates: &Coordinates{},
Contacts: []Contacts{},
Identifiers: []Identifiers{},
BusinessHours: []any{},
Expand Down Expand Up @@ -258,3 +258,109 @@ func TestHealthCRMLib_GetFacilities(t *testing.T) {
})
}
}

func TestHealthCRMLib_UpdateFacility(t *testing.T) {
type args struct {
ctx context.Context
id string
updatePayload *Facility
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: update facility",
args: args{
ctx: context.Background(),
id: "123",
updatePayload: &Facility{
Name: "Makuyu Level 5 Hospital",
},
},
wantErr: false,
},
{
name: "Sad case: unable to update facility",
args: args{
ctx: context.Background(),
id: "123",
updatePayload: &Facility{
Name: "Makuyu Level 5 Hospital",
},
},
wantErr: true,
},
{
name: "Sad case: unable to make request",
args: args{
ctx: context.Background(),
id: gofakeit.UUID(),
updatePayload: &Facility{
Name: "Makuyu Level 5 Hospital",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "Happy case: update facility" {
path := fmt.Sprintf("%s/v1/facilities/facilities/%s/", BaseURL, "123")
httpmock.RegisterResponder(http.MethodPatch, path, func(r *http.Request) (*http.Response, error) {
resp := &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: []any{},
}
return httpmock.NewJsonResponse(http.StatusOK, resp)
})
}
if tt.name == "Sad case: unable to update facility" {
path := fmt.Sprintf("%s/v1/facilities/facilities/%s/", BaseURL, "123")
httpmock.RegisterResponder(http.MethodPatch, 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.UpdateFacility(tt.args.ctx, tt.args.id, tt.args.updatePayload)
if (err != nil) != tt.wantErr {
t.Errorf("HealthCRMLib.UpdateFacility() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
40 changes: 20 additions & 20 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ package healthcrm

// Facility is the hospitals data class
type Facility struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
FacilityType string `json:"facility_type"`
County string `json:"county"`
Country string `json:"country"`
Address string `json:"address"`
Coordinates Coordinates `json:"coordinates"`
Contacts []Contacts `json:"contacts"`
Identifiers []Identifiers `json:"identifiers"`
BusinessHours []any `json:"businesshours"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
FacilityType string `json:"facility_type,omitempty"`
County string `json:"county,omitempty"`
Country string `json:"country,omitempty"`
Address string `json:"address,omitempty"`
Coordinates *Coordinates `json:"coordinates,omitempty"`
Contacts []Contacts `json:"contacts,omitempty"`
Identifiers []Identifiers `json:"identifiers,omitempty"`
BusinessHours []any `json:"businesshours,omitempty"`
}

// Coordinates models the geographical's location data class of a facility
type Coordinates struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
Latitude string `json:"latitude,omitempty"`
Longitude string `json:"longitude,omitempty"`
}

// Contacts models facility's model data class
type Contacts struct {
ContactType string `json:"contact_type"`
ContactValue string `json:"contact_value"`
Role string `json:"role"`
ContactType string `json:"contact_type,omitempty"`
ContactValue string `json:"contact_value,omitempty"`
Role string `json:"role,omitempty"`
}

// Identifiers models facility's identifiers; can be MFL Code, Slade Code etc...
type Identifiers struct {
IdentifierType string `json:"identifier_type"`
IdentifierValue string `json:"identifier_value"`
ValidFrom string `json:"valid_from"`
ValidTo string `json:"valid_to"`
IdentifierType string `json:"identifier_type,omitempty"`
IdentifierValue string `json:"identifier_value,omitempty"`
ValidFrom string `json:"valid_from,omitempty"`
ValidTo string `json:"valid_to,omitempty"`
}

0 comments on commit 5e8406c

Please sign in to comment.