162 lines
5.2 KiB
Go
162 lines
5.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"GoMembership/internal/models"
|
|
"GoMembership/internal/services"
|
|
|
|
// "github.com/gorilla/mux"
|
|
"net/http"
|
|
// "strconv"
|
|
"GoMembership/internal/utils"
|
|
"GoMembership/pkg/logger"
|
|
)
|
|
|
|
type UserController struct {
|
|
service services.UserService
|
|
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, membershipService services.MembershipService) *UserController {
|
|
return &UserController{service, *emailService, consentService, bankAccountService, membershipService}
|
|
}
|
|
|
|
func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
|
rh := utils.NewResponseHandler(w)
|
|
|
|
var regData RegistrationData
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(®Data); err != nil {
|
|
// http.Error(w, err.Error(), http.StatusBadRequest)
|
|
logger.Error.Printf("Couldn't decode Userdata: %v", err)
|
|
rh.RespondWithError(http.StatusBadRequest, "Couldn't decode userdata")
|
|
return
|
|
}
|
|
logger.Info.Printf("registering user: %v", regData.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)
|
|
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User")
|
|
return
|
|
}
|
|
regData.User.ID = id
|
|
|
|
// Register Bank Account
|
|
_, err = uc.bankAccountService.RegisterBankAccount(®Data.BankAccount)
|
|
if err != nil {
|
|
logger.Error.Printf("Couldn't register bank account: %v", err)
|
|
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-BankAccount")
|
|
return
|
|
}
|
|
|
|
// Register Consents
|
|
var consents = [2]models.Consent{
|
|
{
|
|
FirstName: regData.User.FirstName,
|
|
LastName: regData.User.LastName,
|
|
Email: regData.User.Email,
|
|
ConsentType: "TermsOfService",
|
|
},
|
|
{
|
|
FirstName: regData.User.FirstName,
|
|
LastName: regData.User.LastName,
|
|
Email: regData.User.Email,
|
|
ConsentType: "Privacy",
|
|
},
|
|
}
|
|
for _, consent := range consents {
|
|
_, err = uc.consentService.RegisterConsent(&consent)
|
|
if err != nil {
|
|
logger.Error.Printf("Couldn't register consent: %v", err)
|
|
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-consent")
|
|
return
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
// rh.RespondWithError(http.StatusServiceUnavailable, "User creation succeeded, but failed to notify admin of new user registration")
|
|
}
|
|
rh.RespondWithJSON(http.StatusCreated, map[string]interface{}{
|
|
"status": "success",
|
|
"id": regData.User.ID,
|
|
})
|
|
}
|
|
|
|
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"`
|
|
Password string `json:"password"`
|
|
}
|
|
user, err := uc.service.AuthenticateUser(credentials.Email, credentials.Password)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(user)
|
|
}
|
|
*/
|
|
/* func (uc *UserController) GetUserID(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid user ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
user, err := uc.service.GetUserByID(id)
|
|
if err != nil {
|
|
http.Error(w, "User not found: "+err.Error(), http.StatusNotFound)
|
|
}
|
|
} */
|