add ResetAndRequireToChangeUserPassword method

This commit is contained in:
Hoang 2022-02-18 11:28:27 +07:00
parent d85b4c9760
commit 4875c935b7
2 changed files with 42 additions and 0 deletions

View File

@ -49,6 +49,11 @@ func (s Service) ResetUserPassword(userID string, password string) error {
return user.ResetUserPassword(userID, password)
}
// ResetAndRequireToChangeUserPassword ...
func (s Service) ResetAndRequireToChangeUserPassword(userID string, password string) error {
return user.ResetAndRequireToChangeUserPassword(userID, password)
}
// ChangeUserStatus ...
func (s Service) ChangeUserStatus(userID, newStatus string) error {
return user.ChangeUserStatus(userID, newStatus)

View File

@ -403,6 +403,43 @@ func ResetUserPassword(userID string, password string) error {
return nil
}
// ResetAndRequireToChangeUserPassword ...
func ResetAndRequireToChangeUserPassword(userID string, password string) error {
var (
ctx = context.Background()
)
// Validate Password
if password == "" {
return errors.New(internal.ErrorInvalidPassword)
}
// Validate userID
if _, isValid := mongodb.NewIDFromString(userID); !isValid {
return errors.New(internal.ErrorInvalidUser)
}
// Find user
id, _ := mongodb.NewIDFromString(userID)
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
return errors.New(internal.ErrorNotFoundUser)
}
// Update password
if err := updateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{
"$set": bson.M{
"hashedPassword": internal.HashPassword(password),
"requireToChangePassword": true,
"updatedAt": internal.Now(),
},
}); err != nil {
return err
}
return nil
}
// ChangeUserStatus ...
func ChangeUserStatus(userID, newStatus string) error {
var (