Skip to content

Commit

Permalink
Merge pull request #12732 from transcom/B-18545-Safety-Queue-and-Labels
Browse files Browse the repository at this point in the history
B-18545 safety queue and labels
  • Loading branch information
pambecker authored Jun 12, 2024
2 parents 5da6cd8 + 7dd8249 commit c4f2ce7
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 91 deletions.
12 changes: 11 additions & 1 deletion pkg/services/move/move_searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/validate/v3"
"github.com/gofrs/uuid"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
Expand Down Expand Up @@ -38,12 +39,17 @@ func (s moveSearcher) SearchMoves(appCtx appcontext.AppContext, params *services
return models.Moves{}, 0, apperror.NewInvalidInputError(uuid.Nil, nil, verrs, "")
}

privileges, err := models.FetchPrivilegesForUser(appCtx.DB(), appCtx.Session().UserID)
if err != nil {
appCtx.Logger().Error("Error retreiving user privileges", zap.Error(err))
}

// The SQL % operator filters out strings that are below this similarity threshold
// We have to set it here because other areas of the code that do a trigram search
// (eg Duty Location search) may set a different threshold.
// If the threshold is too high, we may filter out too many results and make searching harder.
// If it's too low, the query will get slower/more memory intensive.
err := appCtx.DB().RawQuery("SET pg_trgm.similarity_threshold = 0.1").Exec()
err = appCtx.DB().RawQuery("SET pg_trgm.similarity_threshold = 0.1").Exec()
if err != nil {
return nil, 0, err
}
Expand All @@ -65,6 +71,10 @@ func (s moveSearcher) SearchMoves(appCtx appcontext.AppContext, params *services
GroupBy("moves.id", "service_members.id", "origin_addresses.id", "new_addresses.id").
Where("show = TRUE")

if !privileges.HasPrivilege(models.PrivilegeTypeSafety) {
query.Where("orders.orders_type != (?)", "SAFETY")
}

customerNameQuery := customerNameSearch(params.CustomerName)
locatorQuery := locatorFilter(params.Locator)
dodIDQuery := dodIDFilter(params.DodID)
Expand Down
34 changes: 32 additions & 2 deletions pkg/services/office_user/customer/customer_searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/validate/v3"
"github.com/gofrs/uuid"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
Expand Down Expand Up @@ -41,11 +42,40 @@ func (s customerSearcher) SearchCustomers(appCtx appcontext.AppContext, params *
return nil, 0, err
}

privileges, err := models.FetchPrivilegesForUser(appCtx.DB(), appCtx.Session().UserID)
if err != nil {
appCtx.Logger().Error("Error retreiving user privileges", zap.Error(err))
}

var query *pop.Query

if appCtx.Session().Roles.HasRole(roles.RoleTypeServicesCounselor) {
query = appCtx.DB().Q().
Join("users", "users.id = service_members.user_id")
rawquery := `SELECT DISTINCT ON (id)
service_members.affiliation, service_members.backup_mailing_address_id,
service_members.cac_validated, service_members.created_at, service_members.edipi,
service_members.email_is_preferred, service_members.emplid,
service_members.first_name, service_members.id, service_members.last_name,
service_members.middle_name, service_members.personal_email,
service_members.phone_is_preferred, service_members.residential_address_id,
service_members.secondary_telephone, service_members.suffix,
service_members.telephone, service_members.updated_at, service_members.user_id
FROM service_members AS service_members
JOIN users ON users.id = service_members.user_id
LEFT JOIN orders ON orders.service_member_id = service_members.id`

if !privileges.HasPrivilege(models.PrivilegeTypeSafety) {
rawquery += ` WHERE ((orders.orders_type != 'SAFETY' or orders.orders_type IS NULL) AND`
} else {
rawquery += ` WHERE (`
}

if params.DodID != nil {
rawquery += ` service_members.edipi = $1)`
query = appCtx.DB().RawQuery(rawquery, params.DodID)
} else {
rawquery += ` f_unaccent(lower($1)) % searchable_full_name(first_name, last_name))`
query = appCtx.DB().RawQuery(rawquery, params.CustomerName)
}
}

customerNameQuery := customerNameSearch(params.CustomerName)
Expand Down
38 changes: 34 additions & 4 deletions pkg/services/office_user/customer/customer_searcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (suite CustomerServiceSuite) TestCustomerSearch() {
serviceMember1 := factory.BuildServiceMember(suite.DB(), []factory.Customization{
{
Model: models.ServiceMember{
FirstName: models.StringPointer("Page"),
FirstName: models.StringPointer("Page1"),
LastName: models.StringPointer("McConnell"),
Edipi: models.StringPointer("1018231018"),
},
Expand All @@ -152,15 +152,15 @@ func (suite CustomerServiceSuite) TestCustomerSearch() {
serviceMember2 := factory.BuildServiceMember(suite.DB(), []factory.Customization{
{
Model: models.ServiceMember{
FirstName: models.StringPointer("Page"),
FirstName: models.StringPointer("Page2"),
LastName: models.StringPointer("McConnell"),
Edipi: models.StringPointer("8121581215"),
},
},
}, nil)
// get first page
customers, totalCount, err := searcher.SearchCustomers(suite.AppContextWithSessionForTest(&session), &services.SearchCustomersParams{
CustomerName: models.StringPointer("Page McConnell"),
CustomerName: models.StringPointer("Page1 McConnell"),
PerPage: 1,
Page: 1,
})
Expand All @@ -171,7 +171,7 @@ func (suite CustomerServiceSuite) TestCustomerSearch() {

// get second page
customers, totalCount, err = searcher.SearchCustomers(suite.AppContextWithSessionForTest(&session), &services.SearchCustomersParams{
CustomerName: models.StringPointer("Page McConnell"),
CustomerName: models.StringPointer("Page2 McConnell"),
PerPage: 1,
Page: 2,
})
Expand All @@ -180,4 +180,34 @@ func (suite CustomerServiceSuite) TestCustomerSearch() {
suite.Equal(serviceMember2.Edipi, customers[0].Edipi)
suite.Equal(2, totalCount)
})

suite.Run("search does not return safety moves for those without privileges", func() {
officeUser := factory.BuildOfficeUserWithRoles(suite.DB(), nil, []roles.RoleType{roles.RoleTypeServicesCounselor})
session := auth.Session{
ApplicationName: auth.OfficeApp,
Roles: officeUser.User.Roles,
OfficeUserID: officeUser.ID,
IDToken: "fake_token",
AccessToken: "fakeAccessToken",
}

serviceMember := factory.BuildMove(suite.DB(), []factory.Customization{
{
Model: models.ServiceMember{
FirstName: models.StringPointer("Page"),
LastName: models.StringPointer("McConnell"),
Edipi: models.StringPointer("1018231018"),
},
},
{
Model: models.Order{
OrdersType: "SAFETY",
},
},
}, nil)

customers, _, err := searcher.SearchCustomers(suite.AppContextWithSessionForTest(&session), &services.SearchCustomersParams{DodID: serviceMember.Orders.ServiceMember.Edipi})
suite.NoError(err)
suite.Len(customers, 0)
})
}
17 changes: 17 additions & 0 deletions pkg/services/order/order_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
Expand Down Expand Up @@ -39,6 +40,11 @@ func (f orderFetcher) ListOrders(appCtx appcontext.AppContext, officeUserID uuid
return []models.Move{}, 0, err
}

privileges, err := models.FetchPrivilegesForUser(appCtx.DB(), appCtx.Session().UserID)
if err != nil {
appCtx.Logger().Error("Error retreiving user privileges", zap.Error(err))
}

officeUserGbloc := transportationOffice.Gbloc

// Alright let's build our query based on the filters we got from the handler. These use the FilterOption type above.
Expand Down Expand Up @@ -117,6 +123,7 @@ func (f orderFetcher) ListOrders(appCtx appcontext.AppContext, officeUserID uuid
"Orders.NewDutyLocation.Address",
"Orders.OriginDutyLocation.Address",
"Orders.Entitlement",
"Orders.OrdersType",
"MTOShipments.PPMShipment",
"LockedByOfficeUser",
).InnerJoin("orders", "orders.id = moves.orders_id").
Expand All @@ -127,6 +134,10 @@ func (f orderFetcher) ListOrders(appCtx appcontext.AppContext, officeUserID uuid
LeftJoin("duty_locations as dest_dl", "dest_dl.id = orders.new_duty_location_id").
LeftJoin("office_users", "office_users.id = moves.locked_by").
Where("show = ?", models.BoolPointer(true))

if !privileges.HasPrivilege(models.PrivilegeTypeSafety) {
query.Where("orders.orders_type != (?)", "SAFETY")
}
} else {
query = appCtx.DB().Q().Scope(utilities.ExcludeDeletedScope(models.MTOShipment{})).EagerPreload(
"Orders.ServiceMember",
Expand All @@ -135,6 +146,7 @@ func (f orderFetcher) ListOrders(appCtx appcontext.AppContext, officeUserID uuid
// See note further below about having to do this in a separate Load call due to a Pop issue.
// "Orders.OriginDutyLocation.TransportationOffice",
"Orders.Entitlement",
"Orders.OrdersType",
"MTOShipments",
"MTOServiceItems",
"ShipmentGBLOC",
Expand All @@ -153,6 +165,11 @@ func (f orderFetcher) ListOrders(appCtx appcontext.AppContext, officeUserID uuid
LeftJoin("duty_locations as dest_dl", "dest_dl.id = orders.new_duty_location_id").
LeftJoin("office_users", "office_users.id = moves.locked_by").
Where("show = ?", models.BoolPointer(true))

if !privileges.HasPrivilege(models.PrivilegeTypeSafety) {
query.Where("orders.orders_type != (?)", "SAFETY")
}

if params.NeedsPPMCloseout != nil {
if *params.NeedsPPMCloseout {
query.InnerJoin("ppm_shipments", "ppm_shipments.shipment_id = mto_shipments.id").
Expand Down
Loading

0 comments on commit c4f2ce7

Please sign in to comment.