90 lines
2.9 KiB
Go
90 lines
2.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
type DyteIntegrationHandler struct {
|
|
svc *service.DyteIntegrationService
|
|
}
|
|
|
|
func NewDyteIntegrationHandler(svc *service.DyteIntegrationService) *DyteIntegrationHandler {
|
|
return &DyteIntegrationHandler{svc: svc}
|
|
}
|
|
|
|
type dyteCreateMeetingRequest struct {
|
|
ConversationID uint `json:"conversation_id"`
|
|
}
|
|
|
|
type dyteAddParticipantRequest struct {
|
|
MessageID uint `json:"message_id"`
|
|
}
|
|
|
|
// CreateMeeting starts a Dyte meeting and creates the Chatwoot integration message.
|
|
// Reference: Api::V1::Accounts::Integrations::DyteController#create_a_meeting.
|
|
func (h *DyteIntegrationHandler) CreateMeeting(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil || accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
var req dyteCreateMeetingRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.ConversationID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "conversation_id is required")
|
|
return
|
|
}
|
|
message, conversation, apiErr, svcErr := h.svc.CreateMeeting(c.Request.Context(), accountID, getUserID(c), req.ConversationID, getRole(c))
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
if apiErr != nil {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": apiErr.Payload, "error_code": apiErr.Status})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, serializeMessage(c.Request.Context(), h.svc.DB(), message, conversation))
|
|
}
|
|
|
|
// AddParticipant adds the current user as a participant to an existing Dyte meeting.
|
|
// Reference: Api::V1::Accounts::Integrations::DyteController#add_participant_to_meeting.
|
|
func (h *DyteIntegrationHandler) AddParticipant(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil || accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
var req dyteAddParticipantRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.MessageID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "message_id is required")
|
|
return
|
|
}
|
|
payload, apiErr, svcErr := h.svc.AddParticipant(c.Request.Context(), accountID, getUserID(c), req.MessageID, getRole(c))
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
if apiErr != nil {
|
|
status := http.StatusUnprocessableEntity
|
|
body := gin.H{"error": apiErr.Payload}
|
|
if apiErr.Status != http.StatusUnprocessableEntity {
|
|
body["error_code"] = apiErr.Status
|
|
}
|
|
c.JSON(status, body)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, payload)
|
|
}
|
|
|
|
func RegisterDyteIntegrationRoutes(g *gin.RouterGroup, h *DyteIntegrationHandler) {
|
|
dyte := g.Group("/dyte")
|
|
{
|
|
dyte.POST("/create_a_meeting", h.CreateMeeting)
|
|
dyte.POST("/add_participant_to_meeting", h.AddParticipant)
|
|
}
|
|
}
|