package httpapi import ( "net/http" "github.com/descrybe/descrybe-v2/apps/api/internal/woocommerce" ) func (s *Server) handleGetWooConfig(w http.ResponseWriter, r *http.Request) { cid, _ := CompanyIDFromContext(r.Context()) cfg, err := s.Woo.GetConfig(r.Context(), cid) if err != nil { JSON(w, http.StatusOK, map[string]any{ "store_url": "", "is_enabled": false, "configured": false, "has_credentials": false, }) return } JSON(w, http.StatusOK, cfg) } func (s *Server) handleUpdateWooConfig(w http.ResponseWriter, r *http.Request) { if !s.allowCompanyAdminOrPlatform(w, r) { return } if !s.requireFeatures(w, r, "stores.woocommerce") { return } cid, _ := CompanyIDFromContext(r.Context()) var body struct { StoreURL string `json:"store_url"` ConsumerKey string `json:"consumer_key"` ConsumerSecret string `json:"consumer_secret"` IsEnabled bool `json:"is_enabled"` } if err := DecodeJSON(r, &body); err != nil { Error(w, http.StatusBadRequest, "invalid json") return } cfg, err := s.Woo.UpdateConfig(r.Context(), cid, body.StoreURL, body.ConsumerKey, body.ConsumerSecret, body.IsEnabled) if msg, ok := woocommerce.ClientError(err); ok { Error(w, http.StatusBadRequest, msg) return } if err != nil { LogAndError(w, http.StatusInternalServerError, "update failed", err) return } JSON(w, http.StatusOK, cfg) } func (s *Server) handleUpdateWooMaps(w http.ResponseWriter, r *http.Request) { if !s.allowCompanyAdminOrPlatform(w, r) { return } if !s.requireFeatures(w, r, "stores.woocommerce") { return } cid, _ := CompanyIDFromContext(r.Context()) var body struct { CategoryMappings map[string]woocommerce.CategoryMap `json:"category_mappings"` AttributeMappings map[string]woocommerce.AttributeMap `json:"attribute_mappings"` MatchStrategy string `json:"match_strategy"` } if err := DecodeJSON(r, &body); err != nil { Error(w, http.StatusBadRequest, "invalid json") return } cfg, err := s.Woo.UpdateMaps(r.Context(), cid, body.CategoryMappings, body.AttributeMappings, body.MatchStrategy) if msg, ok := woocommerce.ClientError(err); ok { Error(w, http.StatusBadRequest, msg) return } if err != nil { LogAndError(w, http.StatusInternalServerError, "update maps failed", err) return } JSON(w, http.StatusOK, cfg) } func (s *Server) handleFetchWooRemoteMaps(w http.ResponseWriter, r *http.Request) { if !s.requireFeatures(w, r, "stores.woocommerce") { return } cid, _ := CompanyIDFromContext(r.Context()) result, err := s.Woo.FetchRemoteMaps(r.Context(), cid) if msg, ok := woocommerce.ClientError(err); ok { Error(w, http.StatusBadRequest, msg) return } if err != nil { LogAndError(w, http.StatusBadGateway, "failed to fetch remote maps", err) return } JSON(w, http.StatusOK, result) } func (s *Server) handleTestWoo(w http.ResponseWriter, r *http.Request) { if !s.requireFeatures(w, r, "stores.woocommerce") { return } cid, _ := CompanyIDFromContext(r.Context()) result, err := s.Woo.TestConnection(r.Context(), cid) if result == nil { result = map[string]any{"status": "failed", "message": "connection failed"} } if err != nil { JSON(w, http.StatusOK, result) return } JSON(w, http.StatusOK, result) } func (s *Server) handleSyncWoo(w http.ResponseWriter, r *http.Request) { if !s.requireFeatures(w, r, "stores.woocommerce") { return } cid, _ := CompanyIDFromContext(r.Context()) var scope woocommerce.ProductSyncScope if err := DecodeJSONOptional(r, &scope); err != nil { Error(w, http.StatusBadRequest, "invalid json") return } result, err := s.Woo.EnqueueSync(r.Context(), cid, scope) if msg, ok := woocommerce.ClientError(err); ok { Error(w, http.StatusBadRequest, msg) return } if err != nil { LogAndError(w, http.StatusInternalServerError, "sync enqueue failed", err) return } JSON(w, http.StatusAccepted, result) } func (s *Server) handleUpdateWooSchedule(w http.ResponseWriter, r *http.Request) { if !s.allowCompanyAdminOrPlatform(w, r) { return } if !s.requireFeatures(w, r, "stores.woocommerce") { return } cid, _ := CompanyIDFromContext(r.Context()) var body struct { ScheduleIntervalHours int `json:"schedule_interval_hours"` SchedulePaused bool `json:"schedule_paused"` } if err := DecodeJSON(r, &body); err != nil { Error(w, http.StatusBadRequest, "invalid json") return } cfg, err := s.Woo.UpdateSchedule(r.Context(), cid, body.ScheduleIntervalHours, body.SchedulePaused) if msg, ok := woocommerce.ClientError(err); ok { Error(w, http.StatusBadRequest, msg) return } if err != nil { LogAndError(w, http.StatusInternalServerError, "update schedule failed", err) return } JSON(w, http.StatusOK, cfg) }