package controllers import ( "whatsapp-backend/models/dto" http_error "whatsapp-backend/models/error" "whatsapp-backend/services" "whatsapp-backend/utils" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type ChatController interface { FetchContacts(ctx *gin.Context) SendMessage(ctx *gin.Context) SendImage(ctx *gin.Context) } type chatController struct { chatService services.ChatService } func NewChatController(chatService services.ChatService) ChatController { return &chatController{chatService} } // FetchContacts godoc // @Summary Fetch WhatsApp contacts // @Description Fetch list of contacts from the connected WhatsApp account // @Tags chat // @Security BearerAuth // @Accept json // @Produce json // @Success 200 {object} dto.FetchContactsResponse // @Failure 401 {object} dto.ErrorResponse // @Failure 400 {object} dto.ErrorResponse // @Router /chat/contacts [get] func (c *chatController) FetchContacts(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) contacts, err := c.chatService.FetchContacts(ctx, accountID) if err != nil { utils.SendResponse[any, any](ctx, nil, nil, err) return } response := dto.FetchContactsResponse{ AccountID: accountID, Contacts: contacts, } utils.SendResponse[dto.FetchContactsResponse, any](ctx, nil, response, nil) } // SendMessage godoc // @Summary Send WhatsApp text message // @Description Send a text message to a WhatsApp number // @Tags chat // @Security BearerAuth // @Accept json // @Produce json // @Param request body dto.SendMessageRequest true "Send Message Request" // @Success 200 {object} dto.SendMessageResponse // @Failure 401 {object} dto.ErrorResponse // @Failure 400 {object} dto.ErrorResponse // @Router /chat/send_msg [post] func (c *chatController) SendMessage(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.SendMessageRequest if err := ctx.ShouldBindJSON(&req); err != nil { utils.SendResponse[any, any](ctx, nil, nil, http_error.ERR_BAD_REQUEST) return } msgID, err := c.chatService.SendMessage(ctx, accountID, req.Recipient, req.Message) if err != nil { utils.SendResponse[any, any](ctx, nil, nil, err) return } response := dto.SendMessageResponse{ MessageID: msgID, Status: "SENT", } utils.SendResponse[dto.SendMessageResponse, any](ctx, nil, response, nil) } // SendImage godoc // @Summary Send WhatsApp image // @Description Send an image to a WhatsApp number via file upload // @Tags chat // @Security BearerAuth // @Accept multipart/form-data // @Produce json // @Param image formData file true "Image file to send" // @Param recipient formData string true "Recipient phone number" example(628123456789) // @Param caption formData string false "Image caption" // @Success 200 {object} dto.SendMessageResponse // @Failure 401 {object} dto.ErrorResponse // @Failure 400 {object} dto.ErrorResponse // @Router /chat/send_img [post] func (c *chatController) SendImage(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) // Get form values recipient := ctx.PostForm("recipient") caption := ctx.PostForm("caption") if recipient == "" { utils.SendResponse[any, any](ctx, nil, nil, http_error.ERR_BAD_REQUEST) return } // Get uploaded file file, err := ctx.FormFile("image") if err != nil { utils.SendResponse[any, any](ctx, nil, nil, http_error.ERR_BAD_REQUEST) return } // Open file src, err := file.Open() if err != nil { utils.SendResponse[any, any](ctx, nil, nil, http_error.ERR_BAD_REQUEST) return } defer src.Close() // Read file content imageData := make([]byte, file.Size) _, err = src.Read(imageData) if err != nil { utils.SendResponse[any, any](ctx, nil, nil, http_error.ERR_BAD_REQUEST) return } // Get mimetype from header mimetype := file.Header.Get("Content-Type") if mimetype == "" { mimetype = "image/jpeg" } msgID, err := c.chatService.SendImage(ctx, accountID, recipient, imageData, mimetype, caption) if err != nil { utils.SendResponse[any, any](ctx, nil, nil, err) return } response := dto.SendMessageResponse{ MessageID: msgID, Status: "SENT", } utils.SendResponse[dto.SendMessageResponse, any](ctx, nil, response, nil) }