218 lines
8.1 KiB
Go
218 lines
8.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
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(), 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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
page, _ := parseIntQueryDefault(c, "page", 1)
|
|
pageSize := 25
|
|
assistantID, _ := parseOptionalUintQueryParam(c, "assistant_id")
|
|
documentID, _ := parseOptionalUintQueryParam(c, "document_id")
|
|
status := c.Query("status")
|
|
search := c.Query("search")
|
|
|
|
responses, total, err := h.svc.List(c.Request.Context(), accountID, assistantID, 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
|
|
}
|
|
payload := make([]gin.H, 0, len(responses))
|
|
for i := range responses {
|
|
payload = append(payload, captainAssistantResponsePayload(&responses[i]))
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"payload": payload, "meta": gin.H{"total_count": total, "page": page}})
|
|
}
|
|
|
|
// 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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
responseID, err := parseUintParam(c, "response_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid response_id")
|
|
return
|
|
}
|
|
resp, err := h.svc.Get(c.Request.Context(), accountID, responseID)
|
|
if err != nil {
|
|
applogger.L().Errorf("Get assistant response: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "response not found")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, captainAssistantResponsePayload(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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
responseID, err := parseUintParam(c, "response_id")
|
|
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 := bindNestedJSONPayload(c, "assistant_response", &req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
resp, err := h.svc.Update(c.Request.Context(), accountID, 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
|
|
}
|
|
c.JSON(http.StatusOK, captainAssistantResponsePayload(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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
responseID, err := parseUintParam(c, "response_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid response_id")
|
|
return
|
|
}
|
|
if err := h.svc.Delete(c.Request.Context(), accountID, 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 := parseAccountIDParam(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
userID := getUserID(c)
|
|
|
|
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 := bindNestedJSONPayload(c, "assistant_response", &req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
resp, err := h.svc.Create(c.Request.Context(), 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
|
|
}
|
|
c.JSON(http.StatusOK, captainAssistantResponsePayload(resp))
|
|
}
|
|
|
|
func captainAssistantResponsePayload(resp *model.CaptainAssistantResponse) gin.H {
|
|
payload := gin.H{
|
|
"account_id": resp.AccountID,
|
|
"answer": resp.Answer,
|
|
"assistant": captainResponseAssistantPayload(resp),
|
|
"created_at": resp.CreatedAt.Unix(),
|
|
"id": resp.ID,
|
|
"question": resp.Question,
|
|
"updated_at": resp.UpdatedAt.Unix(),
|
|
"status": resp.Status,
|
|
"edited": resp.Edited,
|
|
}
|
|
if resp.DocumentableID != nil {
|
|
payload["documentable"] = captainResponseDocumentablePayload(resp)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func captainResponseAssistantPayload(resp *model.CaptainAssistantResponse) gin.H {
|
|
if resp.Assistant.ID == 0 {
|
|
return gin.H{"id": resp.AssistantID}
|
|
}
|
|
return captainAssistantPayload(&resp.Assistant)
|
|
}
|
|
|
|
func captainResponseDocumentablePayload(resp *model.CaptainAssistantResponse) gin.H {
|
|
payload := gin.H{"type": resp.DocumentableType, "id": *resp.DocumentableID}
|
|
if resp.DocumentableType == "Conversation" {
|
|
payload["display_id"] = *resp.DocumentableID
|
|
}
|
|
return payload
|
|
}
|