Skip to content

Commit

Permalink
Merge pull request #167 from Viva-con-Agua/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
TobiKaestle authored Jan 29, 2024
2 parents c0932a7 + fe7bf62 commit c18cbed
Show file tree
Hide file tree
Showing 20 changed files with 766 additions and 30 deletions.
4 changes: 3 additions & 1 deletion dao/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var (
AvatarCollection *vmdb.Collection

// PoolRoleCollection represents the database collection of the PoolRole Collection.
PoolRoleCollection *vmdb.Collection
PoolRoleCollection *vmdb.Collection
PoolRoleHistoryCollection *vmdb.Collection

MailboxCollection *vmdb.Collection
MessageCollection *vmdb.Collection
Expand Down Expand Up @@ -103,6 +104,7 @@ func InitialDatabase() {

// PoolRoleCollection represents the database collection of the PoolRole Collection.
PoolRoleCollection = Database.Collection(models.PoolRoleCollection).CreateIndex("user_id", false).CreateMultiIndex(bson.D{{Key: "name", Value: 1}, {Key: "user_id", Value: 1}}, true)
PoolRoleHistoryCollection = Database.Collection(models.PoolRoleHistoryCollection).CreateIndex("user_id", false).CreateIndex("crew_id", false)

//
MailboxCollection = Database.Collection(models.MailboxCollection)
Expand Down
32 changes: 32 additions & 0 deletions dao/crew.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package dao
import (
"context"
"pool-backend/models"
"sort"

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcapool"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -72,6 +74,20 @@ func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *vcapool.Access
return
}
filter := i.PermittedFilter(token)
crew := new(models.Crew)
if err = CrewsCollection.FindOne(ctx, filter, &crew); err != nil {
return
}
// Its not allowed to set the asp_selection to "selected" manually
if crew.AspSelection != "selected" && i.AspSelection == "selected" {
return nil, vcago.NewBadRequest(models.CrewCollection, "It is not allowed to set the asp selection state to selected manually!")
}
strings := []string{"active", "inactive"}
sort.Strings(strings)
match := sort.SearchStrings(strings, i.AspSelection)
if crew.AspSelection == "selected" && match < len(strings) && strings[match] == i.AspSelection {
RoleHistoryDelete(ctx, &models.RoleHistoryRequest{CrewID: i.ID, Confirmed: false}, token)
}
if !token.Roles.Validate("employee;admin") {
if err = CrewsCollection.UpdateOne(ctx, filter, vmdb.UpdateSet(i.ToCrewUpdateASP()), &result); err != nil {
return
Expand All @@ -84,6 +100,22 @@ func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *vcapool.Access
return
}

func CrewUpdateAspSelection(ctx context.Context, i *models.CrewParam, value string, token *vcapool.AccessToken) (result *models.Crew, err error) {
if err = models.CrewUpdatePermission(token); err != nil {
return
}
filter := i.PermittedFilter(token)
crew := new(models.CrewUpdate)
if err = CrewsCollection.FindOne(ctx, filter, &crew); err != nil {
return
}
crew.AspSelection = value
if err = CrewsCollection.UpdateOne(ctx, filter, vmdb.UpdateSet(crew), &result); err != nil {
return
}
return
}

func CrewDelete(ctx context.Context, i *models.CrewParam, token *vcapool.AccessToken) (err error) {
if err = models.CrewPermission(token); err != nil {
return
Expand Down
138 changes: 134 additions & 4 deletions dao/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"log"
"pool-backend/models"
"time"

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcago/vmod"
"github.com/Viva-con-Agua/vcapool"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -30,6 +32,9 @@ func RoleInsert(ctx context.Context, i *models.RoleRequest, token *vcapool.Acces
if err = PoolRoleCollection.InsertOne(ctx, result); err != nil {
return
}
if err = PoolRoleHistoryCollection.InsertOne(ctx, models.NewRoleHistory(result, user)); err != nil {
return
}
return
}

Expand Down Expand Up @@ -72,7 +77,9 @@ func RoleBulkUpdate(ctx context.Context, i *models.RoleBulkRequest, token *vcapo
if err = PoolRoleCollection.InsertOne(ctx, createdRole); err != nil {
return
}

if err = PoolRoleHistoryCollection.InsertOne(ctx, models.NewRoleRequestHistory(&role, user)); err != nil {
return
}
if token.ID != role.UserID {
if userRolesMap[role.UserID] == nil {
userRolesMap[role.UserID] = &models.BulkUserRoles{}
Expand Down Expand Up @@ -106,6 +113,18 @@ func RoleBulkUpdate(ctx context.Context, i *models.RoleBulkRequest, token *vcapo
if err = PoolRoleCollection.DeleteOne(ctx, role.Filter()); err != nil {
return
}
history := new(models.RoleHistoryUpdate)
if err = PoolRoleHistoryCollection.FindOne(
ctx,
role.FilterHistory(),
&history,
); err != nil {
return
}
history.EndDate = time.Now().Unix()
if err = PoolRoleHistoryCollection.UpdateOne(ctx, role.FilterHistory(), vmdb.UpdateSet(history), history); err != nil {
return
}
if token.ID != role.UserID {
if userRolesMap[role.UserID] == nil {
userRolesMap[role.UserID] = &models.BulkUserRoles{}
Expand All @@ -117,6 +136,90 @@ 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.AspBulkUserRoles, err error) {
if err = models.RolesAdminPermission(token); err != nil {
return
}

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)
UserViewCollection.Find(ctx, role_filter, deleted_roles_users)
deleted_roles := new([]vmod.Role)
for _, user := range *deleted_roles_users {
*deleted_roles = append(*deleted_roles, user.PoolRoles...)

for _, role := range user.PoolRoles {
if err = PoolRoleCollection.DeleteOne(ctx, bson.D{{Key: "_id", Value: role.ID}}); err != nil {
return
}
}
}

result = new(models.RoleBulkExport)
for _, role := range *i {
filter := role.MatchUser()
user := new(models.User)
if err = UserCollection.AggregateOne(
ctx,
models.UserPipeline(false).Match(filter).Pipe,
user,
); err != nil {
return
}

if err = models.RolesPermission(role.Role, user, token); err != nil {
return
}
userRole := new(models.RoleDatabase)
result.Users = append(result.Users, models.ExportRole{UserID: user.ID, Role: role.Role})

if err = PoolRoleCollection.FindOne(ctx, role.FilterRole(), userRole); err != nil {

createdRole := new(vmod.Role)
if createdRole, err = role.NewRole(); err != nil {
return
}
if err = PoolRoleCollection.InsertOne(ctx, createdRole); err != nil {
return
}

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.AspBulkUserRoles{}
}
userRolesMap[role.UserID].AddedRoles = append(userRolesMap[role.UserID].AddedRoles, createdRole.Label)
}
}
}
}
for _, role := range *deleted_roles {
if token.ID != role.UserID {
if userRolesMap[role.UserID] == nil {
userRolesMap[role.UserID] = &models.AspBulkUserRoles{}
}
userRolesMap[role.UserID].DeletedRoles = append(userRolesMap[role.UserID].DeletedRoles, role.Label)
}
}
result.CrewID = crew_id
return
}
func getIndex(role *vmod.Role, data []vmod.Role) (index int) {
for index, search := range data {
if search.Name == role.Name && search.UserID == role.UserID {
return index
}
}
return -1
}
func RoleDelete(ctx context.Context, i *models.RoleRequest, token *vcapool.AccessToken) (result *vmod.Role, err error) {
filter := i.MatchUser()
user := new(models.User)
Expand All @@ -140,7 +243,18 @@ func RoleDelete(ctx context.Context, i *models.RoleRequest, token *vcapool.Acces
if err = PoolRoleCollection.DeleteOne(ctx, i.Filter()); err != nil {
return
}

history := new(models.RoleHistory)
if err = PoolRoleHistoryCollection.FindOne(
ctx,
i.Filter(),
&history,
); err != nil {
return
}
history.EndDate = time.Now().Unix()
if err = PoolRoleHistoryCollection.UpdateOne(ctx, history.Filter(), vmdb.UpdateSet(history), history); err != nil {
return
}
return
}

Expand All @@ -162,10 +276,26 @@ 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)
if err = CrewsCollection.FindOne(ctx, crewID.Match(), crew); err != nil {
}
mail := vcago.NewMailData("netzwerk@vivaconagua.org", "pool-backend", "role_network", "pool", "de")
mail.AddContent(models.RoleAdminContent(crew))
vcago.Nats.Publish("system.mail.job", mail)
Expand Down
Loading

0 comments on commit c18cbed

Please sign in to comment.