splitted user registration into user, consents & bankaccount creation
This commit is contained in:
53
internal/services/bank_account_service.go
Normal file
53
internal/services/bank_account_service.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/internal/repositories"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BankAccountService interface {
|
||||
RegisterBankAccount(bankAccount *models.BankAccount) (int64, error)
|
||||
}
|
||||
|
||||
type bankAccountService struct {
|
||||
repo repositories.BankAccountRepository
|
||||
}
|
||||
|
||||
func NewBankAccountService(repo repositories.BankAccountRepository) BankAccountService {
|
||||
return &bankAccountService{repo}
|
||||
}
|
||||
|
||||
func (service *bankAccountService) RegisterBankAccount(bankAccount *models.BankAccount) (int64, error) {
|
||||
bankAccount.MandateDateSigned = time.Now()
|
||||
ref, err := generateSEPAMandateReference(strconv.Itoa(bankAccount.UserID))
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
bankAccount.MandateReference = ref
|
||||
|
||||
return service.repo.CreateBankAccount(bankAccount)
|
||||
}
|
||||
|
||||
func generateUniqueID(length int) (string, error) {
|
||||
bytes := make([]byte, length)
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func generateSEPAMandateReference(userID string) (string, error) {
|
||||
today := time.Now().Format("20060102") // Format YYYYMMDD
|
||||
uniqueID, err := generateUniqueID(4) // 4 Bytes = 8 Hex-Zeichen
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
mandateReference := fmt.Sprintf("%s-%s-%s", userID, today, uniqueID)
|
||||
return mandateReference, nil
|
||||
}
|
||||
25
internal/services/consent_service.go
Normal file
25
internal/services/consent_service.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/internal/repositories"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ConsentService interface {
|
||||
RegisterConsent(consent *models.Consent) (int64, error)
|
||||
}
|
||||
|
||||
type consentService struct {
|
||||
repo repositories.ConsentRepository
|
||||
}
|
||||
|
||||
func NewConsentService(repo repositories.ConsentRepository) ConsentService {
|
||||
return &consentService{repo}
|
||||
}
|
||||
|
||||
func (service *consentService) RegisterConsent(consent *models.Consent) (int64, error) {
|
||||
consent.CreatedAt = time.Now()
|
||||
consent.UpdatedAt = time.Now()
|
||||
return service.repo.CreateConsent(consent)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func ParseTemplate(filename string, data interface{}) (string, error) {
|
||||
|
||||
return tplBuffer.String(), nil
|
||||
}
|
||||
func (s *EmailService) SendWelcomeEmail(user models.User) error {
|
||||
func (s *EmailService) SendWelcomeEmail(user *models.User) error {
|
||||
// Prepare data to be injected into the template
|
||||
data := struct {
|
||||
FirstName string
|
||||
@@ -72,8 +72,8 @@ func (s *EmailService) SendWelcomeEmail(user models.User) error {
|
||||
return s.SendEmail(user.Email, subject, body)
|
||||
}
|
||||
|
||||
func (s *EmailService) NotifyAdminOfNewUser(userEmail string) error {
|
||||
func (s *EmailService) NotifyAdminOfNewUser(user *models.User) error {
|
||||
subject := "New User Registration"
|
||||
body := "<p>A new user has registered with the email: " + userEmail + "</p>"
|
||||
body := "<p>A new user has registered with the email: " + user.Email + "</p>"
|
||||
return s.SendEmail(s.adminEmail, subject, body)
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/internal/repositories"
|
||||
// "GoMembership/pkg/errors"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
// "crypto/rand"
|
||||
// "encoding/base64"
|
||||
// "golang.org/x/crypto/bcrypt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -24,7 +24,7 @@ func NewUserService(repo repositories.UserRepository) UserService {
|
||||
}
|
||||
|
||||
func (service *userService) RegisterUser(user *models.User) (int64, error) {
|
||||
salt := make([]byte, 16)
|
||||
/* salt := make([]byte, 16)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
@@ -34,21 +34,21 @@ func (service *userService) RegisterUser(user *models.User) (int64, error) {
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
user.Password = string(hashedPassword)
|
||||
user.Password = string(hashedPassword) */
|
||||
user.Password = ""
|
||||
user.CreatedAt = time.Now()
|
||||
user.UpdatedAt = time.Now()
|
||||
user.MandateDateSigned = time.Now()
|
||||
return service.repo.CreateUser(user)
|
||||
}
|
||||
|
||||
func HashPassword(password string, salt string) (string, error) {
|
||||
/* func HashPassword(password string, salt string) (string, error) {
|
||||
saltedPassword := password + salt
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(saltedPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(hashedPassword), nil
|
||||
}
|
||||
} */
|
||||
|
||||
/* func (s *userService) AuthenticateUser(email, password string) (*models.User, error) {
|
||||
user, err := s.repo.FindUserByEmail(email)
|
||||
|
||||
Reference in New Issue
Block a user