fix comment
This commit is contained in:
		
							parent
							
								
									d47368c3ae
								
							
						
					
					
						commit
						6e0eb8b415
					
				|  | @ -3,31 +3,38 @@ package devicemngmt | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"fmt" | ||||||
| 
 | 
 | ||||||
| 	"github.com/Selly-Modules/logger" | 	"github.com/Selly-Modules/logger" | ||||||
| 	"github.com/Selly-Modules/mongodb" | 	"github.com/Selly-Modules/mongodb" | ||||||
| 	ua "github.com/mssola/user_agent" |  | ||||||
| 	"go.mongodb.org/mongo-driver/bson" | 	"go.mongodb.org/mongo-driver/bson" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // DeviceCreate ...
 | // CreateOptions ...
 | ||||||
| type DeviceCreate struct { | type CreateOptions struct { | ||||||
| 	DeviceID   string | 	DeviceID   string | ||||||
| 	UserID     string | 	UserID     string | ||||||
| 	UserAgent  string | 	UserAgent  string | ||||||
| 	AppVersion string | 	AppVersion string | ||||||
| 	IP         string | 	IP         string | ||||||
| 	FCMToken   string | 	FCMToken   string | ||||||
|  | 	AuthToken  string | ||||||
| 	Language   string | 	Language   string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Create ...
 | // Create ...
 | ||||||
| func (s Service) Create(payload DeviceCreate) error { | func (s Service) Create(payload CreateOptions) error { | ||||||
| 	var ( | 	var ( | ||||||
| 		col = s.getDeviceCollection() | 		col = s.getDeviceCollection() | ||||||
| 		ctx = context.Background() | 		ctx = context.Background() | ||||||
| 	) | 	) | ||||||
| 
 | 
 | ||||||
|  | 	// Validate payload
 | ||||||
|  | 	err := payload.validate() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// New device data from payload
 | 	// New device data from payload
 | ||||||
| 	deviceData, err := payload.newDevice() | 	deviceData, err := payload.newDevice() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | @ -43,7 +50,7 @@ func (s Service) Create(payload DeviceCreate) error { | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| 	if !device.ID.IsZero() { | 	if !device.ID.IsZero() { | ||||||
| 		return errors.New("deviceID already exists") | 		return errors.New("this device is already existed") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Create device
 | 	// Create device
 | ||||||
|  | @ -53,78 +60,33 @@ func (s Service) Create(payload DeviceCreate) error { | ||||||
| 			"doc": deviceData, | 			"doc": deviceData, | ||||||
| 			"err": err.Error(), | 			"err": err.Error(), | ||||||
| 		}) | 		}) | ||||||
| 		return errors.New("create device fail") | 		return fmt.Errorf("error when create device: %s", err.Error()) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (payload DeviceCreate) newDevice() (result Device, err error) { | func (payload CreateOptions) newDevice() (result Device, err error) { | ||||||
| 	timeNow := now() | 	timeNow := now() | ||||||
| 	device := Device{ | 	device := Device{ | ||||||
| 		ID:             mongodb.NewObjectID(), | 		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, | 		LastActivityAt: timeNow, | ||||||
| 		CreatedAt:      timeNow, | 		CreatedAt:      timeNow, | ||||||
| 		FCMToken:       payload.FCMToken, | 		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
 | 	// App version
 | ||||||
| 	if payload.AppVersion != "" { | 	if payload.AppVersion != "" { | ||||||
| 		device.AppVersion = payload.AppVersion | 		device.AppVersion = payload.AppVersion | ||||||
| 		device.IsMobile = true | 		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 | 	result = device | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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 |  | ||||||
| } |  | ||||||
|  | @ -2,10 +2,10 @@ package devicemngmt | ||||||
| 
 | 
 | ||||||
| // Constant ...
 | // Constant ...
 | ||||||
| const ( | const ( | ||||||
| 	TableDevice = "devices" | 	tableDevice = "devices" | ||||||
| 
 | 
 | ||||||
| 	timezoneHCM = "Asia/Ho_Chi_Minh" | 	timezoneHCM = "Asia/Ho_Chi_Minh" | ||||||
| 
 | 
 | ||||||
| 	viLanguage = "vi" | 	langVi = "vi" | ||||||
| 	enLanguage = "en" | 	langEn = "en" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -19,8 +19,6 @@ type Config struct { | ||||||
| 	MongoDB MongoDBConfig | 	MongoDB MongoDBConfig | ||||||
| 	// Table prefix, each service has its own prefix
 | 	// Table prefix, each service has its own prefix
 | ||||||
| 	TablePrefix string | 	TablePrefix string | ||||||
| 	// Auth secret, used to sign token
 |  | ||||||
| 	AuthSecret string |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Service ...
 | // Service ...
 | ||||||
|  | @ -32,9 +30,9 @@ type Service struct { | ||||||
| var s *Service | var s *Service | ||||||
| 
 | 
 | ||||||
| // Init ...
 | // Init ...
 | ||||||
| func Init(config Config) error { | func Init(config Config) (*Service, error) { | ||||||
| 	if config.MongoDB.Host == "" || config.TablePrefix == "" || config.AuthSecret == "" { | 	if config.MongoDB.Host == "" || config.TablePrefix == "" { | ||||||
| 		return errors.New("please provide all necessary information for init device") | 		return nil, errors.New("please provide all necessary information for init device") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Connect MongoDB
 | 	// Connect MongoDB
 | ||||||
|  | @ -48,7 +46,7 @@ func Init(config Config) error { | ||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fmt.Println("Cannot init module DEVICE MANAGEMENT", err) | 		fmt.Println("Cannot init module DEVICE MANAGEMENT", err) | ||||||
| 		return err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	s = &Service{ | 	s = &Service{ | ||||||
|  | @ -56,7 +54,7 @@ func Init(config Config) error { | ||||||
| 		DB:     db, | 		DB:     db, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return nil | 	return s, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // GetInstance ...
 | // GetInstance ...
 | ||||||
|  |  | ||||||
							
								
								
									
										21
									
								
								helper.go
								
								
								
								
							
							
						
						
									
										21
									
								
								helper.go
								
								
								
								
							|  | @ -3,10 +3,29 @@ package devicemngmt | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 
 | 
 | ||||||
|  | 	ua "github.com/mssola/user_agent" | ||||||
| 	"go.mongodb.org/mongo-driver/mongo" | 	"go.mongodb.org/mongo-driver/mongo" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| //  getDeviceCollection ...
 | //  getDeviceCollection ...
 | ||||||
| func (s Service) getDeviceCollection() *mongo.Collection { | 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 | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										26
									
								
								model.go
								
								
								
								
							
							
						
						
									
										26
									
								
								model.go
								
								
								
								
							|  | @ -8,17 +8,17 @@ import ( | ||||||
| 
 | 
 | ||||||
| // Device ...
 | // Device ...
 | ||||||
| type Device struct { | type Device struct { | ||||||
| 	ID             primitive.ObjectID `bson:"_id"` | 	ID             primitive.ObjectID `bson:"_id" json:"_id"` | ||||||
| 	DeviceID       string             `bson:"deviceID"` // unique
 | 	DeviceID       string             `bson:"deviceID" json:"deviceID"` // unique
 | ||||||
| 	IP             string             `bson:"ip"` | 	IP             string             `bson:"ip" json:"ip"` | ||||||
| 	OSName         string             `bson:"osName"` | 	OSName         string             `bson:"osName" json:"osName"` | ||||||
| 	OSVersion      string             `bson:"osVersion"` | 	OSVersion      string             `bson:"osVersion" json:"osVersion"` | ||||||
| 	AppVersion     string             `bson:"appVersion"` | 	AppVersion     string             `bson:"appVersion" json:"appVersion"` | ||||||
| 	Language       string             `bson:"language"` // vi, en
 | 	Language       string             `bson:"language" json:"language"` // vi, en
 | ||||||
| 	IsMobile       bool               `bson:"isMobile"` | 	IsMobile       bool               `bson:"isMobile" json:"isMobile"` | ||||||
| 	LastActivityAt time.Time          `bson:"lastActivityAt"` | 	LastActivityAt time.Time          `bson:"lastActivityAt" json:"lastActivityAt"` | ||||||
| 	UserID         primitive.ObjectID `bson:"userID"` | 	UserID         primitive.ObjectID `bson:"userID" json:"userID"` | ||||||
| 	AuthToken      string             `bson:"authToken"` | 	AuthToken      string             `bson:"authToken" json:"authToken"` | ||||||
| 	FCMToken       string             `bson:"fcmToken"` | 	FCMToken       string             `bson:"fcmToken" json:"fcmToken"` | ||||||
| 	CreatedAt      time.Time          `bson:"createdAt"` | 	CreatedAt      time.Time          `bson:"createdAt" json:"createdAt"` | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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 | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue