package v1 import ( "net/http" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/service" "github.com/gochat/gochat/pkg/response" ) // ShopifyIntegrationHandler handles Shopify integration endpoints. // Reference: Chatwoot Integrations::ShopifyController type ShopifyIntegrationHandler struct { svc *service.ShopifyIntegrationService } // NewShopifyIntegrationHandler creates a new ShopifyIntegrationHandler. func NewShopifyIntegrationHandler(svc *service.ShopifyIntegrationService) *ShopifyIntegrationHandler { return &ShopifyIntegrationHandler{svc: svc} } // Delete removes a Shopify integration for an account. // DELETE /api/v1/accounts/:account_id/integrations/shopify func (h *ShopifyIntegrationHandler) Delete(c *gin.Context) { accountID, err := parseUintParam(c, "account_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } if svcErr := h.svc.Delete(c.Request.Context(), accountID); svcErr != nil { handleServiceError(c, svcErr) return } response.OK(c, gin.H{"message": "Shopify integration deleted"}) } // Auth returns the Shopify OAuth authorize URL. // POST /api/v1/accounts/:account_id/integrations/shopify/auth func (h *ShopifyIntegrationHandler) Auth(c *gin.Context) { accountID, err := parseUintParam(c, "account_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } var req service.CreateShopifyAuthRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error()) return } redirect, svcErr := h.svc.BuildAuthRedirect(c.Request.Context(), accountID, req) if svcErr != nil { handleServiceError(c, svcErr) return } response.OK(c, redirect) } // GetOrders retrieves Shopify orders for an account. // GET /api/v1/accounts/:account_id/integrations/shopify/orders func (h *ShopifyIntegrationHandler) GetOrders(c *gin.Context) { accountID, err := parseUintParam(c, "account_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id") return } orders, svcErr := h.svc.GetOrders(c.Request.Context(), accountID) if svcErr != nil { handleServiceError(c, svcErr) return } response.OK(c, orders) } // RegisterShopifyIntegrationRoutes registers Shopify integration routes. func RegisterShopifyIntegrationRoutes(g *gin.RouterGroup, h *ShopifyIntegrationHandler) { shopify := g.Group("/shopify") { shopify.DELETE("/", h.Delete) shopify.POST("/auth", h.Auth) shopify.GET("/orders", h.GetOrders) } }