Skip to content

Commit

Permalink
Merge pull request #695 from gclamigueiro/send-email-to-admins-when-u…
Browse files Browse the repository at this point in the history
…ser-leave-the-loop

send email to admins when user leave the loop
  • Loading branch information
lil5 authored Feb 29, 2024
2 parents 6f13dd1 + cff5ce6 commit b8a1958
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
8 changes: 8 additions & 0 deletions server/internal/controllers/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,14 @@ 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 string
if _, isChainAdmin := authUser.IsPartOfChain(chain.UID); isChainAdmin {
excludedEmail = authUser.Email.String
}
// send email to chain admins
services.EmailLoopAdminsOnUserLeft(db, user.Name, user.Email.String, excludedEmail, chain.ID)
}

func ChainApproveUser(c *gin.Context) {
Expand Down
16 changes: 15 additions & 1 deletion server/internal/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(`
Expand All @@ -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)
Expand Down
30 changes: 29 additions & 1 deletion server/internal/services/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
Given a user and a list of ChainIds, this method performs the following actions:
- searchs all admin from the given chains
- sends them an email with the new user's information.
- Also, it sends an email to the user for every loop he intends to join.
*/
func EmailLoopAdminsOnUserJoin(db *gorm.DB, user *models.User, chainIDs ...uint) error {
// find admin users related to the chain to email
Expand Down Expand Up @@ -46,6 +45,35 @@ func EmailLoopAdminsOnUserJoin(db *gorm.DB, user *models.User, chainIDs ...uint)
return nil
}

// 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...)
if err != nil {
return err
}

if len(admins) == 0 {
goscope.Log.Errorf("Empty chain that is still public: ChainID: %d", chainIDs)
return fmt.Errorf("No admins exist for this loop")
}

for _, admin := range admins {
email := admin.Email
if !email.Valid || excludedEmail == email.String || removedUserEmail == email.String {
continue
}
views.EmailSomeoneLeftLoop(db, admin.I18n,
admin.Name,
admin.Email.String,
admin.ChainName,
removedUserName,
)
}

return nil
}

func EmailYouSignedUpForLoop(db *gorm.DB, user *models.User, chainNames ...string) error {
for _, chainName := range chainNames {
if !user.Email.Valid {
Expand Down

0 comments on commit b8a1958

Please sign in to comment.