diff --git a/action_create.go b/action_create.go index 00e9898..d99bac7 100644 --- a/action_create.go +++ b/action_create.go @@ -37,10 +37,7 @@ func (s Service) Create(payload CreateOptions) error { } // New device data from payload - deviceData, err := payload.newDevice() - if err != nil { - return err - } + deviceData := payload.newDevice() // Find deviceID existed or not if s.isDeviceIDExisted(ctx, deviceData.DeviceID) { @@ -60,13 +57,17 @@ func (s Service) Create(payload CreateOptions) error { return nil } -func (payload CreateOptions) newDevice() (result Device, err error) { +func (payload CreateOptions) newDevice() Device { timeNow := now() - device := Device{ + + // Get userAgent data + osName, osVersion, isMobile := getUserAgentData(payload.UserAgent) + + return Device{ ID: mongodb.NewObjectID(), DeviceID: payload.DeviceID, - OSName: getOSName(payload.UserAgent), - OSVersion: getOSVersion(payload.UserAgent), + OSName: osName, + OSVersion: osVersion, IP: payload.IP, Language: getLanguage(payload.Language), AuthToken: payload.AuthToken, @@ -76,14 +77,7 @@ func (payload CreateOptions) newDevice() (result Device, err error) { Model: payload.Model, Manufacturer: payload.Manufacturer, UserID: payload.UserID, + IsMobile: isMobile, + AppVersion: payload.AppVersion, } - - // App version - if payload.AppVersion != "" { - device.AppVersion = payload.AppVersion - device.IsMobile = true - } - - result = device - return } diff --git a/action_update.go b/action_update.go index bc1fb02..b92321c 100644 --- a/action_update.go +++ b/action_update.go @@ -36,11 +36,14 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error return err } + // Get userAgent data + osName, osVersion, isMobile := getUserAgentData(payload.UserAgent) + // Setup update data updateData := bson.M{ "$set": bson.M{ - "osName": getOSName(payload.UserAgent), - "osVersion": getOSVersion(payload.UserAgent), + "osName": osName, + "osVersion": osVersion, "ip": payload.IP, "language": getLanguage(payload.Language), "authToken": payload.AuthToken, @@ -48,7 +51,7 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error "model": payload.Model, "manufacturer": payload.Manufacturer, "appVersion": payload.AppVersion, - "isMobile": payload.AppVersion != "", + "isMobile": isMobile, "lastActivatedAt": now(), }, } diff --git a/helper.go b/helper.go index 120e9d5..2e5d736 100644 --- a/helper.go +++ b/helper.go @@ -31,14 +31,9 @@ func (s Service) isDeviceIDExisted(ctx context.Context, deviceID string) bool { return !device.ID.IsZero() } -func getOSName(userAgent string) string { +func getUserAgentData(userAgent string) (string, string, bool) { uaData := ua.New(userAgent) - return uaData.OSInfo().Name -} - -func getOSVersion(userAgent string) string { - uaData := ua.New(userAgent) - return uaData.OSInfo().Version + return uaData.OSInfo().Name, uaData.OSInfo().Version, uaData.Mobile() } func getLanguage(lang string) string {