Files
gochat/backend/internal/handler/api/v1/dyte_integration_handler.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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)
}
}