From 0feeba07355c81b79c24d0a937e7328d38c6b0f4 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 26 Nov 2021 17:24:14 +0700 Subject: [PATCH] add GetUserIDByDeviceId --- action_get_user_id.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/action_get_user_id.go b/action_get_user_id.go index 42930bc..20086f9 100644 --- a/action_get_user_id.go +++ b/action_get_user_id.go @@ -18,6 +18,10 @@ func (s Service) GetUserIDByAuthToken(authToken string) (userID string) { } ) + if authToken == "" { + return + } + // Find if err := col.FindOne(ctx, cond).Decode(&device); err != nil { logger.Error("devicemngmt - getUserIDByAuthToken", logger.LogData{ @@ -30,3 +34,31 @@ 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 +}