Skip to content

Commit

Permalink
Switch preview start endpoint to take UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Apr 21, 2022
1 parent e8052ef commit 2fc0322
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 59 deletions.
24 changes: 12 additions & 12 deletions core/search/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/mailroom/core/models"
)

Expand All @@ -19,17 +21,14 @@ type Exclusions struct {
}

// BuildStartQuery builds a start query for the given flow and start options
func BuildStartQuery(oa *models.OrgAssets, flow *models.Flow, groupIDs []models.GroupID, contactIDs []models.ContactID, urnz []urns.URN, userQuery string, excs Exclusions) string {
func BuildStartQuery(env envs.Environment, flow *models.Flow, groups []*models.Group, contactUUIDs []flows.ContactUUID, urnz []urns.URN, userQuery string, excs Exclusions) string {
inclusions := make([]string, 0, 10)

for _, groupID := range groupIDs {
group := oa.GroupByID(groupID)
if group != nil {
inclusions = append(inclusions, fmt.Sprintf("group = \"%s\"", group.Name()))
}
for _, group := range groups {
inclusions = append(inclusions, fmt.Sprintf("group = \"%s\"", group.Name()))
}
for _, contactID := range contactIDs {
inclusions = append(inclusions, fmt.Sprintf("id = %d", contactID))
for _, contactUUID := range contactUUIDs {
inclusions = append(inclusions, fmt.Sprintf("uuid = \"%s\"", contactUUID))
}
for _, urn := range urnz {
scheme, path, _, _ := urn.ToParts()
Expand All @@ -54,7 +53,7 @@ func BuildStartQuery(oa *models.OrgAssets, flow *models.Flow, groupIDs []models.
}
if excs.NotSeenRecently {
seenSince := dates.Now().Add(-time.Hour * 24 * 90)
exclusions = append(exclusions, fmt.Sprintf("last_seen_on > %s", formatQueryDate(oa, seenSince)))
exclusions = append(exclusions, fmt.Sprintf("last_seen_on > %s", formatQueryDate(env, seenSince)))
}

inclusionCmp := strings.Join(inclusions, " OR ")
Expand All @@ -74,8 +73,9 @@ func BuildStartQuery(oa *models.OrgAssets, flow *models.Flow, groupIDs []models.
return strings.Join(conditions, " AND ")
}

func formatQueryDate(oa *models.OrgAssets, t time.Time) string {
d := dates.ExtractDate(t.In(oa.Env().Timezone()))
s, _ := d.Format(string(oa.Env().DateFormat()), oa.Env().DefaultLocale().ToBCP47())
// formats a date for use in a query
func formatQueryDate(env envs.Environment, t time.Time) string {
d := dates.ExtractDate(t.In(env.Timezone()))
s, _ := d.Format(string(env.DateFormat()), env.DefaultLocale().ToBCP47())
return s
}
45 changes: 27 additions & 18 deletions core/search/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package search_test

import (
"testing"
"time"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/search"
"github.com/nyaruka/mailroom/testsuite"
Expand All @@ -15,43 +18,49 @@ import (
func TestBuildStartQuery(t *testing.T) {
_, rt, _, _ := testsuite.Get()

dates.SetNowSource(dates.NewFixedNowSource(time.Date(2022, 4, 20, 15, 30, 45, 0, time.UTC)))
defer dates.SetNowSource(dates.DefaultNowSource)

oa := testdata.Org1.Load(rt)
flow, err := oa.FlowByID(testdata.Favorites.ID)
require.NoError(t, err)

doctors := oa.GroupByID(testdata.DoctorsGroup.ID)
testers := oa.GroupByID(testdata.TestersGroup.ID)

tcs := []struct {
groupIDs []models.GroupID
contactIDs []models.ContactID
urns []urns.URN
userQuery string
exclusions search.Exclusions
expected string
groups []*models.Group
contactUUIDs []flows.ContactUUID
urns []urns.URN
userQuery string
exclusions search.Exclusions
expected string
}{
{
groupIDs: []models.GroupID{testdata.DoctorsGroup.ID, testdata.TestersGroup.ID},
contactIDs: []models.ContactID{testdata.Cathy.ID, testdata.George.ID},
urns: []urns.URN{"tel:+1234567890", "telegram:9876543210"},
exclusions: search.Exclusions{},
expected: `group = "Doctors" OR group = "Testers" OR id = 10000 OR id = 10002 OR tel = "+1234567890" OR telegram = "9876543210"`,
groups: []*models.Group{doctors, testers},
contactUUIDs: []flows.ContactUUID{testdata.Cathy.UUID, testdata.George.UUID},
urns: []urns.URN{"tel:+1234567890", "telegram:9876543210"},
exclusions: search.Exclusions{},
expected: `group = "Doctors" OR group = "Testers" OR uuid = "6393abc0-283d-4c9b-a1b3-641a035c34bf" OR uuid = "8d024bcd-f473-4719-a00a-bd0bb1190135" OR tel = "+1234567890" OR telegram = "9876543210"`,
},
{
groupIDs: []models.GroupID{testdata.DoctorsGroup.ID},
contactIDs: []models.ContactID{testdata.Cathy.ID},
urns: []urns.URN{"tel:+1234567890"},
groups: []*models.Group{doctors},
contactUUIDs: []flows.ContactUUID{testdata.Cathy.UUID},
urns: []urns.URN{"tel:+1234567890"},
exclusions: search.Exclusions{
NonActive: true,
InAFlow: true,
StartedPreviously: true,
NotSeenRecently: true,
},
expected: `(group = "Doctors" OR id = 10000 OR tel = "+1234567890") AND status = "active" AND flow = "" AND history != "Favorites" AND last_seen_on > 20-01-2022`,
expected: `(group = "Doctors" OR uuid = "6393abc0-283d-4c9b-a1b3-641a035c34bf" OR tel = "+1234567890") AND status = "active" AND flow = "" AND history != "Favorites" AND last_seen_on > 20-01-2022`,
},
{
contactIDs: []models.ContactID{testdata.Cathy.ID},
contactUUIDs: []flows.ContactUUID{testdata.Cathy.UUID},
exclusions: search.Exclusions{
NonActive: true,
},
expected: `id = 10000 AND status = "active"`,
expected: `uuid = "6393abc0-283d-4c9b-a1b3-641a035c34bf" AND status = "active"`,
},
{
userQuery: "gender = M",
Expand All @@ -71,7 +80,7 @@ func TestBuildStartQuery(t *testing.T) {
}

for _, tc := range tcs {
actual := search.BuildStartQuery(oa, flow, tc.groupIDs, tc.contactIDs, tc.urns, tc.userQuery, tc.exclusions)
actual := search.BuildStartQuery(oa.Env(), flow, tc.groups, tc.contactUUIDs, tc.urns, tc.userQuery, tc.exclusions)
assert.Equal(t, tc.expected, actual)
}
}
34 changes: 22 additions & 12 deletions web/flow/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/search"
Expand All @@ -22,8 +24,8 @@ func init() {
// {
// "org_id": 1,
// "flow_id": 2,
// "group_ids": [123, 345],
// "contact_ids": [12, 34],
// "group_uuids": ["5fa925e4-edd8-4e2a-ab24-b3dbb5932ddd", "2912b95f-5b89-4d39-a2a8-5292602f357f"],
// "contact_uuids": ["e5bb9e6f-7703-4ba1-afba-0b12791de38b"],
// "urns": ["tel:+1234567890"],
// "user_query": "",
// "exclusions": {
Expand All @@ -36,20 +38,20 @@ func init() {
// }
//
// {
// "query": "(id = 12 OR id = 34 OR group = "No Age" OR group = "No Name" OR tel = "+1234567890") AND history != \"Registration\"",
// "query": "(group = "No Age" OR group = "No Name" OR uuid = "e5bb9e6f-7703-4ba1-afba-0b12791de38b" OR tel = "+1234567890") AND history != \"Registration\"",
// "count": 567,
// "sample": [12, 34, 56, 67, 78]
// }
//
type previewStartRequest struct {
OrgID models.OrgID `json:"org_id" validate:"required"`
FlowID models.FlowID `json:"flow_id" validate:"required"`
GroupIDs []models.GroupID `json:"group_ids"`
ContactIDs []models.ContactID `json:"contact_ids"`
URNs []urns.URN `json:"urns"`
Query string `json:"query"`
Exclusions search.Exclusions `json:"exclusions"`
SampleSize int `json:"sample_size" validate:"required"`
OrgID models.OrgID `json:"org_id" validate:"required"`
FlowID models.FlowID `json:"flow_id" validate:"required"`
GroupUUIDs []assets.GroupUUID `json:"group_uuids"`
ContactUUIDs []flows.ContactUUID `json:"contact_uuids"`
URNs []urns.URN `json:"urns"`
Query string `json:"query"`
Exclusions search.Exclusions `json:"exclusions"`
SampleSize int `json:"sample_size" validate:"required"`
}

type previewStartResponse struct {
Expand All @@ -74,7 +76,15 @@ func handlePreviewStart(ctx context.Context, rt *runtime.Runtime, r *http.Reques
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load flow")
}

query := search.BuildStartQuery(oa, flow, request.GroupIDs, request.ContactIDs, request.URNs, request.Query, request.Exclusions)
groups := make([]*models.Group, 0, len(request.GroupUUIDs))
for _, groupUUID := range request.GroupUUIDs {
g := oa.GroupByUUID(groupUUID)
if g != nil {
groups = append(groups, g)
}
}

query := search.BuildStartQuery(oa.Env(), flow, groups, request.ContactUUIDs, request.URNs, request.Query, request.Exclusions)
if query == "" {
return &previewStartResponse{Query: "", Count: 0, Sample: []models.ContactID{}}, http.StatusOK, nil
}
Expand Down
31 changes: 14 additions & 17 deletions web/flow/testdata/preview_start.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
"body": {
"org_id": 1,
"flow_id": 10001,
"group_ids": [
10000,
10001
"group_uuids": [
"c153e265-f7c9-4539-9dbc-9b358714b638",
"5e9d8fab-5e7e-4f51-b533-261af5dea70d"
],
"contact_ids": [
1234,
3456
"contact_uuids": [
"5a8345c1-514a-4d1b-aee5-6f39b2f53cfa",
"bd2aab59-5e28-4db4-b6e8-bbdb75fd7a0a"
],
"urns": [
"tel:+1234567890",
Expand All @@ -58,7 +58,7 @@
},
"status": 200,
"response": {
"query": "group = \"Doctors\" OR group = \"Testers\" OR id = 1234 OR id = 3456 OR tel = \"+1234567890\" OR facebook = 9876543210",
"query": "group = \"Doctors\" OR group = \"Testers\" OR uuid = \"5a8345c1-514a-4d1b-aee5-6f39b2f53cfa\" OR uuid = \"bd2aab59-5e28-4db4-b6e8-bbdb75fd7a0a\" OR tel = \"+1234567890\" OR facebook = 9876543210",
"count": 1,
"sample": [
10000
Expand Down Expand Up @@ -94,13 +94,13 @@
"body": {
"org_id": 1,
"flow_id": 10001,
"group_ids": [
10000,
10001
"group_uuids": [
"c153e265-f7c9-4539-9dbc-9b358714b638",
"5e9d8fab-5e7e-4f51-b533-261af5dea70d"
],
"contact_ids": [
1234,
3456
"contact_uuids": [
"5a8345c1-514a-4d1b-aee5-6f39b2f53cfa",
"bd2aab59-5e28-4db4-b6e8-bbdb75fd7a0a"
],
"urns": [
"tel:+1234567890",
Expand All @@ -117,7 +117,7 @@
},
"status": 200,
"response": {
"query": "(group = \"Doctors\" OR group = \"Testers\" OR id = 1234 OR id = 3456 OR tel = \"+1234567890\" OR facebook = 9876543210) AND status = \"active\" AND flow = \"\" AND history != \"Pick a Number\" AND last_seen_on > \"07-04-2018\"",
"query": "(group = \"Doctors\" OR group = \"Testers\" OR uuid = \"5a8345c1-514a-4d1b-aee5-6f39b2f53cfa\" OR uuid = \"bd2aab59-5e28-4db4-b6e8-bbdb75fd7a0a\" OR tel = \"+1234567890\" OR facebook = 9876543210) AND status = \"active\" AND flow = \"\" AND history != \"Pick a Number\" AND last_seen_on > \"07-04-2018\"",
"count": 1,
"sample": [
10002
Expand All @@ -131,9 +131,6 @@
"body": {
"org_id": 1,
"flow_id": 10001,
"group_ids": [],
"contact_ids": [],
"urns": [],
"query": "gender = M",
"exclusions": {
"non_active": true,
Expand Down

0 comments on commit 2fc0322

Please sign in to comment.