diff --git a/action_create.go b/action_create.go index 8b23c5b..2781186 100644 --- a/action_create.go +++ b/action_create.go @@ -3,31 +3,38 @@ package devicemngmt import ( "context" "errors" + "fmt" "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" - ua "github.com/mssola/user_agent" "go.mongodb.org/mongo-driver/bson" ) -// DeviceCreate ... -type DeviceCreate struct { +// CreateOptions ... +type CreateOptions struct { DeviceID string UserID string UserAgent string AppVersion string IP string FCMToken string + AuthToken string Language string } // Create ... -func (s Service) Create(payload DeviceCreate) error { +func (s Service) Create(payload CreateOptions) error { var ( col = s.getDeviceCollection() ctx = context.Background() ) + // Validate payload + err := payload.validate() + if err != nil { + return err + } + // New device data from payload deviceData, err := payload.newDevice() if err != nil { @@ -43,7 +50,7 @@ func (s Service) Create(payload DeviceCreate) error { }) } if !device.ID.IsZero() { - return errors.New("deviceID already exists") + return errors.New("this device is already existed") } // Create device @@ -53,78 +60,33 @@ func (s Service) Create(payload DeviceCreate) error { "doc": deviceData, "err": err.Error(), }) - return errors.New("create device fail") + return fmt.Errorf("error when create device: %s", err.Error()) } return nil } -func (payload DeviceCreate) newDevice() (result Device, err error) { +func (payload CreateOptions) newDevice() (result Device, err error) { timeNow := now() device := Device{ ID: mongodb.NewObjectID(), + DeviceID: payload.DeviceID, + OSName: getOSName(payload.UserAgent), + OSVersion: getOSVersion(payload.UserAgent), + IP: payload.IP, + Language: getLanguage(payload.Language), + AuthToken: payload.AuthToken, LastActivityAt: timeNow, CreatedAt: timeNow, FCMToken: payload.FCMToken, } - // Set deviceID - if payload.DeviceID == "" { - logger.Error("devicemngt - Create: no deviceID data", logger.LogData{ - "payload": payload, - }) - err = errors.New("no deviceID data") - return - } - device.DeviceID = payload.DeviceID - - // OSName, OSVersion - if payload.UserAgent == "" { - logger.Error("devicemngt - Create: no userAgent data", logger.LogData{ - "payload": payload, - }) - err = errors.New("no userAgent data") - return - } - uaData := ua.New(payload.UserAgent) - device.OSName = uaData.OSInfo().Name - device.OSVersion = uaData.OSInfo().Version - // App version if payload.AppVersion != "" { device.AppVersion = payload.AppVersion device.IsMobile = true } - // IP - if payload.IP == "" { - logger.Error("devicemngt - Create: no ip data", logger.LogData{ - "payload": payload, - }) - err = errors.New("no ip data") - return - } - - // Language, default is vietnamese(vi) - if payload.Language == "" { - device.Language = viLanguage - } else { - device.Language = enLanguage - } - - // userIDe - userID, _ := mongodb.NewIDFromString(payload.UserID) - if userID.IsZero() { - logger.Error("devicemngt - Create: invalid userID data", logger.LogData{ - "payload": payload, - }) - err = errors.New("invalid userID data") - return - } - - // Generate authToken from userID - device.AuthToken = s.generateAuthToken(userID) - result = device return } diff --git a/auth_token.go b/auth_token.go deleted file mode 100644 index 88c7ddd..0000000 --- a/auth_token.go +++ /dev/null @@ -1,17 +0,0 @@ -package devicemngmt - -import ( - "time" - - "github.com/dgrijalva/jwt-go" - "go.mongodb.org/mongo-driver/bson/primitive" -) - -func (s Service) generateAuthToken(userID primitive.ObjectID) string { - token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ - "_id": userID, - "exp": now().Add(time.Second * 15552000).Unix(), // 6 months - }) - tokenString, _ := token.SignedString([]byte(s.AuthSecret)) - return tokenString -} diff --git a/constant.go b/constant.go index a6e1949..ca4c13f 100644 --- a/constant.go +++ b/constant.go @@ -2,10 +2,10 @@ package devicemngmt // Constant ... const ( - TableDevice = "devices" + tableDevice = "devices" timezoneHCM = "Asia/Ho_Chi_Minh" - viLanguage = "vi" - enLanguage = "en" + langVi = "vi" + langEn = "en" ) diff --git a/devicemngt.go b/devicemngt.go index 3574ec1..cc2b7e7 100644 --- a/devicemngt.go +++ b/devicemngt.go @@ -19,8 +19,6 @@ type Config struct { MongoDB MongoDBConfig // Table prefix, each service has its own prefix TablePrefix string - // Auth secret, used to sign token - AuthSecret string } // Service ... @@ -32,9 +30,9 @@ type Service struct { var s *Service // Init ... -func Init(config Config) error { - if config.MongoDB.Host == "" || config.TablePrefix == "" || config.AuthSecret == "" { - return errors.New("please provide all necessary information for init device") +func Init(config Config) (*Service, error) { + if config.MongoDB.Host == "" || config.TablePrefix == "" { + return nil, errors.New("please provide all necessary information for init device") } // Connect MongoDB @@ -48,7 +46,7 @@ func Init(config Config) error { ) if err != nil { fmt.Println("Cannot init module DEVICE MANAGEMENT", err) - return err + return nil, err } s = &Service{ @@ -56,7 +54,7 @@ func Init(config Config) error { DB: db, } - return nil + return s, nil } // GetInstance ... diff --git a/helper.go b/helper.go index 9b7a54f..3c1d026 100644 --- a/helper.go +++ b/helper.go @@ -3,10 +3,29 @@ package devicemngmt import ( "fmt" + ua "github.com/mssola/user_agent" "go.mongodb.org/mongo-driver/mongo" ) // getDeviceCollection ... 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 getOSName(userAgent string) string { + uaData := ua.New(userAgent) + return uaData.OSInfo().Name +} + +func getOSVersion(userAgent string) string { + uaData := ua.New(userAgent) + return uaData.OSInfo().Version +} + +func getLanguage(lang string) string { + // Language, default is vietnamese(vi) + if lang == langEn { + return langEn + } + return langVi } diff --git a/model.go b/model.go index bd684f3..22a2796 100644 --- a/model.go +++ b/model.go @@ -8,17 +8,17 @@ import ( // Device ... type Device struct { - ID primitive.ObjectID `bson:"_id"` - DeviceID string `bson:"deviceID"` // unique - IP string `bson:"ip"` - OSName string `bson:"osName"` - OSVersion string `bson:"osVersion"` - AppVersion string `bson:"appVersion"` - Language string `bson:"language"` // vi, en - IsMobile bool `bson:"isMobile"` - LastActivityAt time.Time `bson:"lastActivityAt"` - UserID primitive.ObjectID `bson:"userID"` - AuthToken string `bson:"authToken"` - FCMToken string `bson:"fcmToken"` - CreatedAt time.Time `bson:"createdAt"` + ID primitive.ObjectID `bson:"_id" json:"_id"` + DeviceID string `bson:"deviceID" json:"deviceID"` // unique + IP string `bson:"ip" json:"ip"` + OSName string `bson:"osName" json:"osName"` + OSVersion string `bson:"osVersion" json:"osVersion"` + AppVersion string `bson:"appVersion" json:"appVersion"` + Language string `bson:"language" json:"language"` // vi, en + IsMobile bool `bson:"isMobile" json:"isMobile"` + LastActivityAt time.Time `bson:"lastActivityAt" json:"lastActivityAt"` + UserID primitive.ObjectID `bson:"userID" json:"userID"` + AuthToken string `bson:"authToken" json:"authToken"` + FCMToken string `bson:"fcmToken" json:"fcmToken"` + CreatedAt time.Time `bson:"createdAt" json:"createdAt"` } diff --git a/validate.go b/validate.go new file mode 100644 index 0000000..9efc49f --- /dev/null +++ b/validate.go @@ -0,0 +1,51 @@ +package devicemngmt + +import ( + "errors" + + "github.com/Selly-Modules/logger" +) + +func (co CreateOptions) validate() error{ + // DeviceID + if co.DeviceID == "" { + logger.Error("devicemngt - Create: no deviceID data", logger.LogData{ + "payload": co, + }) + return errors.New("no deviceID data") + } + + // UserAgent + if co.UserAgent == "" { + logger.Error("devicemngt - Create: no userAgent data", logger.LogData{ + "payload": co, + }) + return errors.New("no userAgent data") + } + + // IP + if co.IP == "" { + logger.Error("devicemngt - Create: no ip data", logger.LogData{ + "payload": co, + }) + return errors.New("no ip data") + } + + // UserID + if co.UserID == "" { + logger.Error("devicemngt - Create: no userID data", logger.LogData{ + "payload": co, + }) + return errors.New("no userID data") + } + + // AuthToken + if co.AuthToken == "" { + logger.Error("devicemngt - Create: no authToken data", logger.LogData{ + "payload": co, + }) + return errors.New("no authToken data") + } + + return nil +}