/
answersHandler.go
78 lines (65 loc) · 2 KB
/
answersHandler.go
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
package http
import (
"net/http"
"time"
"github.com/labstack/echo"
"github.com/line/line-bot-sdk-go/linebot"
log "github.com/sirupsen/logrus"
"github.com/VagabondDataNinjas/gizlinebot/domain"
"github.com/VagabondDataNinjas/gizlinebot/storage"
)
type AnswersRequest struct {
UserId string `json:"user_id"`
Answers []AnswerItem `json:"answers"`
}
type AnswerItem struct {
QuestionId string `json:"question_id"`
Answer string `json:"answer"`
}
func AnswerHandlerBuilder(s *storage.Sql, bot *linebot.Client) func(c echo.Context) error {
return func(c echo.Context) error {
payload := new(AnswersRequest)
if err := c.Bind(payload); err != nil {
log.Printf("Bind error: %s", err)
return err
}
profile, err := s.GetUserProfile(payload.UserId)
if err != nil {
log.Printf("Error fetching user profile %s: %s", payload.UserId, err)
return err
}
if profile.UserId == "" {
log.Printf("Missing user id %s", payload.UserId)
return c.JSON(http.StatusBadRequest, map[string]string{"status": "error", "reason": "user_id not found"})
}
if !profile.SurveyStarted {
s.ToggleUserSurvey(profile.UserId, true)
}
// @TODO check that the question_ids exist in the questions table
for _, answer := range payload.Answers {
if answer.QuestionId == "price" {
err = sendPriceList(bot, s, profile.UserId)
if err != nil {
log.Errorf("Error sending prices to user: %s - %s", profile.UserId, err)
}
go func(bot *linebot.Client, s *storage.Sql, userId string) {
time.Sleep(5 * time.Second)
err = sendThankYouMsg(bot, s, profile.UserId)
if err != nil {
log.Errorf("Error sending thank you msg to user: %s - %s", profile.UserId, err)
}
}(bot, s, profile.UserId)
}
err := s.UserAddAnswer(domain.Answer{
UserId: payload.UserId,
QuestionId: answer.QuestionId,
Answer: answer.Answer,
Channel: "web",
})
if err != nil {
return err
}
}
return c.JSON(http.StatusOK, map[string]string{"status": "success"})
}
}