Skip to content

Commit

Permalink
addded unchanged roles
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiKaestle committed Jan 24, 2024
1 parent 789bef6 commit 1ba1306
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
30 changes: 26 additions & 4 deletions dao/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ func RoleBulkUpdate(ctx context.Context, i *models.RoleBulkRequest, token *vcapo
return
}

func RoleBulkConfirm(ctx context.Context, i *[]models.RoleHistory, crew_id string, token *vcapool.AccessToken) (result *models.RoleBulkExport, userRolesMap map[string]*models.BulkUserRoles, err error) {
func RoleBulkConfirm(ctx context.Context, i *[]models.RoleHistory, crew_id string, token *vcapool.AccessToken) (result *models.RoleBulkExport, userRolesMap map[string]*models.AspBulkUserRoles, err error) {
if err = models.RolesAdminPermission(token); err != nil {
return
}

userRolesMap = make(map[string]*models.BulkUserRoles)
userRolesMap = make(map[string]*models.AspBulkUserRoles)

role_filter := bson.D{{Key: "crew.crew_id", Value: crew_id}, {Key: "pool_roles", Value: bson.D{{Key: "$exists", Value: true}, {Key: "$ne", Value: "[]"}}}}
deleted_roles_users := new([]models.User)
Expand Down Expand Up @@ -186,11 +186,15 @@ func RoleBulkConfirm(ctx context.Context, i *[]models.RoleHistory, crew_id strin
}

if token.ID != role.UserID {
if userRolesMap[role.UserID] == nil {
userRolesMap[role.UserID] = &models.AspBulkUserRoles{}
}
if index := getIndex(createdRole, *deleted_roles); index >= 0 {
userRolesMap[role.UserID].UnchangedRoles = append(userRolesMap[role.UserID].UnchangedRoles, createdRole.Label)
*deleted_roles = (*deleted_roles)[:index+copy((*deleted_roles)[index:], (*deleted_roles)[index+1:])]
} else {
if userRolesMap[role.UserID] == nil {
userRolesMap[role.UserID] = &models.BulkUserRoles{}
userRolesMap[role.UserID] = &models.AspBulkUserRoles{}
}
userRolesMap[role.UserID].AddedRoles = append(userRolesMap[role.UserID].AddedRoles, createdRole.Label)
}
Expand All @@ -200,7 +204,7 @@ func RoleBulkConfirm(ctx context.Context, i *[]models.RoleHistory, crew_id strin
for _, role := range *deleted_roles {
if token.ID != role.UserID {
if userRolesMap[role.UserID] == nil {
userRolesMap[role.UserID] = &models.BulkUserRoles{}
userRolesMap[role.UserID] = &models.AspBulkUserRoles{}
}
userRolesMap[role.UserID].DeletedRoles = append(userRolesMap[role.UserID].DeletedRoles, role.Label)
}
Expand Down Expand Up @@ -272,6 +276,24 @@ func RoleNotification(ctx context.Context, i map[string]*models.BulkUserRoles) (
return
}

func AspRoleNotification(ctx context.Context, i map[string]*models.AspBulkUserRoles) (err error) {
for index, role := range i {
user := new(models.User)
if err = UserCollection.FindOne(
ctx,
bson.D{{Key: "_id", Value: index}},
user,
); err != nil {
return
}
mail := vcago.NewMailData(user.Email, "pool-backend", "asp_role_update", "pool", user.Country)
mail.AddUser(user.User())
mail.AddContent(user.AspRoleContent(role))
vcago.Nats.Publish("system.mail.job", mail)
}
return
}

func RoleAdminNotification(ctx context.Context, crewID *models.CrewParam) (err error) {
crew := new(models.Crew)
mail := vcago.NewMailData("netzwerk@vivaconagua.org", "pool-backend", "role_network", "pool", "de")
Expand Down
4 changes: 2 additions & 2 deletions handlers/token/role_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (i *RoleHistoryHandler) ConfirmSelection(cc echo.Context) (err error) {
return
}
result := new(models.RoleBulkExport)
userRolesMap := make(map[string]*models.BulkUserRoles)
userRolesMap := make(map[string]*models.AspBulkUserRoles)
if result, userRolesMap, err = dao.RoleBulkConfirm(c.Ctx(), history, body.CrewID, token); err != nil {
return
}
Expand All @@ -91,7 +91,7 @@ func (i *RoleHistoryHandler) ConfirmSelection(cc echo.Context) (err error) {
log.Print(err)
}
}()
if err = dao.RoleNotification(c.Ctx(), userRolesMap); err != nil {
if err = dao.AspRoleNotification(c.Ctx(), userRolesMap); err != nil {
return
}
return c.Created(result)
Expand Down
6 changes: 6 additions & 0 deletions models/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ type BulkUserRoles struct {
DeletedRoles []string `bson:"deleted" json:"deleted"`
}

type AspBulkUserRoles struct {
AddedRoles []string `bson:"created" json:"created"`
DeletedRoles []string `bson:"deleted" json:"deleted"`
UnchangedRoles []string `bson:"unchanged" json:"unchanged"`
}

var PoolRoleCollection = "pool_roles"

func (i *RoleRequest) NewRole() (r *vmod.Role, err error) {
Expand Down
10 changes: 10 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ func (i *User) RoleContent(roles *BulkUserRoles) *vmod.Content {
return content
}

func (i *User) AspRoleContent(roles *AspBulkUserRoles) *vmod.Content {
content := &vmod.Content{
Fields: make(map[string]interface{}),
}
content.Fields["AddedRoles"] = strings.Join(roles.AddedRoles, ", ")
content.Fields["DeletedRoles"] = strings.Join(roles.DeletedRoles, ", ")
content.Fields["UnchangedRoles"] = strings.Join(roles.UnchangedRoles, ", ")
return content
}

func RoleAdminContent(crew *Crew) *vmod.Content {
content := &vmod.Content{
Fields: make(map[string]interface{}),
Expand Down

0 comments on commit 1ba1306

Please sign in to comment.