Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SM-374 AutoPi user device metadata setting for Country and Region #246

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd/devices-api/update_autopi_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"time"

"github.com/DIMO-Network/devices-api/internal/constants"

"github.com/volatiletech/sqlboiler/v4/queries/qm"

"github.com/DIMO-Network/devices-api/internal/config"
Expand Down Expand Up @@ -56,7 +58,7 @@ func updateState(ctx context.Context, pdb db.Store, logger *zerolog.Logger, auto
const (
autopiInteg = "27qftVRWQYpVDcO5DltO5Ojbjxk"
)

// get all autopi paired devices
apiInts, err := models.UserDeviceAPIIntegrations(
models.UserDeviceAPIIntegrationWhere.IntegrationID.EQ(autopiInteg),
models.UserDeviceAPIIntegrationWhere.ExternalID.IsNotNull(),
Expand All @@ -68,14 +70,19 @@ func updateState(ctx context.Context, pdb db.Store, logger *zerolog.Logger, auto
logger.Info().Msgf("found %d connected autopis to update status for", len(apiInts))

for _, apiInt := range apiInts {
err := autoPiSvc.UpdateState(apiInt.ExternalID.String, apiInt.Status)
reg := ""
ci := constants.FindCountry(apiInt.R.UserDevice.CountryCode.String)
if ci != nil {
reg = ci.Region
}
err := autoPiSvc.UpdateState(apiInt.ExternalID.String, apiInt.Status, apiInt.R.UserDevice.CountryCode.String, reg)
if err != nil {
logger.Err(err).Msgf("failed to update status when calling autopi api for deviceId: %s", apiInt.ExternalID.String)
} else {
logger.Info().Msgf("successfully updated state for %s", apiInt.ExternalID.String)
}
time.Sleep(500)

// also update the AP vehicle Call Name to make it easier to find in AP dashboard
autoPiDevice, err := autoPiSvc.GetDeviceByUnitID(apiInt.Serial.String)
if err == nil {
dd, _ := deviceDefSvc.GetDeviceDefinitionByID(ctx, apiInt.R.UserDevice.DeviceDefinitionID)
Expand Down
8 changes: 7 additions & 1 deletion internal/controllers/webhooks_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (wc *WebhooksController) ProcessCommand(c *fiber.Ctx) error {
apiIntegration, err := models.UserDeviceAPIIntegrations(models.UserDeviceAPIIntegrationWhere.IntegrationID.EQ(autoPiInteg.Id),
models.UserDeviceAPIIntegrationWhere.ExternalID.EQ(null.StringFrom(apwDeviceID.String())),
models.UserDeviceAPIIntegrationWhere.UserDeviceID.EQ(autopiJob.UserDeviceID.String),
qm.Load(models.UserDeviceAPIIntegrationRels.UserDevice),
qm.OrderBy("updated_at desc"), qm.Limit(1)).One(c.Context(), wc.dbs().Reader)
if err != nil {
logger.Err(err).Msg("could not get user device api integrations")
Expand Down Expand Up @@ -121,7 +122,12 @@ func (wc *WebhooksController) ProcessCommand(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusNoContent)
}

err = wc.autoPiSvc.UpdateState(apiIntegration.ExternalID.String, apiIntegration.Status)
reg := ""
ci := constants.FindCountry(apiIntegration.R.UserDevice.CountryCode.String)
if ci != nil {
reg = ci.Region
}
err = wc.autoPiSvc.UpdateState(apiIntegration.ExternalID.String, apiIntegration.Status, apiIntegration.R.UserDevice.CountryCode.String, reg)
if err != nil {
logger.Err(err).Msgf("failed to update status when calling autopi api for deviceId: %s", apiIntegration.ExternalID.String)
return c.SendStatus(fiber.StatusNoContent)
Expand Down
3 changes: 2 additions & 1 deletion internal/controllers/webhooks_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func (s *WebHooksControllerTestSuite) TestPostWebhookSyncCommand() {
ddDefIntSvc.EXPECT().GetAutoPiIntegration(gomock.Any()).Return(integ, nil)

autoAPISvc.EXPECT().UpdateJob(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(autopiJob, nil)
autoAPISvc.EXPECT().UpdateState(gomock.Any(), gomock.Any()).Return(nil)
ci := constants.FindCountry(ud.CountryCode.String)
autoAPISvc.EXPECT().UpdateState(gomock.Any(), gomock.Any(), ud.CountryCode.String, ci.Region).Return(nil)

udiai := models.UserDeviceAPIIntegration{
// create user device api integration
Expand Down
11 changes: 9 additions & 2 deletions internal/services/autopi_api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type AutoPiAPIService interface {
GetCommandStatus(ctx context.Context, jobID string) (*AutoPiCommandJob, *models.AutopiJob, error)
GetCommandStatusFromAutoPi(deviceID string, jobID string) ([]byte, error)
UpdateJob(ctx context.Context, jobID, newState string, result *AutoPiCommandResult) (*models.AutopiJob, error)
UpdateState(deviceID string, state string) error
UpdateState(deviceID string, state, country, region string) error
}

type autoPiAPIService struct {
Expand Down Expand Up @@ -400,9 +400,16 @@ func (a *autoPiAPIService) GetCommandStatus(ctx context.Context, jobID string) (
}

// UpdateState calls https://api.dimo.autopi.io/dongle/devices/{DEVICE_ID}/ Note that the deviceID is the autoPi one.
func (a *autoPiAPIService) UpdateState(deviceID string, state string) error {
// state is the device pairing state from our end for AP's troubleshooting usage, country and region to be used by AP for region balancing traffic
func (a *autoPiAPIService) UpdateState(deviceID string, state, country, region string) error {
userMetaDataStateInfo := make(map[string]interface{})
userMetaDataStateInfo["state"] = state
if country != "" {
userMetaDataStateInfo["country_code_iso3"] = country
}
if region != "" {
userMetaDataStateInfo["region"] = region
}

userMetaDataInfo := make(map[string]interface{})
userMetaDataInfo["user_metadata"] = userMetaDataStateInfo
Expand Down
8 changes: 4 additions & 4 deletions internal/services/mocks/autopi_api_service_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading