diff --git a/action_upsert.go b/action_upsert.go index aed7459..40c0ce8 100644 --- a/action_upsert.go +++ b/action_upsert.go @@ -2,10 +2,12 @@ package devicemngt import ( "context" + "net/http" "time" "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" + "github.com/kr/pretty" ua "github.com/mssola/user_agent" ) @@ -13,7 +15,7 @@ import ( type UpsertPayload struct { DeviceID string IP string - UserAgent string + Headers http.Header AuthToken string FCMToken string OwnerID string @@ -30,23 +32,58 @@ func (s Service) Upsert(payload UpsertPayload) { // Read UA var ( - uaData = ua.New(payload.UserAgent) - platform = uaData.Platform() - osInfo = uaData.OSInfo() - browserName, browserVersion = uaData.Browser() + headerData = getHeaderData(payload.Headers) + uaData = ua.New(headerData.UserAgent) ) + // DB data + var ( + platform = "" + osName = "" + osVersion = "" + appVersion = "" + appVersionCode = "" + browserName = "" + browserVersion = "" + ) + + // OS, if there is os name, means mobile app, else browser + if headerData.OSName != "" { + platform = headerData.OSName + osName = headerData.OSName + osVersion = headerData.OSVersion + } else { + platform = uaData.Platform() + osName = uaData.OSInfo().Name + osVersion = uaData.OSInfo().Version + browserName, browserVersion = uaData.Browser() + } + + // App version + if headerData.AppVersion != "" { + appVersion = headerData.AppVersion + appVersionCode = headerData.AppVersionCode + } + + pretty.Println("platform", platform) + pretty.Println("osName", osName) + pretty.Println("osVersion", osVersion) + pretty.Println("appVersion", appVersion) + pretty.Println("appVersionCode", appVersionCode) + pretty.Println("browserName", browserName) + pretty.Println("browserVersion", browserVersion) + if !mongodb.IsValidID(device.ID) { // If not exist, create new stm, args, _ := s.Builder.Insert(TableDeviceMngt). Columns( - "id", "device_id", "ip", "platform", - "os_name", "os_version", "browser_name", "browser_version", + "id", "device_id", "ip", "platform", "app_version", + "app_version_code", "os_name", "os_version", "browser_name", "browser_version", "auth_token", "fcm_token", "owner_id", "owner_type", "first_sign_in_at", "last_activity_at", ).Values( - mongodb.NewStringID(), payload.DeviceID, payload.IP, platform, - osInfo.Name, osInfo.Version, browserName, browserVersion, + mongodb.NewStringID(), payload.DeviceID, payload.IP, platform, appVersion, + appVersionCode, osName, osVersion, browserName, browserVersion, payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType, payload.FirstSignInAt, now(), ).ToSql() @@ -62,8 +99,10 @@ func (s Service) Upsert(payload UpsertPayload) { stm, args, _ := s.Builder.Update(TableDeviceMngt). Set("ip", payload.IP). Set("platform", platform). - Set("os_name", osInfo.Name). - Set("os_version", osInfo.Version). + Set("app_version", appVersion). + Set("app_version_code", appVersionCode). + Set("os_name", osName). + Set("os_version", osVersion). Set("browser_name", browserName). Set("browser_version", browserVersion). Set("auth_token", payload.AuthToken). diff --git a/get_header_data.go b/get_header_data.go new file mode 100644 index 0000000..4489ea0 --- /dev/null +++ b/get_header_data.go @@ -0,0 +1,15 @@ +package devicemngt + +import "net/http" + +// getHeaderData ... +func getHeaderData(headers http.Header) HeaderData { + return HeaderData{ + UserAgent: headers.Get("User-Agent"), + DeviceID: headers.Get("Deviceid"), + AppVersion: headers.Get("App-Version"), + AppVersionCode: headers.Get("App-Version-Code"), + OSName: headers.Get("Os-Name"), + OSVersion: headers.Get("Os-Version"), + } +} diff --git a/model.go b/model.go index b02be14..f5acac9 100644 --- a/model.go +++ b/model.go @@ -42,3 +42,13 @@ type ResponseBrowser struct { Name string `json:"name"` Version string `json:"version"` } + +// HeaderData ... +type HeaderData struct { + UserAgent string + DeviceID string + AppVersion string + AppVersionCode string + OSName string + OSVersion string +} diff --git a/schema.go b/schema.go index f9bcc0e..50f25dd 100644 --- a/schema.go +++ b/schema.go @@ -13,6 +13,8 @@ const DeviceManagementSchema = ` platform text NOT NULL, os_name text NOT NULL, os_version text NOT NULL, + app_version text NOT NULL, + app_version_code text NOT NULL, browser_name text NOT NULL, browser_version text NOT NULL, auth_token text NOT NULL,