/
mail.go
148 lines (127 loc) · 4.62 KB
/
mail.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package routes
import (
"errors"
"fmt"
"github.com/DusanKasan/parsemail"
"github.com/gin-gonic/gin"
c "github.com/openline-ai/openline-customer-os/packages/server/comms-api/config"
"github.com/openline-ai/openline-customer-os/packages/server/comms-api/model"
"github.com/openline-ai/openline-customer-os/packages/server/comms-api/routes/ContactHub"
s "github.com/openline-ai/openline-customer-os/packages/server/comms-api/service"
"github.com/openline-ai/openline-customer-os/packages/server/comms-api/tracing"
tracingLog "github.com/opentracing/opentracing-go/log"
"log"
"net/http"
"strings"
)
func addMailRoutes(conf *c.Config, rg *gin.RouterGroup, mailService s.MailService, hub *ContactHub.ContactHub) {
rg.POST("/mail/send", func(c *gin.Context) {
span, _ := tracing.StartHttpServerTracerSpanWithHeader(c.Request.Context(), "mail/send", c.Request.Header)
defer span.Finish()
var request model.MailReplyRequest
if err := c.BindJSON(&request); err != nil {
tracing.TraceErr(span, err)
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
return
}
if conf.Mail.ApiKey != c.GetHeader("X-Openline-Mail-Api-Key") {
errorMsg := "invalid mail API Key!"
tracing.TraceErr(span, errors.New(errorMsg))
log.Printf(errorMsg)
c.JSON(http.StatusForbidden, gin.H{"error": errorMsg})
return
}
username := c.GetHeader("X-Openline-USERNAME")
if username == "" {
errMsg := "username header not found"
tracing.TraceErr(span, errors.New(errMsg))
c.JSON(http.StatusBadRequest, gin.H{"msg": errMsg})
return
}
replyMail, err := mailService.SendMail(&request, &username)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
return
}
mail, err := mailService.SaveMail(replyMail, nil, &request.Username)
if err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to save email: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
}
span.LogFields(tracingLog.String("result - interactionEventId", (*mail).InteractionEventCreate.Id))
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("interaction event created with id: %s", (*mail).InteractionEventCreate.Id),
})
})
rg.POST("/mail/fwd/", func(c *gin.Context) {
span, _ := tracing.StartHttpServerTracerSpanWithHeader(c.Request.Context(), "mail/fwd/", c.Request.Header)
defer span.Finish()
var req model.MailFwdRequest
if err := c.BindJSON(&req); err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to parse json: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusBadRequest, gin.H{
"error": errorMsg,
})
return
}
if conf.Mail.ApiKey != req.ApiKey {
errorMsg := "invalid mail API Key!"
tracing.TraceErr(span, errors.New(errorMsg))
log.Printf(errorMsg)
c.JSON(http.StatusForbidden, gin.H{"error": errorMsg})
return
}
if err := validateMailPostRequest(req); err != nil {
tracing.TraceErr(span, err)
log.Printf("Invalid request: %v", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
email, err := parsemail.Parse(strings.NewReader(req.RawMessage))
if err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to parse email: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusBadRequest, gin.H{"error": errorMsg})
return
}
saveResponse, err := mailService.SaveMail(&email, &req.Tenant, nil)
if err != nil {
tracing.TraceErr(span, err)
errorMsg := fmt.Sprintf("unable to save forwarded email: %v", err.Error())
log.Printf(errorMsg)
c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
return
}
for _, participant := range saveResponse.InteractionEventCreate.SentTo {
contacts := participant.EmailParticipant.Contacts
for _, contact := range contacts {
log.Printf("Broadcasting to participant %s", contact.Id)
contactItem := ContactHub.ContactEvent{
ContactId: contact.Id,
InteractionEvent: saveResponse.InteractionEventCreate,
}
hub.Broadcast <- contactItem
}
}
span.LogFields(tracingLog.String("result - interactionEventId", (*saveResponse).InteractionEventCreate.Id))
c.JSON(http.StatusOK, gin.H{
"result": fmt.Sprintf("interaction event created with id: %s", (*saveResponse).InteractionEventCreate.Id),
})
})
}
func validateMailPostRequest(req model.MailFwdRequest) error {
// Add validation checks for other fields in the MailPostRequest struct
if req.Tenant == "" {
return errors.New("missing tenant field")
}
if req.RawMessage == "" {
return errors.New("missing raw message field")
}
return nil
}