update userUpdateMethod

This commit is contained in:
Hoang 2021-11-19 11:17:58 +07:00
parent 14ad6b51d8
commit 49be1c3c96
3 changed files with 18 additions and 14 deletions

View File

@ -30,7 +30,7 @@ type DBUser struct {
RequireToChangePassword bool `bson:"requireToChangePassword"`
Avatar string `bson:"avatar"`
Deleted bool `bson:"deleted"`
Other interface{} `bson:"other"`
Other interface{} `bson:"other"` // object
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
}

View File

@ -24,7 +24,7 @@ type UserUpdateOptions struct {
Phone string
Email string
RoleID string
Other interface{}
Other map[string]interface{}
}
// ChangePasswordOptions ...

View File

@ -228,21 +228,25 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
"_id": id,
}
// Setup update data
updateData := bson.M{
"$set": bson.M{
// Setup Set operator
setOperator := bson.M{
"name": payload.Name,
"searchString": internal.GetSearchString(payload.Name, payload.Phone, payload.Email),
"phone": payload.Phone,
"email": payload.Email,
"roleId": roleID,
"other": payload.Other,
"updatedAt": internal.Now(),
},
}
if len(payload.Other) > 0 {
for key, value := range payload.Other {
setOperator["other."+key] = value
}
}
// Update
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
if err := updateOneByCondition(ctx, cond, bson.M{
"$set": setOperator,
}); err != nil {
return err
}