From f5ace0d16dff0597a9aa31f05b4b7719bebbd36a Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 26 Nov 2021 17:37:15 +0700 Subject: [PATCH] update UpdateByDeviceID --- action_get_user_id.go | 28 ---------------------------- action_update.go | 4 ++++ validate.go | 11 +++++++++++ 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/action_get_user_id.go b/action_get_user_id.go index 20086f9..3397456 100644 --- a/action_get_user_id.go +++ b/action_get_user_id.go @@ -34,31 +34,3 @@ func (s Service) GetUserIDByAuthToken(authToken string) (userID string) { userID = device.UserID.Hex() return } - -// GetUserIDByDeviceId ... -func (s Service) GetUserIDByDeviceId(deviceId string) (userID string) { - var ( - ctx = context.Background() - col = s.getDeviceCollection() - device = Device{} - cond = bson.M{ - "deviceId": deviceId, - } - ) - - if deviceId == "" { - return - } - - // Find - if err := col.FindOne(ctx, cond).Decode(&device); err != nil { - logger.Error("devicemngmt - getUserIDByDeviceId", logger.LogData{ - "deviceId": deviceId, - "err": err.Error(), - }) - return - } - - userID = device.UserID.Hex() - return -} diff --git a/action_update.go b/action_update.go index e4f7b22..5d2c229 100644 --- a/action_update.go +++ b/action_update.go @@ -6,11 +6,13 @@ import ( "fmt" "github.com/Selly-Modules/logger" + "github.com/Selly-Modules/mongodb" "go.mongodb.org/mongo-driver/bson" ) // UpdateOptions ... type UpdateOptions struct { + UserID string UserAgent string AppVersion string IP string @@ -46,8 +48,10 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error osName, osVersion, isMobile := getUserAgentData(payload.UserAgent) // Setup update data + userID, _ := mongodb.NewIDFromString(payload.UserID) updateData := bson.M{ "$set": bson.M{ + "userId": userID, "osName": osName, "osVersion": osVersion, "ip": payload.IP, diff --git a/validate.go b/validate.go index 3df02f5..5673812 100644 --- a/validate.go +++ b/validate.go @@ -55,6 +55,17 @@ func (co CreateOptions) validate() error { } func (uo UpdateOptions) validate() error { + // UserID + if uo.UserID == "" { + logger.Error("devicemngmt - Update: no userID data", logger.LogData{ + "payload": uo, + }) + return errors.New("no userID data") + } + if _, isValid := mongodb.NewIDFromString(uo.UserID); !isValid { + return errors.New("invalid userID data") + } + // UserAgent if uo.UserAgent == "" { logger.Error("devicemngmt - Update: no userAgent data", logger.LogData{