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

B-22766-MAIN Bulk assignment priority changes #14902

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
87 changes: 62 additions & 25 deletions pkg/services/move/move_assignment.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package move

import (
"container/list"

"github.com/gofrs/uuid"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/gen/ghcmessages"
"github.com/transcom/mymove/pkg/gen/internalmessages"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)
Expand All @@ -22,38 +25,72 @@ func (a moveAssigner) BulkMoveAssignment(appCtx appcontext.AppContext, queueType
return nil, apperror.NewBadDataError("No moves to assign")
}

var assign func(*models.Move, uuid.UUID)
switch queueType {
case string(models.QueueTypeCounseling), string(models.QueueTypeCloseout):
assign = func(move *models.Move, userID uuid.UUID) { move.SCAssignedID = &userID }
case string(models.QueueTypeTaskOrder):
assign = func(move *models.Move, userID uuid.UUID) { move.TOOAssignedID = &userID }
case string(models.QueueTypePaymentRequest):
assign = func(move *models.Move, userID uuid.UUID) { move.TIOAssignedID = &userID }
default:
return nil, apperror.NewBadDataError("Invalid queue type")
}

// make a map to track users and their assignment counts
// and a queue of userIDs
moveAssignments := make(map[uuid.UUID]int)
queue := list.New()
for _, user := range officeUserData {
if user != nil && user.MoveAssignments > 0 {
userID := uuid.FromStringOrNil(user.ID.String())
moveAssignments[userID] = int(user.MoveAssignments)
queue.PushBack(userID)
}
}

// point at the index in the movesToAssign set
moveIndex := 0

// keep track of the updatedMoves to batch save
updatedMoves := make([]models.Move, 0, len(movesToAssign))

transactionErr := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
for _, move := range movesToAssign {
for _, officeUser := range officeUserData {
if officeUser != nil && officeUser.MoveAssignments > 0 {
officeUserId := uuid.FromStringOrNil(officeUser.ID.String())

switch queueType {
case string(models.QueueTypeCounseling):
move.SCAssignedID = &officeUserId
case string(models.QueueTypeCloseout):
move.SCAssignedID = &officeUserId
case string(models.QueueTypeTaskOrder):
move.TOOAssignedID = &officeUserId
case string(models.QueueTypePaymentRequest):
move.TIOAssignedID = &officeUserId
}

officeUser.MoveAssignments -= 1

verrs, err := appCtx.DB().ValidateAndUpdate(&move)
if err != nil || verrs.HasAny() {
return apperror.NewInvalidInputError(move.ID, err, verrs, "")
}

break
}
// while we have a queue...
for moveIndex < len(movesToAssign) && queue.Len() > 0 {
// grab that ID off the front
user := queue.Front()
userID := user.Value.(uuid.UUID)
queue.Remove(user)

// do our assignment logic
move := movesToAssign[moveIndex]
ordersType := move.Orders.OrdersType
if ordersType != internalmessages.OrdersTypeSAFETY && ordersType != internalmessages.OrdersTypeBLUEBARK && ordersType != internalmessages.OrdersTypeWOUNDEDWARRIOR {
assign(&move, userID)
updatedMoves = append(updatedMoves, move)
}

// decrement the user's assignment count
moveAssignments[userID]--
moveIndex++

// If user still has remaining assignments, re-queue them
if moveAssignments[userID] > 0 {
queue.PushBack(userID)
}
}

return nil
})

if len(updatedMoves) > 0 {
verrs, err := appCtx.DB().ValidateAndUpdate(updatedMoves) // Bulk update
if err != nil || verrs.HasAny() {
return nil, apperror.NewInvalidInputError(uuid.Nil, err, verrs, "Bulk assignment failed")
}
}

if transactionErr != nil {
return nil, transactionErr
}
Expand Down
137 changes: 137 additions & 0 deletions pkg/services/move/move_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,143 @@ func (suite *MoveServiceSuite) TestBulkMoveAssignment() {
return transportationOffice, move1, move2, move3
}

suite.Run("properly distributes moves", func() {
transportationOffice, move1, move2, move3 := setupTestData()
move4 := factory.BuildMoveWithShipment(suite.DB(), []factory.Customization{
{
Model: models.Move{
Status: models.MoveStatusNeedsServiceCounseling,
},
},
{
Model: transportationOffice,
LinkOnly: true,
Type: &factory.TransportationOffices.CounselingOffice,
},
}, nil)
move5 := factory.BuildMoveWithShipment(suite.DB(), []factory.Customization{
{
Model: models.Move{
Status: models.MoveStatusNeedsServiceCounseling,
},
},
{
Model: transportationOffice,
LinkOnly: true,
Type: &factory.TransportationOffices.CounselingOffice,
},
}, nil)
move6 := factory.BuildMoveWithShipment(suite.DB(), []factory.Customization{
{
Model: models.Move{
Status: models.MoveStatusNeedsServiceCounseling,
},
},
{
Model: transportationOffice,
LinkOnly: true,
Type: &factory.TransportationOffices.CounselingOffice,
},
}, nil)

officeUser1 := factory.BuildOfficeUserWithPrivileges(suite.DB(), []factory.Customization{
{
Model: models.OfficeUser{
Email: "officeuser1@example.com",
Active: true,
},
},
{
Model: transportationOffice,
LinkOnly: true,
Type: &factory.TransportationOffices.CounselingOffice,
},
{
Model: models.User{
Privileges: []models.Privilege{
{
PrivilegeType: models.PrivilegeTypeSupervisor,
},
},
Roles: []roles.Role{
{
RoleType: roles.RoleTypeServicesCounselor,
},
},
},
},
}, nil)
officeUser2 := factory.BuildOfficeUserWithPrivileges(suite.DB(), []factory.Customization{
{
Model: models.OfficeUser{
Email: "officeuser2@example.com",
Active: true,
},
},
{
Model: transportationOffice,
LinkOnly: true,
Type: &factory.TransportationOffices.CounselingOffice,
},
{
Model: models.User{
Roles: []roles.Role{
{
RoleType: roles.RoleTypeServicesCounselor,
},
},
},
},
}, nil)
officeUser3 := factory.BuildOfficeUserWithPrivileges(suite.DB(), []factory.Customization{
{
Model: models.OfficeUser{
Email: "officeuser3@example.com",
Active: true,
},
},
{
Model: transportationOffice,
LinkOnly: true,
Type: &factory.TransportationOffices.CounselingOffice,
},
{
Model: models.User{
Roles: []roles.Role{
{
RoleType: roles.RoleTypeServicesCounselor,
},
},
},
},
}, nil)

moves := []models.Move{move1, move2, move3, move4, move5, move6}
userData := []*ghcmessages.BulkAssignmentForUser{
{ID: strfmt.UUID(officeUser1.ID.String()), MoveAssignments: 1},
{ID: strfmt.UUID(officeUser2.ID.String()), MoveAssignments: 2},
{ID: strfmt.UUID(officeUser3.ID.String()), MoveAssignments: 3},
}

_, err := moveAssigner.BulkMoveAssignment(suite.AppContextForTest(), string(models.QueueTypeCounseling), userData, moves)
suite.NoError(err)

// reload move data to check assigned
suite.NoError(suite.DB().Reload(&move1))
suite.NoError(suite.DB().Reload(&move2))
suite.NoError(suite.DB().Reload(&move3))
suite.NoError(suite.DB().Reload(&move4))
suite.NoError(suite.DB().Reload(&move5))
suite.NoError(suite.DB().Reload(&move6))

suite.Equal(officeUser1.ID, *move1.SCAssignedID)
suite.Equal(officeUser2.ID, *move2.SCAssignedID)
suite.Equal(officeUser3.ID, *move3.SCAssignedID)
suite.Equal(officeUser2.ID, *move4.SCAssignedID)
suite.Equal(officeUser3.ID, *move5.SCAssignedID)
suite.Equal(officeUser3.ID, *move6.SCAssignedID)
})

suite.Run("successfully assigns multiple counseling moves to a SC user", func() {
transportationOffice, move1, move2, move3 := setupTestData()

Expand Down
7 changes: 4 additions & 3 deletions pkg/services/move/move_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ func (f moveFetcher) FetchMove(appCtx appcontext.AppContext, locator string, sea
func (f moveFetcher) FetchMovesByIdArray(appCtx appcontext.AppContext, moveIds []ghcmessages.BulkAssignmentMoveData) (models.Moves, error) {
moves := models.Moves{}

err := appCtx.DB().Q().
Where("id in (?)", moveIds).
query := appCtx.DB().Q().EagerPreload("Orders").
Where("show = TRUE").
All(&moves)
Where("id in (?)", moveIds).
Order("ARRAY_POSITION(ARRAY[?]::uuid[], id)", moveIds)
err := query.All(&moves)

if err != nil {
return nil, err
Expand Down
Loading