devicemngmt_old/action_upsert.go

143 lines
3.7 KiB
Go
Raw Permalink Normal View History

2021-08-17 04:03:50 +00:00
package devicemngt
import (
"context"
2021-08-19 08:06:39 +00:00
"net/http"
2021-08-21 12:46:28 +00:00
"strings"
2021-08-17 04:03:50 +00:00
"time"
2022-10-10 03:22:40 +00:00
"git.selly.red/Selly-Modules/logger"
"git.selly.red/Selly-Modules/mongodb"
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-19 08:51:09 +00:00
authToken = ""
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:51:09 +00:00
// Auth token
if headerData.AuthToken != "" {
2021-08-21 12:46:28 +00:00
// Get token only, remove "Bearer "
authToken = strings.Split(headerData.AuthToken, " ")[1]
2021-08-19 08:51:09 +00:00
} else {
authToken = payload.AuthToken
}
2021-08-19 08:48:05 +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-19 08:51:09 +00:00
authToken, payload.FCMToken, payload.OwnerID, payload.OwnerType,
2021-08-17 04:03:50 +00:00
payload.FirstSignInAt, now(),
).ToSql()
2021-08-19 08:31:48 +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).
2021-08-19 08:51:09 +00:00
Set("auth_token", authToken).
2021-08-17 04:03:50 +00:00
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:31:48 +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(),
})
}
}
}