Files
gochat/internal/handler/api/v1/shopify_integration_handler.go
T
2026-06-04 15:44:48 +08:00

88 lines
2.6 KiB
Go

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 creates/updates Shopify OAuth credentials.
// 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
}
hook, svcErr := h.svc.Auth(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, hook)
}
// 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)
}
}