From 351ed1ab8ac06645b5a25c2080eff34255368455 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 14 Oct 2024 18:42:27 +0200 Subject: [PATCH] Do requested changes --- models/user/email_address.go | 18 +----------------- modules/structs/org.go | 10 +++++----- routers/api/v1/org/org.go | 30 ++++++++++++++++-------------- tests/integration/api_org_test.go | 20 +++++++++++++++++--- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/models/user/email_address.go b/models/user/email_address.go index e8c81a730c..b3078e4c6c 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -214,24 +214,8 @@ func DeletePrimaryEmailAddressOfUser(ctx context.Context, uid int64) error { return fmt.Errorf("%s is not a organization", user.Name) } - ctx, committer, err := db.TxContext(ctx) - if err != nil { - return err - } - defer committer.Close() - - _, err = db.GetEngine(ctx).Exec("DELETE FROM email_address WHERE uid = ? AND is_primary = true", uid) - if err != nil { - return err - } - user.Email = "" - err = UpdateUserCols(ctx, user, "email") - if err != nil { - return err - } - - return committer.Commit() + return UpdateUserCols(ctx, user, "email") } // GetEmailAddresses returns all email addresses belongs to given user. diff --git a/modules/structs/org.go b/modules/structs/org.go index b2b2c61a01..686345b2c3 100644 --- a/modules/structs/org.go +++ b/modules/structs/org.go @@ -47,11 +47,11 @@ type CreateOrgOption struct { // EditOrgOption options for editing an organization type EditOrgOption struct { - FullName string `json:"full_name" binding:"MaxSize(100)"` - Email string `json:"email" binding:"MaxSize(255)"` - Description string `json:"description" binding:"MaxSize(255)"` - Website string `json:"website" binding:"ValidUrl;MaxSize(255)"` - Location string `json:"location" binding:"MaxSize(50)"` + FullName string `json:"full_name" binding:"MaxSize(100)"` + Email *string `json:"email" binding:"MaxSize(255)"` + Description string `json:"description" binding:"MaxSize(255)"` + Website string `json:"website" binding:"ValidUrl;MaxSize(255)"` + Location string `json:"location" binding:"MaxSize(50)"` // possible values are `public`, `limited` or `private` // enum: ["public", "limited", "private"] Visibility string `json:"visibility" binding:"In(,public,limited,private)"` diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 5f4fc8f4b6..50a8b0ff76 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -345,21 +345,23 @@ func Edit(ctx *context.APIContext) { form := web.GetForm(ctx).(*api.EditOrgOption) - if form.Email == "" { - err := user_model.DeletePrimaryEmailAddressOfUser(ctx, ctx.Org.Organization.ID) - if err != nil { - ctx.Error(http.StatusInternalServerError, "DeletePrimaryEmailAddressOfUser", err) - return - } - ctx.Org.Organization.Email = "" - } else { - if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), form.Email); err != nil { - if user_model.IsErrEmailInvalid(err) || user_model.IsErrEmailCharIsNotSupported(err) { - ctx.Error(http.StatusUnprocessableEntity, "ReplacePrimaryEmailAddress", err) - } else { - ctx.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err) + if form.Email != nil { + if *form.Email == "" { + err := user_model.DeletePrimaryEmailAddressOfUser(ctx, ctx.Org.Organization.ID) + if err != nil { + ctx.Error(http.StatusInternalServerError, "DeletePrimaryEmailAddressOfUser", err) + return + } + ctx.Org.Organization.Email = "" + } else { + if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), *form.Email); err != nil { + if user_model.IsErrEmailInvalid(err) || user_model.IsErrEmailCharIsNotSupported(err) { + ctx.Error(http.StatusUnprocessableEntity, "ReplacePrimaryEmailAddress", err) + } else { + ctx.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err) + } + return } - return } } diff --git a/tests/integration/api_org_test.go b/tests/integration/api_org_test.go index 4aff75cbd9..57bc6b5d7c 100644 --- a/tests/integration/api_org_test.go +++ b/tests/integration/api_org_test.go @@ -234,7 +234,8 @@ func TestAPIOrgChangeEmail(t *testing.T) { token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) t.Run("Invalid", func(t *testing.T) { - settings := api.EditOrgOption{Email: "invalid"} + newMail := "invalid" + settings := api.EditOrgOption{Email: &newMail} resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusUnprocessableEntity) @@ -245,7 +246,19 @@ func TestAPIOrgChangeEmail(t *testing.T) { }) t.Run("Valid", func(t *testing.T) { - settings := api.EditOrgOption{Email: "example@example.com"} + newMail := "example@example.com" + settings := api.EditOrgOption{Email: &newMail} + + resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK) + + var org *api.Organization + DecodeJSON(t, resp, &org) + + assert.Equal(t, "example@example.com", org.Email) + }) + + t.Run("NoChange", func(t *testing.T) { + settings := api.EditOrgOption{} resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK) @@ -256,7 +269,8 @@ func TestAPIOrgChangeEmail(t *testing.T) { }) t.Run("Empty", func(t *testing.T) { - settings := api.EditOrgOption{Email: ""} + newMail := "" + settings := api.EditOrgOption{Email: &newMail} resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK)