fix comment

This commit is contained in:
Hoang 2021-11-05 15:40:07 +07:00
parent d47368c3ae
commit 6e0eb8b415
7 changed files with 112 additions and 99 deletions

View File

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

View File

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

View File

@ -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"
)

View File

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

View File

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

View File

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

51
validate.go Normal file
View File

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