273 lines
7.9 KiB
Go
273 lines
7.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/canned"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// CannedResponseHandler handles CannedResponse CRUD + search endpoints.
|
|
// Reference: Chatwoot CannedResponsesController — CRUD + search by short_code/content
|
|
type CannedResponseHandler struct {
|
|
svc *canned.CannedResponseService
|
|
}
|
|
|
|
type cannedResponseInput struct {
|
|
ShortCode *string `json:"short_code"`
|
|
Content *string `json:"content"`
|
|
}
|
|
|
|
type cannedResponsePayload struct {
|
|
ID uint `json:"id"`
|
|
AccountID uint `json:"account_id"`
|
|
ShortCode string `json:"short_code"`
|
|
Content string `json:"content"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// NewCannedResponseHandler creates a new CannedResponseHandler.
|
|
func NewCannedResponseHandler(svc *canned.CannedResponseService) *CannedResponseHandler {
|
|
return &CannedResponseHandler{svc: svc}
|
|
}
|
|
|
|
func bindCannedResponseInput(c *gin.Context) (cannedResponseInput, error) {
|
|
var body map[string]json.RawMessage
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
return cannedResponseInput{}, err
|
|
}
|
|
|
|
if raw, ok := body["canned_response"]; ok {
|
|
var wrapped cannedResponseInput
|
|
if err := json.Unmarshal(raw, &wrapped); err != nil {
|
|
return cannedResponseInput{}, err
|
|
}
|
|
return wrapped, nil
|
|
}
|
|
|
|
var input cannedResponseInput
|
|
if raw, ok := body["short_code"]; ok {
|
|
if err := json.Unmarshal(raw, &input.ShortCode); err != nil {
|
|
return cannedResponseInput{}, err
|
|
}
|
|
}
|
|
if raw, ok := body["content"]; ok {
|
|
if err := json.Unmarshal(raw, &input.Content); err != nil {
|
|
return cannedResponseInput{}, err
|
|
}
|
|
}
|
|
return input, nil
|
|
}
|
|
|
|
func serializeCannedResponse(cr canned.CannedResponse) cannedResponsePayload {
|
|
return cannedResponsePayload{
|
|
ID: cr.ID,
|
|
AccountID: cr.AccountID,
|
|
ShortCode: cr.ShortCode,
|
|
Content: cr.Content,
|
|
CreatedAt: cr.CreatedAt,
|
|
UpdatedAt: cr.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func serializeCannedResponses(responses []canned.CannedResponse) []cannedResponsePayload {
|
|
payload := make([]cannedResponsePayload, 0, len(responses))
|
|
for _, cr := range responses {
|
|
payload = append(payload, serializeCannedResponse(cr))
|
|
}
|
|
return payload
|
|
}
|
|
|
|
// Create creates a new canned response.
|
|
// POST /api/v1/accounts/:account_id/canned_responses
|
|
// Reference: Chatwoot CannedResponsesController#create
|
|
func (h *CannedResponseHandler) Create(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
req, err := bindCannedResponseInput(c)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
if req.ShortCode == nil || *req.ShortCode == "" || req.Content == nil || *req.Content == "" {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "short_code and content are required")
|
|
return
|
|
}
|
|
|
|
cr := &canned.CannedResponse{
|
|
AccountID: accountID,
|
|
ShortCode: *req.ShortCode,
|
|
Content: *req.Content,
|
|
}
|
|
|
|
if err := h.svc.Create(c.Request.Context(), cr); err != nil {
|
|
applogger.L().Errorf("Create canned response: %v", err)
|
|
handleServiceError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeCannedResponse(*cr))
|
|
}
|
|
|
|
// Get retrieves a canned response by ID.
|
|
// GET /api/v1/accounts/:account_id/canned_responses/:id
|
|
// Reference: Chatwoot CannedResponsesController#show
|
|
func (h *CannedResponseHandler) Get(c *gin.Context) {
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
cr, svcErr := h.svc.GetByAccountAndID(c.Request.Context(), accountID, id)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeCannedResponse(*cr))
|
|
}
|
|
|
|
// Update updates a canned response.
|
|
// PUT /api/v1/accounts/:account_id/canned_responses/:id
|
|
// Reference: Chatwoot CannedResponsesController#update
|
|
func (h *CannedResponseHandler) Update(c *gin.Context) {
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
req, err := bindCannedResponseInput(c)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
updates := map[string]interface{}{}
|
|
if req.ShortCode != nil {
|
|
updates["short_code"] = *req.ShortCode
|
|
}
|
|
if req.Content != nil {
|
|
updates["content"] = *req.Content
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "no fields to update")
|
|
return
|
|
}
|
|
|
|
if svcErr := h.svc.UpdateByAccountAndID(c.Request.Context(), accountID, id, updates); svcErr != nil {
|
|
applogger.L().Errorf("Update canned response: %v", svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
cr, svcErr := h.svc.GetByAccountAndID(c.Request.Context(), accountID, id)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeCannedResponse(*cr))
|
|
}
|
|
|
|
// Delete soft-deletes a canned response.
|
|
// DELETE /api/v1/accounts/:account_id/canned_responses/:id
|
|
// Reference: Chatwoot CannedResponsesController#destroy
|
|
func (h *CannedResponseHandler) Delete(c *gin.Context) {
|
|
id, err := parseUintParam(c, "id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
|
return
|
|
}
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
if svcErr := h.svc.DeleteByAccountAndID(c.Request.Context(), accountID, id); svcErr != nil {
|
|
applogger.L().Errorf("Delete canned response: %v", svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
// List returns all canned responses for an account.
|
|
// GET /api/v1/accounts/:account_id/canned_responses
|
|
// Reference: Chatwoot CannedResponsesController#index
|
|
func (h *CannedResponseHandler) List(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
searchQuery := c.Query("search")
|
|
if searchQuery == "" {
|
|
searchQuery = c.Query("q")
|
|
}
|
|
if searchQuery != "" {
|
|
responses, svcErr := h.svc.Search(c.Request.Context(), accountID, searchQuery)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, serializeCannedResponses(responses))
|
|
return
|
|
}
|
|
|
|
responses, svcErr := h.svc.ListByAccount(c.Request.Context(), accountID)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeCannedResponses(responses))
|
|
}
|
|
|
|
// Search performs a ranked search over canned responses.
|
|
// GET /api/v1/accounts/:account_id/canned_responses/search?q=...
|
|
// Reference: Chatwoot CannedResponsesController#index with search parameter (order_by_search scope)
|
|
func (h *CannedResponseHandler) Search(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
searchQuery := c.Query("search")
|
|
if searchQuery == "" {
|
|
searchQuery = c.Query("q")
|
|
}
|
|
responses, svcErr := h.svc.Search(c.Request.Context(), accountID, searchQuery)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeCannedResponses(responses))
|
|
}
|