Skip to content

Commit da76c4e

Browse files
committed
Add URN field to trigger_session action instead of urn_match inside the contact reference
1 parent 6b43aa0 commit da76c4e

File tree

8 files changed

+135
-30
lines changed

8 files changed

+135
-30
lines changed

Diff for: flows/actions/base_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,6 @@ func TestConstructors(t *testing.T) {
697697
actions.NewStartSession(
698698
actionUUID,
699699
assets.NewFlowReference(assets.FlowUUID("fece6eac-9127-4343-9269-56e88f391562"), "Parent"),
700-
701700
[]*assets.GroupReference{
702701
assets.NewGroupReference(assets.GroupUUID("b7cf0d83-f1c9-411c-96fd-c511a4cfa86d"), "Testers"),
703702
},
@@ -734,6 +733,28 @@ func TestConstructors(t *testing.T) {
734733
"create_contact": true
735734
}`,
736735
},
736+
{
737+
actions.NewTriggerSession(
738+
actionUUID,
739+
assets.NewFlowReference(assets.FlowUUID("fece6eac-9127-4343-9269-56e88f391562"), "Parent"),
740+
flows.NewContactReference(flows.ContactUUID("cbe87f5c-cda2-4f90-b5dd-0ac93a884950"), "Bob Smith"),
741+
"",
742+
true,
743+
),
744+
`{
745+
"type": "trigger_session",
746+
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
747+
"flow": {
748+
"uuid": "fece6eac-9127-4343-9269-56e88f391562",
749+
"name": "Parent"
750+
},
751+
"contact": {
752+
"uuid": "cbe87f5c-cda2-4f90-b5dd-0ac93a884950",
753+
"name": "Bob Smith"
754+
},
755+
"interrupt": true
756+
}`,
757+
},
737758
}
738759

739760
for _, tc := range tests {

Diff for: flows/actions/testdata/send_broadcast.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@
195195
"name": "Stavros"
196196
},
197197
{
198-
"uuid": "11708c34-d4ab-4b04-b82a-2578f6e0013c"
198+
"uuid": "11708c34-d4ab-4b04-b82a-2578f6e0013c",
199+
"name": ""
199200
}
200201
],
201202
"contact_query": "name = \"Bob\"",

Diff for: flows/actions/testdata/trigger_session.json

+70
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,75 @@
129129
}
130130
}
131131
]
132+
},
133+
{
134+
"description": "Session triggered event with URN",
135+
"action": {
136+
"type": "trigger_session",
137+
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
138+
"flow": {
139+
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
140+
"name": "Collect Age"
141+
},
142+
"urn": "@(\"tel:+593979123456\")",
143+
"interrupt": true
144+
},
145+
"events": [
146+
{
147+
"type": "session_triggered",
148+
"created_on": "2018-10-18T14:20:30.000123456Z",
149+
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
150+
"flow": {
151+
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
152+
"name": "Collect Age"
153+
},
154+
"urn": "tel:+593979123456",
155+
"interrupt": true,
156+
"exclusions": {},
157+
"run_summary": {
158+
"uuid": "e7187099-7d38-4f60-955c-325957214c42",
159+
"flow": {
160+
"uuid": "bead76f5-dac4-4c9d-996c-c62b326e8c0a",
161+
"name": "Action Tester",
162+
"revision": 123
163+
},
164+
"contact": {
165+
"uuid": "5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f",
166+
"name": "Ryan Lewis",
167+
"language": "eng",
168+
"last_seen_on": "2018-10-18T14:20:30.000123456Z",
169+
"status": "active",
170+
"timezone": "America/Guayaquil",
171+
"created_on": "2018-06-20T11:40:30.123456789Z",
172+
"urns": [
173+
"tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d&id=123",
174+
"twitterid:54784326227#nyaruka"
175+
],
176+
"groups": [
177+
{
178+
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
179+
"name": "Testers"
180+
},
181+
{
182+
"uuid": "0ec97956-c451-48a0-a180-1ce766623e31",
183+
"name": "Males"
184+
}
185+
],
186+
"fields": {
187+
"gender": {
188+
"text": "Male"
189+
}
190+
}
191+
},
192+
"status": "active",
193+
"results": {}
194+
},
195+
"history": {
196+
"parent_uuid": "1ae96956-4b34-433e-8d1a-f05fe6923d6d",
197+
"ancestors": 1,
198+
"ancestors_since_input": 0
199+
}
200+
}
201+
]
132202
}
133203
]

Diff for: flows/actions/trigger_session.go

+29-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package actions
22

33
import (
4+
"fmt"
5+
46
"github.com/nyaruka/gocommon/jsonx"
57
"github.com/nyaruka/gocommon/urns"
68
"github.com/nyaruka/goflow/assets"
@@ -21,8 +23,8 @@ const TypeTriggerSession string = "trigger_session"
2123

2224
// TriggerSessionAction can be used to trigger sessions for another contact. A [event:session_triggered] event will be
2325
// created and it's the responsibility of the caller to act on that by initiating a new session with the flow engine.
24-
// The contact can be specified via a concrete reference or as a URN via the scheme and path fields. In the latter case
25-
// the contact will be created if they don't exist.
26+
// The contact can be specified via a reference or as a URN. In the latter case the contact will be created if they
27+
// don't exist.
2628
//
2729
// {
2830
// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
@@ -37,26 +39,36 @@ type TriggerSessionAction struct {
3739
baseAction
3840
onlineAction
3941

40-
Flow *assets.FlowReference `json:"flow" validate:"required"`
41-
Contact *flows.ContactReference `json:"contact" validate:"required"`
42+
Flow *assets.FlowReference `json:"flow" validate:"required"`
43+
Contact *flows.ContactReference `json:"contact,omitempty"`
44+
URN string `json:"urn,omitempty" engine:"evaluated"`
4245
Interrupt bool `json:"interrupt"`
4346
}
4447

4548
// NewTriggerSession creates a new trigger session action
46-
func NewTriggerSession(uuid flows.ActionUUID, flow *assets.FlowReference, contact *flows.ContactReference, interrupt bool) *TriggerSessionAction {
49+
func NewTriggerSession(uuid flows.ActionUUID, flow *assets.FlowReference, contact *flows.ContactReference, urn string, interrupt bool) *TriggerSessionAction {
4750
return &TriggerSessionAction{
4851
baseAction: newBaseAction(TypeTriggerSession, uuid),
4952
Flow: flow,
5053
Contact: contact,
54+
URN: urn,
5155
Interrupt: interrupt,
5256
}
5357
}
5458

59+
// Validate validates our action is valid
60+
func (a *TriggerSessionAction) Validate() error {
61+
if (a.Contact != nil && a.URN != "") || (a.Contact == nil && a.URN == "") {
62+
return fmt.Errorf("must specify either contact or urn")
63+
}
64+
return nil
65+
}
66+
5567
// Execute runs our action
5668
func (a *TriggerSessionAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error {
57-
contact := a.resolveContact(run, logEvent)
58-
if contact == nil {
59-
logEvent(events.NewDependencyError(a.Contact))
69+
urn := a.resolveURN(run, logEvent)
70+
71+
if urn == urns.NilURN && a.Contact == nil {
6072
return nil
6173
}
6274

@@ -81,34 +93,33 @@ func (a *TriggerSessionAction) Execute(run flows.Run, step flows.Step, logModifi
8193

8294
history := flows.NewChildHistory(run.Session())
8395

84-
logEvent(events.NewSessionTriggered(flow.Reference(false), contact, a.Interrupt, runSnapshot, history))
96+
logEvent(events.NewSessionTriggered(flow.Reference(false), a.Contact, urn, a.Interrupt, runSnapshot, history))
8597
return nil
8698
}
8799

88-
func (a *TriggerSessionAction) resolveContact(run flows.Run, logEvent flows.EventCallback) *flows.ContactReference {
89-
// if this is a concrete reference, return as is
90-
if !a.Contact.Variable() {
91-
return a.Contact
100+
func (a *TriggerSessionAction) resolveURN(run flows.Run, logEvent flows.EventCallback) urns.URN {
101+
if a.URN == "" {
102+
return urns.NilURN
92103
}
93104

94105
// otherwise this is a variable reference so evaluate it
95-
evaluatedURN, ok := run.EvaluateTemplate(a.Contact.URNMatch, logEvent)
106+
evaluatedURN, ok := run.EvaluateTemplate(a.URN, logEvent)
96107
if !ok {
97-
return nil
108+
return urns.NilURN
98109
}
99110

100111
// if we have a valid URN now, return it
101112
urn := urns.URN(evaluatedURN)
102113
if urn.Validate() == nil {
103-
return &flows.ContactReference{URNMatch: string(urn.Normalize())}
114+
return urn.Normalize()
104115
}
105116

106117
// otherwise try to parse as phone number
107118
parsedTel := utils.ParsePhoneNumber(evaluatedURN, run.Session().MergedEnvironment().DefaultCountry())
108119
if parsedTel != "" {
109120
urn, _ := urns.New(urns.Phone, parsedTel)
110-
return &flows.ContactReference{URNMatch: string(urn.Normalize())}
121+
return urn.Normalize()
111122
}
112123

113-
return nil
124+
return urns.NilURN
114125
}

Diff for: flows/contact.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,8 @@ var _ contactql.Queryable = (*Contact)(nil)
544544

545545
// ContactReference is used to reference a contact
546546
type ContactReference struct {
547-
UUID ContactUUID `json:"uuid,omitempty" validate:"omitempty,uuid4"`
548-
Name string `json:"name,omitempty"`
549-
URNMatch string `json:"urn_match,omitempty" engine:"evaluated"`
547+
UUID ContactUUID `json:"uuid" validate:"required,uuid4"`
548+
Name string `json:"name"`
550549
}
551550

552551
// NewContactReference creates a new contact reference with the given UUID and name

Diff for: flows/definition/legacy/testdata/actions.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@
474474
"name": "Horatio"
475475
},
476476
{
477-
"uuid": "cd0d8605-5abc-428c-b34b-c6f6e7a3ef42"
477+
"uuid": "cd0d8605-5abc-428c-b34b-c6f6e7a3ef42",
478+
"name": ""
478479
}
479480
],
480481
"groups": [
@@ -595,10 +596,12 @@
595596
"uuid": "5a4d00aa-807e-44af-9693-64b9fdedd352",
596597
"contacts": [
597598
{
598-
"uuid": "879ace1b-740b-45f1-9198-c2f2f08a825f"
599+
"uuid": "879ace1b-740b-45f1-9198-c2f2f08a825f",
600+
"name": ""
599601
},
600602
{
601-
"uuid": "cd0d8605-5abc-428c-b34b-c6f6e7a3ef42"
603+
"uuid": "cd0d8605-5abc-428c-b34b-c6f6e7a3ef42",
604+
"name": ""
602605
}
603606
],
604607
"groups": [

Diff for: flows/definition/migrations/specdata/templates.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"send_broadcast": [
3838
".attachments[*]",
3939
".contact_query",
40-
".contacts[*].urn_match",
4140
".groups[*].name_match",
4241
".legacy_vars[*]",
4342
".quick_replies[*]",
@@ -73,13 +72,12 @@
7372
],
7473
"start_session": [
7574
".contact_query",
76-
".contacts[*].urn_match",
7775
".groups[*].name_match",
7876
".legacy_vars[*]"
7977
],
8078
"transfer_airtime": [],
8179
"trigger_session": [
82-
".contact.urn_match"
80+
".urn"
8381
]
8482
},
8583
"routers": {

Diff for: flows/events/session_triggered.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type SessionTriggeredEvent struct {
5858

5959
Flow *assets.FlowReference `json:"flow" validate:"required"`
6060
Contact *flows.ContactReference `json:"contact,omitempty"`
61+
URN urns.URN `json:"urn,omitempty" validate:"omitempty,urn"`
6162
Interrupt bool `json:"interrupt,omitempty"`
6263
RunSummary json.RawMessage `json:"run_summary"`
6364
History *flows.SessionHistory `json:"history"`
@@ -72,11 +73,12 @@ type SessionTriggeredEvent struct {
7273
}
7374

7475
// NewSessionTriggered returns a new session triggered event
75-
func NewSessionTriggered(flow *assets.FlowReference, contact *flows.ContactReference, interrupt bool, runSummary json.RawMessage, history *flows.SessionHistory) *SessionTriggeredEvent {
76+
func NewSessionTriggered(flow *assets.FlowReference, contact *flows.ContactReference, urn urns.URN, interrupt bool, runSummary json.RawMessage, history *flows.SessionHistory) *SessionTriggeredEvent {
7677
return &SessionTriggeredEvent{
7778
BaseEvent: NewBaseEvent(TypeSessionTriggered),
7879
Flow: flow,
7980
Contact: contact,
81+
URN: urn,
8082
Interrupt: interrupt,
8183
RunSummary: runSummary,
8284
History: history,

0 commit comments

Comments
 (0)