diff --git a/server/internal/controllers/chain.go b/server/internal/controllers/chain.go index c6d05fa6e..b61e1a13f 100644 --- a/server/internal/controllers/chain.go +++ b/server/internal/controllers/chain.go @@ -14,7 +14,6 @@ import ( "github.com/the-clothing-loop/website/server/internal/services" "github.com/the-clothing-loop/website/server/internal/views" "github.com/the-clothing-loop/website/server/pkg/tsp" - "gopkg.in/guregu/null.v3/zero" "github.com/gin-gonic/gin" uuid "github.com/satori/go.uuid" @@ -549,12 +548,12 @@ func ChainRemoveUser(c *gin.Context) { chain.ClearAllLastNotifiedIsUnapprovedAt(db) // if the user is removed by an admin, do not send an email to this one - var excludedEmail zero.String + var excludedEmail string if _, isChainAdmin := authUser.IsPartOfChain(chain.UID); isChainAdmin { - excludedEmail = authUser.Email + excludedEmail = authUser.Email.String } // send email to chain admins - services.EmailLoopAdminsOnUserLeft(db, user, chain.ID, excludedEmail) + services.EmailLoopAdminsOnUserLeft(db, user.Name, user.Email.String, excludedEmail, chain.ID) } func ChainApproveUser(c *gin.Context) { diff --git a/server/internal/controllers/users.go b/server/internal/controllers/users.go index e8d4cbab9..4364a9e31 100644 --- a/server/internal/controllers/users.go +++ b/server/internal/controllers/users.go @@ -12,6 +12,7 @@ import ( "github.com/the-clothing-loop/website/server/internal/app/auth" "github.com/the-clothing-loop/website/server/internal/app/goscope" "github.com/the-clothing-loop/website/server/internal/models" + "github.com/the-clothing-loop/website/server/internal/services" "github.com/the-clothing-loop/website/server/internal/views" "github.com/the-clothing-loop/website/server/pkg/noderoute" "gopkg.in/guregu/null.v3" @@ -344,6 +345,19 @@ func UserPurge(c *gin.Context) { } } + err := user.AddUserChainsToObject(db) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + // notify connected hosts, send email to chain admins + chainIDs := []uint{} + for _, uc := range user.Chains { + chainIDs = append(chainIDs, uc.ChainID) + } + services.EmailLoopAdminsOnUserLeft(db, user.Name, user.Email.String, "", chainIDs...) + // find chains where user is the last chain admin chainIDsToDelete := []uint{} db.Raw(` @@ -360,7 +374,7 @@ HAVING COUNT(uc.id) = 1 tx := db.Begin() - err := user.DeleteUserChainDependenciesAllChains(tx) + err = user.DeleteUserChainDependenciesAllChains(tx) if err != nil { tx.Rollback() goscope.Log.Errorf("UserPurge: %v", err) diff --git a/server/internal/services/email.go b/server/internal/services/email.go index 57191c79f..64d8f9750 100644 --- a/server/internal/services/email.go +++ b/server/internal/services/email.go @@ -6,7 +6,6 @@ import ( "github.com/the-clothing-loop/website/server/internal/app/goscope" "github.com/the-clothing-loop/website/server/internal/models" "github.com/the-clothing-loop/website/server/internal/views" - "gopkg.in/guregu/null.v3/zero" "gorm.io/gorm" ) @@ -46,9 +45,10 @@ func EmailLoopAdminsOnUserJoin(db *gorm.DB, user *models.User, chainIDs ...uint) return nil } -func EmailLoopAdminsOnUserLeft(db *gorm.DB, user *models.User, chainIDs uint, excludedEmail zero.String) error { +// excludedEmail can be an empty string +func EmailLoopAdminsOnUserLeft(db *gorm.DB, removedUserName, removedUserEmail, excludedEmail string, chainIDs ...uint) error { // find admin users related to the chain to email - admins, err := models.UserGetAdminsByChain(db, chainIDs) + admins, err := models.UserGetAdminsByChain(db, chainIDs...) if err != nil { return err } @@ -60,14 +60,14 @@ func EmailLoopAdminsOnUserLeft(db *gorm.DB, user *models.User, chainIDs uint, ex for _, admin := range admins { email := admin.Email - if !email.Valid || email.String == excludedEmail.String || email.String == user.Email.String { + if !email.Valid || excludedEmail == email.String || removedUserEmail == email.String { continue } views.EmailSomeoneLeftLoop(db, admin.I18n, admin.Name, admin.Email.String, admin.ChainName, - user.Name, + removedUserName, ) }