remove device id out of payload

This commit is contained in:
Nam Huynh 2021-08-19 15:17:34 +07:00
parent 2d0d871211
commit dceb7b9aa6
1 changed files with 17 additions and 8 deletions

View File

@ -13,7 +13,6 @@ import (
// UpsertPayload ... // UpsertPayload ...
type UpsertPayload struct { type UpsertPayload struct {
DeviceID string
IP string IP string
Headers http.Header Headers http.Header
AuthToken string AuthToken string
@ -27,9 +26,6 @@ type UpsertPayload struct {
func (s Service) Upsert(payload UpsertPayload) { func (s Service) Upsert(payload UpsertPayload) {
ctx := context.Background() ctx := context.Background()
// Find device id existed or not
device := s.findByDeviceID(ctx, payload.DeviceID)
// Read UA // Read UA
var ( var (
headerData = getHeaderData(payload.Headers) headerData = getHeaderData(payload.Headers)
@ -38,6 +34,7 @@ func (s Service) Upsert(payload UpsertPayload) {
// DB data // DB data
var ( var (
deviceID = ""
platform = "" platform = ""
osName = "" osName = ""
osVersion = "" osVersion = ""
@ -47,6 +44,15 @@ func (s Service) Upsert(payload UpsertPayload) {
browserVersion = "" browserVersion = ""
) )
// Set deviceID
deviceID = headerData.DeviceID
if deviceID == "" {
logger.Error("devicemngt - Upsert: no device_id data", logger.LogData{
"payload": payload,
})
return
}
// OS, if there is os name, means mobile app, else browser // OS, if there is os name, means mobile app, else browser
if headerData.OSName != "" { if headerData.OSName != "" {
platform = headerData.OSName platform = headerData.OSName
@ -73,6 +79,9 @@ func (s Service) Upsert(payload UpsertPayload) {
pretty.Println("browserName", browserName) pretty.Println("browserName", browserName)
pretty.Println("browserVersion", browserVersion) pretty.Println("browserVersion", browserVersion)
// Find device id existed or not
device := s.findByDeviceID(ctx, deviceID)
if !mongodb.IsValidID(device.ID) { if !mongodb.IsValidID(device.ID) {
// If not exist, create new // If not exist, create new
stm, args, _ := s.Builder.Insert(TableDeviceMngt). stm, args, _ := s.Builder.Insert(TableDeviceMngt).
@ -82,14 +91,14 @@ func (s Service) Upsert(payload UpsertPayload) {
"auth_token", "fcm_token", "owner_id", "owner_type", "auth_token", "fcm_token", "owner_id", "owner_type",
"first_sign_in_at", "last_activity_at", "first_sign_in_at", "last_activity_at",
).Values( ).Values(
mongodb.NewStringID(), payload.DeviceID, payload.IP, platform, appVersion, mongodb.NewStringID(), deviceID, payload.IP, platform, appVersion,
appVersionCode, osName, osVersion, browserName, browserVersion, appVersionCode, osName, osVersion, browserName, browserVersion,
payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType, payload.AuthToken, payload.FCMToken, payload.OwnerID, payload.OwnerType,
payload.FirstSignInAt, now(), payload.FirstSignInAt, now(),
).ToSql() ).ToSql()
if _, err := s.DB.ExecContext(ctx, stm, args); err != nil { if _, err := s.DB.ExecContext(ctx, stm, args); err != nil {
logger.Error("devicemngt - Upsert - Create new", logger.LogData{ logger.Error("devicemngt - Upsert: Create new", logger.LogData{
"payload": payload, "payload": payload,
"error": err.Error(), "error": err.Error(),
}) })
@ -110,11 +119,11 @@ func (s Service) Upsert(payload UpsertPayload) {
Set("owner_id", payload.OwnerID). Set("owner_id", payload.OwnerID).
Set("owner_type", payload.OwnerType). Set("owner_type", payload.OwnerType).
Set("last_activity_at", now()). Set("last_activity_at", now()).
Where("device_id = ?", payload.DeviceID). Where("device_id = ?", deviceID).
ToSql() ToSql()
if _, err := s.DB.ExecContext(ctx, stm, args); err != nil { if _, err := s.DB.ExecContext(ctx, stm, args); err != nil {
logger.Error("devicemngt - Upsert - Update", logger.LogData{ logger.Error("devicemngt - Upsert: Update", logger.LogData{
"payload": payload, "payload": payload,
"error": err.Error(), "error": err.Error(),
}) })