Skip to content

Commit

Permalink
Merge branch 'main' into MAIN-B-22692
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmanley-caci authored Mar 7, 2025
2 parents cd2f180 + dca9ab2 commit 208ef0d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ stages:
- unset AWS_SECRET_ACCESS_KEY
- unset AWS_ACCOUNT_ID
- unset AWS_DEFAULT_REGION
- export AWS_DEFAULT_REGION=$COM_REGION
- unset AWS_REGION
- export AWS_REGION=$COM_REGION
- export AWS_ACCOUNT_ID=$DEV_ACCOUNT_ID
- export AWS_ACCESS_KEY_ID=$DEV_ACCESS_KEY_ID
- export AWS_SECRET_ACCESS_KEY=$DEV_SECRET_KEY
Expand Down
15 changes: 8 additions & 7 deletions pkg/services/lock_move/move_locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (m moveLocker) LockMove(appCtx appcontext.AppContext, move *models.Move, of
EagerPreload("Address", "Address.Country").
First(&transportationOffice)

if err != nil {
return nil, err
}

if move.LockedByOfficeUserID != models.UUIDPointer(officeUserID) {
move.LockedByOfficeUserID = models.UUIDPointer(officeUserID)
}
Expand All @@ -60,14 +64,11 @@ func (m moveLocker) LockMove(appCtx appcontext.AppContext, move *models.Move, of
expirationTime := now.Add(30 * time.Minute)
move.LockExpiresAt = &expirationTime

transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
verrs, saveErr := appCtx.DB().ValidateAndSave(move)
if verrs != nil && verrs.HasAny() {
invalidInputError := apperror.NewInvalidInputError(move.ID, nil, verrs, "Could not validate move while locking it.")
// Store move before update
var moveBeforeUpdate = *move

return invalidInputError
}
if saveErr != nil {
transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
if err := appCtx.DB().RawQuery("UPDATE moves SET locked_by=?, lock_expires_at=?, updated_at=? WHERE id=?", officeUserID, expirationTime, moveBeforeUpdate.UpdatedAt, move.ID).Exec(); err != nil {
return err
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/services/lock_move/move_locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ func (suite *MoveLockerServiceSuite) TestLockMove() {
suite.Equal(move.LockedByOfficeUser.TransportationOffice.Name, tooUser.TransportationOffice.Name)
suite.Equal(move.LockExpiresAt.Truncate(time.Minute), expirationTime)
})

suite.Run("locking a move doesn't change the moves updated_at value", func() {
tooUser := factory.BuildOfficeUserWithRoles(suite.DB(), nil, []roles.RoleType{roles.RoleTypeTOO})
appCtx := suite.AppContextWithSessionForTest(&auth.Session{
ApplicationName: auth.OfficeApp,
Roles: tooUser.User.Roles,
OfficeUserID: tooUser.ID,
IDToken: "fake_token",
AccessToken: "fakeAccessToken",
})

move := factory.BuildMove(suite.DB(), nil, nil)

actualMove, err := moveLocker.LockMove(appCtx, &move, tooUser.ID)
suite.FatalNoError(err)

suite.Equal(move.ID, actualMove.ID)
suite.Equal(move.LockedByOfficeUserID, &tooUser.ID)
suite.Equal(move.LockedByOfficeUser.TransportationOffice.Name, tooUser.TransportationOffice.Name)
suite.Equal(actualMove.UpdatedAt, move.UpdatedAt)
})
}
13 changes: 5 additions & 8 deletions pkg/services/lock_move/move_unlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ func (m moveUnlocker) UnlockMove(appCtx appcontext.AppContext, move *models.Move
move.LockedByOfficeUser = nil
}

transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
verrs, saveErr := appCtx.DB().ValidateAndSave(move)
if verrs != nil && verrs.HasAny() {
invalidInputError := apperror.NewInvalidInputError(move.ID, nil, verrs, "Could not validate move while unlocking it.")
// Store move before update
var moveBeforeUpdate = *move

return invalidInputError
}
if saveErr != nil {
return saveErr
transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
if err := appCtx.DB().RawQuery("UPDATE moves SET locked_by=?, lock_expires_at=?, updated_at=? WHERE id=?", nil, nil, moveBeforeUpdate.UpdatedAt, move.ID).Exec(); err != nil {
return err
}

return nil
Expand Down
23 changes: 23 additions & 0 deletions pkg/services/lock_move/move_unlocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ func (suite *MoveLockerServiceSuite) TestCheckForLockedMovesAndUnlock() {
suite.Nil(moveThreeInDB.LockedByOfficeUser)
suite.Nil(moveThreeInDB.LockExpiresAt)
})

suite.Run("successfully unlock move without changing updated_at", func() {
tooUser := factory.BuildOfficeUserWithRoles(suite.DB(), nil, []roles.RoleType{roles.RoleTypeTOO})
appCtx := suite.AppContextWithSessionForTest(&auth.Session{
ApplicationName: auth.OfficeApp,
Roles: tooUser.User.Roles,
OfficeUserID: tooUser.ID,
IDToken: "fake_token",
AccessToken: "fakeAccessToken",
})

move := factory.BuildMove(suite.DB(), nil, nil)

lockedMove, err := moveLocker.LockMove(appCtx, &move, tooUser.ID)
suite.FatalNoError(err)
suite.Equal(move.ID, lockedMove.ID)
suite.Equal(lockedMove.LockedByOfficeUserID, &tooUser.ID)
suite.Equal(lockedMove.UpdatedAt, move.UpdatedAt)

unlockedMove, err := moveUnlocker.UnlockMove(appCtx, lockedMove, tooUser.ID)
suite.FatalNoError(err)
suite.Equal(unlockedMove.UpdatedAt, move.UpdatedAt)
})
}

0 comments on commit 208ef0d

Please sign in to comment.