File size: 3,378 Bytes
83d6851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package poll_handler

import (
	"encoding/json"
	"net/http"

	logger_wrapper "agentdeck-whatsapp-service/pkg/logger"
	poll_model "agentdeck-whatsapp-service/pkg/poll/model"
	poll_service "agentdeck-whatsapp-service/pkg/poll/service"
	"github.com/gin-gonic/gin"
)

// Keep poll_model referenced so the package import is not dropped
// (swag reads Go source, not pre-processed, and needs the alias to be in scope).
var _ = poll_model.PollResults{}

type PollHandler struct {
	pollService   poll_service.PollService
	loggerWrapper *logger_wrapper.LoggerManager
}

// NewPollHandler cria handler usando PollService existente (evita dupla inicialização)
func NewPollHandler(pollService poll_service.PollService, loggerWrapper *logger_wrapper.LoggerManager) *PollHandler {
	return &PollHandler{
		pollService:   pollService,
		loggerWrapper: loggerWrapper,
	}
}

// GetPollResults retorna os resultados de uma enquete
// @Summary Get poll results
// @Description Retorna todos os votos de uma enquete específica
// @Tags Polls
// @Accept json
// @Produce json
// @Param pollMessageId path string true "ID da mensagem da enquete"
// @Success 200 {object} poll_model.PollResults
// @Failure 400 {object} gin.H
// @Failure 404 {object} gin.H
// @Failure 500 {object} gin.H
// @Router /polls/{pollMessageId}/results [get]
func (h *PollHandler) GetPollResults(c *gin.Context) {
	pollMessageID := c.Param("pollMessageId")

	// Pegar instance do contexto de autenticação
	instanceInterface, exists := c.Get("instance")
	if !exists {
		h.loggerWrapper.GetLogger("poll-handler").LogWarn("[POLL] Instance not found in context")
		c.JSON(http.StatusUnauthorized, gin.H{
			"error": "Authentication required",
		})
		return
	}

	// Converter para struct Instance
	type Instance struct {
		Id string `json:"id"`
	}
	instanceBytes, _ := json.Marshal(instanceInterface)
	var instance Instance
	if err := json.Unmarshal(instanceBytes, &instance); err != nil {
		h.loggerWrapper.GetLogger("poll-handler").LogError("[POLL] Failed to parse instance: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": "Failed to get instance information",
		})
		return
	}

	instanceID := instance.Id

	// Validações de segurança
	if pollMessageID == "" {
		h.loggerWrapper.GetLogger("poll-handler").LogWarn("[POLL] Missing pollMessageId")
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "pollMessageId is required",
		})
		return
	}

	h.loggerWrapper.GetLogger("poll-handler").LogInfo("[POLL] Fetching results for poll %s (instance: %s)", pollMessageID, instanceID)

	// Buscar resultados do banco
	results, err := h.pollService.GetPollResults(c.Request.Context(), pollMessageID, instanceID)
	if err != nil {
		h.loggerWrapper.GetLogger("poll-handler").LogError("[POLL] Error fetching results: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": "Failed to fetch poll results",
		})
		return
	}

	if results.TotalVotes == 0 {
		h.loggerWrapper.GetLogger("poll-handler").LogInfo("[POLL] No votes found for poll %s", pollMessageID)
		c.JSON(http.StatusNotFound, gin.H{
			"error":   "No votes found for this poll",
			"message": "This poll has no votes yet, or the pollMessageId is incorrect",
		})
		return
	}

	h.loggerWrapper.GetLogger("poll-handler").LogInfo("[POLL] Returning %d votes for poll %s", results.TotalVotes, pollMessageID)
	c.JSON(http.StatusOK, results)
}