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 Keyword string
RoleID string RoleID string
Status string Status string
Deleted string // true or false
Sort interface{} Sort interface{}
Other map[string]interface{} // query fields in other object 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 // Find
cond := bson.M{ cond := bson.M{
"deleted": false,
"$or": []bson.M{ "$or": []bson.M{
{ {
"phone": phone, "phone": phone,
@ -45,6 +46,7 @@ func isPhoneNumberExisted(ctx context.Context, phone string) bool {
// Find // Find
cond := bson.M{ cond := bson.M{
"phone": phone, "phone": phone,
"deleted": false,
} }
total, err := col.CountDocuments(ctx, cond) total, err := col.CountDocuments(ctx, cond)
if err != nil { if err != nil {
@ -64,6 +66,7 @@ func isEmailExisted(ctx context.Context, email string) bool {
// Find // Find
cond := bson.M{ cond := bson.M{
"email": email, "email": email,
"deleted": false,
} }
total, err := col.CountDocuments(ctx, cond) total, err := col.CountDocuments(ctx, cond)
if err != nil { if err != nil {
@ -76,7 +79,7 @@ func isEmailExisted(ctx context.Context, email string) bool {
return total != 0 return total != 0
} }
func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool { func isRoleExisted(ctx context.Context, roleID primitive.ObjectID) bool {
var ( var (
col = database.GetRoleCol() col = database.GetRoleCol()
) )
@ -90,7 +93,6 @@ func isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
"condition": cond, "condition": cond,
"err": err.Error(), "err": err.Error(),
}) })
return false
} }
return total != 0 return total != 0
} }
@ -174,7 +176,7 @@ func findByID(ctx context.Context, id primitive.ObjectID) (model.DBUser, error)
doc model.DBUser doc model.DBUser
col = database.GetUserCol() 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 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") err = errors.New("invalid role id data")
return return
} }
if !isRoleIDExisted(ctx, roleID) { if !isRoleExisted(ctx, roleID) {
err = errors.New("role id does not exist") err = errors.New("role id does not exist")
return return
} }
@ -113,7 +113,6 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) {
Status: queryParams.Status, Status: queryParams.Status,
Sort: queryParams.Sort, Sort: queryParams.Sort,
Other: queryParams.Other, Other: queryParams.Other,
Deleted: queryParams.Deleted,
} }
// Assign condition // Assign condition
@ -123,6 +122,7 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) {
query.AssignStatus(cond) query.AssignStatus(cond)
query.AssignDeleted(cond) query.AssignDeleted(cond)
query.AssignOther(cond) query.AssignOther(cond)
cond["deleted"] = false
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -202,7 +202,7 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
if !isValid { if !isValid {
return errors.New("invalid role id data") return errors.New("invalid role id data")
} }
if !isRoleIDExisted(ctx, roleID) { if !isRoleExisted(ctx, roleID) {
return errors.New("role id does not exist") return errors.New("role id does not exist")
} }
@ -316,6 +316,9 @@ func ChangeUserStatus(userID, newStatus string) error {
if !isValid { if !isValid {
return errors.New("invalid user id data") return errors.New("invalid user id data")
} }
if user, _ := findByID(ctx, id); user.ID.IsZero() {
return errors.New("user not found")
}
// Update status // Update status
if err := updateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ if err := updateOneByCondition(ctx, bson.M{"_id": id}, bson.M{
@ -341,6 +344,9 @@ func ChangeAllUsersStatus(roleID, status string) error {
if !isValid { if !isValid {
return errors.New("invalid role id data") return errors.New("invalid role id data")
} }
if !isRoleExisted(ctx, id) {
return errors.New("role not found")
}
// Setup condition // Setup condition
cond := bson.M{ cond := bson.M{
@ -378,6 +384,7 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e
// Find user // Find user
user, _ := findOneByCondition(ctx, bson.M{ user, _ := findOneByCondition(ctx, bson.M{
"email": email, "email": email,
"deleted": false,
}) })
if user.ID.IsZero() { if user.ID.IsZero() {
err = errors.New("user not found") err = errors.New("user not found")