diff --git a/model/user_request.go b/model/user_request.go index ccdc684..883fd39 100644 --- a/model/user_request.go +++ b/model/user_request.go @@ -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 } diff --git a/user/db.go b/user/db.go index 93a96d1..a467222 100644 --- a/user/db.go +++ b/user/db.go @@ -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 } diff --git a/user/handle.go b/user/handle.go index 6f0166d..614d5c6 100644 --- a/user/handle.go +++ b/user/handle.go @@ -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")