Merge pull request #3 from Selly-Modules/feature/changeStatus-password

Change status and password method
This commit is contained in:
Nam Huynh 2021-11-09 11:03:28 +07:00 committed by GitHub
commit b7a14d995c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 9 deletions

View File

@ -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
}

View File

@ -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
}
// 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
}

9
db.go
View File

@ -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
}

View File

@ -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
}