From 2acbe703ebb8461ee1516c68dced97dc4e44f555 Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Sun, 29 Sep 2024 21:06:30 +0200 Subject: [PATCH] add: subscription seeding, enhance admin creation --- internal/database/db.go | 49 ++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/internal/database/db.go b/internal/database/db.go index 728a118..c81eda1 100644 --- a/internal/database/db.go +++ b/internal/database/db.go @@ -38,11 +38,24 @@ func Open(dbPath string, adminMail string) error { var count int64 db.Model(&models.User{}).Count(&count) if count == 0 { + subscriptionModels := createSubscriptionModels() + for _, model := range subscriptionModels { + result := db.Create(&model) + if result.Error != nil { + return result.Error + } + } + logger.Error.Printf("Init Subscriptions: %v", subscriptionModels) + var createdModel models.SubscriptionModel + if err := db.First(&createdModel).Error; err != nil { + return err + } + admin, err := createAdmin(adminMail, createdModel.ID) if err != nil { return err } - result := db.Create(&admin) + result := db.Session(&gorm.Session{FullSaveAssociations: true}).Create(&admin) if result.Error != nil { return result.Error } @@ -50,7 +63,18 @@ func Open(dbPath string, adminMail string) error { return nil } +func createSubscriptionModels() []models.SubscriptionModel { + return []models.SubscriptionModel{ + { + Name: "Keins", + Details: "Dieses Modell ist für Vereinsmitglieder, die keinen Wunsch haben, an dem Carhsharing teilzunehmen.", + HourlyRate: 999, + MonthlyFee: 0, + }, + } +} // TODO: Landing page to create an admin + func createAdmin(userMail string, subscriptionModelID uint) (*models.User, error) { passwordBytes := make([]byte, 12) _, err := rand.Read(passwordBytes) @@ -59,10 +83,8 @@ func createAdmin(userMail string, subscriptionModelID uint) (*models.User, error } // Encode into a URL-safe base64 string - password, err := base64.URLEncoding.EncodeToString(passwordBytes)[:12], nil - if err != nil { - return nil, err - } + password := base64.URLEncoding.EncodeToString(passwordBytes)[:12] + hash, err := argon2id.CreateHash(password, argon2id.DefaultParams) if err != nil { return nil, err @@ -76,14 +98,25 @@ func createAdmin(userMail string, subscriptionModelID uint) (*models.User, error return &models.User{ FirstName: "ad", LastName: "min", - DateOfBirth: time.Now(), + DateOfBirth: time.Now().AddDate(-20, 0, 0), Password: hash, Address: "Downhill 4", - ZipCode: "9999", + ZipCode: "99999", City: "TechTown", + Phone: "0123455678", Email: userMail, Status: constants.ActiveStatus, - RoleID: constants.Roles.Editor, + RoleID: constants.Roles.Admin, + Membership: models.Membership{ + Status: constants.DisabledStatus, + StartDate: time.Now(), + SubscriptionModelID: subscriptionModelID, + }, + BankAccount: models.BankAccount{ + AccountHolderName: "Niemand", + Bank: "Keine", + IBAN: "DE49700500000008447644", //fake + }, }, nil }