Skip to content

Commit

Permalink
Merge pull request #11945 from transcom/B-18191-Moved-Progear-Ticket-…
Browse files Browse the repository at this point in the history
…Query

B-18191 moved progear weight ticket delete query out of handler and into service
  • Loading branch information
pambecker authored Feb 8, 2024
2 parents b40c012 + 912f1c1 commit 8771b88
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
38 changes: 2 additions & 36 deletions pkg/handlers/internalapi/progear_weight_ticket.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package internalapi

import (
"database/sql"

"github.com/go-openapi/runtime/middleware"
"github.com/gofrs/uuid"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/db/utilities"
progearops "github.com/transcom/mymove/pkg/gen/internalapi/internaloperations/ppm"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/handlers/internalapi/internal/payloads"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)

Expand Down Expand Up @@ -159,39 +155,9 @@ func (h DeleteProGearWeightTicketHandler) Handle(params progearops.DeleteProGear

// Make sure the service member is not modifying another service member's PPM
ppmID := uuid.FromStringOrNil(params.PpmShipmentID.String())
var ppmShipment models.PPMShipment
err := appCtx.DB().Scope(utilities.ExcludeDeletedScope()).
EagerPreload(
"Shipment.MoveTaskOrder.Orders",
"ProgearWeightTickets",
).
Find(&ppmShipment, ppmID)
if err != nil {
if err == sql.ErrNoRows {
return progearops.NewDeleteWeightTicketNotFound(), err
}
return progearops.NewDeleteProGearWeightTicketInternalServerError(), err
}
if ppmShipment.Shipment.MoveTaskOrder.Orders.ServiceMemberID != appCtx.Session().ServiceMemberID {
wrongServiceMemberIDErr := apperror.NewSessionError("Attempted delete by wrong service member")
appCtx.Logger().Error("internalapi.DeleteProgearWeightTicketHandler", zap.Error(wrongServiceMemberIDErr))
return progearops.NewDeleteProGearWeightTicketForbidden(), wrongServiceMemberIDErr
}
progearWeightTicketID := uuid.FromStringOrNil(params.ProGearWeightTicketID.String())
found := false
for _, lineItem := range ppmShipment.ProgearWeightTickets {
if lineItem.ID == progearWeightTicketID {
found = true
break
}
}
if !found {
mismatchedPPMShipmentAndProgearWeightTicketIDErr := apperror.NewSessionError("Pro-gear weight ticket does not exist on ppm shipment")
appCtx.Logger().Error("internalapi.DeleteProGearWeightTicketHandler", zap.Error(mismatchedPPMShipmentAndProgearWeightTicketIDErr))
return progearops.NewDeleteProGearWeightTicketNotFound(), mismatchedPPMShipmentAndProgearWeightTicketIDErr
}

err = h.progearDeleter.DeleteProgearWeightTicket(appCtx, progearWeightTicketID)
progearWeightTicketID := uuid.FromStringOrNil(params.ProGearWeightTicketID.String())
err := h.progearDeleter.DeleteProgearWeightTicket(appCtx, ppmID, progearWeightTicketID)
if err != nil {
appCtx.Logger().Error("internalapi.DeleteProgearWeightTicketHandler", zap.Error(err))

Expand Down
1 change: 1 addition & 0 deletions pkg/handlers/internalapi/progear_weight_ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func (suite *HandlerSuite) TestDeleteProgearWeightTicketHandler() {
mockDeleter.On("DeleteProgearWeightTicket",
mock.AnythingOfType("*appcontext.appContext"),
mock.AnythingOfType("uuid.UUID"),
mock.AnythingOfType("uuid.UUID"),
).Return(err)

// Use createS3HandlerConfig for the HandlerConfig because we are required to upload a doc
Expand Down
10 changes: 5 additions & 5 deletions pkg/services/mocks/ProgearWeightTicketDeleter.go

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

2 changes: 1 addition & 1 deletion pkg/services/progear_weight_ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ type ProgearWeightTicketUpdater interface {
//
//go:generate mockery --name ProgearWeightTicketDeleter
type ProgearWeightTicketDeleter interface {
DeleteProgearWeightTicket(appCtx appcontext.AppContext, progearWeightTicketID uuid.UUID) error
DeleteProgearWeightTicket(appCtx appcontext.AppContext, ppmID uuid.UUID, progearWeightTicketID uuid.UUID) error
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package progearweightticket

import (
"database/sql"

"github.com/gofrs/uuid"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/db/utilities"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)

Expand All @@ -15,7 +20,40 @@ func NewProgearWeightTicketDeleter() services.ProgearWeightTicketDeleter {
return &progearWeightTicketDeleter{}
}

func (d *progearWeightTicketDeleter) DeleteProgearWeightTicket(appCtx appcontext.AppContext, progearWeightTicketID uuid.UUID) error {
func (d *progearWeightTicketDeleter) DeleteProgearWeightTicket(appCtx appcontext.AppContext, ppmID uuid.UUID, progearWeightTicketID uuid.UUID) error {
var ppmShipment models.PPMShipment
err := appCtx.DB().Scope(utilities.ExcludeDeletedScope()).
EagerPreload(
"Shipment.MoveTaskOrder.Orders",
"ProgearWeightTickets",
).
Find(&ppmShipment, ppmID)
if err != nil {
if err == sql.ErrNoRows {
return apperror.NewNotFoundError(progearWeightTicketID, "while looking for ProgearWeightTicket")
}
return apperror.NewQueryError("Progear Weight Ticket fetch original", err, "")
}

if ppmShipment.Shipment.MoveTaskOrder.Orders.ServiceMemberID != appCtx.Session().ServiceMemberID && !appCtx.Session().IsOfficeUser() {
wrongServiceMemberIDErr := apperror.NewForbiddenError("Attempted delete by wrong service member")
appCtx.Logger().Error("internalapi.DeleteProgearWeightTicketHandler", zap.Error(wrongServiceMemberIDErr))
return wrongServiceMemberIDErr
}

found := false
for _, lineItem := range ppmShipment.ProgearWeightTickets {
if lineItem.ID == progearWeightTicketID {
found = true
break
}
}
if !found {
mismatchedPPMShipmentAndProgearWeightTicketIDErr := apperror.NewNotFoundError(progearWeightTicketID, "Pro-gear weight ticket does not exist on ppm shipment")
appCtx.Logger().Error("internalapi.DeleteProGearWeightTicketHandler", zap.Error(mismatchedPPMShipmentAndProgearWeightTicketIDErr))
return mismatchedPPMShipmentAndProgearWeightTicketIDErr
}

progearWeightTicket, err := FetchProgearWeightTicketByIDExcludeDeletedUploads(appCtx, progearWeightTicketID)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (suite *ProgearWeightTicketSuite) TestDeleteProgearWeightTicket() {
notFoundProgearWeightTicketID := uuid.Must(uuid.NewV4())
deleter := NewProgearWeightTicketDeleter()

err := deleter.DeleteProgearWeightTicket(session, notFoundProgearWeightTicketID)
err := deleter.DeleteProgearWeightTicket(session, uuid.Nil, notFoundProgearWeightTicketID)

if suite.Error(err) {
suite.IsType(apperror.NotFoundError{}, err)
Expand All @@ -95,7 +95,7 @@ func (suite *ProgearWeightTicketSuite) TestDeleteProgearWeightTicket() {
deleter := NewProgearWeightTicketDeleter()

suite.Nil(originalProgearWeightTicket.DeletedAt)
err := deleter.DeleteProgearWeightTicket(session, originalProgearWeightTicket.ID)
err := deleter.DeleteProgearWeightTicket(session, originalProgearWeightTicket.PPMShipmentID, originalProgearWeightTicket.ID)
suite.NoError(err)

var progearWeightTicketInDB models.ProgearWeightTicket
Expand Down

0 comments on commit 8771b88

Please sign in to comment.