Skip to content

Commit

Permalink
GetUserDeviceByEthAddr and protos
Browse files Browse the repository at this point in the history
  • Loading branch information
Laura Deng authored and Laura Deng committed Sep 18, 2023
1 parent 0b27387 commit 079f37c
Show file tree
Hide file tree
Showing 5 changed files with 572 additions and 416 deletions.
45 changes: 45 additions & 0 deletions internal/rpc/user_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"log"
"math/big"

"strings"
Expand Down Expand Up @@ -122,6 +123,50 @@ func (s *userDeviceRPCServer) GetUserDeviceByVIN(ctx context.Context, req *pb.Ge
return s.deviceModelToAPI(dbDevice), nil
}

func (s *userDeviceRPCServer) GetUserDeviceByEthAddr(ctx context.Context, req *pb.GetUserDeviceByEthAddrRequest) (*pb.UserDevice, error) {
aftermarketDevice, err := models.FindAftermarketDevice(ctx, s.dbs().Reader, req.EthAddr)
if err != nil {
log.Printf("Error finding AftermarketDevice: %v", err)
return nil, err
}
if aftermarketDevice == nil {
return nil, status.Error(codes.NotFound, "No AftermarketDevice found for the given Ethereum address")
}

if aftermarketDevice.R == nil || aftermarketDevice.R.VehicleToken == nil {
return nil, status.Error(codes.NotFound, "No VehicleToken associated with the AftermarketDevice")
}

userDeviceID := aftermarketDevice.R.VehicleToken.UserDeviceID
if !userDeviceID.Valid {
return nil, status.Error(codes.NotFound, "No UserDeviceID found in VehicleNFT")
}

dbDevice, err := models.UserDevices(
models.UserDeviceWhere.ID.EQ(userDeviceID.String),
qm.Load(
qm.Rels(models.UserDeviceRels.VehicleNFT,
models.VehicleNFTRels.VehicleTokenAftermarketDevice),
),
qm.Load(models.UserDeviceRels.UserDeviceAPIIntegrations),
qm.Load(
qm.Rels(
models.UserDeviceRels.VehicleNFT,
models.VehicleNFTRels.Claim,
),
),
).One(ctx, s.dbs().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, status.Error(codes.NotFound, "No device with that ID found.")
}
s.logger.Err(err).Str("userDeviceId", userDeviceID.String).Msg("Database failure retrieving device.")
return nil, status.Error(codes.Internal, "Internal error.")
}

return s.deviceModelToAPI(dbDevice), nil
}

func (s *userDeviceRPCServer) GetUserDeviceByTokenId(ctx context.Context, req *pb.GetUserDeviceByTokenIdRequest) (*pb.UserDevice, error) { //nolint

tknID := types.NewNullDecimal(decimal.New(req.TokenId, 0))
Expand Down
2 changes: 1 addition & 1 deletion internal/services/registry/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *StorageTestSuite) TestMintVehicle() {
},
Data: common.FromHex(
"000000000000000000000000000000000000000000000000000000000000386b" +
"000000000000000000000000000000000000000000000000000000000000386b" +
"000000000000000000000000000000000000000000000000000000000000386b" +
"0000000000000000000000007e74d0f663d58d12817b8bef762bcde3af1f63d6",
),
},
Expand Down
Loading

0 comments on commit 079f37c

Please sign in to comment.