fix comment

This commit is contained in:
Hoang 2021-11-08 09:46:17 +07:00
parent 1f62e375e7
commit 39d2ca65fc
3 changed files with 19 additions and 27 deletions

View File

@ -37,10 +37,7 @@ func (s Service) Create(payload CreateOptions) error {
} }
// New device data from payload // New device data from payload
deviceData, err := payload.newDevice() deviceData := payload.newDevice()
if err != nil {
return err
}
// Find deviceID existed or not // Find deviceID existed or not
if s.isDeviceIDExisted(ctx, deviceData.DeviceID) { if s.isDeviceIDExisted(ctx, deviceData.DeviceID) {
@ -60,13 +57,17 @@ func (s Service) Create(payload CreateOptions) error {
return nil return nil
} }
func (payload CreateOptions) newDevice() (result Device, err error) { func (payload CreateOptions) newDevice() Device {
timeNow := now() timeNow := now()
device := Device{
// Get userAgent data
osName, osVersion, isMobile := getUserAgentData(payload.UserAgent)
return Device{
ID: mongodb.NewObjectID(), ID: mongodb.NewObjectID(),
DeviceID: payload.DeviceID, DeviceID: payload.DeviceID,
OSName: getOSName(payload.UserAgent), OSName: osName,
OSVersion: getOSVersion(payload.UserAgent), OSVersion: osVersion,
IP: payload.IP, IP: payload.IP,
Language: getLanguage(payload.Language), Language: getLanguage(payload.Language),
AuthToken: payload.AuthToken, AuthToken: payload.AuthToken,
@ -76,14 +77,7 @@ func (payload CreateOptions) newDevice() (result Device, err error) {
Model: payload.Model, Model: payload.Model,
Manufacturer: payload.Manufacturer, Manufacturer: payload.Manufacturer,
UserID: payload.UserID, UserID: payload.UserID,
IsMobile: isMobile,
AppVersion: payload.AppVersion,
} }
// App version
if payload.AppVersion != "" {
device.AppVersion = payload.AppVersion
device.IsMobile = true
}
result = device
return
} }

View File

@ -36,11 +36,14 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error
return err return err
} }
// Get userAgent data
osName, osVersion, isMobile := getUserAgentData(payload.UserAgent)
// Setup update data // Setup update data
updateData := bson.M{ updateData := bson.M{
"$set": bson.M{ "$set": bson.M{
"osName": getOSName(payload.UserAgent), "osName": osName,
"osVersion": getOSVersion(payload.UserAgent), "osVersion": osVersion,
"ip": payload.IP, "ip": payload.IP,
"language": getLanguage(payload.Language), "language": getLanguage(payload.Language),
"authToken": payload.AuthToken, "authToken": payload.AuthToken,
@ -48,7 +51,7 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error
"model": payload.Model, "model": payload.Model,
"manufacturer": payload.Manufacturer, "manufacturer": payload.Manufacturer,
"appVersion": payload.AppVersion, "appVersion": payload.AppVersion,
"isMobile": payload.AppVersion != "", "isMobile": isMobile,
"lastActivatedAt": now(), "lastActivatedAt": now(),
}, },
} }

View File

@ -31,14 +31,9 @@ func (s Service) isDeviceIDExisted(ctx context.Context, deviceID string) bool {
return !device.ID.IsZero() return !device.ID.IsZero()
} }
func getOSName(userAgent string) string { func getUserAgentData(userAgent string) (string, string, bool) {
uaData := ua.New(userAgent) uaData := ua.New(userAgent)
return uaData.OSInfo().Name return uaData.OSInfo().Name, uaData.OSInfo().Version, uaData.Mobile()
}
func getOSVersion(userAgent string) string {
uaData := ua.New(userAgent)
return uaData.OSInfo().Version
} }
func getLanguage(lang string) string { func getLanguage(lang string) string {