diff --git a/internal/api/api.go b/internal/api/api.go index 66704c6f..6c03621c 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -132,6 +132,8 @@ func Run(ctx context.Context, logger zerolog.Logger, settings *config.Settings, mediator.WithHandler(&queries.GetCompatibilityR1SheetQuery{}, queries.NewCompatibilityR1SheetQueryHandler(settings)), mediator.WithHandler(&queries.GetDeviceDefinitionByIDQueryV2{}, queries.NewGetDeviceDefinitionByIDQueryV2Handler(ddOnChainService, pdb.DBS)), mediator.WithHandler(&queries.GetVINProfileQuery{}, queries.NewGetVINProfileQueryHandler(pdb.DBS, &logger)), + + mediator.WithHandler(&queries.UpsertDecodingQuery{}, queries.NewUpsertDecodingQueryHandler(pdb.DBS, &logger, ddOnChainService)), ) //fiber diff --git a/internal/api/grpc_vin_decoder_service.go b/internal/api/grpc_vin_decoder_service.go index c2e52d72..7db34e8c 100644 --- a/internal/api/grpc_vin_decoder_service.go +++ b/internal/api/grpc_vin_decoder_service.go @@ -3,6 +3,8 @@ package api import ( "context" + "google.golang.org/protobuf/types/known/emptypb" + "github.com/DIMO-Network/device-definitions-api/internal/core/mediator" "github.com/DIMO-Network/device-definitions-api/internal/core/queries" p_grpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc" @@ -34,3 +36,14 @@ func (s *GrpcVinDecoderService) DecodeVin(ctx context.Context, in *p_grpc.Decode return result, nil } + +func (s *GrpcVinDecoderService) UpsertDecoding(ctx context.Context, in *p_grpc.UpsertDecodingRequest) (*emptypb.Empty, error) { + _, err := s.Mediator.Send(ctx, &queries.UpsertDecodingQuery{ + VIN: in.Vin, + DefinitionID: in.TargetDefinitionId, + }) + if err != nil { + return nil, err + } + return &emptypb.Empty{}, nil +} diff --git a/internal/core/queries/upsert_vin_decode.go b/internal/core/queries/upsert_vin_decode.go new file mode 100644 index 00000000..f991e25b --- /dev/null +++ b/internal/core/queries/upsert_vin_decode.go @@ -0,0 +1,84 @@ +package queries + +import ( + "context" + "fmt" + + "github.com/DIMO-Network/device-definitions-api/internal/core/mediator" + "github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/models" + "github.com/DIMO-Network/device-definitions-api/internal/infrastructure/exceptions" + "github.com/DIMO-Network/device-definitions-api/internal/infrastructure/gateways" + "github.com/DIMO-Network/shared/pkg/db" + "github.com/DIMO-Network/shared/pkg/logfields" + vinutils "github.com/DIMO-Network/shared/pkg/vin" + "github.com/pkg/errors" + "github.com/rs/zerolog" + "github.com/volatiletech/null/v8" + "github.com/volatiletech/sqlboiler/v4/boil" +) + +type UpsertDecodingQueryHandler struct { + dbs func() *db.ReaderWriter + logger *zerolog.Logger + deviceDefinitionOnChainService gateways.DeviceDefinitionOnChainService +} + +type UpsertDecodingQuery struct { + VIN string `json:"vin"` + DefinitionID string `json:"definitionId"` +} + +func (*UpsertDecodingQuery) Key() string { return "UpsertDecodingQuery" } + +func NewUpsertDecodingQueryHandler(dbs func() *db.ReaderWriter, + logger *zerolog.Logger, + deviceDefinitionOnChainService gateways.DeviceDefinitionOnChainService) UpsertDecodingQueryHandler { + return UpsertDecodingQueryHandler{ + dbs: dbs, + logger: logger, + deviceDefinitionOnChainService: deviceDefinitionOnChainService, + } +} + +func (dc UpsertDecodingQueryHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) { + qry := query.(*UpsertDecodingQuery) + if len(qry.VIN) < 10 || len(qry.VIN) > 17 { + return nil, &exceptions.ValidationError{Err: fmt.Errorf("invalid vin %s", qry.VIN)} + } + vin := vinutils.VIN(qry.VIN) + wmi := vin.Wmi() + + localLog := dc.logger.With(). + Str("vin", vin.String()). + Str("handler", query.Key()). + Logger() + + // check if the definition id exists on chain + dd, manuf, err := dc.deviceDefinitionOnChainService.GetDefinitionByID(ctx, qry.DefinitionID) + if err != nil { + return nil, errors.Wrapf(err, "failed to find device definition by id %s when upserting vin decoding", qry.DefinitionID) + } + manufacturerName, err := dc.deviceDefinitionOnChainService.GetManufacturerNameByID(ctx, manuf) + if err != nil { + return nil, errors.Wrapf(err, "failed to find manufacturer name by id %s when upserting vin decoding", manuf) + } + //upsert the vin + vinNumber := &models.VinNumber{ + Vin: qry.VIN, + Wmi: null.StringFrom(wmi), + VDS: null.StringFrom(vin.VDS()), + CheckDigit: null.StringFrom(vin.CheckDigit()), + SerialNumber: vin.SerialNumber(), + Vis: null.StringFrom(vin.VIS()), + DecodeProvider: null.StringFrom("manual entry"), + Year: dd.Year, + DefinitionID: dd.ID, + ManufacturerName: manufacturerName, + } + err = vinNumber.Upsert(ctx, dc.dbs().Writer, true, []string{"vin"}, boil.Infer(), boil.Infer()) + if err != nil { + return nil, errors.Wrapf(err, "failed to upsert vin number %s for manual update", qry.VIN) + } + localLog.Info().Str(logfields.VIN, qry.VIN).Str(logfields.FunctionName, qry.Key()).Msg("manually upserted new vin number") + return nil, nil +} diff --git a/pkg/grpc/decoder.pb.go b/pkg/grpc/decoder.pb.go index c1bb842d..cbcb6b2d 100644 --- a/pkg/grpc/decoder.pb.go +++ b/pkg/grpc/decoder.pb.go @@ -206,7 +206,7 @@ func (x *DecodeVinResponse) GetManufacturer() string { return "" } -type ChangeDecodingRequest struct { +type UpsertDecodingRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields @@ -215,8 +215,8 @@ type ChangeDecodingRequest struct { TargetDefinitionId string `protobuf:"bytes,2,opt,name=target_definition_id,json=targetDefinitionId,proto3" json:"target_definition_id,omitempty"` } -func (x *ChangeDecodingRequest) Reset() { - *x = ChangeDecodingRequest{} +func (x *UpsertDecodingRequest) Reset() { + *x = UpsertDecodingRequest{} if protoimpl.UnsafeEnabled { mi := &file_pkg_grpc_decoder_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -224,13 +224,13 @@ func (x *ChangeDecodingRequest) Reset() { } } -func (x *ChangeDecodingRequest) String() string { +func (x *UpsertDecodingRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*ChangeDecodingRequest) ProtoMessage() {} +func (*UpsertDecodingRequest) ProtoMessage() {} -func (x *ChangeDecodingRequest) ProtoReflect() protoreflect.Message { +func (x *UpsertDecodingRequest) ProtoReflect() protoreflect.Message { mi := &file_pkg_grpc_decoder_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -242,19 +242,19 @@ func (x *ChangeDecodingRequest) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use ChangeDecodingRequest.ProtoReflect.Descriptor instead. -func (*ChangeDecodingRequest) Descriptor() ([]byte, []int) { +// Deprecated: Use UpsertDecodingRequest.ProtoReflect.Descriptor instead. +func (*UpsertDecodingRequest) Descriptor() ([]byte, []int) { return file_pkg_grpc_decoder_proto_rawDescGZIP(), []int{2} } -func (x *ChangeDecodingRequest) GetVin() string { +func (x *UpsertDecodingRequest) GetVin() string { if x != nil { return x.Vin } return "" } -func (x *ChangeDecodingRequest) GetTargetDefinitionId() string { +func (x *UpsertDecodingRequest) GetTargetDefinitionId() string { if x != nil { return x.TargetDefinitionId } @@ -295,7 +295,7 @@ var file_pkg_grpc_decoder_proto_rawDesc = []byte{ 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x12, 0x22, 0x0a, 0x0c, 0x6d, 0x61, 0x6e, 0x75, 0x66, 0x61, 0x63, 0x74, 0x75, 0x72, 0x65, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x6d, 0x61, 0x6e, 0x75, 0x66, 0x61, 0x63, 0x74, 0x75, 0x72, 0x65, 0x72, - 0x22, 0x5b, 0x0a, 0x15, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x69, + 0x22, 0x5b, 0x0a, 0x15, 0x55, 0x70, 0x73, 0x65, 0x72, 0x74, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x69, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x76, 0x69, 0x6e, 0x12, 0x30, 0x0a, 0x14, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, @@ -306,9 +306,9 @@ var file_pkg_grpc_decoder_proto_rawDesc = []byte{ 0x12, 0x16, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x56, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x56, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x45, 0x0a, 0x0e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x44, 0x65, 0x63, 0x6f, 0x64, - 0x69, 0x6e, 0x67, 0x12, 0x1b, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x43, 0x68, 0x61, 0x6e, 0x67, - 0x65, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x65, 0x12, 0x45, 0x0a, 0x0e, 0x55, 0x70, 0x73, 0x65, 0x72, 0x74, 0x44, 0x65, 0x63, 0x6f, 0x64, + 0x69, 0x6e, 0x67, 0x12, 0x1b, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x55, 0x70, 0x73, 0x65, 0x72, + 0x74, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x42, 0x39, 0x5a, 0x37, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x44, 0x49, 0x4d, 0x4f, 0x2d, 0x4e, 0x65, 0x74, 0x77, @@ -333,14 +333,14 @@ var file_pkg_grpc_decoder_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_pkg_grpc_decoder_proto_goTypes = []interface{}{ (*DecodeVinRequest)(nil), // 0: grpc.DecodeVinRequest (*DecodeVinResponse)(nil), // 1: grpc.DecodeVinResponse - (*ChangeDecodingRequest)(nil), // 2: grpc.ChangeDecodingRequest + (*UpsertDecodingRequest)(nil), // 2: grpc.UpsertDecodingRequest (*emptypb.Empty)(nil), // 3: google.protobuf.Empty } var file_pkg_grpc_decoder_proto_depIdxs = []int32{ 0, // 0: grpc.VinDecoderService.DecodeVin:input_type -> grpc.DecodeVinRequest - 2, // 1: grpc.VinDecoderService.ChangeDecoding:input_type -> grpc.ChangeDecodingRequest + 2, // 1: grpc.VinDecoderService.UpsertDecoding:input_type -> grpc.UpsertDecodingRequest 1, // 2: grpc.VinDecoderService.DecodeVin:output_type -> grpc.DecodeVinResponse - 3, // 3: grpc.VinDecoderService.ChangeDecoding:output_type -> google.protobuf.Empty + 3, // 3: grpc.VinDecoderService.UpsertDecoding:output_type -> google.protobuf.Empty 2, // [2:4] is the sub-list for method output_type 0, // [0:2] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name @@ -379,7 +379,7 @@ func file_pkg_grpc_decoder_proto_init() { } } file_pkg_grpc_decoder_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ChangeDecodingRequest); i { + switch v := v.(*UpsertDecodingRequest); i { case 0: return &v.state case 1: diff --git a/pkg/grpc/decoder.proto b/pkg/grpc/decoder.proto index 8f58a3ef..12bd6f88 100644 --- a/pkg/grpc/decoder.proto +++ b/pkg/grpc/decoder.proto @@ -26,12 +26,13 @@ message DecodeVinResponse { string manufacturer = 11; } -message ChangeDecodingRequest { +message UpsertDecodingRequest { string vin = 1; string target_definition_id = 2; } service VinDecoderService { rpc DecodeVin(DecodeVinRequest) returns (DecodeVinResponse); - rpc ChangeDecoding(ChangeDecodingRequest) returns (google.protobuf.Empty); + // used to change what a vin points to or insert a new vin to definition id mapping + rpc UpsertDecoding(UpsertDecodingRequest) returns (google.protobuf.Empty); } diff --git a/pkg/grpc/decoder_grpc.pb.go b/pkg/grpc/decoder_grpc.pb.go index b2ba0200..0f2df787 100644 --- a/pkg/grpc/decoder_grpc.pb.go +++ b/pkg/grpc/decoder_grpc.pb.go @@ -21,7 +21,7 @@ const _ = grpc.SupportPackageIsVersion7 const ( VinDecoderService_DecodeVin_FullMethodName = "/grpc.VinDecoderService/DecodeVin" - VinDecoderService_ChangeDecoding_FullMethodName = "/grpc.VinDecoderService/ChangeDecoding" + VinDecoderService_UpsertDecoding_FullMethodName = "/grpc.VinDecoderService/UpsertDecoding" ) // VinDecoderServiceClient is the client API for VinDecoderService service. @@ -29,7 +29,8 @@ const ( // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type VinDecoderServiceClient interface { DecodeVin(ctx context.Context, in *DecodeVinRequest, opts ...grpc.CallOption) (*DecodeVinResponse, error) - ChangeDecoding(ctx context.Context, in *ChangeDecodingRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) + // used to change what a vin points to or insert a new vin to definition id mapping + UpsertDecoding(ctx context.Context, in *UpsertDecodingRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) } type vinDecoderServiceClient struct { @@ -49,9 +50,9 @@ func (c *vinDecoderServiceClient) DecodeVin(ctx context.Context, in *DecodeVinRe return out, nil } -func (c *vinDecoderServiceClient) ChangeDecoding(ctx context.Context, in *ChangeDecodingRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { +func (c *vinDecoderServiceClient) UpsertDecoding(ctx context.Context, in *UpsertDecodingRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { out := new(emptypb.Empty) - err := c.cc.Invoke(ctx, VinDecoderService_ChangeDecoding_FullMethodName, in, out, opts...) + err := c.cc.Invoke(ctx, VinDecoderService_UpsertDecoding_FullMethodName, in, out, opts...) if err != nil { return nil, err } @@ -63,7 +64,8 @@ func (c *vinDecoderServiceClient) ChangeDecoding(ctx context.Context, in *Change // for forward compatibility type VinDecoderServiceServer interface { DecodeVin(context.Context, *DecodeVinRequest) (*DecodeVinResponse, error) - ChangeDecoding(context.Context, *ChangeDecodingRequest) (*emptypb.Empty, error) + // used to change what a vin points to or insert a new vin to definition id mapping + UpsertDecoding(context.Context, *UpsertDecodingRequest) (*emptypb.Empty, error) mustEmbedUnimplementedVinDecoderServiceServer() } @@ -74,8 +76,8 @@ type UnimplementedVinDecoderServiceServer struct { func (UnimplementedVinDecoderServiceServer) DecodeVin(context.Context, *DecodeVinRequest) (*DecodeVinResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method DecodeVin not implemented") } -func (UnimplementedVinDecoderServiceServer) ChangeDecoding(context.Context, *ChangeDecodingRequest) (*emptypb.Empty, error) { - return nil, status.Errorf(codes.Unimplemented, "method ChangeDecoding not implemented") +func (UnimplementedVinDecoderServiceServer) UpsertDecoding(context.Context, *UpsertDecodingRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpsertDecoding not implemented") } func (UnimplementedVinDecoderServiceServer) mustEmbedUnimplementedVinDecoderServiceServer() {} @@ -108,20 +110,20 @@ func _VinDecoderService_DecodeVin_Handler(srv interface{}, ctx context.Context, return interceptor(ctx, in, info, handler) } -func _VinDecoderService_ChangeDecoding_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(ChangeDecodingRequest) +func _VinDecoderService_UpsertDecoding_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(UpsertDecodingRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(VinDecoderServiceServer).ChangeDecoding(ctx, in) + return srv.(VinDecoderServiceServer).UpsertDecoding(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: VinDecoderService_ChangeDecoding_FullMethodName, + FullMethod: VinDecoderService_UpsertDecoding_FullMethodName, } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(VinDecoderServiceServer).ChangeDecoding(ctx, req.(*ChangeDecodingRequest)) + return srv.(VinDecoderServiceServer).UpsertDecoding(ctx, req.(*UpsertDecodingRequest)) } return interceptor(ctx, in, info, handler) } @@ -138,8 +140,8 @@ var VinDecoderService_ServiceDesc = grpc.ServiceDesc{ Handler: _VinDecoderService_DecodeVin_Handler, }, { - MethodName: "ChangeDecoding", - Handler: _VinDecoderService_ChangeDecoding_Handler, + MethodName: "UpsertDecoding", + Handler: _VinDecoderService_UpsertDecoding_Handler, }, }, Streams: []grpc.StreamDesc{},