Skip to content

Commit

Permalink
feat: add get services rest endpoint (#986)
Browse files Browse the repository at this point in the history
Signed-off-by: Allan Sifuna <allansifuna324@gmail.com>
  • Loading branch information
allansifuna committed Dec 14, 2023
1 parent d4ccbb3 commit 983d77e
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/mycarehub/presentation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ func Router(ctx context.Context) (*mux.Router, error) {
http.MethodOptions,
http.MethodGet,
).HandlerFunc(internalHandlers.SyncFacilities())
r.Path("/services").Methods(
http.MethodOptions,
http.MethodGet,
).HandlerFunc(internalHandlers.GetServices())

// TODO: Refactor to implement delete client, staff and caregiver
// r.Path("/delete-user").Methods(
Expand Down
23 changes: 23 additions & 0 deletions pkg/mycarehub/presentation/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type MyCareHubHandlersInterfaces interface {
CreatePinResetServiceRequest() http.HandlerFunc
AddPatientsRecords() http.HandlerFunc
SyncFacilities() http.HandlerFunc
GetServices() http.HandlerFunc
AppointmentsServiceRequests() http.HandlerFunc
// DeleteUser() http.HandlerFunc
FetchContactOrganisations() http.HandlerFunc
Expand Down Expand Up @@ -231,6 +232,28 @@ func (h *MyCareHubHandlersInterfacesImpl) SyncFacilities() http.HandlerFunc {
}
}

// GetServices is an unauthenticated endpoint that returns a list of services
func (h *MyCareHubHandlersInterfacesImpl) GetServices() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

paginationInput := &dto.PaginationsInput{}
serverutils.DecodeJSONToTargetStruct(w, r, paginationInput)
resp, err := h.usecase.Facility.GetServices(ctx, paginationInput)
if err != nil {
helpers.ReportErrorToSentry(err)
serverutils.WriteJSONResponse(w, errorcodeutil.CustomError{
Err: err,
Message: err.Error(),
Code: exceptions.GetErrorCode(err),
}, http.StatusBadRequest)
return
}

serverutils.WriteJSONResponse(w, resp, http.StatusOK)
}
}

// VerifyPhone is an unauthenticated endpoint that does a check on the provided username,
// performs a check to ascertain whether the user exists. it verifies whether the user also has a phone number where the otp will be sent
func (h *MyCareHubHandlersInterfacesImpl) VerifyPhone() http.HandlerFunc {
Expand Down
130 changes: 130 additions & 0 deletions pkg/mycarehub/presentation/rest/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,136 @@ func TestMyCareHubHandlersInterfacesImpl_GetAppointmentServiceRequests(t *testin
}
}

func TestMyCareHubHandlersInterfacesImpl_GetServices(t *testing.T) {
validPaginationsInput := &dto.PaginationsInput{
CurrentPage: 1,
Limit: 10,
}
validPaginationsPayload, err := json.Marshal(validPaginationsInput)
if err != nil {
t.Errorf("failed to marshal payload")
return
}

invalidPaginationsInput := &dto.PaginationsInput{
CurrentPage: -11,
Limit: 10,
}
invalidPaginationsPayload, err := json.Marshal(invalidPaginationsInput)
if err != nil {
t.Errorf("failed to marshal payload")
return
}

type args struct {
url string
httpMethod string
body io.Reader
}
tests := []struct {
name string
args args
wantStatus int
wantErr bool
}{
{
name: "Happy Case - Successfully get services",
args: args{
url: fmt.Sprintf(
"%s/services",
baseURL,
),
httpMethod: http.MethodGet,
body: bytes.NewBuffer(validPaginationsPayload),
},
wantStatus: http.StatusOK,
wantErr: false,
},
{
name: "Sad Case - invalid pagination payload",
args: args{
url: fmt.Sprintf(
"%s/services",
baseURL,
),
httpMethod: http.MethodGet,
body: bytes.NewBuffer(invalidPaginationsPayload),
},
wantStatus: http.StatusBadRequest,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := http.NewRequest(
tt.args.httpMethod,
tt.args.url,
tt.args.body,
)
if err != nil {
t.Errorf("unable to compose request: %s", err)
return
}

if r == nil {
t.Errorf("nil request")
return
}
client := http.DefaultClient
r.Header.Add("Accept", "application/json")
r.Header.Add("Content-Type", "application/json")
resp, err := client.Do(r)
if err != nil {
t.Errorf("request error: %s", err)
return
}

if resp == nil && !tt.wantErr {
t.Errorf("nil response")
return
}

dataResponse, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("can't read request body: %s", err)
return
}
if dataResponse == nil {
t.Errorf("nil response data")
return
}

data := map[string]interface{}{}
err = json.Unmarshal(dataResponse, &data)
if tt.wantErr && err != nil {
t.Errorf("bad data returned: %v", err)
return
}

if tt.wantErr {
errMsg, ok := data["error"]
if !ok {
t.Errorf("expected error: %s", errMsg)
return
}
}

if !tt.wantErr {
_, ok := data["error"]
if ok {
t.Errorf("error not expected")
return
}
}

if resp.StatusCode != tt.wantStatus {
t.Errorf("expected status %d, got %s", tt.wantStatus, resp.Status)
return
}
})
}
}

// TODO: Refactor to implement delete client, staff and caregiver
// func TestMyCareHubHandlersInterfacesImpl_DeleteUser(t *testing.T) {
// ctx := context.Background()
Expand Down

0 comments on commit 983d77e

Please sign in to comment.