diff --git a/backend/routes/mod/create_report.go b/backend/routes/mod/create_report.go index 8bc61ed..7e8264e 100644 --- a/backend/routes/mod/create_report.go +++ b/backend/routes/mod/create_report.go @@ -37,6 +37,10 @@ func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) error return errors.Wrap(err, "getting user") } + if u.DeletedAt != nil { + return server.APIError{Code: server.ErrUserNotFound} + } + var req CreateReportRequest err = render.Decode(r, &req) if err != nil { @@ -56,3 +60,52 @@ func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) error render.JSON(w, r, map[string]any{"created": true, "created_at": report.CreatedAt}) return nil } + +func (s *Server) createMemberReport(w http.ResponseWriter, r *http.Request) error { + ctx := r.Context() + claims, _ := server.ClaimsFromContext(ctx) + + memberID, err := xid.FromString(chi.URLParam(r, "id")) + if err != nil { + return server.APIError{Code: server.ErrBadRequest, Details: "Invalid member ID"} + } + + m, err := s.DB.Member(ctx, memberID) + if err != nil { + if err == db.ErrMemberNotFound { + return server.APIError{Code: server.ErrMemberNotFound} + } + + log.Errorf("getting member %v: %v", memberID, err) + return errors.Wrap(err, "getting member") + } + + u, err := s.DB.User(ctx, m.UserID) + if err != nil { + log.Errorf("getting user %v: %v", m.UserID, err) + return errors.Wrap(err, "getting user") + } + + if u.DeletedAt != nil { + return server.APIError{Code: server.ErrMemberNotFound} + } + + var req CreateReportRequest + err = render.Decode(r, &req) + if err != nil { + return server.APIError{Code: server.ErrBadRequest} + } + + if len(req.Reason) > MaxReasonLength { + return server.APIError{Code: server.ErrBadRequest, Details: "Reason cannot exceed 2000 characters"} + } + + report, err := s.DB.CreateReport(ctx, claims.UserID, u.ID, &m.ID, req.Reason) + if err != nil { + log.Errorf("creating report for %v: %v", m.ID, err) + return errors.Wrap(err, "creating report") + } + + render.JSON(w, r, map[string]any{"created": true, "created_at": report.CreatedAt}) + return nil +} diff --git a/backend/routes/mod/routes.go b/backend/routes/mod/routes.go index c7917f9..ca3db57 100644 --- a/backend/routes/mod/routes.go +++ b/backend/routes/mod/routes.go @@ -24,7 +24,7 @@ func Mount(srv *server.Server, r chi.Router) { }) r.With(server.MustAuth).Post("/users/{id}/reports", server.WrapHandler(s.createUserReport)) - r.With(server.MustAuth).Post("/members/{id}/reports", nil) + r.With(server.MustAuth).Post("/members/{id}/reports", server.WrapHandler(s.createMemberReport)) } func MustAdmin(next http.Handler) http.Handler {