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

Add syntheticDevice to UserDevice #214

Merged
merged 18 commits into from
Oct 13, 2023
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
24 changes: 22 additions & 2 deletions internal/rpc/user_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (s *userDeviceRPCServer) GetUserDevice(ctx context.Context, req *pb.GetUser
models.VehicleNFTRels.Claim,
),
),
qm.Load(
qm.Rels(models.UserDeviceRels.VehicleNFT,
models.VehicleNFTRels.VehicleTokenSyntheticDevice),
),
).One(ctx, s.dbs().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand All @@ -105,6 +109,10 @@ func (s *userDeviceRPCServer) GetUserDeviceByVIN(ctx context.Context, req *pb.Ge
models.VehicleNFTRels.Claim,
),
),
qm.Load(
qm.Rels(models.UserDeviceRels.VehicleNFT,
models.VehicleNFTRels.VehicleTokenSyntheticDevice),
),
).One(ctx, s.dbs().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down Expand Up @@ -154,6 +162,7 @@ func (s *userDeviceRPCServer) GetUserDeviceByEthAddr(ctx context.Context, req *p
models.VehicleNFTRels.Claim,
),
),
qm.Load(qm.Rels(models.UserDeviceRels.VehicleNFT, models.VehicleNFTRels.VehicleTokenSyntheticDevice)),
).One(ctx, s.dbs().Reader)

if err != nil {
Expand Down Expand Up @@ -212,6 +221,7 @@ func (s *userDeviceRPCServer) ListUserDevicesForUser(ctx context.Context, req *p

query = append(query,
qm.Load(qm.Rels(models.UserDeviceRels.VehicleNFT, models.VehicleNFTRels.VehicleTokenAftermarketDevice)),
qm.Load(qm.Rels(models.UserDeviceRels.VehicleNFT, models.VehicleNFTRels.VehicleTokenSyntheticDevice)),
qm.Load(models.UserDeviceRels.UserDeviceAPIIntegrations),
qm.OrderBy(models.UserDeviceTableColumns.CreatedAt+" DESC"),
)
Expand Down Expand Up @@ -369,8 +379,9 @@ func (s *userDeviceRPCServer) GetUserDeviceByAutoPIUnitId(ctx context.Context, r
func (s *userDeviceRPCServer) GetAllUserDevice(req *pb.GetAllUserDeviceRequest, stream pb.UserDeviceService_GetAllUserDeviceServer) error {
ctx := context.Background()
all, err := models.UserDevices(
models.UserDeviceWhere.VinConfirmed.EQ(true)).
All(ctx, s.dbs().Reader)
models.UserDeviceWhere.VinConfirmed.EQ(true),
qm.Load(qm.Rels(models.UserDeviceRels.VehicleNFT, models.VehicleNFTRels.VehicleTokenSyntheticDevice)),
).All(ctx, s.dbs().Reader)
if err != nil {
s.logger.Err(err).Msg("Database failure retrieving all user devices.")
return status.Error(codes.Internal, "Internal error.")
Expand Down Expand Up @@ -448,6 +459,15 @@ func (s *userDeviceRPCServer) deviceModelToAPI(ud *models.UserDevice) *pb.UserDe
Expiration: timestamppb.New(vc.ExpirationDate),
}
}

if sd := vnft.R.VehicleTokenSyntheticDevice; sd != nil && !sd.TokenID.IsZero() {
stk, _ := sd.TokenID.Uint64()
iTkID, _ := sd.IntegrationTokenID.Uint64()
out.SyntheticDevice = &pb.SyntheticDevice{
TokenId: stk,
IntegrationTokenId: iTkID,
}
}
}

for i, udai := range ud.R.UserDeviceAPIIntegrations {
Expand Down
71 changes: 71 additions & 0 deletions internal/rpc/user_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func populateDB(ctx context.Context, pdb db.Store) (string, error) {
userID := ksuid.New().String()
ownerAddress := null.BytesFrom(common.Hex2Bytes("448cF8Fd88AD914e3585401241BC434FbEA94bbb"))
claimID := ksuid.New().String()
_, childWallet, _ := test.GenerateWallet()

ud := models.UserDevice{
ID: ksuid.New().String(),
Expand Down Expand Up @@ -66,6 +67,15 @@ func populateDB(ctx context.Context, pdb db.Store) (string, error) {
DeviceManufacturerTokenID: types.NewDecimal(new(decimal.Big).SetBigMantScale(big.NewInt(42), 0)),
}

sd := models.SyntheticDevice{
VehicleTokenID: vnft.TokenID,
IntegrationTokenID: types.NewDecimal(new(decimal.Big).SetBigMantScale(big.NewInt(19), 0)),
MintRequestID: vnft.MintRequestID,
WalletChildNumber: 100,
TokenID: types.NewNullDecimal(decimal.New(6, 0)),
WalletAddress: childWallet.Bytes(),
}

credential := models.VerifiableCredential{
ClaimID: claimID,
Credential: []byte{},
Expand Down Expand Up @@ -97,6 +107,10 @@ func populateDB(ctx context.Context, pdb db.Store) (string, error) {
return "", err
}

if err := sd.Insert(ctx, pdb.DBS().Writer, boil.Infer()); err != nil {
return "", err
}

return ud.ID, nil
}

Expand Down Expand Up @@ -178,5 +192,62 @@ func TestGetUserDevice_PopulateDeprecatedFields(t *testing.T) {
assert.Equal(udResult.AftermarketDevice.Beneficiary, udResult.AftermarketDeviceBeneficiaryAddress) //nolint:staticcheck
assert.Equal(udResult.AftermarketDevice.TokenId, *udResult.AftermarketDeviceTokenId) //nolint:staticcheck
assert.NotEmpty(udResult.AftermarketDeviceBeneficiaryAddress) //nolint:staticcheck
}

func TestGetUserDevice_PopulateSyntheticDeviceFields(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
pdb, container := test.StartContainerDatabase(ctx, t, migrationsDirRelPath)
defer func() {
if err := container.Terminate(ctx); err != nil {
t.Fatal(err)
}
}()

userDeviceID, err := populateDB(ctx, pdb)
assert.NoError(err)

logger := zerolog.Logger{}
userDeviceSvc := services.NewUserDeviceService(nil, logger, pdb.DBS, nil)
udService := NewUserDeviceRPCService(pdb.DBS, nil, nil, nil, nil, nil, nil, userDeviceSvc)

udResult, err := udService.GetUserDevice(ctx, &pb_devices.GetUserDeviceRequest{Id: userDeviceID})
assert.NoError(err)

assert.Equal(udResult.SyntheticDevice.TokenId, uint64(6))
assert.Equal(udResult.SyntheticDevice.IntegrationTokenId, uint64(19))
}

func TestGetUserDevice_NoSyntheticDeviceFields_WhenNoTokenID(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
pdb, container := test.StartContainerDatabase(ctx, t, migrationsDirRelPath)
defer func() {
if err := container.Terminate(ctx); err != nil {
t.Fatal(err)
}
}()

userDeviceID, err := populateDB(ctx, pdb)
assert.NoError(err)

sd, err := models.SyntheticDevices(
models.SyntheticDeviceWhere.TokenID.EQ(types.NewNullDecimal(decimal.New(6, 0))),
models.SyntheticDeviceWhere.IntegrationTokenID.EQ(types.NewDecimal(new(decimal.Big).SetBigMantScale(big.NewInt(19), 0))),
).One(ctx, pdb.DBS().Reader)
assert.NoError(err)

sd.TokenID = types.NullDecimal{}

_, err = sd.Update(ctx, pdb.DBS().Writer, boil.Whitelist(models.SyntheticDeviceColumns.TokenID))
assert.NoError(err)

logger := zerolog.Logger{}
userDeviceSvc := services.NewUserDeviceService(nil, logger, pdb.DBS, nil)
udService := NewUserDeviceRPCService(pdb.DBS, nil, nil, nil, nil, nil, nil, userDeviceSvc)

udResult, err := udService.GetUserDevice(ctx, &pb_devices.GetUserDeviceRequest{Id: userDeviceID})
assert.NoError(err)

assert.Nil(udResult.SyntheticDevice)
}
Loading
Loading