diff --git a/model/db.go b/model/db.go index e4b4d16..6eec155 100644 --- a/model/db.go +++ b/model/db.go @@ -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"` } diff --git a/model/user_request.go b/model/user_request.go index 19037cf..42678b2 100644 --- a/model/user_request.go +++ b/model/user_request.go @@ -24,7 +24,7 @@ type UserUpdateOptions struct { Phone string Email string RoleID string - Other interface{} + Other map[string]interface{} } // ChangePasswordOptions ... diff --git a/user/handle.go b/user/handle.go index 01959a3..b011edf 100644 --- a/user/handle.go +++ b/user/handle.go @@ -228,21 +228,25 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error { "_id": id, } - // Setup update data - updateData := bson.M{ - "$set": 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(), - }, + // 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, + "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 }