Skip to content

Commit

Permalink
get clearnce docs, feed, membs
Browse files Browse the repository at this point in the history
  • Loading branch information
pai0id committed Jul 19, 2024
1 parent c2d89ea commit 20158f4
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/app/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *MembersService) UpdateMember(ctx context.Context, member *domain.Member
return nil
}

func (s *MembersService) GetClearancePost(ctx context.Context, resp *responses.CheckResponse) (*responses.GetClearance, error) {
func (s *MembersService) GetClearance(ctx context.Context, resp *responses.CheckResponse) (*responses.GetClearance, error) {
if resp.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
Expand Down
40 changes: 40 additions & 0 deletions internal/ports/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (h *DocumentsHandler) Routes() chi.Router {
r.Post("/categories/", h.r.Wrap(h.PostCategory))
r.Delete("/categories/{id}", h.r.Wrap(h.DeleteCategory))
r.Put("/categories/{id}", h.r.Wrap(h.UpdateCategory))
r.Get("/clearance/post/", h.r.Wrap(h.GetClearancePost))

return r
}
Expand Down Expand Up @@ -717,3 +718,42 @@ func (h *DocumentsHandler) UpdateCategory(w http.ResponseWriter, req *http.Reque

return handler.OkResponse(nil)
}

// GetClearancePost checks if the member is allowed to post/del/put
//
// @Summary Check member`s post clearance
// @Description Check if the member is allowed to post/del/put
// @Tags auth.documents
// @Success 200 {object} responses.GetClearance
// @Failure 401
// @Failure 500
// @Router /documents/clearance/post/ [get]
// @Security Authorised
func (h *DocumentsHandler) GetClearancePost(w http.ResponseWriter, req *http.Request) handler.Response {
h.logger.Info("DocumentsHandler: got GetClearancePost request")

accessToken, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token GetClearancePost: %v", err)
return handler.UnauthorizedResponse()
}

checkResp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: accessToken})
if err != nil || !checkResp.Valid {
h.logger.Warnf("can't GuardService.Check on GetClearancePost: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("DocumentsHandler: GetClearancePost Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.documents.GetClearancePost(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't DocumentsService.GetClearancePost: %v", err)
return handler.InternalServerErrorResponse()
}

h.logger.Info("DocumentsHandler: GetClearancePost Done")

return handler.OkResponse(cleaResp)
}
40 changes: 40 additions & 0 deletions internal/ports/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (h *FeedHandler) Routes() chi.Router {
r.Post("/encounters/", h.r.Wrap(h.PostEncounter))
r.Delete("/encounters/{id}", h.r.Wrap(h.DeleteEncounter))
r.Put("/encounters/{id}", h.r.Wrap(h.UpdateEncounter))
r.Get("/clearance/post/", h.r.Wrap(h.GetClearancePost))

return r
}
Expand Down Expand Up @@ -626,3 +627,42 @@ func (h *FeedHandler) UpdateEncounter(w http.ResponseWriter, req *http.Request)

return handler.OkResponse(nil)
}

// GetClearancePost checks if the member is allowed to post/del/put
//
// @Summary Check member`s post clearance
// @Description Check if the member is allowed to post/del/put
// @Tags auth.feed
// @Success 200 {object} responses.GetClearance
// @Failure 401
// @Failure 500
// @Router /feed/clearance/post/ [get]
// @Security Authorised
func (h *FeedHandler) GetClearancePost(w http.ResponseWriter, req *http.Request) handler.Response {
h.logger.Info("FeedHandler: got GetClearancePost request")

accessToken, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token GetClearancePost: %v", err)
return handler.UnauthorizedResponse()
}

checkResp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: accessToken})
if err != nil || !checkResp.Valid {
h.logger.Warnf("can't GuardService.Check on GetClearancePost: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("FeedHandler: GetClearancePost Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.feed.GetClearancePost(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't FeedService.GetClearancePost: %v", err)
return handler.InternalServerErrorResponse()
}

h.logger.Info("FeedHandler: GetClearancePost Done")

return handler.OkResponse(cleaResp)
}
60 changes: 50 additions & 10 deletions internal/ports/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (h *MembersHandler) Routes() chi.Router {
r.Get("/search/{name}", h.r.Wrap(h.GetMembersByName))
r.Delete("/{id}", h.r.Wrap(h.DeleteMember))
r.Put("/{id}", h.r.Wrap(h.UpdateMember))
r.Get("/clearance/", h.r.Wrap(h.GetClearance))

return r
}
Expand Down Expand Up @@ -78,10 +79,10 @@ func (h *MembersHandler) GetAllMembers(w http.ResponseWriter, req *http.Request)

h.logger.Infof("MembersHandler: GetAllMembers Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp)
cleaResp, err := h.members.GetClearance(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err)
h.logger.Warnf("can't MembersHandler.GetClearance: %v", err)
return handler.InternalServerErrorResponse()
}

Expand Down Expand Up @@ -138,10 +139,10 @@ func (h *MembersHandler) GetMember(w http.ResponseWriter, req *http.Request) han

h.logger.Infof("MembersHandler: GetAllMembers Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp)
cleaResp, err := h.members.GetClearance(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err)
h.logger.Warnf("can't MembersHandler.GetClearance: %v", err)
return handler.InternalServerErrorResponse()
}

Expand Down Expand Up @@ -208,10 +209,10 @@ func (h *MembersHandler) GetMembersByName(w http.ResponseWriter, req *http.Reque

h.logger.Infof("MembersHandler: GetMembersByName Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp)
cleaResp, err := h.members.GetClearance(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err)
h.logger.Warnf("can't MembersHandler.GetClearance: %v", err)
return handler.InternalServerErrorResponse()
}

Expand Down Expand Up @@ -278,10 +279,10 @@ func (h *MembersHandler) DeleteMember(w http.ResponseWriter, req *http.Request)

h.logger.Infof("MembersHandler: DeleteMember Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp)
cleaResp, err := h.members.GetClearance(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err)
h.logger.Warnf("can't MembersHandler.GetClearance: %v", err)
return handler.InternalServerErrorResponse()
}

Expand Down Expand Up @@ -351,10 +352,10 @@ func (h *MembersHandler) UpdateMember(w http.ResponseWriter, req *http.Request)

h.logger.Infof("MembersHandler: UpdateMember Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp)
cleaResp, err := h.members.GetClearance(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err)
h.logger.Warnf("can't MembersHandler.GetClearance: %v", err)
return handler.InternalServerErrorResponse()
}

Expand Down Expand Up @@ -393,3 +394,42 @@ func (h *MembersHandler) UpdateMember(w http.ResponseWriter, req *http.Request)

return handler.OkResponse(nil)
}

// GetClearance checks if the member is allowed to access members
//
// @Summary Check member`s clearance
// @Description Check if the member is allowed to access members
// @Tags auth.members
// @Success 200 {object} responses.GetClearance
// @Failure 401
// @Failure 500
// @Router /members/clearance/post/ [get]
// @Security Authorised
func (h *MembersHandler) GetClearance(w http.ResponseWriter, req *http.Request) handler.Response {
h.logger.Info("MembersHandler: got GetClearance request")

accessToken, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token GetClearance: %v", err)
return handler.UnauthorizedResponse()
}

checkResp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: accessToken})
if err != nil || !checkResp.Valid {
h.logger.Warnf("can't GuardService.Check on GetClearance: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("MembersHandler: GetClearance Authenticated: %v", checkResp.MemberID)

cleaResp, err := h.members.GetClearance(context.Background(), checkResp)

if err != nil {
h.logger.Warnf("can't MembersService.GetClearance: %v", err)
return handler.InternalServerErrorResponse()
}

h.logger.Info("MembersHandler: GetClearance Done")

return handler.OkResponse(cleaResp)
}

0 comments on commit 20158f4

Please sign in to comment.