package commandcenter import ( "encoding/json" "errors" "net/http" "time" "github.com/go-chi/chi/v5" "github.com/missionworkforce/missionmatch/internal/platform/tenant" ) // Handler serves the Workforce Command Center endpoint. type Handler struct{ svc *Service } func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} } // Routes mounts GET /command-center under the tenant-scoped group. func (h *Handler) Routes(r chi.Router) { r.Get("/command-center", h.get) } func (h *Handler) get(w http.ResponseWriter, r *http.Request) { asOf := h.svc.clock() if q := r.URL.Query().Get("as_of"); q != "" { if t, err := time.Parse("2006-01-02", q); err == nil { asOf = t } } v, err := h.svc.CommandCenter(r.Context(), asOf) if err != nil { w.Header().Set("Content-Type", "application/json") switch { case errors.Is(err, tenant.ErrNoTenant): w.WriteHeader(http.StatusForbidden) _, _ = w.Write([]byte(`{"error":"no validated tenant"}`)) case errors.Is(err, ErrNoDatabase): w.WriteHeader(http.StatusServiceUnavailable) _, _ = w.Write([]byte(`{"error":"database not configured"}`)) default: w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(`{"error":"internal error"}`)) } return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(v) }