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

MAIN-B-21618 Prime Sim Create NTS/NTSR Shipments #14624

Merged
merged 11 commits into from
Jan 23, 2025
100 changes: 58 additions & 42 deletions pkg/services/mto_shipment/mto_shipment_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (f mtoShipmentCreator) CreateMTOShipment(appCtx appcontext.AppContext, ship

transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
// create pickup and destination addresses
if shipment.PickupAddress != nil && shipment.ShipmentType != models.MTOShipmentTypeHHGOutOfNTS {
if shipment.PickupAddress != nil {
pickupAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.PickupAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create pickup address "+errAddress.Error())
Expand All @@ -215,34 +215,42 @@ func (f mtoShipmentCreator) CreateMTOShipment(appCtx appcontext.AppContext, ship
}

if shipment.SecondaryPickupAddress != nil {
secondaryPickupAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.SecondaryPickupAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create secondary pickup address "+errAddress.Error())
}
shipment.SecondaryPickupAddress = secondaryPickupAddress
shipment.SecondaryPickupAddressID = &shipment.SecondaryPickupAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.SecondaryPickupAddress.PostalCode)
if errCounty != nil {
return errCounty
if shipment.ShipmentType != models.MTOShipmentTypeHHGOutOfNTS {
secondaryPickupAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.SecondaryPickupAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create secondary pickup address "+errAddress.Error())
}
shipment.SecondaryPickupAddress = secondaryPickupAddress
shipment.SecondaryPickupAddressID = &shipment.SecondaryPickupAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.SecondaryPickupAddress.PostalCode)
if errCounty != nil {
return errCounty
}
shipment.SecondaryPickupAddress.County = county
} else {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "Secondary pickup address cannot be created for shipment Type "+string(models.MTOShipmentTypeHHGOutOfNTS))
}
shipment.SecondaryPickupAddress.County = county
}

if shipment.TertiaryPickupAddress != nil {
tertiaryPickupAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.TertiaryPickupAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create tertiary pickup address "+errAddress.Error())
}
shipment.TertiaryPickupAddress = tertiaryPickupAddress
shipment.TertiaryPickupAddressID = &shipment.TertiaryPickupAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.TertiaryPickupAddress.PostalCode)
if errCounty != nil {
return errCounty
if shipment.ShipmentType != models.MTOShipmentTypeHHGOutOfNTS {
tertiaryPickupAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.TertiaryPickupAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create tertiary pickup address "+errAddress.Error())
}
shipment.TertiaryPickupAddress = tertiaryPickupAddress
shipment.TertiaryPickupAddressID = &shipment.TertiaryPickupAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.TertiaryPickupAddress.PostalCode)
if errCounty != nil {
return errCounty
}
shipment.TertiaryPickupAddress.County = county
} else {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "Tertiary pickup address cannot be created for shipment Type "+string(models.MTOShipmentTypeHHGOutOfNTS))
}
shipment.TertiaryPickupAddress.County = county
}

if shipment.DestinationAddress != nil && shipment.ShipmentType != models.MTOShipmentTypeHHGIntoNTS {
if shipment.DestinationAddress != nil {
destinationAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.DestinationAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create destination address "+errAddress.Error())
Expand All @@ -257,31 +265,39 @@ func (f mtoShipmentCreator) CreateMTOShipment(appCtx appcontext.AppContext, ship
}

if shipment.SecondaryDeliveryAddress != nil {
secondaryDeliveryAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.SecondaryDeliveryAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create secondary delivery address "+errAddress.Error())
}
shipment.SecondaryDeliveryAddress = secondaryDeliveryAddress
shipment.SecondaryDeliveryAddressID = &shipment.SecondaryDeliveryAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.SecondaryDeliveryAddress.PostalCode)
if errCounty != nil {
return errCounty
if shipment.ShipmentType != models.MTOShipmentTypeHHGIntoNTS {
secondaryDeliveryAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.SecondaryDeliveryAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create secondary delivery address "+errAddress.Error())
}
shipment.SecondaryDeliveryAddress = secondaryDeliveryAddress
shipment.SecondaryDeliveryAddressID = &shipment.SecondaryDeliveryAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.SecondaryDeliveryAddress.PostalCode)
if errCounty != nil {
return errCounty
}
shipment.SecondaryDeliveryAddress.County = county
} else {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "Secondary delivery address cannot be created for shipment Type "+string(models.MTOShipmentTypeHHGIntoNTS))
}
shipment.SecondaryDeliveryAddress.County = county
}

if shipment.TertiaryDeliveryAddress != nil {
tertiaryDeliveryAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.TertiaryDeliveryAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create tertiary delivery address "+errAddress.Error())
}
shipment.TertiaryDeliveryAddress = tertiaryDeliveryAddress
shipment.TertiaryDeliveryAddressID = &shipment.TertiaryDeliveryAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.TertiaryDeliveryAddress.PostalCode)
if errCounty != nil {
return errCounty
if shipment.ShipmentType != models.MTOShipmentTypeHHGIntoNTS {
tertiaryDeliveryAddress, errAddress := f.addressCreator.CreateAddress(txnAppCtx, shipment.TertiaryDeliveryAddress)
if errAddress != nil {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "failed to create tertiary delivery address "+errAddress.Error())
}
shipment.TertiaryDeliveryAddress = tertiaryDeliveryAddress
shipment.TertiaryDeliveryAddressID = &shipment.TertiaryDeliveryAddress.ID
county, errCounty := models.FindCountyByZipCode(appCtx.DB(), shipment.TertiaryDeliveryAddress.PostalCode)
if errCounty != nil {
return errCounty
}
shipment.TertiaryDeliveryAddress.County = county
} else {
return apperror.NewInvalidInputError(uuid.Nil, nil, nil, "Tertiary delivery address cannot be created for shipment Type "+string(models.MTOShipmentTypeHHGIntoNTS))
}
shipment.TertiaryDeliveryAddress.County = county
}

if shipment.StorageFacility != nil {
Expand Down
78 changes: 78 additions & 0 deletions pkg/services/mto_shipment/mto_shipment_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,84 @@ func (suite *MTOShipmentServiceSuite) TestCreateMTOShipment() {
_, err := creator.CreateMTOShipment(suite.AppContextForTest(), clearedChildShipment)
suite.Error(err)
})

suite.Run("InvalidInputError - NTS shipment cannot specify a secondary delivery address", func() {
subtestData := suite.createSubtestData(nil)
creator := subtestData.shipmentCreator

pickupAddress := factory.BuildDefaultAddress(suite.DB())
deliveryAddress := factory.BuildDefaultAddress(suite.DB())
secondaryDeliveryAddress := factory.BuildDefaultAddress(suite.DB())

shipment := factory.BuildMTOShipmentMinimal(suite.DB(), []factory.Customization{
{
Model: pickupAddress,
Type: &factory.Addresses.PickupAddress,
LinkOnly: true,
},
{
Model: deliveryAddress,
Type: &factory.Addresses.DeliveryAddress,
LinkOnly: true,
},
{
Model: secondaryDeliveryAddress,
Type: &factory.Addresses.SecondaryDeliveryAddress,
LinkOnly: true,
},
{
Model: models.MTOShipment{
ShipmentType: models.MTOShipmentTypeHHGIntoNTS,
},
},
}, nil)
clearShipmentIDFields(&shipment)

_, err := creator.CreateMTOShipment(suite.AppContextForTest(), &shipment)

suite.Error(err)
suite.Equal("Secondary delivery address cannot be created for shipment Type "+string(models.MTOShipmentTypeHHGIntoNTS), err.Error())
suite.IsType(apperror.InvalidInputError{}, err)
})

suite.Run("InvalidInputError - NTSR shipment cannot specify a secondary pickup address", func() {
subtestData := suite.createSubtestData(nil)
creator := subtestData.shipmentCreator

pickupAddress := factory.BuildDefaultAddress(suite.DB())
deliveryAddress := factory.BuildDefaultAddress(suite.DB())
secondaryPickupAddress := factory.BuildDefaultAddress(suite.DB())

shipment := factory.BuildMTOShipmentMinimal(suite.DB(), []factory.Customization{
{
Model: pickupAddress,
Type: &factory.Addresses.PickupAddress,
LinkOnly: true,
},
{
Model: deliveryAddress,
Type: &factory.Addresses.DeliveryAddress,
LinkOnly: true,
},
{
Model: secondaryPickupAddress,
Type: &factory.Addresses.SecondaryPickupAddress,
LinkOnly: true,
},
{
Model: models.MTOShipment{
ShipmentType: models.MTOShipmentTypeHHGOutOfNTS,
},
},
}, nil)
clearShipmentIDFields(&shipment)

_, err := creator.CreateMTOShipment(suite.AppContextForTest(), &shipment)

suite.Error(err)
suite.Equal("Secondary pickup address cannot be created for shipment Type "+string(models.MTOShipmentTypeHHGOutOfNTS), err.Error())
suite.IsType(apperror.InvalidInputError{}, err)
})
}

// Clears all the ID fields that we need to be null for a new shipment to get created:
Expand Down
Loading