-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_register_verification_resend_post.go
executable file
·113 lines (95 loc) · 3.04 KB
/
handler_register_verification_resend_post.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
package handlers
import (
"encoding/json"
"io"
"net/http"
"gopkg.in/mgo.v2"
"github.com/IlijevskiSymphony/symphonyGopher/server/configuration"
"github.com/IlijevskiSymphony/symphonyGopher/server/partners"
"github.com/IlijevskiSymphony/symphonyGopher/server/registrations"
"github.com/Sirupsen/logrus"
"github.com/pborman/uuid"
)
type ResendPost struct {
Email string `json:"email"`
}
type HandlerRegisterVerificationResendPost struct {
Configuration configuration.Configuration
SessionFn func() *mgo.Session
}
func (h HandlerRegisterVerificationResendPost) ServeHTTP(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var posted ResendPost
if err := decoder.Decode(&posted); err != nil {
logrus.Infof("Cannot decode email. Error: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if posted.Email != "" {
registrationsRepo := registrations.NewRepository(h.SessionFn())
defer registrationsRepo.Close()
partnersRepo := partners.NewRepository(h.SessionFn())
defer partnersRepo.Close()
partner, err := partnersRepo.FindByEmail(posted.Email)
if err != nil {
logrus.Infof("Cannot find partner. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if partner == nil {
logrus.Info("Partner doesn't exists")
w.WriteHeader(http.StatusNotFound)
return
}
registration, err := registrationsRepo.FindByPartner(partner.ID)
if err != nil {
logrus.Infof("Cannot find registration. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if registration == nil {
logrus.Info("Registration doesn't exists")
w.WriteHeader(http.StatusNotFound)
return
}
registration.Reference = uuid.New()
if err := registrationsRepo.Update(registration); err != nil {
logrus.Infof("Cannot update registration. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
//Email sent here
//
// ms := mail.Service{Host: h.Configuration.MandrillHost}
// m := mail.Mail{
// Key: h.Configuration.MandrillApiKey,
// Message: mail.Message{
// Html: fmt.Sprintf(mail.Template.Message, registration.Reference, registration.Reference),
// Text: "",
// Subject: mail.Template.Subject,
// FromEmail: mail.Template.Sender.Email,
// FromName: mail.Template.Sender.Name,
// To: mail.Receivers{mail.Receiver{Email: partner.Email, Name: "", Type: "to"}},
// },
// Async: false,
// }
//
// if err := ms.Send(m); err != nil {
// logrus.Infof("Cannot send registration mail. Error: %s", err)
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
registration.Status = registrations.StatusVerificationSent
if err := registrationsRepo.Update(registration); err != nil {
logrus.Infof("Cannot update registration. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, "Please check your inbox!")
} else {
logrus.Info("Empty email.")
w.WriteHeader(http.StatusBadRequest)
return
}
}