refactoring db; added email-verification
This commit is contained in:
@@ -18,15 +18,17 @@ type UserController struct {
|
||||
emailService services.EmailService
|
||||
consentService services.ConsentService
|
||||
bankAccountService services.BankAccountService
|
||||
membershipService services.MembershipService
|
||||
}
|
||||
|
||||
type RegistrationData struct {
|
||||
User models.User `json:"user"`
|
||||
BankAccount models.BankAccount `json:"bank_account"`
|
||||
Membership models.Membership `json:"membership"`
|
||||
}
|
||||
|
||||
func NewUserController(service services.UserService, emailService *services.EmailService, consentService services.ConsentService, bankAccountService services.BankAccountService) *UserController {
|
||||
return &UserController{service, *emailService, consentService, bankAccountService}
|
||||
func NewUserController(service services.UserService, emailService *services.EmailService, consentService services.ConsentService, bankAccountService services.BankAccountService, membershipService services.MembershipService) *UserController {
|
||||
return &UserController{service, *emailService, consentService, bankAccountService, membershipService}
|
||||
}
|
||||
|
||||
func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -42,7 +44,8 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
logger.Info.Printf("registering user: %v", regData.User)
|
||||
|
||||
id, err := uc.service.RegisterUser(®Data.User)
|
||||
// Register User
|
||||
id, token, err := uc.service.RegisterUser(®Data.User)
|
||||
if err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
logger.Error.Printf("Couldn't register User: %v", err)
|
||||
@@ -50,13 +53,16 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
regData.User.ID = id
|
||||
|
||||
// Register Bank Account
|
||||
_, err = uc.bankAccountService.RegisterBankAccount(®Data.BankAccount)
|
||||
if err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
logger.Error.Printf("Couldn't register bank account: %v", err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User")
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-BankAccount")
|
||||
return
|
||||
}
|
||||
|
||||
// Register Consents
|
||||
var consents = [2]models.Consent{
|
||||
{
|
||||
FirstName: regData.User.FirstName,
|
||||
@@ -74,19 +80,25 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
for _, consent := range consents {
|
||||
_, err = uc.consentService.RegisterConsent(&consent)
|
||||
if err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
logger.Error.Printf("Couldn't register consent: %v", err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User")
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-consent")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Send welcome email to the user
|
||||
if err := uc.emailService.SendWelcomeEmail(®Data.User); err != nil {
|
||||
logger.Error.Printf("Failed to send welcome email to user: %v", err)
|
||||
// rh.RespondWithError(http.StatusServiceUnavailable, "User creation succeeded, but failed to send welcome email to user")
|
||||
// Register Membership
|
||||
_, err = uc.membershipService.RegisterMembership(®Data.Membership)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Couldn't register membership: %v", err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-membership")
|
||||
return
|
||||
}
|
||||
|
||||
// Send notifications
|
||||
if err := uc.emailService.SendVerificationEmail(®Data.User, &token); err != nil {
|
||||
logger.Error.Printf("Failed to send email verification email to user: %v", err)
|
||||
// rh.RespondWithError(http.StatusServiceUnavailable, "User creation succeeded, but failed to send welcome email to user")
|
||||
}
|
||||
// Notify admin of new user registration
|
||||
if err := uc.emailService.NotifyAdminOfNewUser(®Data.User); err != nil {
|
||||
logger.Error.Printf("Failed to notify admin of new user registration: %v", err)
|
||||
@@ -98,6 +110,30 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func (uc *UserController) VerifyMailHandler(w http.ResponseWriter, r *http.Request) {
|
||||
rh := utils.NewResponseHandler(w)
|
||||
|
||||
token := r.URL.Query().Get("token")
|
||||
if token == "" {
|
||||
logger.Error.Println("Missing token to verify mail")
|
||||
rh.RespondWithError(http.StatusNoContent, "Missing token")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := uc.service.VerifyUser(&token)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Cannot verify user: %v", err)
|
||||
rh.RespondWithError(http.StatusUnauthorized, "Cannot verify user")
|
||||
}
|
||||
|
||||
membership, err := uc.membershipService.FindMembershipByUserID(user.ID)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Cannot get membership of user %v: %v", user.ID, err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Cannot get Membership of user")
|
||||
}
|
||||
uc.emailService.SendWelcomeEmail(user, membership)
|
||||
}
|
||||
|
||||
/* func (uc *UserController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
var credentials struct {
|
||||
Email string `json:"email"`
|
||||
|
||||
Reference in New Issue
Block a user