Merge pull request #5 from Selly-Modules/feature/updateMethod
add update method
This commit is contained in:
commit
61106ecdc1
|
@ -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
|
||||
}
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package devicemngmt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Selly-Modules/logger"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// UpdateOptions ...
|
||||
type UpdateOptions struct {
|
||||
UserAgent string
|
||||
AppVersion string
|
||||
IP string
|
||||
FCMToken string
|
||||
AuthToken string
|
||||
Language string
|
||||
Model string
|
||||
Manufacturer string
|
||||
}
|
||||
|
||||
// UpdateByDeviceID ...
|
||||
func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
col = s.getDeviceCollection()
|
||||
cond = bson.M{
|
||||
"deviceID": deviceID,
|
||||
}
|
||||
)
|
||||
|
||||
// Validate payload
|
||||
err := payload.validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get userAgent data
|
||||
osName, osVersion, isMobile := getUserAgentData(payload.UserAgent)
|
||||
|
||||
// Setup update data
|
||||
updateData := bson.M{
|
||||
"$set": bson.M{
|
||||
"osName": osName,
|
||||
"osVersion": osVersion,
|
||||
"ip": payload.IP,
|
||||
"language": getLanguage(payload.Language),
|
||||
"authToken": payload.AuthToken,
|
||||
"fcmToken": payload.FCMToken,
|
||||
"model": payload.Model,
|
||||
"manufacturer": payload.Manufacturer,
|
||||
"appVersion": payload.AppVersion,
|
||||
"isMobile": isMobile,
|
||||
"lastActivatedAt": now(),
|
||||
},
|
||||
}
|
||||
|
||||
// Update
|
||||
_, err = col.UpdateOne(ctx, cond, updateData)
|
||||
if err != nil {
|
||||
logger.Error("devicemngt - updateByDeviceID", logger.LogData{
|
||||
"deviceID": deviceID,
|
||||
"err": err.Error(),
|
||||
})
|
||||
return fmt.Errorf("error when update device: %s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -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 {
|
||||
|
|
28
validate.go
28
validate.go
|
@ -49,3 +49,31 @@ func (co CreateOptions) validate() error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uo UpdateOptions) validate() error {
|
||||
// UserAgent
|
||||
if uo.UserAgent == "" {
|
||||
logger.Error("devicemngt - Update: no userAgent data", logger.LogData{
|
||||
"payload": uo,
|
||||
})
|
||||
return errors.New("no userAgent data")
|
||||
}
|
||||
|
||||
// IP
|
||||
if uo.IP == "" {
|
||||
logger.Error("devicemngt - Update: no ip data", logger.LogData{
|
||||
"payload": uo,
|
||||
})
|
||||
return errors.New("no ip data")
|
||||
}
|
||||
|
||||
// AuthToken
|
||||
if uo.AuthToken == "" {
|
||||
logger.Error("devicemngt - Update: no authToken data", logger.LogData{
|
||||
"payload": uo,
|
||||
})
|
||||
return errors.New("no authToken data")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue