This commit is contained in:
Hoang 2021-11-24 13:15:40 +07:00
parent 54c9a2cc07
commit 828fec0abb
3 changed files with 18 additions and 10 deletions

View File

@ -41,7 +41,6 @@ type UserAllQuery struct {
Keyword string
RoleID string
Status string
Deleted string // true or false
Sort interface{}
Other map[string]interface{} // query fields in other object
}

View File

@ -18,6 +18,7 @@ func isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool
)
// Find
cond := bson.M{
"deleted": false,
"$or": []bson.M{
{
"phone": phone,
@ -44,7 +45,8 @@ func isPhoneNumberExisted(ctx context.Context, phone string) bool {
)
// Find
cond := bson.M{
"phone": phone,
"phone": phone,
"deleted": false,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
@ -63,7 +65,8 @@ func isEmailExisted(ctx context.Context, email string) bool {
)
// Find
cond := bson.M{
"email": email,
"email": email,
"deleted": false,
}
total, err := col.CountDocuments(ctx, cond)
if err != nil {
@ -76,7 +79,7 @@ func isEmailExisted(ctx context.Context, email string) bool {
return total != 0
}
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
func isRoleExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var (
col = database.GetRoleCol()
)
@ -90,7 +93,6 @@ func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
"condition": cond,
"err": err.Error(),
})
return false
}
return total != 0
}
@ -174,7 +176,7 @@ func findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error)
doc model.DBUser
col = database.GetUserCol()
)
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
err := col.FindOne(ctx, bson.M{"_id": id, "deleted": false}).Decode(&doc)
return doc, err
}

View File

@ -32,7 +32,7 @@ func Create(payload model.UserCreateOptions) (result string, err error) {
err = errors.New("invalid role id data")
return
}
if !isRoleIDExisted(ctx, roleID) {
if !isRoleExisted(ctx, roleID) {
err = errors.New("role id does not exist")
return
}
@ -113,7 +113,6 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) {
Status: queryParams.Status,
Sort: queryParams.Sort,
Other: queryParams.Other,
Deleted: queryParams.Deleted,
}
// Assign condition
@ -123,6 +122,7 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) {
query.AssignStatus(cond)
query.AssignDeleted(cond)
query.AssignOther(cond)
cond["deleted"] = false
wg.Add(1)
go func() {
@ -202,7 +202,7 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
if !isValid {
return errors.New("invalid role id data")
}
if !isRoleIDExisted(ctx, roleID) {
if !isRoleExisted(ctx, roleID) {
return errors.New("role id does not exist")
}
@ -316,6 +316,9 @@ func ChangeUserStatus(userID, newStatus string) error {
if !isValid {
return errors.New("invalid user id data")
}
if user, _ := findByID(ctx, id); user.ID.IsZero() {
return errors.New("user not found")
}
// Update status
if err := updateOneByCondition(ctx, bson.M{"_id": id}, bson.M{
@ -341,6 +344,9 @@ func ChangeAllUsersStatus(roleID, status string) error {
if !isValid {
return errors.New("invalid role id data")
}
if !isRoleExisted(ctx, id) {
return errors.New("role not found")
}
// Setup condition
cond := bson.M{
@ -377,7 +383,8 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e
// Find user
user, _ := findOneByCondition(ctx, bson.M{
"email": email,
"email": email,
"deleted": false,
})
if user.ID.IsZero() {
err = errors.New("user not found")