update UpdateByDeviceID

This commit is contained in:
Hoang 2021-11-26 17:37:15 +07:00
parent 0feeba0735
commit f5ace0d16d
3 changed files with 15 additions and 28 deletions

View File

@ -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
}

View File

@ -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,

View File

@ -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{