Change status and password method #3
			
				
			
		
		
		
	|  | @ -24,8 +24,7 @@ func (s Service) Create(payload CreateOptions) error { | |||
| 	) | ||||
| 
 | ||||
| 	// Validate payload
 | ||||
| 	err := payload.validate(ctx) | ||||
| 	if err != nil { | ||||
| 	if err := payload.validate(ctx); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
|  | @ -36,8 +35,7 @@ func (s Service) Create(payload CreateOptions) error { | |||
| 	} | ||||
| 
 | ||||
| 	// Create user
 | ||||
| 	err = s.userCreate(ctx, doc) | ||||
| 	if err != nil { | ||||
| 	if err = s.userCreate(ctx, doc); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -2,6 +2,7 @@ package usermngmt | |||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 
 | ||||
| 	"github.com/Selly-Modules/mongodb" | ||||
| 	"go.mongodb.org/mongo-driver/bson" | ||||
|  | @ -16,6 +17,12 @@ type UpdateOptions struct { | |||
| 	Other  string | ||||
| } | ||||
| 
 | ||||
| // ChangePasswordOptions ...
 | ||||
| type ChangePasswordOptions struct { | ||||
| 	OldPassword string | ||||
| 	NewPassword string | ||||
| } | ||||
| 
 | ||||
| // UpdateByUserID ...
 | ||||
| func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | ||||
| 	var ( | ||||
|  | @ -23,8 +30,7 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | |||
| 	) | ||||
| 
 | ||||
| 	// Validate payload
 | ||||
| 	err := payload.validate(ctx) | ||||
| 	if err != nil { | ||||
| 	if err := payload.validate(ctx); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
|  | @ -48,8 +54,69 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | |||
| 	} | ||||
| 
 | ||||
|  | ||||
| 	// Update
 | ||||
| 	err = s.userUpdateOneByCondition(ctx, cond, updateData) | ||||
| 	if err != nil { | ||||
| 	if err := s.userUpdateOneByCondition(ctx, cond, updateData); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
|   cái này cũng move về dạng if như trên đi chú ý mấy cái khác tương tự luôn, đưa về cách trên cho gọn code cái này cũng move về dạng if như trên đi
```go
if a := ...; a {
  // ...
}
```
chú ý mấy cái khác tương tự luôn, đưa về cách trên cho gọn code   fix như trên fix như trên   done done | ||||
| } | ||||
| 
 | ||||
| // ChangeUserPassword ...
 | ||||
| func (s Service) ChangeUserPassword(userID string, opt ChangePasswordOptions) error { | ||||
| 	var ( | ||||
| 		ctx = context.Background() | ||||
| 	) | ||||
| 
 | ||||
| 	// Validate payload
 | ||||
| 	err := opt.validate(userID) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	// Find user
 | ||||
| 	id, _ := mongodb.NewIDFromString(userID) | ||||
| 	user, _ := s.userFindByID(ctx, id) | ||||
| 	if user.ID.IsZero() { | ||||
| 		return errors.New("user not found") | ||||
| 	} | ||||
| 
 | ||||
| 	// Check old password
 | ||||
| 	if isValid := checkPasswordHash(opt.OldPassword, user.HashedPassword); !isValid { | ||||
| 		return errors.New("the password is incorrect") | ||||
| 	} | ||||
| 
 | ||||
| 	// Update password
 | ||||
| 	if err = s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ | ||||
| 		"$set": bson.M{ | ||||
| 			"hashedPassword": hashPassword(opt.NewPassword), | ||||
| 			"updatedAt":      now(), | ||||
| 		}, | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| // ChangeUserStatus ...
 | ||||
| func (s Service) ChangeUserStatus(userID, newStatus string) error { | ||||
| 	var ( | ||||
| 		ctx = context.Background() | ||||
| 	) | ||||
| 
 | ||||
| 	// Validate userID
 | ||||
| 	id, isValid := mongodb.NewIDFromString(userID) | ||||
| 	if !isValid { | ||||
| 		return errors.New("invalid user id data") | ||||
|   chỗ này cũng check được như trên nè chỗ này cũng check được như trên nè | ||||
| 	} | ||||
| 
 | ||||
| 	// Update status
 | ||||
| 	if err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ | ||||
| 		"$set": bson.M{ | ||||
| 			"status":    newStatus, | ||||
| 			"updatedAt": now(), | ||||
| 		}, | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										9
									
								
								db.go
								
								
								
								
							
							
						
						
									
										9
									
								
								db.go
								
								
								
								
							|  | @ -100,4 +100,13 @@ func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{}, | |||
| 	} | ||||
| 
 | ||||
| 	return err | ||||
| } | ||||
| 
 | ||||
| func (s Service) userFindByID(ctx context.Context, id primitive.ObjectID) (User, error) { | ||||
| 	var ( | ||||
| 		col = s.getUserCollection() | ||||
| 		doc User | ||||
| 	) | ||||
| 	err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) | ||||
| 	return doc, err | ||||
| } | ||||
							
								
								
									
										22
									
								
								validate.go
								
								
								
								
							
							
						
						
									
										22
									
								
								validate.go
								
								
								
								
							|  | @ -122,4 +122,24 @@ func (co UpdateOptions) validate(ctx context.Context) error { | |||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| } | ||||
| 
 | ||||
| func (co ChangePasswordOptions) validate(userID string) error { | ||||
| 	// OldPassword, NewPassword
 | ||||
| 	if co.OldPassword == "" || co.NewPassword == "" { | ||||
| 		logger.Error("usermngmt - ChangePassword: old or new password cannot be empty", logger.LogData{ | ||||
| 			"payload": co, | ||||
| 		}) | ||||
| 		return errors.New("old or new password cannot be empty") | ||||
| 	} | ||||
| 
 | ||||
| 	// UserID
 | ||||
| 	if _, isValid := mongodb.NewIDFromString(userID); !isValid { | ||||
| 		logger.Error("usermngmt - ChangePassword: invalid userID data", logger.LogData{ | ||||
| 			"payload": co, | ||||
| 		}) | ||||
| 		return errors.New("invalid user id data") | ||||
| 	} | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	
ChangePasswordByUserID -> ChangeUserPassword
đưa ra validate:
old or new password cannot be empty
user not found
ChangeUserStatus
user.Status == newStatusthì return luôn ko cần làm gì nữamọi id client gửi qua đều phải validate lại
done
done
done
done
done
done
done