Merge pull request #3 from Selly-Modules/feature/changeStatus-password
Change status and password method
This commit is contained in:
		
						commit
						b7a14d995c
					
				|  | @ -24,8 +24,7 @@ func (s Service) Create(payload CreateOptions) error { | ||||||
| 	) | 	) | ||||||
| 
 | 
 | ||||||
| 	// Validate payload
 | 	// Validate payload
 | ||||||
| 	err := payload.validate(ctx) | 	if err := payload.validate(ctx); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -36,8 +35,7 @@ func (s Service) Create(payload CreateOptions) error { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Create user
 | 	// Create user
 | ||||||
| 	err = s.userCreate(ctx, doc) | 	if err = s.userCreate(ctx, doc); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ package usermngmt | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"errors" | ||||||
| 
 | 
 | ||||||
| 	"github.com/Selly-Modules/mongodb" | 	"github.com/Selly-Modules/mongodb" | ||||||
| 	"go.mongodb.org/mongo-driver/bson" | 	"go.mongodb.org/mongo-driver/bson" | ||||||
|  | @ -16,6 +17,12 @@ type UpdateOptions struct { | ||||||
| 	Other  string | 	Other  string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // ChangePasswordOptions ...
 | ||||||
|  | type ChangePasswordOptions struct { | ||||||
|  | 	OldPassword string | ||||||
|  | 	NewPassword string | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // UpdateByUserID ...
 | // UpdateByUserID ...
 | ||||||
| func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | ||||||
| 	var ( | 	var ( | ||||||
|  | @ -23,8 +30,7 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | ||||||
| 	) | 	) | ||||||
| 
 | 
 | ||||||
| 	// Validate payload
 | 	// Validate payload
 | ||||||
| 	err := payload.validate(ctx) | 	if err := payload.validate(ctx); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -48,8 +54,69 @@ func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Update
 | 	// Update
 | ||||||
| 	err = s.userUpdateOneByCondition(ctx, cond, updateData) | 	if err := s.userUpdateOneByCondition(ctx, cond, updateData); err != nil { | ||||||
| 	if err != nil { | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // 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") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Update status
 | ||||||
|  | 	if err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{ | ||||||
|  | 		"$set": bson.M{ | ||||||
|  | 			"status":    newStatus, | ||||||
|  | 			"updatedAt": now(), | ||||||
|  | 		}, | ||||||
|  | 	}); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										9
									
								
								db.go
								
								
								
								
							
							
						
						
									
										9
									
								
								db.go
								
								
								
								
							|  | @ -101,3 +101,12 @@ func (s Service) userUpdateOneByCondition(ctx context.Context, cond interface{}, | ||||||
| 
 | 
 | ||||||
| 	return err | 	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 | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								validate.go
								
								
								
								
							
							
						
						
									
										20
									
								
								validate.go
								
								
								
								
							|  | @ -123,3 +123,23 @@ func (co UpdateOptions) validate(ctx context.Context) error { | ||||||
| 
 | 
 | ||||||
| 	return nil | 	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