forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual.go
84 lines (67 loc) · 2.4 KB
/
manual.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
75
76
77
78
79
80
81
82
83
84
package triggers
import (
"encoding/json"
"time"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
)
// TypeManual is the type for manually triggered sessions
const TypeManual string = "manual"
// ManualTrigger is used when a session was triggered manually by a user
//
// ```
// {
// "type": "manual",
// "flow": {"uuid": "ea7d8b6b-a4b2-42c1-b9cf-c0370a95a721", "name": "Registration"},
// "contact": {
// "uuid": "9f7ede93-4b16-4692-80ad-b7dc54a1cd81",
// "name": "Bob"
// },
// "triggered_on": "2000-01-01T00:00:00.000000000-00:00"
// }
// ```
type ManualTrigger struct {
baseTrigger
}
// NewManualTrigger creates a new manual trigger
func NewManualTrigger(env utils.Environment, contact *flows.Contact, flow flows.Flow, params types.XValue, triggeredOn time.Time) flows.Trigger {
return &ManualTrigger{baseTrigger{environment: env, contact: contact, flow: flow, triggeredOn: triggeredOn}}
}
// Type returns the type of this trigger
func (t *ManualTrigger) Type() string { return TypeManual }
// Resolve resolves the given key when this trigger is referenced in an expression
func (t *ManualTrigger) Resolve(key string) types.XValue {
switch key {
case "type":
return types.NewXString(TypeManual)
}
return t.baseTrigger.Resolve(key)
}
// ToXJSON is called when this type is passed to @(json(...))
func (t *ManualTrigger) ToXJSON() types.XString {
return types.ResolveKeys(t, "type", "params").ToXJSON()
}
var _ flows.Trigger = (*ManualTrigger)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
func ReadManualTrigger(session flows.Session, envelope *utils.TypedEnvelope) (flows.Trigger, error) {
trigger := ManualTrigger{}
e := baseTriggerEnvelope{}
if err := utils.UnmarshalAndValidate(envelope.Data, &e, "trigger[type=manual]"); err != nil {
return nil, err
}
if err := unmarshalBaseTrigger(session, &trigger.baseTrigger, &e); err != nil {
return nil, err
}
return &trigger, nil
}
// MarshalJSON marshals this trigger into JSON
func (t *ManualTrigger) MarshalJSON() ([]byte, error) {
var envelope baseTriggerEnvelope
if err := marshalBaseTrigger(&t.baseTrigger, &envelope); err != nil {
return nil, err
}
return json.Marshal(envelope)
}