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() userID = device.UserID.Hex()
return 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" "fmt"
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
"github.com/Selly-Modules/mongodb"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
) )
// UpdateOptions ... // UpdateOptions ...
type UpdateOptions struct { type UpdateOptions struct {
UserID string
UserAgent string UserAgent string
AppVersion string AppVersion string
IP string IP string
@ -46,8 +48,10 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error
osName, osVersion, isMobile := getUserAgentData(payload.UserAgent) osName, osVersion, isMobile := getUserAgentData(payload.UserAgent)
// Setup update data // Setup update data
userID, _ := mongodb.NewIDFromString(payload.UserID)
updateData := bson.M{ updateData := bson.M{
"$set": bson.M{ "$set": bson.M{
"userId": userID,
"osName": osName, "osName": osName,
"osVersion": osVersion, "osVersion": osVersion,
"ip": payload.IP, "ip": payload.IP,

View File

@ -55,6 +55,17 @@ func (co CreateOptions) validate() error {
} }
func (uo UpdateOptions) 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 // UserAgent
if uo.UserAgent == "" { if uo.UserAgent == "" {
logger.Error("devicemngmt - Update: no userAgent data", logger.LogData{ logger.Error("devicemngmt - Update: no userAgent data", logger.LogData{