-
Notifications
You must be signed in to change notification settings - Fork 19
/
open_ticket.go
98 lines (83 loc) · 2.99 KB
/
open_ticket.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package actions
import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/modifiers"
)
func init() {
registerType(TypeOpenTicket, func() flows.Action { return &OpenTicketAction{} })
}
// TypeOpenTicket is the type for the open ticket action
const TypeOpenTicket string = "open_ticket"
// OpenTicketAction is used to open a ticket for the contact if they don't already have an open ticket.
//
// {
// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
// "type": "open_ticket",
// "topic": {
// "uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
// "name": "Weather"
// },
// "body": "@input",
// "assignee": {"email": "bob@nyaruka.com", "name": "Bob McTickets"},
// "result_name": "Help Ticket"
// }
//
// @action open_ticket
type OpenTicketAction struct {
baseAction
onlineAction
Topic *assets.TopicReference `json:"topic" validate:"omitempty"`
Body string `json:"body" engine:"evaluated"`
Assignee *assets.UserReference `json:"assignee" validate:"omitempty"`
ResultName string `json:"result_name" validate:"required"`
}
// NewOpenTicket creates a new open ticket action
func NewOpenTicket(uuid flows.ActionUUID, topic *assets.TopicReference, body string, assignee *assets.UserReference, resultName string) *OpenTicketAction {
return &OpenTicketAction{
baseAction: newBaseAction(TypeOpenTicket, uuid),
Topic: topic,
Body: body,
Assignee: assignee,
ResultName: resultName,
}
}
// Execute runs this action
func (a *OpenTicketAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error {
sa := run.Session().Assets()
var topic *flows.Topic
if a.Topic != nil {
topic = sa.Topics().Get(a.Topic.UUID)
} else {
topic = sa.Topics().FindByName("General") // TODO remove when editor adds topics
}
var assignee *flows.User
if a.Assignee != nil {
assignee = resolveUser(run, a.Assignee, logEvent)
}
evaluatedBody, _ := run.EvaluateTemplate(a.Body, logEvent)
ticket := a.open(run, topic, evaluatedBody, assignee, logModifier, logEvent)
if ticket != nil {
a.saveResult(run, step, a.ResultName, string(ticket.UUID()), CategorySuccess, "", "", nil, logEvent)
} else {
a.saveResult(run, step, a.ResultName, "", CategoryFailure, "", "", nil, logEvent)
}
return nil
}
func (a *OpenTicketAction) open(run flows.Run, topic *flows.Topic, body string, assignee *flows.User, logModifier flows.ModifierCallback, logEvent flows.EventCallback) *flows.Ticket {
if run.Session().BatchStart() {
logEvent(events.NewErrorf("can't open tickets during batch starts"))
return nil
}
if a.Topic != nil && topic == nil {
logEvent(events.NewDependencyError(a.Topic))
return nil
}
mod := modifiers.NewTicket(topic, body, assignee)
if a.applyModifier(run, mod, logModifier, logEvent) {
// if we were able to open a ticket, return it
return run.Session().Contact().Ticket()
}
return nil
}