From 49be1c3c969ef41645cd8382340218c719e02b13 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 19 Nov 2021 11:17:58 +0700 Subject: [PATCH] update userUpdateMethod --- model/db.go | 2 +- model/user_request.go | 2 +- user/handle.go | 28 ++++++++++++++++------------ 3 files changed, 18 insertions(+), 14 deletions(-) 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 }