Spaces:
Running
Running
| 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) | |
| } | |