Skip to content

Commit

Permalink
DeviceStyleId
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardovivanco committed Aug 25, 2023
1 parent 369f321 commit cb58c17
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
8 changes: 2 additions & 6 deletions cmd/devices-api/populate_powertrain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"github.com/DIMO-Network/devices-api/internal/constants"

"github.com/google/subcommands"

Expand Down Expand Up @@ -48,11 +49,6 @@ func (p *populateUSAPowertrainCmd) Execute(ctx context.Context, _ *flag.FlagSet,
}

func populateUSAPowertrain(ctx context.Context, logger *zerolog.Logger, pdb db.Store, nhtsaService services.INHTSAService, deviceDefSvc services.DeviceDefinitionService, useNHTSA bool) error {

const (
PowerTrainType = "powertrain_type"
)

devices, err := models.UserDevices(
models.UserDeviceWhere.CountryCode.EQ(null.StringFrom("USA")),
models.UserDeviceWhere.VinIdentifier.IsNotNull(),
Expand Down Expand Up @@ -100,7 +96,7 @@ func populateUSAPowertrain(ctx context.Context, logger *zerolog.Logger, pdb db.S
if len(resp.DeviceAttributes) > 0 {
// Find device attribute (powertrain_type)
for _, item := range resp.DeviceAttributes {
if item.Name == PowerTrainType {
if item.Name == constants.PowerTrainType {
powertrainType := deviceDefSvc.ConvertPowerTrainStringToPowertrain(item.Value)
md.PowertrainType = &powertrainType
if err := device.Metadata.Marshal(md); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/constants/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package constants

const UserDeviceCreationEventType = "com.dimo.zone.device.create"

const (
PowerTrainType = "powertrain_type"
)

const (
SmartCarVendor = "SmartCar"
TeslaVendor = "Tesla"
Expand Down
19 changes: 19 additions & 0 deletions internal/controllers/user_devices_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,25 @@ func (udc *UserDevicesController) RegisterDeviceForUserFromVIN(c *fiber.Ctx) err
if reg.CANProtocol != "" {
udMd = &services.UserDeviceMetadata{CANProtocol: &reg.CANProtocol}
}
// Validate if it has device style id
if len(decodeVIN.DeviceStyleId) > 0 {
ds, err := udc.DeviceDefSvc.GetDeviceStyleByID(c.Context(), decodeVIN.DeviceStyleId)
if err != nil {
return errors.Wrapf(err, "failed to get device style %s", decodeVIN.DeviceStyleId)
}

if len(ds.DeviceAttributes) > 0 {
// Find device attribute (powertrain_type)
for _, item := range ds.DeviceAttributes {
if item.Name == constants.PowerTrainType {
powertrainType := udc.DeviceDefSvc.ConvertPowerTrainStringToPowertrain(item.Value)
udMd.PowertrainType = &powertrainType
break
}
}
}
}

udFull, err = udc.createUserDevice(c.Context(), deviceDefinitionID, reg.CountryCode, userID, &vin, udMd)
if err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion internal/controllers/user_devices_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,22 @@ func (s *UserDevicesControllerTestSuite) TestPostUserDeviceFromVIN() {
dd := test.BuildDeviceDefinitionGRPC(ksuid.New().String(), "Ford", "F150", 2020, integration)
// act request
const vinny = "4T3R6RFVXMU023395"
const deviceStyleID = "24GE7Mlc4c9o4j5P4mcD1Fzinx1"
reg := RegisterUserDeviceVIN{VIN: vinny, CountryCode: "USA", CANProtocol: "06"}
j, _ := json.Marshal(reg)

s.deviceDefSvc.EXPECT().DecodeVIN(gomock.Any(), vinny, "", 0, reg.CountryCode).Times(1).Return(&grpc.DecodeVinResponse{
DeviceMakeId: dd[0].Make.Id,
DeviceDefinitionId: dd[0].DeviceDefinitionId,
DeviceStyleId: "",
DeviceStyleId: deviceStyleID,
Year: dd[0].Type.Year,
}, nil)
s.deviceDefSvc.EXPECT().GetDeviceDefinitionByID(gomock.Any(), dd[0].DeviceDefinitionId).Times(1).Return(dd[0], nil)
s.deviceDefSvc.EXPECT().GetDeviceStyleByID(gomock.Any(), deviceStyleID).Times(1).Return(&grpc.DeviceStyle{
Id: deviceStyleID,
DeviceAttributes: dd[0].DeviceAttributes,
}, nil)
s.deviceDefSvc.EXPECT().ConvertPowerTrainStringToPowertrain(gomock.Any()).Times(1).Return(services.BEV)
apInteg := test.BuildIntegrationGRPC(constants.AutoPiVendor, 10, 10)
s.deviceDefIntSvc.EXPECT().GetAutoPiIntegration(gomock.Any()).Times(1).Return(apInteg, nil)
s.deviceDefIntSvc.EXPECT().CreateDeviceDefinitionIntegration(gomock.Any(), apInteg.Id, dd[0].DeviceDefinitionId, "Americas")
Expand Down
19 changes: 19 additions & 0 deletions internal/services/device_definitions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DeviceDefinitionService interface {
DecodeVIN(ctx context.Context, vin string, model string, year int, countryCode string) (*ddgrpc.DecodeVinResponse, error)
GetIntegrationByTokenID(ctx context.Context, tokenID uint64) (*ddgrpc.Integration, error)
ConvertPowerTrainStringToPowertrain(value string) PowertrainType
GetDeviceStyleByID(ctx context.Context, id string) (*ddgrpc.DeviceStyle, error)
}

type deviceDefinitionService struct {
Expand Down Expand Up @@ -560,6 +561,24 @@ func (d *deviceDefinitionService) PullDrivlyData(ctx context.Context, userDevice
return PulledValuationDrivlyStatus, nil
}

func (d *deviceDefinitionService) GetDeviceStyleByID(ctx context.Context, id string) (*ddgrpc.DeviceStyle, error) {
definitionsClient, conn, err := d.getDeviceDefsGrpcClient()
if err != nil {
return nil, err
}
defer conn.Close()

ds, err := definitionsClient.GetDeviceStyleByID(ctx, &ddgrpc.GetDeviceStyleByIDRequest{
Id: id,
})

if err != nil {
return nil, errors.Wrap(err, "failed to call grpc endpoint GetDeviceStyleByID")
}

return ds, nil
}

func (d *deviceDefinitionService) updateDeviceDefAttrs(ctx context.Context, deviceDef *ddgrpc.GetDeviceDefinitionItemResponse, vinInfo map[string]any) error {
deviceAttributes := buildDeviceAttributes(deviceDef.DeviceAttributes, vinInfo)

Expand Down
15 changes: 15 additions & 0 deletions internal/services/mocks/device_definitions_service_mock.go

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

0 comments on commit cb58c17

Please sign in to comment.