Change status and password method #3

Merged
lqhoang99 merged 3 commits from feature/changeStatus-password into master 2021-11-09 04:03:28 +00:00
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 {
}
namhq1989 commented 2021-11-09 02:46:11 +00:00 (Migrated from github.com)
Review

ChangePasswordByUserID -> ChangeUserPassword

ChangePasswordByUserID -> ChangeUserPassword
namhq1989 commented 2021-11-09 02:47:10 +00:00 (Migrated from github.com)
Review

đưa ra validate:

  • user id -> valid mongo id
  • old password != ""
  • new password != ""
đưa ra validate: - user id -> valid mongo id - old password != "" - new password != ""
namhq1989 commented 2021-11-09 02:47:26 +00:00 (Migrated from github.com)
Review

old or new password cannot be empty

old or new password cannot be empty
namhq1989 commented 2021-11-09 02:47:42 +00:00 (Migrated from github.com)
Review

user not found

user not found
namhq1989 commented 2021-11-09 02:48:21 +00:00 (Migrated from github.com)
Review
if isValid := checkPasswordHash(oldPassword, user.HashedPassword); !isValid {
  return errors.New("the password is incorrect")
}
```go if isValid := checkPasswordHash(oldPassword, user.HashedPassword); !isValid { return errors.New("the password is incorrect") } ```
namhq1989 commented 2021-11-09 02:48:54 +00:00 (Migrated from github.com)
Review

ChangeUserStatus

  • func này check thêm nếu user.Status == newStatus thì return luôn ko cần làm gì nữa
ChangeUserStatus - func này check thêm nếu `user.Status == newStatus` thì return luôn ko cần làm gì nữa
namhq1989 commented 2021-11-09 02:49:16 +00:00 (Migrated from github.com)
Review

mọi id client gửi qua đều phải validate lại

mọi id client gửi qua đều phải validate lại
lqhoang99 commented 2021-11-09 03:22:59 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-09 03:23:03 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-09 03:23:08 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-09 03:23:12 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-09 03:23:16 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-09 03:23:19 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-09 03:23:28 +00:00 (Migrated from github.com)
Review

done

done
// Update
err = s.userUpdateOneByCondition(ctx, cond, updateData)
if err != nil {
if err := s.userUpdateOneByCondition(ctx, cond, updateData); err != nil {
return err
}
return nil
namhq1989 commented 2021-11-09 03:42:23 +00:00 (Migrated from github.com)
Review

cái này cũng move về dạng if như trên đi

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

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
namhq1989 commented 2021-11-09 03:42:50 +00:00 (Migrated from github.com)
Review

fix như trên

fix như trên
lqhoang99 commented 2021-11-09 03:54:32 +00:00 (Migrated from github.com)
Review

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")
namhq1989 commented 2021-11-09 03:56:42 +00:00 (Migrated from github.com)
Review

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