Skip to content

Commit

Permalink
Merge branch 'main' into B-21997-UI-Order
Browse files Browse the repository at this point in the history
  • Loading branch information
antgmann authored Jan 8, 2025
2 parents 1817b65 + c3d53aa commit 61148c2
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 22 deletions.
20 changes: 20 additions & 0 deletions pkg/handlers/primeapi/mto_service_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ func (h CreateMTOServiceItemHandler) Handle(params mtoserviceitemops.CreateMTOSe
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {

// ** Create service item can not be done for PPM shipment **/
shipment, err := models.FetchShipmentByID(appCtx.DB(), uuid.FromStringOrNil(params.Body.MtoShipmentID().String()))
if err != nil {
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler Error Fetch Shipment", zap.Error(err))
switch err {
case models.ErrFetchNotFound:
return mtoserviceitemops.NewCreateMTOServiceItemNotFound().WithPayload(payloads.ClientError(handlers.NotFoundMessage, "Fetch Shipment", h.GetTraceIDFromRequest(params.HTTPRequest))), err
default:
return mtoserviceitemops.NewCreateMTOServiceItemInternalServerError().WithPayload(payloads.InternalServerError(nil, h.GetTraceIDFromRequest(params.HTTPRequest))), err
}
}

if shipment.ShipmentType == models.MTOShipmentTypePPM {
verrs := validate.NewErrors()
verrs.Add("mtoShipmentID", params.Body.MtoShipmentID().String())
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler - Create Service Item is not allowed for PPM shipments", zap.Error(verrs))
return mtoserviceitemops.NewCreateMTOServiceItemUnprocessableEntity().WithPayload(payloads.ValidationError(
"Create Service Item is not allowed for PPM shipments", h.GetTraceIDFromRequest(params.HTTPRequest), verrs)), verrs
}

/** Feature Flag - Alaska **/
isAlaskaEnabled := false
featureFlagName := "enable_alaska"
Expand Down
97 changes: 90 additions & 7 deletions pkg/handlers/primeapi/mto_service_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,33 @@ func (suite *HandlerSuite) TestCreateMTOServiceItemHandler() {
mtoServiceItem models.MTOServiceItem
}

makeSubtestData := func() (subtestData *localSubtestData) {
makeSubtestDataWithPPMShipmentType := func(isPPM bool) (subtestData *localSubtestData) {
subtestData = &localSubtestData{}
mtoShipmentID, _ := uuid.NewV4()

mto := factory.BuildAvailableToPrimeMove(suite.DB(), nil, nil)
subtestData.mtoShipment = factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: mto,
LinkOnly: true,
},
}, nil)
if isPPM {
subtestData.mtoShipment = factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: mto,
LinkOnly: true,
},
{
Model: models.MTOShipment{
ID: mtoShipmentID,
ShipmentType: models.MTOShipmentTypePPM,
},
},
}, nil)
} else {
subtestData.mtoShipment = factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: mto,
LinkOnly: true,
},
}, nil)
}

factory.FetchReServiceByCode(suite.DB(), models.ReServiceCodeDOFSIT)
req := httptest.NewRequest("POST", "/mto-service-items", nil)
sitEntryDate := time.Now()
Expand Down Expand Up @@ -86,6 +103,10 @@ func (suite *HandlerSuite) TestCreateMTOServiceItemHandler() {
return subtestData
}

makeSubtestData := func() (subtestData *localSubtestData) {
return makeSubtestDataWithPPMShipmentType(false)
}

suite.Run("Successful POST - Integration Test", func() {
subtestData := makeSubtestData()
moveRouter := moverouter.NewMoveRouter()
Expand Down Expand Up @@ -427,6 +448,68 @@ func (suite *HandlerSuite) TestCreateMTOServiceItemHandler() {
// Validate outgoing payload
suite.NoError(responsePayload.Validate(strfmt.Default))
})

suite.Run("POST failure - Shipment fetch not found", func() {
subtestData := makeSubtestDataWithPPMShipmentType(true)
moveRouter := moverouter.NewMoveRouter()
planner := &routemocks.Planner{}
planner.On("ZipTransitDistance",
mock.AnythingOfType("*appcontext.appContext"),
mock.Anything,
mock.Anything,
).Return(400, nil)
creator := mtoserviceitem.NewMTOServiceItemCreator(planner, builder, moveRouter, ghcrateengine.NewDomesticUnpackPricer(), ghcrateengine.NewDomesticPackPricer(), ghcrateengine.NewDomesticLinehaulPricer(), ghcrateengine.NewDomesticShorthaulPricer(), ghcrateengine.NewDomesticOriginPricer(), ghcrateengine.NewDomesticDestinationPricer(), ghcrateengine.NewFuelSurchargePricer())
handler := CreateMTOServiceItemHandler{
suite.HandlerConfig(),
creator,
mtoChecker,
}

// Validate incoming payload
suite.NoError(subtestData.params.Body.Validate(strfmt.Default))

// we are going to mock fake UUID to force NOT FOUND ERROR
subtestData.params.Body.SetMtoShipmentID(subtestData.params.Body.ID())

response := handler.Handle(subtestData.params)
suite.IsType(&mtoserviceitemops.CreateMTOServiceItemNotFound{}, response)
typedResponse := response.(*mtoserviceitemops.CreateMTOServiceItemNotFound)

// Validate outgoing payload
suite.NoError(typedResponse.Payload.Validate(strfmt.Default))

suite.Contains(*typedResponse.Payload.Detail, "Fetch Shipment")
})

suite.Run("POST failure - 422 - PPM not allowed to create service item", func() {
subtestData := makeSubtestDataWithPPMShipmentType(true)
moveRouter := moverouter.NewMoveRouter()
planner := &routemocks.Planner{}
planner.On("ZipTransitDistance",
mock.AnythingOfType("*appcontext.appContext"),
mock.Anything,
mock.Anything,
).Return(400, nil)
creator := mtoserviceitem.NewMTOServiceItemCreator(planner, builder, moveRouter, ghcrateengine.NewDomesticUnpackPricer(), ghcrateengine.NewDomesticPackPricer(), ghcrateengine.NewDomesticLinehaulPricer(), ghcrateengine.NewDomesticShorthaulPricer(), ghcrateengine.NewDomesticOriginPricer(), ghcrateengine.NewDomesticDestinationPricer(), ghcrateengine.NewFuelSurchargePricer())
handler := CreateMTOServiceItemHandler{
suite.HandlerConfig(),
creator,
mtoChecker,
}

// Validate incoming payload
suite.NoError(subtestData.params.Body.Validate(strfmt.Default))

response := handler.Handle(subtestData.params)
suite.IsType(&mtoserviceitemops.CreateMTOServiceItemUnprocessableEntity{}, response)
typedResponse := response.(*mtoserviceitemops.CreateMTOServiceItemUnprocessableEntity)

// Validate outgoing payload
suite.NoError(typedResponse.Payload.Validate(strfmt.Default))

suite.Contains(*typedResponse.Payload.Detail, "Create Service Item is not allowed for PPM shipments")
suite.Contains(typedResponse.Payload.InvalidFields["mtoShipmentID"][0], subtestData.params.Body.MtoShipmentID().String())
})
}

func (suite *HandlerSuite) TestCreateMTOServiceItemDomesticCratingHandler() {
Expand Down
20 changes: 20 additions & 0 deletions pkg/handlers/primeapiv2/mto_service_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ func (h CreateMTOServiceItemHandler) Handle(params mtoserviceitemops.CreateMTOSe
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {

// ** Create service item can not be done for PPM shipment **/
shipment, err := models.FetchShipmentByID(appCtx.DB(), uuid.FromStringOrNil(params.Body.MtoShipmentID().String()))
if err != nil {
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler.v2 Error Fetch Shipment", zap.Error(err))
switch err {
case models.ErrFetchNotFound:
return mtoserviceitemops.NewCreateMTOServiceItemNotFound().WithPayload(primeapipayloads.ClientError(handlers.NotFoundMessage, "Fetch Shipment", h.GetTraceIDFromRequest(params.HTTPRequest))), err
default:
return mtoserviceitemops.NewCreateMTOServiceItemInternalServerError().WithPayload(primeapipayloads.InternalServerError(nil, h.GetTraceIDFromRequest(params.HTTPRequest))), err
}
}

if shipment.ShipmentType == models.MTOShipmentTypePPM {
verrs := validate.NewErrors()
verrs.Add("mtoShipmentID", params.Body.MtoShipmentID().String())
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler.v2 - Create Service Item is not allowed for PPM shipments", zap.Error(verrs))
return mtoserviceitemops.NewCreateMTOServiceItemUnprocessableEntity().WithPayload(primeapipayloads.ValidationError(
"Create Service Item is not allowed for PPM shipments", h.GetTraceIDFromRequest(params.HTTPRequest), verrs)), verrs
}

/** Feature Flag - Alaska **/
isAlaskaEnabled := false
featureFlagName := "enable_alaska"
Expand Down
97 changes: 90 additions & 7 deletions pkg/handlers/primeapiv2/mto_service_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,33 @@ func (suite *HandlerSuite) TestCreateMTOServiceItemHandler() {
mtoServiceItem models.MTOServiceItem
}

makeSubtestData := func() (subtestData *localSubtestData) {
makeSubtestDataWithPPMShipmentType := func(isPPM bool) (subtestData *localSubtestData) {
subtestData = &localSubtestData{}

mtoShipmentID, _ := uuid.NewV4()

mto := factory.BuildAvailableToPrimeMove(suite.DB(), nil, nil)
subtestData.mtoShipment = factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: mto,
LinkOnly: true,
},
}, nil)
if isPPM {
subtestData.mtoShipment = factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: mto,
LinkOnly: true,
},
{
Model: models.MTOShipment{
ID: mtoShipmentID,
ShipmentType: models.MTOShipmentTypePPM,
},
},
}, nil)
} else {
subtestData.mtoShipment = factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: mto,
LinkOnly: true,
},
}, nil)
}
factory.FetchReServiceByCode(suite.DB(), models.ReServiceCodeDOFSIT)
req := httptest.NewRequest("POST", "/mto-service-items", nil)
sitEntryDate := time.Now()
Expand Down Expand Up @@ -80,6 +97,10 @@ func (suite *HandlerSuite) TestCreateMTOServiceItemHandler() {
return subtestData
}

makeSubtestData := func() (subtestData *localSubtestData) {
return makeSubtestDataWithPPMShipmentType(false)
}

suite.Run("Successful POST - Integration Test", func() {
subtestData := makeSubtestData()
moveRouter := moverouter.NewMoveRouter()
Expand Down Expand Up @@ -421,6 +442,68 @@ func (suite *HandlerSuite) TestCreateMTOServiceItemHandler() {
// Validate outgoing payload
suite.NoError(responsePayload.Validate(strfmt.Default))
})

suite.Run("POST failure - Shipment fetch not found", func() {
subtestData := makeSubtestDataWithPPMShipmentType(true)
moveRouter := moverouter.NewMoveRouter()
planner := &routemocks.Planner{}
planner.On("ZipTransitDistance",
mock.AnythingOfType("*appcontext.appContext"),
mock.Anything,
mock.Anything,
).Return(400, nil)
creator := mtoserviceitem.NewMTOServiceItemCreator(planner, builder, moveRouter, ghcrateengine.NewDomesticUnpackPricer(), ghcrateengine.NewDomesticPackPricer(), ghcrateengine.NewDomesticLinehaulPricer(), ghcrateengine.NewDomesticShorthaulPricer(), ghcrateengine.NewDomesticOriginPricer(), ghcrateengine.NewDomesticDestinationPricer(), ghcrateengine.NewFuelSurchargePricer())
handler := CreateMTOServiceItemHandler{
suite.HandlerConfig(),
creator,
mtoChecker,
}

// Validate incoming payload
suite.NoError(subtestData.params.Body.Validate(strfmt.Default))

// we are going to mock fake UUID to force NOT FOUND ERROR
subtestData.params.Body.SetMtoShipmentID(subtestData.params.Body.ID())

response := handler.Handle(subtestData.params)
suite.IsType(&mtoserviceitemops.CreateMTOServiceItemNotFound{}, response)
typedResponse := response.(*mtoserviceitemops.CreateMTOServiceItemNotFound)

// Validate outgoing payload
suite.NoError(typedResponse.Payload.Validate(strfmt.Default))

suite.Contains(*typedResponse.Payload.Detail, "Fetch Shipment")
})

suite.Run("POST failure - 422 - PPM not allowed to create service item", func() {
subtestData := makeSubtestDataWithPPMShipmentType(true)
moveRouter := moverouter.NewMoveRouter()
planner := &routemocks.Planner{}
planner.On("ZipTransitDistance",
mock.AnythingOfType("*appcontext.appContext"),
mock.Anything,
mock.Anything,
).Return(400, nil)
creator := mtoserviceitem.NewMTOServiceItemCreator(planner, builder, moveRouter, ghcrateengine.NewDomesticUnpackPricer(), ghcrateengine.NewDomesticPackPricer(), ghcrateengine.NewDomesticLinehaulPricer(), ghcrateengine.NewDomesticShorthaulPricer(), ghcrateengine.NewDomesticOriginPricer(), ghcrateengine.NewDomesticDestinationPricer(), ghcrateengine.NewFuelSurchargePricer())
handler := CreateMTOServiceItemHandler{
suite.HandlerConfig(),
creator,
mtoChecker,
}

// Validate incoming payload
suite.NoError(subtestData.params.Body.Validate(strfmt.Default))

response := handler.Handle(subtestData.params)
suite.IsType(&mtoserviceitemops.CreateMTOServiceItemUnprocessableEntity{}, response)
typedResponse := response.(*mtoserviceitemops.CreateMTOServiceItemUnprocessableEntity)

// Validate outgoing payload
suite.NoError(typedResponse.Payload.Validate(strfmt.Default))

suite.Contains(*typedResponse.Payload.Detail, "Create Service Item is not allowed for PPM shipments")
suite.Contains(typedResponse.Payload.InvalidFields["mtoShipmentID"][0], subtestData.params.Body.MtoShipmentID().String())
})
}

func (suite *HandlerSuite) TestCreateMTOServiceItemDomesticCratingHandler() {
Expand Down
22 changes: 21 additions & 1 deletion pkg/handlers/primeapiv3/mto_service_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ func (h CreateMTOServiceItemHandler) Handle(params mtoserviceitemops.CreateMTOSe
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {

// ** Create service item can not be done for PPM shipment **/
shipment, err := models.FetchShipmentByID(appCtx.DB(), uuid.FromStringOrNil(params.Body.MtoShipmentID().String()))
if err != nil {
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler.v3 Error Fetch Shipment", zap.Error(err))
switch err {
case models.ErrFetchNotFound:
return mtoserviceitemops.NewCreateMTOServiceItemNotFound().WithPayload(primeapipayloads.ClientError(handlers.NotFoundMessage, "Fetch Shipment", h.GetTraceIDFromRequest(params.HTTPRequest))), err
default:
return mtoserviceitemops.NewCreateMTOServiceItemInternalServerError().WithPayload(primeapipayloads.InternalServerError(nil, h.GetTraceIDFromRequest(params.HTTPRequest))), err
}
}

if shipment.ShipmentType == models.MTOShipmentTypePPM {
verrs := validate.NewErrors()
verrs.Add("mtoShipmentID", params.Body.MtoShipmentID().String())
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler.v3 - Create Service Item is not allowed for PPM shipments", zap.Error(verrs))
return mtoserviceitemops.NewCreateMTOServiceItemUnprocessableEntity().WithPayload(primeapipayloads.ValidationError(
"Create Service Item is not allowed for PPM shipments", h.GetTraceIDFromRequest(params.HTTPRequest), verrs)), verrs
}

/** Feature Flag - Alaska **/
isAlaskaEnabled := false
featureFlagName := "enable_alaska"
Expand All @@ -65,7 +85,7 @@ func (h CreateMTOServiceItemHandler) Handle(params mtoserviceitemops.CreateMTOSe
verrs := validate.NewErrors()
verrs.Add("modelType", fmt.Sprintf("allowed modelType() %v", mapKeys))

appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler error", zap.Error(verrs))
appCtx.Logger().Error("primeapi.CreateMTOServiceItemHandler.v3 error", zap.Error(verrs))
return mtoserviceitemops.NewCreateMTOServiceItemUnprocessableEntity().WithPayload(primeapipayloads.ValidationError(
detailErr, h.GetTraceIDFromRequest(params.HTTPRequest), verrs)), verrs
}
Expand Down
Loading

0 comments on commit 61148c2

Please sign in to comment.