Spaces:
Sleeping
Sleeping
File size: 3,512 Bytes
44c4b7e c099d87 44c4b7e c099d87 44c4b7e c099d87 44c4b7e 6e3fa96 c099d87 6e3fa96 44c4b7e ca18868 c099d87 ca18868 44c4b7e 6e3fa96 44c4b7e c099d87 44c4b7e c099d87 44c4b7e c099d87 44c4b7e c099d87 44c4b7e f5271f7 44c4b7e f5271f7 44c4b7e c099d87 44c4b7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
package controllers
import (
"context"
"whatsapp-backend/models/dto"
entity "whatsapp-backend/models/entity"
http_error "whatsapp-backend/models/error"
"whatsapp-backend/services"
"whatsapp-backend/utils"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type ConnectionController interface {
Connect(ctx *gin.Context)
GetStatus(ctx *gin.Context)
}
type connectionController struct {
connectionService services.ConnectionService
}
func NewConnectionController(connectionService services.ConnectionService) ConnectionController {
return &connectionController{connectionService}
}
// Connect godoc
// @Summary Connect new WhatsApp account
// @Description Initiate a connection request for a new WhatsApp account. Returns a QR code if not connected.
// @Tags whatsapp
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param request body dto.ConnectRequest true "Connect Request"
// @Success 200 {object} dto.ConnectResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 500 {object} dto.ErrorResponse
// @Router /whatsapp/connect [post]
func (cc *connectionController) Connect(ctx *gin.Context) {
userID, exists := ctx.Get("user_id")
if !exists {
utils.SendResponse[any, any](ctx, nil, nil, http_error.UNAUTHORIZED)
return
}
accountID := userID.(uuid.UUID)
var req dto.ConnectRequest
_ = ctx.ShouldBindJSON(&req)
qrCode, err := cc.connectionService.Connect(context.Background(), accountID)
if err != nil {
utils.SendResponse[any, any](ctx, nil, nil, err)
return
}
if qrCode == "" {
utils.SendResponse[any, any](ctx, nil, dto.ConnectResponse{
Message: entity.CONNECTION_ALREADY_CONNECTED,
AccountID: accountID,
Details: entity.CONNECTION_ALREADY_ACTIVE,
}, nil)
return
}
response := dto.ConnectResponse{
Message: entity.CONNECTION_INIT_SUCCESS,
AccountID: accountID,
Details: entity.QR_SCAN_INSTRUCTION,
QRCode: qrCode,
}
utils.SendResponse[dto.ConnectResponse, any](ctx, nil, response, nil)
}
// GetStatus godoc
// @Summary Get WhatsApp connection status
// @Description Get the current status of the WhatsApp connection
// @Tags whatsapp
// @Security BearerAuth
// @Accept json
// @Produce json
// @Success 200 {object} dto.ConnectionStatusResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 500 {object} dto.ErrorResponse
// @Router /whatsapp/status [get]
func (cc *connectionController) GetStatus(ctx *gin.Context) {
userID, exists := ctx.Get("user_id")
if !exists {
utils.SendResponse[any, any](ctx, nil, nil, http_error.UNAUTHORIZED)
return
}
accountID := userID.(uuid.UUID)
client, err := cc.connectionService.GetActiveClient(accountID)
if err != nil {
response := dto.ConnectionStatusResponse{
AccountID: accountID,
Status: entity.WHATSAPP_STATUS_DISCONNECTED,
}
utils.SendResponse[dto.ConnectionStatusResponse, any](ctx, nil, response, nil)
return
}
status := entity.WHATSAPP_STATUS_DISCONNECTED
if client.IsConnected() {
status = entity.WHATSAPP_STATUS_CONNECTED
} else if client.IsLoggedIn() {
status = entity.WHATSAPP_STATUS_CONNECTING
}
jid := ""
if client.Store != nil && client.Store.ID != nil {
jid = client.Store.ID.String()
}
response := dto.ConnectionStatusResponse{
AccountID: accountID,
Status: status,
JID: jid,
}
utils.SendResponse[dto.ConnectionStatusResponse, any](ctx, nil, response, nil)
}
|