2021-11-09 02:08:56 +00:00
|
|
|
package usermngmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-09 02:38:24 +00:00
|
|
|
"errors"
|
2021-11-09 02:08:56 +00:00
|
|
|
|
|
|
|
"github.com/Selly-Modules/mongodb"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateOptions ...
|
|
|
|
type UpdateOptions struct {
|
|
|
|
Name string
|
|
|
|
Phone string
|
|
|
|
Email string
|
|
|
|
RoleID string
|
|
|
|
Other string
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:22:15 +00:00
|
|
|
// ChangePasswordOptions ...
|
|
|
|
type ChangePasswordOptions struct {
|
|
|
|
OldPassword string
|
|
|
|
NewPassword string
|
|
|
|
}
|
|
|
|
|
2021-11-09 02:08:56 +00:00
|
|
|
// UpdateByUserID ...
|
|
|
|
func (s Service) UpdateByUserID(userID string, payload UpdateOptions) error {
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validate payload
|
|
|
|
err := payload.validate(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup condition
|
|
|
|
id, _ := mongodb.NewIDFromString(userID)
|
|
|
|
cond := bson.M{
|
|
|
|
"_id": id,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup update data
|
|
|
|
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
|
|
|
updateData := bson.M{
|
|
|
|
"$set": bson.M{
|
|
|
|
"name": payload.Name,
|
|
|
|
"phone": payload.Phone,
|
|
|
|
"email": payload.Email,
|
|
|
|
"roleId": roleID,
|
|
|
|
"other": payload.Other,
|
|
|
|
"updatedAt": now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update
|
|
|
|
err = s.userUpdateOneByCondition(ctx, cond, updateData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-09 02:38:24 +00:00
|
|
|
|
2021-11-09 03:22:15 +00:00
|
|
|
// ChangeUserPassword ...
|
|
|
|
func (s Service) ChangeUserPassword(userID string, opt ChangePasswordOptions) error {
|
2021-11-09 02:38:24 +00:00
|
|
|
var (
|
2021-11-09 03:22:15 +00:00
|
|
|
ctx = context.Background()
|
2021-11-09 02:38:24 +00:00
|
|
|
)
|
|
|
|
|
2021-11-09 03:22:15 +00:00
|
|
|
// Validate payload
|
|
|
|
err := opt.validate(userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-11-09 02:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find user
|
2021-11-09 03:22:15 +00:00
|
|
|
id, _ := mongodb.NewIDFromString(userID)
|
2021-11-09 02:38:24 +00:00
|
|
|
user, _ := s.userFindByID(ctx, id)
|
|
|
|
if user.ID.IsZero() {
|
2021-11-09 03:22:15 +00:00
|
|
|
return errors.New("user not found")
|
2021-11-09 02:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check old password
|
2021-11-09 03:22:15 +00:00
|
|
|
if isValid := checkPasswordHash(opt.OldPassword, user.HashedPassword); !isValid {
|
2021-11-09 02:38:24 +00:00
|
|
|
return errors.New("the password is incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update password
|
2021-11-09 03:22:15 +00:00
|
|
|
err = s.userUpdateOneByCondition(ctx, bson.M{"_id": user.ID}, bson.M{
|
2021-11-09 02:38:24 +00:00
|
|
|
"$set": bson.M{
|
2021-11-09 03:22:15 +00:00
|
|
|
"hashedPassword": hashPassword(opt.NewPassword),
|
2021-11-09 02:38:24 +00:00
|
|
|
"updatedAt": now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:22:15 +00:00
|
|
|
// ChangeUserStatus ...
|
|
|
|
func (s Service) ChangeUserStatus(userID, newStatus string) error {
|
2021-11-09 02:38:24 +00:00
|
|
|
var (
|
2021-11-09 03:22:15 +00:00
|
|
|
ctx = context.Background()
|
2021-11-09 02:38:24 +00:00
|
|
|
)
|
|
|
|
|
2021-11-09 03:22:15 +00:00
|
|
|
// Validate userID
|
|
|
|
id, isValid := mongodb.NewIDFromString(userID)
|
|
|
|
if !isValid {
|
|
|
|
return errors.New("invalid user id data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update status
|
2021-11-09 02:38:24 +00:00
|
|
|
err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{
|
|
|
|
"$set": bson.M{
|
|
|
|
"status": newStatus,
|
|
|
|
"updatedAt": now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|