splitted user registration into user, consents & bankaccount creation

This commit is contained in:
$(pass /github/name)
2024-07-07 21:39:58 +02:00
parent a76d73aa82
commit 555d1be575
13 changed files with 270 additions and 53 deletions

View 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
}