diff --git a/core/models/contacts.go b/core/models/contacts.go index d5d8b464e..fc73178a2 100644 --- a/core/models/contacts.go +++ b/core/models/contacts.go @@ -917,10 +917,7 @@ func URNForID(ctx context.Context, db Queryer, org *OrgAssets, urnID URNID) (urn // CalculateDynamicGroups recalculates all the dynamic groups for the passed in contact, recalculating // campaigns as necessary based on those group changes. func CalculateDynamicGroups(ctx context.Context, db Queryer, org *OrgAssets, contact *flows.Contact) error { - added, removed, errs := contact.ReevaluateQueryBasedGroups(org.Env()) - if len(errs) > 0 { - return errors.Wrapf(errs[0], "error calculating dynamic groups") - } + added, removed := contact.ReevaluateQueryBasedGroups(org.Env()) campaigns := make(map[CampaignID]*Campaign) diff --git a/go.mod b/go.mod index 84eb1a78d..cc1d736bb 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/mattn/go-sqlite3 v1.10.0 // indirect github.com/nyaruka/ezconf v0.2.1 github.com/nyaruka/gocommon v1.13.0 - github.com/nyaruka/goflow v0.128.0 + github.com/nyaruka/goflow v0.130.1 github.com/nyaruka/librato v1.0.0 github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d github.com/nyaruka/null v1.2.0 diff --git a/go.sum b/go.sum index b05fc3f3a..751a73d9c 100644 --- a/go.sum +++ b/go.sum @@ -134,8 +134,8 @@ github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0= github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw= github.com/nyaruka/gocommon v1.13.0 h1:WPL//ekajA30KinYRr6IrdP1igNZpcUAfABleHCuxPQ= github.com/nyaruka/gocommon v1.13.0/go.mod h1:Jn7UIE8zwIr4JaviDf4PZrrQlN8r6QGVhOuaF/JoKus= -github.com/nyaruka/goflow v0.128.0 h1:BQ6JhtJp6TCcMb5ZwEfyz8nvAwmpGgJgjToWYM7L6xk= -github.com/nyaruka/goflow v0.128.0/go.mod h1:Xp1p21TyYiMM/fVQNWQRok/fZ1ZeNoeQGUd//LvYxq4= +github.com/nyaruka/goflow v0.130.1 h1:ai84idJkjgn2XJdv735/4qz72L0AuOYtEX8+GpL4xs0= +github.com/nyaruka/goflow v0.130.1/go.mod h1:Xp1p21TyYiMM/fVQNWQRok/fZ1ZeNoeQGUd//LvYxq4= github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0= github.com/nyaruka/librato v1.0.0/go.mod h1:pkRNLFhFurOz0QqBz6/DuTFhHHxAubWxs4Jx+J7yUgg= github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d h1:hyp9u36KIwbTCo2JAJ+TuJcJBc+UZzEig7RI/S5Dvkc= diff --git a/web/contact/search.go b/web/contact/search.go index dff111bc4..33a57fc12 100644 --- a/web/contact/search.go +++ b/web/contact/search.go @@ -136,6 +136,7 @@ func handleSearch(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in type parseRequest struct { OrgID models.OrgID `json:"org_id" validate:"required"` Query string `json:"query" validate:"required"` + ParseOnly bool `json:"parse_only"` GroupUUID assets.GroupUUID `json:"group_uuid"` } @@ -155,10 +156,6 @@ type parseResponse struct { Query string `json:"query"` ElasticQuery interface{} `json:"elastic_query"` Metadata *contactql.Inspection `json:"metadata,omitempty"` - - // deprecated - Fields []string `json:"fields"` - AllowAsGroup bool `json:"allow_as_group"` } // handles a query parsing request @@ -175,8 +172,12 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request) } env := oa.Env() - parsed, err := contactql.ParseQuery(env, request.Query, oa.SessionAssets()) + var resolver contactql.Resolver + if !request.ParseOnly { + resolver = oa.SessionAssets() + } + parsed, err := contactql.ParseQuery(env, request.Query, resolver) if err != nil { isQueryError, qerr := contactql.IsQueryError(err) if isQueryError { @@ -186,34 +187,23 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request) } // normalize and inspect the query - normalized := "" - var metadata *contactql.Inspection - allowAsGroup := false - fields := make([]string, 0) - - if parsed != nil { - normalized = parsed.String() - metadata = contactql.Inspect(parsed) - fields = append(fields, metadata.Attributes...) - for _, f := range metadata.Fields { - fields = append(fields, f.Key) + normalized := parsed.String() + metadata := contactql.Inspect(parsed) + + var elasticSource interface{} + if !request.ParseOnly { + eq := models.BuildElasticQuery(oa, request.GroupUUID, models.NilContactStatus, nil, parsed) + elasticSource, err = eq.Source() + if err != nil { + return nil, http.StatusInternalServerError, errors.Wrap(err, "error getting elastic source") } - allowAsGroup = metadata.AllowAsGroup - } - - eq := models.BuildElasticQuery(oa, request.GroupUUID, models.NilContactStatus, nil, parsed) - eqj, err := eq.Source() - if err != nil { - return nil, http.StatusInternalServerError, err } // build our response response := &parseResponse{ Query: normalized, - ElasticQuery: eqj, + ElasticQuery: elasticSource, Metadata: metadata, - Fields: fields, - AllowAsGroup: allowAsGroup, } return response, http.StatusOK, nil diff --git a/web/contact/search_test.go b/web/contact/search_test.go index e9def182d..b884e96f4 100644 --- a/web/contact/search_test.go +++ b/web/contact/search_test.go @@ -237,7 +237,7 @@ func TestSearch(t *testing.T) { } } -func TestParse(t *testing.T) { +func TestParseQuery(t *testing.T) { testsuite.Reset() web.RunWebTests(t, "testdata/parse_query.json", nil) diff --git a/web/contact/testdata/parse_query.json b/web/contact/testdata/parse_query.json index 19b5aed1d..a4e0d0b64 100644 --- a/web/contact/testdata/parse_query.json +++ b/web/contact/testdata/parse_query.json @@ -43,6 +43,35 @@ } } }, + { + "label": "query with invalid property but parse_only = true", + "method": "POST", + "path": "/mr/contact/parse_query", + "body": { + "org_id": 1, + "query": "birthday = tomorrow AND tel = 12345", + "parse_only": true + }, + "status": 200, + "response": { + "query": "birthday = \"tomorrow\" AND tel = 12345", + "elastic_query": null, + "metadata": { + "attributes": [], + "schemes": [ + "tel" + ], + "fields": [ + { + "key": "birthday", + "name": "" + } + ], + "groups": [], + "allow_as_group": true + } + } + }, { "label": "valid query without group", "method": "POST", @@ -107,11 +136,7 @@ ], "groups": [], "allow_as_group": true - }, - "fields": [ - "age" - ], - "allow_as_group": true + } } }, { @@ -184,11 +209,7 @@ ], "groups": [], "allow_as_group": true - }, - "fields": [ - "age" - ], - "allow_as_group": true + } } }, { @@ -236,11 +257,7 @@ } ], "allow_as_group": false - }, - "fields": [ - "group" - ], - "allow_as_group": false + } } } ] \ No newline at end of file