@@ -73,7 +73,7 @@ func NewDecodeVINQueryHandler(dbs func() *db.ReaderWriter, vinDecodingService se
7373
7474func (dc DecodeVINQueryHandler ) Handle (ctx context.Context , query mediator.Message ) (interface {}, error ) {
7575 qry := query .(* DecodeVINQuery )
76- if len (qry .VIN ) != 17 {
76+ if ! ( len (qry .VIN ) >= 13 && len ( qry . VIN ) <= 17 ) {
7777 return nil , & exceptions.ValidationError {Err : fmt .Errorf ("invalid vin %s" , qry .VIN )}
7878 }
7979 resp := & p_grpc.DecodeVinResponse {}
@@ -111,30 +111,10 @@ func (dc DecodeVINQueryHandler) Handle(ctx context.Context, query mediator.Messa
111111 metrics .InternalError .With (prometheus.Labels {"method" : VinErrors }).Inc ()
112112 return nil , errors .Wrap (err , "error when querying for existing VIN number" )
113113 }
114- // todo refactor: func hydrateResponseFromVinNumber(...)
115- if vinDecodeNumber != nil {
116- // get from tableland, probably don't need this here anymore
117- //tblDef, errTbl := dc.deviceDefinitionOnChainService.GetDefinitionByID(ctx, vinDecodeNumber.DefinitionID, dc.dbs().Reader)
118- // should mark this deprecated
119- //resp.DeviceMakeId = vinDecodeNumber.DeviceMakeID
120- resp .Manufacturer = vinDecodeNumber .ManufacturerName
121- resp .Year = int32 (vinDecodeNumber .Year )
122- resp .DeviceStyleId = vinDecodeNumber .StyleID .String
123- resp .Source = vinDecodeNumber .DecodeProvider .String
124- resp .DefinitionId = vinDecodeNumber .DefinitionID
125- split := strings .Split (vinDecodeNumber .DefinitionID , "_" )
126- if len (split ) != 3 {
127- return nil , errors .New ("invalid definition ID encountered: " + vinDecodeNumber .DefinitionID )
128- }
129- pt , err := dc .powerTrainTypeService .ResolvePowerTrainType (split [0 ], split [1 ], vinDecodeNumber .DrivlyData , vinDecodeNumber .VincarioData )
130- if err != nil {
131- pt = coremodels .ICE .String ()
132- }
133- resp .Powertrain = pt
134-
114+ // if database vin_number match found, just return it here
115+ if r := dc .hydrateResponseFromVinNumber (vinDecodeNumber ); r != nil {
135116 metrics .Success .With (prometheus.Labels {"method" : VinExists }).Inc ()
136-
137- return resp , nil
117+ return r , nil
138118 }
139119
140120 // todo: this should be a separate specific gRPC endpoint for setting or updating vin number to DD mapping
@@ -161,10 +141,10 @@ func (dc DecodeVINQueryHandler) Handle(ctx context.Context, query mediator.Messa
161141 vinDecodeNumber = & models.VinNumber {
162142 Vin : vin .String (),
163143 ManufacturerName : dm .Name ,
164- Wmi : dbWMI .Wmi ,
165- VDS : vin .VDS (),
166- Vis : vin .VIS (),
167- CheckDigit : vin .CheckDigit (),
144+ Wmi : null . StringFrom ( dbWMI .Wmi ) ,
145+ VDS : null . StringFrom ( vin .VDS () ),
146+ Vis : null . StringFrom ( vin .VIS () ),
147+ CheckDigit : null . StringFrom ( vin .CheckDigit () ),
168148 SerialNumber : vin .SerialNumber (),
169149 DecodeProvider : null .StringFrom ("manual" ),
170150 Year : tblDef .Year ,
@@ -373,6 +353,38 @@ func resolveMetadataFromInfo(powertrain string, _ *coremodels.VINDecodingInfoDat
373353 return & md
374354}
375355
356+ // hydrateResponseFromVinNumber pass in a vin_number database object and converts to vin decode response
357+ func (dc DecodeVINQueryHandler ) hydrateResponseFromVinNumber (vn * models.VinNumber ) * p_grpc.DecodeVinResponse {
358+ if vn == nil {
359+ return nil
360+ }
361+ // call on-chain svc to get the DD and pull out the powertrain
362+ pt := ""
363+ tblDef , manufID , err := dc .deviceDefinitionOnChainService .GetDefinitionByID (context .Background (), vn .DefinitionID , dc .dbs ().Reader )
364+ if err == nil && tblDef != nil {
365+ for _ , attribute := range tblDef .Metadata .DeviceAttributes {
366+ if attribute .Name == common .PowerTrainType {
367+ pt = attribute .Value
368+ }
369+ }
370+ if pt == "" {
371+ makeName , _ := dc .deviceDefinitionOnChainService .GetManufacturerNameByID (context .Background (), manufID )
372+ pt , _ = dc .powerTrainTypeService .ResolvePowerTrainType (shared .SlugString (makeName ), shared .SlugString (tblDef .Model ), null.JSON {}, null.JSON {})
373+ }
374+ }
375+
376+ resp := & p_grpc.DecodeVinResponse {
377+ Manufacturer : vn .ManufacturerName ,
378+ Year : int32 (vn .Year ),
379+ DeviceStyleId : vn .StyleID .String ,
380+ Source : vn .DecodeProvider .String ,
381+ DefinitionId : vn .DefinitionID ,
382+ Powertrain : pt ,
383+ }
384+
385+ return resp
386+ }
387+
376388// processDeviceStyle saves new styles if needed to db and returns the style database ID
377389func (dc DecodeVINQueryHandler ) processDeviceStyle (ctx context.Context , vinInfo * coremodels.VINDecodingInfoData , definitionID , powertrain string ) (string , error ) {
378390 externalStyleID := shared .SlugString (vinInfo .StyleName )
@@ -425,10 +437,10 @@ func (dc DecodeVINQueryHandler) saveVinDecodeNumber(ctx context.Context, vin sha
425437 vinDecodeNumber := & models.VinNumber {
426438 Vin : vin .String (),
427439 ManufacturerName : resp .Manufacturer ,
428- Wmi : vin .Wmi (),
429- VDS : vin .VDS (),
430- Vis : vin .VIS (),
431- CheckDigit : vin .CheckDigit (),
440+ Wmi : null . StringFrom ( vin .Wmi () ),
441+ VDS : null . StringFrom ( vin .VDS () ),
442+ Vis : null . StringFrom ( vin .VIS () ),
443+ CheckDigit : null . StringFrom ( vin .CheckDigit () ),
432444 SerialNumber : vin .SerialNumber (),
433445 DecodeProvider : null .StringFrom (string (vinInfo .Source )),
434446 Year : int (resp .Year ),
0 commit comments