-
Notifications
You must be signed in to change notification settings - Fork 20
/
urn.go
74 lines (59 loc) · 2.12 KB
/
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
69
70
71
72
73
74
package modifiers
import (
"encoding/json"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
)
func init() {
registerType(TypeURN, readURNModifier)
}
// TypeURN is the type of our URN modifier
const TypeURN string = "urn"
// URNModification is the type of modification to make
type URNModification string
// the supported types of modification
const (
URNAppend URNModification = "append"
URNRemove URNModification = "remove"
)
// URNModifier modifies a URN on a contact. This has been replaced by URNsModifier but is kept here for now
// to support processing of old Surveyor submissions.
type URNModifier struct {
baseModifier
URN urns.URN `json:"urn" validate:"required"`
Modification URNModification `json:"modification" validate:"required,eq=append|eq=remove"`
}
// NewURN creates a new name modifier
func NewURN(urn urns.URN, modification URNModification) *URNModifier {
return &URNModifier{
baseModifier: newBaseModifier(TypeURN),
URN: urn,
Modification: modification,
}
}
// Apply applies this modification to the given contact
func (m *URNModifier) Apply(env envs.Environment, assets flows.SessionAssets, contact *flows.Contact, log flows.EventCallback) {
urn := m.URN.Normalize(string(env.DefaultCountry()))
modified := false
if m.Modification == URNAppend {
modified = contact.AddURN(urn, nil)
} else {
modified = contact.RemoveURN(urn)
}
if modified {
log(events.NewContactURNsChanged(contact.URNs().RawURNs()))
m.reevaluateGroups(env, assets, contact, log)
}
}
var _ flows.Modifier = (*URNModifier)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
func readURNModifier(assets flows.SessionAssets, data json.RawMessage, missing assets.MissingCallback) (flows.Modifier, error) {
m := &URNModifier{}
return m, utils.UnmarshalAndValidate(data, m)
}