forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_contact_urn.go
68 lines (56 loc) · 1.98 KB
/
add_contact_urn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package actions
import (
"fmt"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
)
// TypeAddContactURN is our type for the add URN action
const TypeAddContactURN string = "add_contact_urn"
// AddContactURNAction can be used to add a URN to the current contact. An `contact_urn_added` event
// will be created when this action is encountered. If there is no contact then this
// action will be ignored.
//
// {
// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
// "type": "add_contact_urn",
// "scheme": "tel",
// "path": "@run.results.phone_number"
// }
//
// @action add_contact_urn
type AddContactURNAction struct {
BaseAction
Scheme string `json:"scheme" validate:"urnscheme"`
Path string `json:"path" validate:"required"`
}
// Type returns the type of this action
func (a *AddContactURNAction) Type() string { return TypeAddContactURN }
// Validate validates our action is valid and has all the assets it needs
func (a *AddContactURNAction) Validate(assets flows.SessionAssets) error {
return nil
}
// Execute runs the labeling action
func (a *AddContactURNAction) Execute(run flows.FlowRun, step flows.Step, log flows.EventLog) error {
// only generate event if run has a contact
contact := run.Contact()
if contact == nil {
log.Add(events.NewFatalErrorEvent(fmt.Errorf("can't execute action in session without a contact")))
return nil
}
evaluatedPath, err := run.EvaluateTemplateAsString(a.Path, false)
// if we received an error, log it although it might just be a non-expression like foo@bar.com
if err != nil {
log.Add(events.NewErrorEvent(err))
}
// if we don't have a valid URN, log error
urn, err := urns.NewURNFromParts(a.Scheme, evaluatedPath, "", "")
if err != nil {
log.Add(events.NewErrorEvent(fmt.Errorf("unable to add URN '%s:%s': %s", a.Scheme, evaluatedPath, err.Error())))
return nil
}
if !run.Contact().HasURN(urn) {
log.Add(events.NewURNAddedEvent(urn))
}
return nil
}