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

PRO-1738-AD-req-fields #16

Closed
Closed
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
36 changes: 16 additions & 20 deletions graph/generated.go

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

4 changes: 2 additions & 2 deletions graph/model/models_gen.go

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

4 changes: 2 additions & 2 deletions graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type VehicleEdge {
type AftermarketDevice {
id: ID!
address: Address
owner: Address
owner: Address!
serial: String
imei: String
mintedAt: Time
mintedAt: Time!
vehicle: Vehicle
beneficiary: Address
}
Expand Down
5 changes: 3 additions & 2 deletions internal/loader/aftermarket_device_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/DIMO-Network/identity-api/internal/repositories"
"github.com/DIMO-Network/identity-api/models"
"github.com/DIMO-Network/shared/db"
"github.com/ethereum/go-ethereum/common"
"github.com/graph-gophers/dataloader/v7"
)

Expand Down Expand Up @@ -58,10 +59,10 @@ func (ad *AftermarketDeviceLoader) BatchGetLinkedAftermarketDeviceByVehicleID(ct
v := &model.AftermarketDevice{
ID: strconv.Itoa(device.ID),
Address: repositories.BytesToAddr(device.Address),
Owner: repositories.BytesToAddr(device.Owner),
Owner: common.BytesToAddress(device.Owner),
Serial: device.Serial.Ptr(),
Imei: device.Imei.Ptr(),
MintedAt: device.MintedAt.Ptr(),
MintedAt: device.MintedAt,
Beneficiary: repositories.BytesToAddr(device.Beneficiary),
}
results[keyOrder[device.VehicleID.Int]] = &dataloader.Result[*model.AftermarketDevice]{Data: v, Error: nil}
Expand Down
6 changes: 3 additions & 3 deletions internal/loader/vehicle_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"strconv"

"github.com/DIMO-Network/identity-api/graph/model"
"github.com/DIMO-Network/identity-api/internal/repositories"
"github.com/DIMO-Network/identity-api/models"
"github.com/DIMO-Network/shared/db"
"github.com/ethereum/go-ethereum/common"
"github.com/graph-gophers/dataloader/v7"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
Expand Down Expand Up @@ -64,11 +64,11 @@ func (v *VehicleLoader) BatchGetLinkedVehicleByAftermarketID(ctx context.Context

v := &model.Vehicle{
ID: strconv.Itoa(device.R.Vehicle.ID),
Owner: *repositories.BytesToAddr(device.R.Vehicle.OwnerAddress),
Owner: common.BytesToAddress(device.R.Vehicle.OwnerAddress),
Make: device.R.Vehicle.Make.Ptr(),
Model: device.R.Vehicle.Model.Ptr(),
Year: device.R.Vehicle.Year.Ptr(),
MintedAt: device.R.Vehicle.MintedAt.Time,
MintedAt: device.R.Vehicle.MintedAt,
}
results[keyOrder[device.ID]] = &dataloader.Result[*model.Vehicle]{Data: v, Error: nil}
delete(keyOrder, device.ID)
Expand Down
16 changes: 8 additions & 8 deletions internal/repositories/aftermarket_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func BytesToAddr(addrB null.Bytes) *common.Address {

func (v *VehiclesRepo) GetOwnedAftermarketDevices(ctx context.Context, addr common.Address, first *int, after *string) (*gmodel.AftermarketDeviceConnection, error) {
ownedADCount, err := models.AftermarketDevices(
models.AftermarketDeviceWhere.Owner.EQ(null.BytesFrom(addr.Bytes())),
// models.AftermarketDeviceWhere.Owner.EQ(addr.Bytes()),
).Count(ctx, v.pdb.DBS().Reader)
if err != nil {
return nil, err
Expand All @@ -47,7 +47,7 @@ func (v *VehiclesRepo) GetOwnedAftermarketDevices(ctx context.Context, addr comm
}

queryMods := []qm.QueryMod{
models.AftermarketDeviceWhere.Owner.EQ(null.BytesFrom(addr.Bytes())),
models.AftermarketDeviceWhere.Owner.EQ(addr.Bytes()),
qm.Load(models.AftermarketDeviceRels.Vehicle),
// Use limit + 1 here to check if there's a next page.
qm.Limit(limit + 1),
Expand Down Expand Up @@ -83,22 +83,22 @@ func (v *VehiclesRepo) GetOwnedAftermarketDevices(ctx context.Context, addr comm
var vehicle gmodel.Vehicle
if d.R.Vehicle != nil {
vehicle.ID = strconv.Itoa(d.R.Vehicle.ID)
vehicle.Owner = *BytesToAddr(d.R.Vehicle.OwnerAddress)
vehicle.Owner = common.BytesToAddress(d.R.Vehicle.OwnerAddress)
vehicle.Make = d.R.Vehicle.Make.Ptr()
vehicle.Model = d.R.Vehicle.Model.Ptr()
vehicle.Year = d.R.Vehicle.Year.Ptr()
vehicle.MintedAt = d.R.Vehicle.MintedAt.Time
vehicle.MintedAt = d.R.Vehicle.MintedAt
}

adEdges = append(adEdges,
&gmodel.AftermarketDeviceEdge{
Node: &gmodel.AftermarketDevice{
ID: strconv.Itoa(d.ID),
Address: BytesToAddr(d.Address),
Owner: BytesToAddr(d.Owner),
Owner: common.BytesToAddress(d.Owner),
Serial: d.Serial.Ptr(),
Imei: d.Imei.Ptr(),
MintedAt: d.MintedAt.Ptr(),
MintedAt: d.MintedAt,
Vehicle: &vehicle,
Beneficiary: BytesToAddr(d.Beneficiary),
},
Expand Down Expand Up @@ -142,10 +142,10 @@ func (v *VehiclesRepo) GetLinkedAftermarketDeviceByVehicleID(ctx context.Context
res := &gmodel.AftermarketDevice{
ID: strconv.Itoa(ad.ID),
Address: BytesToAddr(ad.Address),
Owner: BytesToAddr(ad.Address),
Owner: common.BytesToAddress(ad.Address.Bytes),
Serial: ad.Serial.Ptr(),
Imei: ad.Imei.Ptr(),
MintedAt: ad.MintedAt.Ptr(),
MintedAt: ad.MintedAt,
Beneficiary: BytesToAddr(ad.Beneficiary),
}

Expand Down
18 changes: 9 additions & 9 deletions internal/repositories/aftermarket_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ import (
var aftermarketDevice = models.AftermarketDevice{
ID: 1,
Address: null.BytesFrom(common.HexToAddress("46a3A41bd932244Dd08186e4c19F1a7E48cbcDf5").Bytes()),
Owner: null.BytesFrom(common.HexToAddress("46a3A41bd932244Dd08186e4c19F1a7E48cbcDf4").Bytes()),
Owner: common.HexToAddress("46a3A41bd932244Dd08186e4c19F1a7E48cbcDf4").Bytes(),
Serial: null.StringFrom("aftermarketDeviceSerial-1"),
Imei: null.StringFrom("aftermarketDeviceIMEI-1"),
MintedAt: null.TimeFrom(time.Now()),
MintedAt: time.Now(),
VehicleID: null.IntFrom(11),
Beneficiary: null.BytesFrom(common.HexToAddress("46a3A41bd932244Dd08186e4c19F1a7E48cbcDf3").Bytes()),
}

var vehicle = models.Vehicle{
ID: 11,
OwnerAddress: null.BytesFrom(common.HexToAddress("46a3A41bd932244Dd08186e4c19F1a7E48cbcDf4").Bytes()),
OwnerAddress: common.HexToAddress("46a3A41bd932244Dd08186e4c19F1a7E48cbcDf4").Bytes(),
Make: null.StringFrom("Ford"),
Model: null.StringFrom("Bronco"),
Year: null.IntFrom(2022),
MintedAt: null.TimeFrom(time.Now()),
MintedAt: time.Now(),
}

func createTestServerAndDB(ctx context.Context, t *testing.T, aftermarketDevices []models.AftermarketDevice, vehicles []models.Vehicle) *httptest.Server {
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestOwnedAftermarketDevices(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, strconv.Itoa(aftermarketDevice.ID), respBody.ID)
assert.Equal(t, common.BytesToAddress(aftermarketDevice.Address.Bytes), *respBody.Address)
assert.Equal(t, common.BytesToAddress(aftermarketDevice.Owner.Bytes), *respBody.Owner)
assert.Equal(t, common.BytesToAddress(aftermarketDevice.Owner), respBody.Owner)
assert.Equal(t, aftermarketDevice.Serial.String, *respBody.Serial)
assert.Equal(t, aftermarketDevice.Imei.String, *respBody.Imei)
}
Expand Down Expand Up @@ -144,16 +144,16 @@ func TestOwnedAftermarketDeviceAndLinkedVehicle(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, strconv.Itoa(aftermarketDevice.ID), adBody.ID)
assert.Equal(t, common.BytesToAddress(aftermarketDevice.Address.Bytes), *adBody.Address)
assert.Equal(t, common.BytesToAddress(aftermarketDevice.Owner.Bytes), *adBody.Owner)
assert.Equal(t, common.BytesToAddress(aftermarketDevice.Owner), adBody.Owner)
assert.Equal(t, aftermarketDevice.Serial.String, *adBody.Serial)
assert.Equal(t, aftermarketDevice.Imei.String, *adBody.Imei)

assert.Equal(t, strconv.Itoa(vehicle.ID), vehicleBody.ID)
assert.Equal(t, common.BytesToAddress(vehicle.OwnerAddress.Bytes), vehicleBody.Owner)
assert.Equal(t, common.BytesToAddress(vehicle.OwnerAddress), vehicleBody.Owner)
assert.Equal(t, vehicle.Make.String, *vehicleBody.Make)
assert.Equal(t, vehicle.Model.String, *vehicleBody.Model)
assert.Equal(t, vehicle.Year.Int, *vehicleBody.Year)
assert.Equal(t, vehicle.MintedAt.Time.UTC().Format(time.RFC1123), vehicleBody.MintedAt.UTC().Format(time.RFC1123))
assert.Equal(t, vehicle.MintedAt.UTC().Format(time.RFC1123), vehicleBody.MintedAt.UTC().Format(time.RFC1123))
}

func TestAftermarketDeviceNodeMintMultiResponse(t *testing.T) {
Expand All @@ -164,7 +164,7 @@ func TestAftermarketDeviceNodeMintMultiResponse(t *testing.T) {
for i := 1; i < 6; i++ {
ad := models.AftermarketDevice{
ID: i,
Owner: null.BytesFrom(common.HexToAddress("0x46a3A41bd932244Dd08186e4c19F1a7E48cbcDf4").Bytes()),
Owner: common.HexToAddress("0x46a3A41bd932244Dd08186e4c19F1a7E48cbcDf4").Bytes(),
}

err := ad.Insert(ctx, pdb.DBS().Writer, boil.Infer())
Expand Down
25 changes: 12 additions & 13 deletions internal/repositories/owned_vehicles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/DIMO-Network/identity-api/models"
"github.com/DIMO-Network/shared/db"
"github.com/ethereum/go-ethereum/common"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)

Expand All @@ -31,7 +30,7 @@ func NewVehiclesRepo(pdb db.Store) VehiclesRepo {

func (v *VehiclesRepo) GetOwnedVehicles(ctx context.Context, addr common.Address, first *int, after *string) (*gmodel.VehicleConnection, error) {
totalCount, err := models.Vehicles(
models.VehicleWhere.OwnerAddress.EQ(null.BytesFrom(addr.Bytes())),
models.VehicleWhere.OwnerAddress.EQ(addr.Bytes()),
).Count(ctx, v.pdb.DBS().Reader)
if err != nil {
return nil, err
Expand All @@ -53,7 +52,7 @@ func (v *VehiclesRepo) GetOwnedVehicles(ctx context.Context, addr common.Address
}

queryMods := []qm.QueryMod{
models.VehicleWhere.OwnerAddress.EQ(null.BytesFrom(addr.Bytes())),
models.VehicleWhere.OwnerAddress.EQ(addr.Bytes()),
qm.Load(models.VehicleRels.AftermarketDevice),
// Use limit + 1 here to check if there's a next page.
qm.Limit(limit + 1),
Expand Down Expand Up @@ -94,23 +93,23 @@ func (v *VehiclesRepo) GetOwnedVehicles(ctx context.Context, addr common.Address
edge := &gmodel.VehicleEdge{
Node: &gmodel.Vehicle{
ID: strconv.Itoa(v.ID),
Owner: *BytesToAddr(v.OwnerAddress),
Owner: common.BytesToAddress(v.OwnerAddress),
Make: v.Make.Ptr(),
Model: v.Model.Ptr(),
Year: v.Year.Ptr(),
MintedAt: v.MintedAt.Time,
MintedAt: v.MintedAt,
},
Cursor: base64.StdEncoding.EncodeToString([]byte(strconv.Itoa(v.ID))),
}

if v.R.AftermarketDevice != nil {
edge.Node.AftermarketDevice = &gmodel.AftermarketDevice{
ID: strconv.Itoa(v.R.AftermarketDevice.ID),
Address: BytesToAddr(v.R.AftermarketDevice.Address),
Owner: BytesToAddr(v.R.AftermarketDevice.Owner),
Serial: v.R.AftermarketDevice.Serial.Ptr(),
Imei: v.R.AftermarketDevice.Imei.Ptr(),
MintedAt: v.R.AftermarketDevice.MintedAt.Ptr(),
ID: strconv.Itoa(v.R.AftermarketDevice.ID),
Address: BytesToAddr(v.R.AftermarketDevice.Address),
// Owner: BytesToAddr(v.R.AftermarketDevice.Owner),
Serial: v.R.AftermarketDevice.Serial.Ptr(),
Imei: v.R.AftermarketDevice.Imei.Ptr(),
// MintedAt: v.R.AftermarketDevice.MintedAt.Ptr(),
Beneficiary: BytesToAddr(v.R.AftermarketDevice.Beneficiary),
}
}
Expand Down Expand Up @@ -149,11 +148,11 @@ func (v *VehiclesRepo) GetLinkedVehicleByID(ctx context.Context, aftermarketDevI

res := &gmodel.Vehicle{
ID: strconv.Itoa(ad.R.Vehicle.ID),
Owner: *BytesToAddr(ad.R.Vehicle.OwnerAddress),
Owner: common.BytesToAddress(ad.R.Vehicle.OwnerAddress),
Make: ad.R.Vehicle.Make.Ptr(),
Model: ad.R.Vehicle.Model.Ptr(),
Year: ad.R.Vehicle.Year.Ptr(),
MintedAt: ad.R.Vehicle.MintedAt.Time,
MintedAt: ad.R.Vehicle.MintedAt,
}

return res, nil
Expand Down
Loading
Loading