second commit
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
)
|
||||
|
||||
// WhatsAppCallHandler handles WhatsApp voice call API endpoints.
|
||||
type WhatsAppCallHandler struct {
|
||||
svc *service.WhatsAppCallService
|
||||
}
|
||||
|
||||
// NewWhatsAppCallHandler creates a new WhatsAppCallHandler.
|
||||
func NewWhatsAppCallHandler(svc *service.WhatsAppCallService) *WhatsAppCallHandler {
|
||||
return &WhatsAppCallHandler{svc: svc}
|
||||
}
|
||||
|
||||
// Get retrieves a WhatsApp call by call_id.
|
||||
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/whatsapp_calls/:call_id
|
||||
func (h *WhatsAppCallHandler) Get(c *gin.Context) {
|
||||
callID := c.Param("call_id")
|
||||
if callID == "" {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid call_id")
|
||||
return
|
||||
}
|
||||
|
||||
call, svcErr := h.svc.GetByCallID(c.Request.Context(), callID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, call)
|
||||
}
|
||||
|
||||
// List retrieves all WhatsApp calls for a conversation.
|
||||
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/whatsapp_calls
|
||||
func (h *WhatsAppCallHandler) List(c *gin.Context) {
|
||||
conversationID, err := parseUintParam(c, "conversation_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
||||
return
|
||||
}
|
||||
|
||||
calls, svcErr := h.svc.ListByConversation(c.Request.Context(), conversationID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"whatsapp_calls": calls})
|
||||
}
|
||||
|
||||
// Create creates a new WhatsApp call record.
|
||||
// POST /api/v1/accounts/:account_id/conversations/:conversation_id/whatsapp_calls
|
||||
func (h *WhatsAppCallHandler) Create(c *gin.Context) {
|
||||
var req struct {
|
||||
CallID string `json:"call_id" binding:"required"`
|
||||
CallStatus string `json:"call_status" binding:"required"`
|
||||
Duration int `json:"duration"`
|
||||
CallerNumber string `json:"caller_number"`
|
||||
InboxID uint `json:"inbox_id" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
conversationID, err := parseUintParam(c, "conversation_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
||||
return
|
||||
}
|
||||
|
||||
call := &service.WhatsAppCallCreateRequest{
|
||||
CallID: req.CallID,
|
||||
InboxID: req.InboxID,
|
||||
ConversationID: conversationID,
|
||||
CallStatus: req.CallStatus,
|
||||
Duration: req.Duration,
|
||||
CallerNumber: req.CallerNumber,
|
||||
}
|
||||
|
||||
result, svcErr := h.svc.CreateFromRequest(c.Request.Context(), call)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
// Update updates a WhatsApp call (e.g. status transition from ringing -> active -> ended).
|
||||
// PUT /api/v1/accounts/:account_id/conversations/:conversation_id/whatsapp_calls/:call_id
|
||||
func (h *WhatsAppCallHandler) Update(c *gin.Context) {
|
||||
callID := c.Param("call_id")
|
||||
if callID == "" {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid call_id")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
CallStatus string `json:"call_status" binding:"required"`
|
||||
Duration int `json:"duration"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
result, svcErr := h.svc.UpdateByCallID(c.Request.Context(), callID, req.CallStatus, req.Duration)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
// Delete removes a WhatsApp call record.
|
||||
// DELETE /api/v1/accounts/:account_id/conversations/:conversation_id/whatsapp_calls/:call_id
|
||||
func (h *WhatsAppCallHandler) Delete(c *gin.Context) {
|
||||
callID := c.Param("call_id")
|
||||
if callID == "" {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid call_id")
|
||||
return
|
||||
}
|
||||
|
||||
svcErr := h.svc.DeleteByCallID(c.Request.Context(), callID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"message": "deleted"})
|
||||
}
|
||||
Reference in New Issue
Block a user