From 4d4bf4db8f50b067638908b62849aae69e80e9d8 Mon Sep 17 00:00:00 2001 From: Hoang Date: Wed, 1 Dec 2021 17:51:44 +0700 Subject: [PATCH] add resetPassword --- action.go | 5 +++++ user/handle.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/action.go b/action.go index 280d94c..caa2e78 100644 --- a/action.go +++ b/action.go @@ -44,6 +44,11 @@ func (s Service) ChangeUserPassword(userID string, payload model.ChangePasswordO return user.ChangeUserPassword(userID, payload) } +// ResetUserPassword ... +func (s Service) ResetUserPassword(userID string, password string) error { + return user.ResetUserPassword(userID, password) +} + // ChangeUserStatus ... func (s Service) ChangeUserStatus(userID, newStatus string) error { return user.ChangeUserStatus(userID, newStatus) diff --git a/user/handle.go b/user/handle.go index bd95f60..88a2e5a 100644 --- a/user/handle.go +++ b/user/handle.go @@ -349,6 +349,43 @@ func ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error { return nil } +// ResetUserPassword ... +func ResetUserPassword(userID string, password string) error { + var ( + ctx = context.Background() + ) + + // Validate Password + if password == "" { + return errors.New("password cannot be empty") + } + + // Validate userID + if _, isValid := mongodb.NewIDFromString(userID); !isValid { + return errors.New("invalid user id data") + } + + // Find user + id, _ := mongodb.NewIDFromString(userID) + user, _ := findByID(ctx, id) + if user.ID.IsZero() { + return errors.New("user not found") + } + + // Update password + if err := updateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{ + "$set": bson.M{ + "hashedPassword": internal.HashPassword(password), + "requireToChangePassword": false, + "updatedAt": internal.Now(), + }, + }); err != nil { + return err + } + + return nil +} + // ChangeUserStatus ... func ChangeUserStatus(userID, newStatus string) error { var (