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"` RequireToChangePassword bool `bson:"requireToChangePassword"`
Avatar string `bson:"avatar"` Avatar string `bson:"avatar"`
Deleted bool `bson:"deleted"` Deleted bool `bson:"deleted"`
Other interface{} `bson:"other"` Other interface{} `bson:"other"` // object
CreatedAt time.Time `bson:"createdAt"` CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"` UpdatedAt time.Time `bson:"updatedAt"`
} }

View File

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

View File

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