usermngmt/action_update.go

126 lines
2.4 KiB
Go
Raw Permalink Normal View History

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
2021-11-09 03:54:14 +00:00
if err := payload.validate(ctx); err != nil {
2021-11-09 02:08:56 +00:00
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{
2021-11-09 05:26:51 +00:00
"name": payload.Name,
"searchString": getSearchString(payload.Name, payload.Phone, payload.Email),
"phone": payload.Phone,
"email": payload.Email,
"roleId": roleID,
"other": payload.Other,
"updatedAt": now(),
2021-11-09 02:08:56 +00:00
},
}
// Update
2021-11-09 03:54:14 +00:00
if err := s.userUpdateOneByCondition(ctx, cond, updateData); err != nil {
2021-11-09 02:08:56 +00:00
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:54:14 +00:00
if 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(),
},
2021-11-09 03:54:14 +00:00
}); err != nil {
2021-11-09 02:38:24 +00:00
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 03:54:14 +00:00
if err := s.userUpdateOneByCondition(ctx, bson.M{"_id": id}, bson.M{
2021-11-09 02:38:24 +00:00
"$set": bson.M{
"status": newStatus,
"updatedAt": now(),
},
2021-11-09 03:54:14 +00:00
}); err != nil {
2021-11-09 02:38:24 +00:00
return err
}
return nil
}