From ff3106b8bedeac92e80d127a845832ff56f5f6f4 Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Sun, 2 Mar 2025 23:14:03 +0100 Subject: [PATCH] backend: fixed wrong error codes --- go-backend/internal/constants/constants.go | 11 ++++++----- .../internal/controllers/user_controller.go | 16 +++++++++++++++- .../internal/controllers/user_controller_test.go | 8 ++++---- go-backend/internal/database/db.go | 4 ++-- go-backend/internal/middlewares/auth.go | 4 ++-- go-backend/internal/utils/response_handler.go | 2 +- .../validation/subscription_validation.go | 2 +- .../internal/validation/user_validation.go | 10 +++++----- go-backend/pkg/errors/errors.go | 2 +- 9 files changed, 37 insertions(+), 22 deletions(-) diff --git a/go-backend/internal/constants/constants.go b/go-backend/internal/constants/constants.go index a5c0158..b761977 100644 --- a/go-backend/internal/constants/constants.go +++ b/go-backend/internal/constants/constants.go @@ -9,11 +9,12 @@ const ( DelayedPaymentStatus SettledPaymentStatus AwaitingPaymentStatus - MailVerificationSubject = "Nur noch ein kleiner Schritt!" - MailChangePasswordSubject = "Passwort Änderung angefordert" - MailRegistrationSubject = "Neues Mitglied hat sich registriert" - MailWelcomeSubject = "Willkommen beim Dörpsmobil Hasloh e.V." - MailContactSubject = "Jemand hat das Kontaktformular gefunden" + MailVerificationSubject = "Nur noch ein kleiner Schritt!" + MailChangePasswordSubject = "Passwort Änderung angefordert" + MailRegistrationSubject = "Neues Mitglied hat sich registriert" + MailWelcomeSubject = "Willkommen beim Dörpsmobil Hasloh e.V." + MailContactSubject = "Jemand hat das Kontaktformular gefunden" + SupporterSubscriptionModelName = "Keins" ) var Licences = struct { diff --git a/go-backend/internal/controllers/user_controller.go b/go-backend/internal/controllers/user_controller.go index 4114de1..cf4af6a 100644 --- a/go-backend/internal/controllers/user_controller.go +++ b/go-backend/internal/controllers/user_controller.go @@ -274,7 +274,11 @@ func (uc *UserController) RegisterUser(c *gin.Context) { return } } - regData.User.RoleID = constants.Roles.Member + if regData.User.Membership.SubscriptionModel.Name == constants.SupporterSubscriptionModelName { + regData.User.RoleID = constants.Roles.Supporter + } else { + regData.User.RoleID = constants.Roles.Member + } // Register User id, token, err := uc.Service.RegisterUser(®Data.User) @@ -288,6 +292,16 @@ func (uc *UserController) RegisterUser(c *gin.Context) { } regData.User.ID = id + // if this is a supporter don't send mails and he never did give any consent. So stop here + if regData.User.RoleID == constants.Roles.Supporter { + + c.JSON(http.StatusCreated, gin.H{ + "message": "Supporter Registration successuful", + "id": regData.User.ID, + }) + return + } + // Register Consents var consents = [2]models.Consent{ { diff --git a/go-backend/internal/controllers/user_controller_test.go b/go-backend/internal/controllers/user_controller_test.go index 53d792b..fa9ff58 100644 --- a/go-backend/internal/controllers/user_controller_test.go +++ b/go-backend/internal/controllers/user_controller_test.go @@ -316,7 +316,7 @@ func testCurrentUserHandler(t *testing.T, loginEmail string, loginCookie http.Co setupCookie: func(req *http.Request) {}, expectedStatus: http.StatusUnauthorized, expectedErrors: []map[string]string{ - {"field": "general", "key": "server.error.no_auth_token"}, + {"field": "server.general", "key": "server.error.no_auth_token"}, }, }, { @@ -326,7 +326,7 @@ func testCurrentUserHandler(t *testing.T, loginEmail string, loginCookie http.Co }, expectedStatus: http.StatusUnauthorized, expectedErrors: []map[string]string{ - {"field": "general", "key": "server.error.no_auth_token"}, + {"field": "server.general", "key": "server.error.no_auth_token"}, }, }, } @@ -507,7 +507,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook }, expectedStatus: http.StatusUnauthorized, expectedErrors: []map[string]string{ - {"field": "general", "key": "server.error.no_auth_token"}, + {"field": "server.general", "key": "server.error.no_auth_token"}, }, }, { @@ -948,7 +948,7 @@ func verifyMail(verificationURL string) error { router := gin.New() router.LoadHTMLGlob(filepath.Join(config.Templates.HTMLPath, "*")) - router.GET("/users/verify", Uc.VerifyMailHandler) + router.GET("api/users/verify", Uc.VerifyMailHandler) wv := httptest.NewRecorder() cv, _ := gin.CreateTestContext(wv) var err error diff --git a/go-backend/internal/database/db.go b/go-backend/internal/database/db.go index 64d408f..3fe0ee3 100644 --- a/go-backend/internal/database/db.go +++ b/go-backend/internal/database/db.go @@ -91,8 +91,8 @@ func Open(dbPath string, adminMail string) error { func createSubscriptionModels() []models.SubscriptionModel { return []models.SubscriptionModel{ { - Name: "Keins", - Details: "Dieses Modell ist für Vereinsmitglieder, die keinen Wunsch haben, an dem Carhsharing teilzunehmen.", + Name: constants.SupporterSubscriptionModelName, + Details: "Dieses Modell ist für Sponsoren und Nichtmitglieder, die keinen Vereinsmitglied sind.", HourlyRate: 999, MonthlyFee: 0, }, diff --git a/go-backend/internal/middlewares/auth.go b/go-backend/internal/middlewares/auth.go index 2f5d651..3693a42 100644 --- a/go-backend/internal/middlewares/auth.go +++ b/go-backend/internal/middlewares/auth.go @@ -80,7 +80,7 @@ func AuthMiddleware() gin.HandlerFunc { logger.Error.Printf("No Auth token: %v\n", err) c.JSON(http.StatusUnauthorized, gin.H{"errors": []gin.H{{ - "field": "general", + "field": "server.general", "key": "server.error.no_auth_token", }}}) c.Abort() @@ -97,7 +97,7 @@ func AuthMiddleware() gin.HandlerFunc { logger.Error.Printf("Token(%v) is invalid: %v\n", tokenString, err) c.JSON(http.StatusUnauthorized, gin.H{"errors": []gin.H{{ - "field": "general", + "field": "server.general", "key": "server.error.no_auth_token", }}}) c.Abort() diff --git a/go-backend/internal/utils/response_handler.go b/go-backend/internal/utils/response_handler.go index 26ebca4..9611f51 100644 --- a/go-backend/internal/utils/response_handler.go +++ b/go-backend/internal/utils/response_handler.go @@ -30,7 +30,7 @@ func HandleValidationError(c *gin.Context, err error) { } } else { validationErrors = append(validationErrors, gin.H{ - "field": "general", + "field": "server.general", "key": "server.error.invalid_json", }) } diff --git a/go-backend/internal/validation/subscription_validation.go b/go-backend/internal/validation/subscription_validation.go index a4a2cc2..6cda34c 100644 --- a/go-backend/internal/validation/subscription_validation.go +++ b/go-backend/internal/validation/subscription_validation.go @@ -40,7 +40,7 @@ func ValidateSubscription(sl validator.StructLevel) { // This is a nested probably user struct. We are only checking if the model exists existingSubscription, err := repositories.GetSubscriptionByName(&subscription.Name) if err != nil || existingSubscription == nil { - sl.ReportError(subscription.Name, "Subscription_Name", "name", "exists", "") + sl.ReportError(subscription.Name, "subscription.name", "name", "duplicate", "") } } } diff --git a/go-backend/internal/validation/user_validation.go b/go-backend/internal/validation/user_validation.go index a5927df..26929be 100644 --- a/go-backend/internal/validation/user_validation.go +++ b/go-backend/internal/validation/user_validation.go @@ -23,17 +23,17 @@ func validateUser(sl validator.StructLevel) { } } // Validate User > 18 years old - if user.DateOfBirth.After(time.Now().AddDate(-18, 0, 0)) { - sl.ReportError(user.DateOfBirth, "DateOfBirth", "dateofbirth", "age", "") + if user.RoleID > constants.Roles.Supporter && user.DateOfBirth.After(time.Now().AddDate(-18, 0, 0)) { + sl.ReportError(user.DateOfBirth, "user.user", "user.dateofbirth", "age", "") } // validate subscriptionModel if user.Membership.SubscriptionModel.Name == "" { - sl.ReportError(user.Membership.SubscriptionModel.Name, "SubscriptionModel.Name", "name", "required", "") + sl.ReportError(user.Membership.SubscriptionModel.Name, "subscription.name", "name", "required", "") } else { selectedModel, err := repositories.GetSubscriptionByName(&user.Membership.SubscriptionModel.Name) if err != nil { logger.Error.Printf("Error finding subscription model for user %v: %v", user.Email, err) - sl.ReportError(user.Membership.SubscriptionModel.Name, "SubscriptionModel.Name", "name", "invalid", "") + sl.ReportError(user.Membership.SubscriptionModel.Name, "subscription.name", "name", "invalid", "") } else { user.Membership.SubscriptionModel = *selectedModel } @@ -42,7 +42,7 @@ func validateUser(sl validator.StructLevel) { validateMembership(sl) if !isSuper { validateBankAccount(sl) - if user.Licence != nil { + if user.Licence != nil && user.RoleID > constants.Roles.Supporter { validateDriverslicence(sl) } } diff --git a/go-backend/pkg/errors/errors.go b/go-backend/pkg/errors/errors.go index 60a4a55..5b474c2 100644 --- a/go-backend/pkg/errors/errors.go +++ b/go-backend/pkg/errors/errors.go @@ -74,7 +74,7 @@ var Responses = struct { UndeliveredVerificationMail: "server.error.undelivered_verification_mail", }, Fields: ValidationFields{ - General: "general", + General: "server.general", ParentMemberShipID: "parent_membership_id", SubscriptionModel: "subscription_model", Login: "user.login",