fix comment again

This commit is contained in:
Hoang 2021-11-05 16:10:12 +07:00
parent 6e0eb8b415
commit 9905ab89ae
3 changed files with 26 additions and 12 deletions

View File

@ -7,7 +7,6 @@ import (
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
"github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/mongodb"
"go.mongodb.org/mongo-driver/bson"
) )
// CreateOptions ... // CreateOptions ...
@ -41,15 +40,8 @@ func (s Service) Create(payload CreateOptions) error {
return err return err
} }
// Find device id existed or not // Find deviceID existed or not
device := Device{} if s.isDeviceIDExisted(ctx, deviceData.DeviceID) {
if err = col.FindOne(ctx, bson.M{"deviceID": deviceData.DeviceID}).Decode(&device); err != nil {
logger.Error("devicemngt - findByDeviceID", logger.LogData{
"deviceID": deviceData.DeviceID,
"err": err.Error(),
})
}
if !device.ID.IsZero() {
return errors.New("this device is already existed") return errors.New("this device is already existed")
} }

View File

@ -1,9 +1,12 @@
package devicemngmt package devicemngmt
import ( import (
"context"
"fmt" "fmt"
"github.com/Selly-Modules/logger"
ua "github.com/mssola/user_agent" ua "github.com/mssola/user_agent"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
@ -12,6 +15,25 @@ func (s Service) getDeviceCollection() *mongo.Collection {
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableDevice)) return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableDevice))
} }
func (s Service) isDeviceIDExisted(ctx context.Context, deviceID string) bool {
var (
col = s.getDeviceCollection()
device = Device{}
)
if err := col.FindOne(ctx, bson.M{"deviceID": deviceID}).Decode(&device); err != nil {
logger.Error("devicemngt - findByDeviceID", logger.LogData{
"deviceID": deviceID,
"err": err.Error(),
})
}
if !device.ID.IsZero() {
return true
}
return false
}
func getOSName(userAgent string) string { func getOSName(userAgent string) string {
uaData := ua.New(userAgent) uaData := ua.New(userAgent)
return uaData.OSInfo().Name return uaData.OSInfo().Name

View File

@ -9,7 +9,7 @@ import (
// Device ... // Device ...
type Device struct { type Device struct {
ID primitive.ObjectID `bson:"_id" json:"_id"` ID primitive.ObjectID `bson:"_id" json:"_id"`
DeviceID string `bson:"deviceID" json:"deviceID"` // unique DeviceID string `bson:"deviceID" json:"deviceId"` // unique
IP string `bson:"ip" json:"ip"` IP string `bson:"ip" json:"ip"`
OSName string `bson:"osName" json:"osName"` OSName string `bson:"osName" json:"osName"`
OSVersion string `bson:"osVersion" json:"osVersion"` OSVersion string `bson:"osVersion" json:"osVersion"`
@ -17,7 +17,7 @@ type Device struct {
Language string `bson:"language" json:"language"` // vi, en Language string `bson:"language" json:"language"` // vi, en
IsMobile bool `bson:"isMobile" json:"isMobile"` IsMobile bool `bson:"isMobile" json:"isMobile"`
LastActivityAt time.Time `bson:"lastActivityAt" json:"lastActivityAt"` LastActivityAt time.Time `bson:"lastActivityAt" json:"lastActivityAt"`
UserID primitive.ObjectID `bson:"userID" json:"userID"` UserID primitive.ObjectID `bson:"userID" json:"userId"`
AuthToken string `bson:"authToken" json:"authToken"` AuthToken string `bson:"authToken" json:"authToken"`
FCMToken string `bson:"fcmToken" json:"fcmToken"` FCMToken string `bson:"fcmToken" json:"fcmToken"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` CreatedAt time.Time `bson:"createdAt" json:"createdAt"`