From 983d77e76ec8e33c2b0807381f9a74819e6ed6dc Mon Sep 17 00:00:00 2001 From: Allan Sifuna <35229350+allansifuna@users.noreply.github.com> Date: Thu, 14 Dec 2023 14:28:29 +0300 Subject: [PATCH] feat: add get services rest endpoint (#986) Signed-off-by: Allan Sifuna --- pkg/mycarehub/presentation/config.go | 4 + pkg/mycarehub/presentation/rest/handlers.go | 23 ++++ .../presentation/rest/handlers_test.go | 130 ++++++++++++++++++ 3 files changed, 157 insertions(+) diff --git a/pkg/mycarehub/presentation/config.go b/pkg/mycarehub/presentation/config.go index caafacb8..e4d7caa3 100644 --- a/pkg/mycarehub/presentation/config.go +++ b/pkg/mycarehub/presentation/config.go @@ -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( diff --git a/pkg/mycarehub/presentation/rest/handlers.go b/pkg/mycarehub/presentation/rest/handlers.go index 65c3394b..109d77f3 100644 --- a/pkg/mycarehub/presentation/rest/handlers.go +++ b/pkg/mycarehub/presentation/rest/handlers.go @@ -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 @@ -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 { diff --git a/pkg/mycarehub/presentation/rest/handlers_test.go b/pkg/mycarehub/presentation/rest/handlers_test.go index 5f8d77cb..73eee8d1 100644 --- a/pkg/mycarehub/presentation/rest/handlers_test.go +++ b/pkg/mycarehub/presentation/rest/handlers_test.go @@ -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()