2021-08-17 04:03:50 +00:00
|
|
|
package devicemngt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-19 08:06:39 +00:00
|
|
|
"net/http"
|
2021-08-17 04:03:50 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/logger"
|
|
|
|
"github.com/Selly-Modules/mongodb"
|
2021-08-19 08:06:39 +00:00
|
|
|
"github.com/kr/pretty"
|
2021-08-17 04:03:50 +00:00
|
|
|
ua "github.com/mssola/user_agent"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpsertPayload ...
|
|
|
|
type UpsertPayload struct {
|
|
|
|
IP string
|
2021-08-19 08:06:39 +00:00
|
|
|
Headers http.Header
|
2021-08-17 04:03:50 +00:00
|
|
|
AuthToken string
|
|
|
|
FCMToken string
|
|
|
|
OwnerID string
|
|
|
|
OwnerType string
|
|
|
|
FirstSignInAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upsert ...
|
|
|
|
func (s Service) Upsert(payload UpsertPayload) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Read UA
|
|
|
|
var (
|
2021-08-19 08:06:39 +00:00
|
|
|
headerData = getHeaderData(payload.Headers)
|
|
|
|
uaData = ua.New(headerData.UserAgent)
|
|
|
|
)
|
|
|
|
|
|
|
|
// DB data
|
|
|
|
var (
|
2021-08-19 08:17:34 +00:00
|
|
|
deviceID = ""
|
2021-08-19 08:06:39 +00:00
|
|
|
platform = ""
|
|
|
|
osName = ""
|
|
|
|
osVersion = ""
|
|
|
|
appVersion = ""
|
|
|
|
appVersionCode = ""
|
|
|
|
browserName = ""
|
|
|
|
browserVersion = ""
|
2021-08-17 04:03:50 +00:00
|
|
|
)
|
|
|
|
|
2021-08-19 08:17:34 +00:00
|
|
|
// Set deviceID
|
|
|
|
deviceID = headerData.DeviceID
|
|
|
|
if deviceID == "" {
|
|
|
|
logger.Error("devicemngt - Upsert: no device_id data", logger.LogData{
|
|
|
|
"payload": payload,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-19 08:06:39 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-08-19 08:29:17 +00:00
|
|
|
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)
|
|
|
|
pretty.Println("----------------")
|
2021-08-19 08:06:39 +00:00
|
|
|
|
2021-08-19 08:17:34 +00:00
|
|
|
// Find device id existed or not
|
|
|
|
device := s.findByDeviceID(ctx, deviceID)
|
|
|
|
|
2021-08-17 04:03:50 +00:00
|
|
|
if !mongodb.IsValidID(device.ID) {
|
|
|
|
// If not exist, create new
|
|
|
|
stm, args, _ := s.Builder.Insert(TableDeviceMngt).
|
|
|
|
Columns(
|
2021-08-19 08:06:39 +00:00
|
|
|
"id", "device_id", "ip", "platform", "app_version",
|
|
|
|
"app_version_code", "os_name", "os_version", "browser_name", "browser_version",
|
2021-08-17 04:03:50 +00:00
|
|
|
"auth_token", "fcm_token", "owner_id", "owner_type",
|
|
|
|
"first_sign_in_at", "last_activity_at",
|
|
|
|
).Values(
|
2021-08-19 08:17:34 +00:00
|
|
|
mongodb.NewStringID(), deviceID, payload.IP, platform, appVersion,
|
2021-08-19 08:06:39 +00:00
|
|
|
appVersionCode, osName, osVersion, browserName, browserVersion,
|
2021-08-17 04:03:50 +00:00
|
|
|
payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType,
|
|
|
|
payload.FirstSignInAt, now(),
|
|
|
|
).ToSql()
|
|
|
|
|
2021-08-19 08:29:17 +00:00
|
|
|
pretty.Println("Create new")
|
|
|
|
pretty.Println("stm -", stm)
|
|
|
|
pretty.Println("args -", args)
|
|
|
|
|
2021-08-17 04:03:50 +00:00
|
|
|
if _, err := s.DB.ExecContext(ctx, stm, args); err != nil {
|
2021-08-19 08:17:34 +00:00
|
|
|
logger.Error("devicemngt - Upsert: Create new", logger.LogData{
|
2021-08-17 04:03:50 +00:00
|
|
|
"payload": payload,
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Else update
|
|
|
|
stm, args, _ := s.Builder.Update(TableDeviceMngt).
|
|
|
|
Set("ip", payload.IP).
|
|
|
|
Set("platform", platform).
|
2021-08-19 08:06:39 +00:00
|
|
|
Set("app_version", appVersion).
|
|
|
|
Set("app_version_code", appVersionCode).
|
|
|
|
Set("os_name", osName).
|
|
|
|
Set("os_version", osVersion).
|
2021-08-17 04:03:50 +00:00
|
|
|
Set("browser_name", browserName).
|
|
|
|
Set("browser_version", browserVersion).
|
|
|
|
Set("auth_token", payload.AuthToken).
|
|
|
|
Set("fcm_token", payload.FCMToken).
|
|
|
|
Set("owner_id", payload.OwnerID).
|
|
|
|
Set("owner_type", payload.OwnerType).
|
|
|
|
Set("last_activity_at", now()).
|
2021-08-19 08:17:34 +00:00
|
|
|
Where("device_id = ?", deviceID).
|
2021-08-17 04:03:50 +00:00
|
|
|
ToSql()
|
|
|
|
|
2021-08-19 08:29:17 +00:00
|
|
|
pretty.Println("Update")
|
|
|
|
pretty.Println("stm -", stm)
|
|
|
|
pretty.Println("args -", args)
|
|
|
|
|
2021-08-17 04:03:50 +00:00
|
|
|
if _, err := s.DB.ExecContext(ctx, stm, args); err != nil {
|
2021-08-19 08:17:34 +00:00
|
|
|
logger.Error("devicemngt - Upsert: Update", logger.LogData{
|
2021-08-17 04:03:50 +00:00
|
|
|
"payload": payload,
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|