185 lines
7.1 KiB
Go
185 lines
7.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/service"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// CaptainAssistantResponseHandler handles assistant response generation and storage.
|
|
// Reference: Chatwoot Captain::AssistantResponsesController
|
|
type CaptainAssistantResponseHandler struct {
|
|
svc *service.CaptainAssistantResponseService
|
|
}
|
|
|
|
func NewCaptainAssistantResponseHandler(svc *service.CaptainAssistantResponseService) *CaptainAssistantResponseHandler {
|
|
return &CaptainAssistantResponseHandler{svc: svc}
|
|
}
|
|
|
|
// ProcessResponse generates and optionally stores an assistant response.
|
|
// POST /api/v1/accounts/:id/captain/assistant_responses
|
|
func (h *CaptainAssistantResponseHandler) ProcessResponse(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
var req service.ProcessResponseRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.ProcessResponse(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Process assistant response: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, response.ErrInternal, "failed to process assistant response")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// List returns paginated assistant responses.
|
|
// GET /api/v1/accounts/:id/captain/assistant_responses
|
|
// Reference: Chatwoot Captain::AssistantResponsesController#index
|
|
func (h *CaptainAssistantResponseHandler) List(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("per_page", "25"))
|
|
assistantID, _ := strconv.ParseUint(c.Query("assistant_id"), 10, 64)
|
|
documentID, _ := strconv.ParseUint(c.Query("document_id"), 10, 64)
|
|
status := c.Query("status")
|
|
search := c.Query("search")
|
|
|
|
responses, total, err := h.svc.List(c.Request.Context(), uint(accountID), uint(assistantID), uint(documentID), status, search, page, pageSize)
|
|
if err != nil {
|
|
applogger.L().Errorf("List assistant responses: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list responses")
|
|
return
|
|
}
|
|
response.OKWithMeta(c, responses, page, pageSize, total)
|
|
}
|
|
|
|
// Get returns a single assistant response.
|
|
// GET /api/v1/accounts/:id/captain/assistant_responses/:response_id
|
|
// Reference: Chatwoot Captain::AssistantResponsesController#show
|
|
func (h *CaptainAssistantResponseHandler) Get(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
responseID, err := strconv.ParseUint(c.Param("response_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid response_id")
|
|
return
|
|
}
|
|
resp, err := h.svc.Get(c.Request.Context(), uint(accountID), uint(responseID))
|
|
if err != nil {
|
|
applogger.L().Errorf("Get assistant response: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "response not found")
|
|
return
|
|
}
|
|
response.OK(c, resp)
|
|
}
|
|
|
|
// Update modifies an assistant response.
|
|
// PUT /api/v1/accounts/:id/captain/assistant_responses/:response_id
|
|
// Reference: Chatwoot Captain::AssistantResponsesController#update
|
|
func (h *CaptainAssistantResponseHandler) Update(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
responseID, err := strconv.ParseUint(c.Param("response_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid response_id")
|
|
return
|
|
}
|
|
var req struct {
|
|
Question string `json:"question"`
|
|
Answer string `json:"answer"`
|
|
Status string `json:"status"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
resp, err := h.svc.Update(c.Request.Context(), uint(accountID), uint(responseID), req.Question, req.Answer, req.Status)
|
|
if err != nil {
|
|
applogger.L().Errorf("Update assistant response: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update response")
|
|
return
|
|
}
|
|
response.OK(c, resp)
|
|
}
|
|
|
|
// Delete removes an assistant response.
|
|
// DELETE /api/v1/accounts/:id/captain/assistant_responses/:response_id
|
|
// Reference: Chatwoot Captain::AssistantResponsesController#destroy (head :no_content)
|
|
func (h *CaptainAssistantResponseHandler) Delete(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
responseID, err := strconv.ParseUint(c.Param("response_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid response_id")
|
|
return
|
|
}
|
|
if err := h.svc.Delete(c.Request.Context(), uint(accountID), uint(responseID)); err != nil {
|
|
applogger.L().Errorf("Delete assistant response: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete response")
|
|
return
|
|
}
|
|
// 1:1 Chatwoot: head :no_content → 204
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// Create creates a new assistant response.
|
|
// POST /api/v1/accounts/:id/captain/assistant_responses
|
|
// Reference: Chatwoot Captain::AssistantResponsesController#create
|
|
func (h *CaptainAssistantResponseHandler) Create(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
userID := getUserID(c)
|
|
if userID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "user not authenticated")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Question string `json:"question" validate:"required"`
|
|
Answer string `json:"answer" validate:"required"`
|
|
AssistantID uint `json:"assistant_id" validate:"required"`
|
|
Status string `json:"status"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
resp, err := h.svc.Create(c.Request.Context(), uint(accountID), userID, req.AssistantID, req.Question, req.Answer, req.Status)
|
|
if err != nil {
|
|
applogger.L().Errorf("Create assistant response: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create response")
|
|
return
|
|
}
|
|
response.OK(c, resp)
|
|
}
|