add GetUsersByPermissionMetho

This commit is contained in:
Hoang 2022-03-25 14:16:52 +07:00
parent ab9bfaacb7
commit eb52943468
5 changed files with 137 additions and 0 deletions

View File

@ -64,6 +64,11 @@ func (s Service) GetAllUsers(query model.UserAllQuery) model.UserAll {
return user.All(query) return user.All(query)
} }
// GetUsersByPermission ...
func (s Service) GetUsersByPermission(query model.UserByPermissionQuery) model.UserAll {
return user.GetUsersByPermission(query)
}
// CountAllUsers ... // CountAllUsers ...
func (s Service) CountAllUsers(query model.UserCountQuery) int64 { func (s Service) CountAllUsers(query model.UserCountQuery) int64 {
return user.Count(query) return user.Count(query)

View File

@ -17,6 +17,7 @@ type CommonQuery struct {
Deleted string Deleted string
Sort interface{} Sort interface{}
Other map[string]interface{} Other map[string]interface{}
RoleIDs []primitive.ObjectID
} }
// AssignDeleted ... // AssignDeleted ...
@ -45,6 +46,17 @@ func (q *CommonQuery) AssignRoleID(cond bson.M) {
} }
} }
// AssignRoleIDs ...
func (q *CommonQuery) AssignRoleIDs(cond bson.M) {
if len(q.RoleIDs) == 1 {
cond["roleId"] = q.RoleIDs[0]
} else if len(q.RoleIDs) > 1 {
cond["roleId"] = bson.M{
"$in": q.RoleIDs,
}
}
}
// AssignStatus ... // AssignStatus ...
func (q *CommonQuery) AssignStatus(cond bson.M) { func (q *CommonQuery) AssignStatus(cond bson.M) {
if q.Status != "" { if q.Status != "" {

View File

@ -48,6 +48,18 @@ type UserAllQuery struct {
Cond bson.M Cond bson.M
} }
// UserByPermissionQuery ...
type UserByPermissionQuery struct {
Page int64
Limit int64
Keyword string
Status string
Permission string // permission code
Sort interface{}
Other map[string]interface{} // query fields in other object
Cond bson.M
}
// UserCountQuery ... // UserCountQuery ...
type UserCountQuery struct { type UserCountQuery struct {
RoleID string RoleID string
@ -163,3 +175,16 @@ func (co ChangePasswordOptions) Validate() error {
return nil return nil
} }
// Validate ...
func (q UserByPermissionQuery) Validate() error {
// OldPassword, NewPassword
if q.Permission == "" {
logger.Error("usermngmt - User - GetUsersByPermission : invalid permission", logger.LogData{
"payload": q,
})
return errors.New(internal.ErrorInvalidPermission)
}
return nil
}

View File

@ -79,6 +79,33 @@ func roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, err
return doc, err return doc, err
} }
func permissionFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) {
var (
col = database.GetPermissionCol()
)
docs = make([]model.DBPermission, 0)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
logger.Error("usermngmt - Permission - Find", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
logger.Error("usermngmt - Permission - Decode", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
return
}
// permissionCountByCondition ... // permissionCountByCondition ...
func permissionCountByCondition(ctx context.Context, cond interface{}) int64 { func permissionCountByCondition(ctx context.Context, cond interface{}) int64 {
var ( var (

View File

@ -203,6 +203,74 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) {
return return
} }
// GetUsersByPermission ...
func GetUsersByPermission(queryParams model.UserByPermissionQuery) (r model.UserAll) {
var (
ctx = context.Background()
wg sync.WaitGroup
cond = bson.M{}
roles = make([]primitive.ObjectID, 0)
)
// Validate query
if err := queryParams.Validate(); err != nil {
return
}
if queryParams.Cond != nil {
cond = queryParams.Cond
}
// Get role by permission
permissions := permissionFindByCondition(ctx, bson.M{"code": queryParams.Permission})
for _, value := range permissions {
roles = append(roles, value.RoleID)
}
if len(roles) < 0 {
return
}
query := model.CommonQuery{
Page: queryParams.Page,
Limit: queryParams.Limit,
Keyword: queryParams.Keyword,
Status: queryParams.Status,
Sort: queryParams.Sort,
Other: queryParams.Other,
RoleIDs: roles,
}
// Assign condition
query.SetDefaultLimit()
query.AssignKeyword(cond)
query.AssignRoleIDs(cond)
query.AssignStatus(cond)
query.AssignDeleted(cond)
query.AssignOther(cond)
cond["deleted"] = false
wg.Add(1)
go func() {
defer wg.Done()
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
res := make([]model.User, 0)
for _, doc := range docs {
res = append(res, getResponse(ctx, doc))
}
r.List = res
}()
wg.Add(1)
go func() {
defer wg.Done()
r.Total = countByCondition(ctx, cond)
}()
wg.Wait()
r.Limit = query.Limit
return
}
// Count ... // Count ...
func Count(queryParams model.UserCountQuery) int64 { func Count(queryParams model.UserCountQuery) int64 {
var ( var (