feat(conversations): align draft message route
This commit is contained in:
@@ -19,6 +19,98 @@ func NewDraftMessageHandler(draftSvc *service.DraftMessageService) *DraftMessage
|
||||
return &DraftMessageHandler{draftSvc: draftSvc}
|
||||
}
|
||||
|
||||
// Show retrieves the Chatwoot conversation-scoped draft message.
|
||||
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/draft_messages
|
||||
func (h *DraftMessageHandler) Show(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
conversationID, err := parseUintParam(c, "conversation_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
||||
return
|
||||
}
|
||||
|
||||
draft, svcErr := h.draftSvc.ShowConversationDraft(c.Request.Context(), accountID, conversationID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
if draft == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"has_draft": false})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"has_draft": true, "message": draft.Content})
|
||||
}
|
||||
|
||||
// UpdateConversationDraft upserts the Chatwoot conversation-scoped draft message.
|
||||
// PATCH/PUT /api/v1/accounts/:account_id/conversations/:conversation_id/draft_messages
|
||||
func (h *DraftMessageHandler) UpdateConversationDraft(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
conversationID, err := parseUintParam(c, "conversation_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
DraftMessage struct {
|
||||
Message *string `json:"message"`
|
||||
} `json:"draft_message"`
|
||||
Message *string `json:"message"`
|
||||
Content *string `json:"content"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
message := ""
|
||||
if req.DraftMessage.Message != nil {
|
||||
message = *req.DraftMessage.Message
|
||||
} else if req.Message != nil {
|
||||
message = *req.Message
|
||||
} else if req.Content != nil {
|
||||
message = *req.Content
|
||||
}
|
||||
|
||||
if svcErr := h.draftSvc.SetConversationDraft(c.Request.Context(), accountID, conversationID, getUserID(c), message); svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// DeleteConversationDraft deletes the Chatwoot conversation-scoped draft message.
|
||||
// DELETE /api/v1/accounts/:account_id/conversations/:conversation_id/draft_messages
|
||||
func (h *DraftMessageHandler) DeleteConversationDraft(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
conversationID, err := parseUintParam(c, "conversation_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.draftSvc.DeleteConversationDraft(c.Request.Context(), accountID, conversationID); svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// List retrieves all draft messages for a conversation (filtered by current user).
|
||||
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/draft_messages
|
||||
func (h *DraftMessageHandler) List(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user