refactoring:

* removed extra for loop
* commenting on functions
This commit is contained in:
lemonScaletech 2023-12-08 10:38:09 +05:30
parent 5cb94a7820
commit b8c2ab4cf8

View File

@ -94,6 +94,8 @@ func clearSessionIfRequired(currentData, updatedData map[string]interface{}) {
} }
} }
// updateRoles will update DB for user roles, if a role is deleted by admin
// then this function will those roles from user roles if exists
func updateRoles(ctx context.Context, deletedRoles []string) error { func updateRoles(ctx context.Context, deletedRoles []string) error {
data, err := db.Provider.ListUsers(ctx, &model.Pagination{ data, err := db.Provider.ListUsers(ctx, &model.Pagination{
Limit: 1, Limit: 1,
@ -111,14 +113,10 @@ func updateRoles(ctx context.Context, deletedRoles []string) error {
} }
for i := range allData.Users { for i := range allData.Users {
now := time.Now().Unix() roles := utils.DeleteFromArray(allData.Users[i].Roles, deletedRoles)
allData.Users[i].Roles = utils.DeleteFromArray(allData.Users[i].Roles, deletedRoles) if len(allData.Users[i].Roles) != len(roles) {
allData.Users[i].UpdatedAt = &now
}
for i := range allData.Users {
updatedValues := map[string]interface{}{ updatedValues := map[string]interface{}{
"roles": strings.Join(allData.Users[i].Roles, ","), "roles": strings.Join(roles, ","),
"updated_at": time.Now().Unix(), "updated_at": time.Now().Unix(),
} }
id := []string{allData.Users[i].ID} id := []string{allData.Users[i].ID}
@ -127,6 +125,7 @@ func updateRoles(ctx context.Context, deletedRoles []string) error {
return err return err
} }
} }
}
return nil return nil
} }