-
Notifications
You must be signed in to change notification settings - Fork 19
/
contact_language_changed.go
66 lines (54 loc) · 1.71 KB
/
contact_language_changed.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
package events
import (
"fmt"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
)
func init() {
RegisterType(TypeContactLanguageChanged, func() flows.Event { return &ContactLanguageChangedEvent{} })
}
// TypeContactLanguageChanged is the type of our contact language changed event
const TypeContactLanguageChanged string = "contact_language_changed"
// ContactLanguageChangedEvent events are created when a Language of a contact has been changed
//
// {
// "type": "contact_language_changed",
// "created_on": "2006-01-02T15:04:05Z",
// "language": "eng"
// }
//
// @event contact_language_changed
type ContactLanguageChangedEvent struct {
BaseEvent
callerOrEngineEvent
Language string `json:"language"`
}
// NewContactLanguageChangedEvent returns a new contact language changed event
func NewContactLanguageChangedEvent(language string) *ContactLanguageChangedEvent {
return &ContactLanguageChangedEvent{
BaseEvent: NewBaseEvent(),
Language: language,
}
}
// Type returns the type of this event
func (e *ContactLanguageChangedEvent) Type() string { return TypeContactLanguageChanged }
// Validate validates our event is valid and has all the assets it needs
func (e *ContactLanguageChangedEvent) Validate(assets flows.SessionAssets) error {
return nil
}
// Apply applies this event to the given run
func (e *ContactLanguageChangedEvent) Apply(run flows.FlowRun) error {
if run.Contact() == nil {
return fmt.Errorf("can't apply event in session without a contact")
}
if e.Language != "" {
lang, err := utils.ParseLanguage(e.Language)
if err != nil {
return err
}
run.Contact().SetLanguage(lang)
} else {
run.Contact().SetLanguage(utils.NilLanguage)
}
return nil
}