Skip to content

Commit

Permalink
Merge pull request #1279 from nyaruka/open_ticket_with_note
Browse files Browse the repository at this point in the history
Replace ticket body with initial note
  • Loading branch information
rowanseymour authored Aug 6, 2024
2 parents 8c99100 + f09a719 commit c95e757
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 107 deletions.
2 changes: 1 addition & 1 deletion flows/actions/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) {

if tc.HasTicket {
topic := sa.Topics().Get("0d9a2c56-6fc2-4f27-93c5-a6322e26b740")
contact.SetTicket(flows.NewTicket("7f44b065-ec28-4d7a-bbb4-0bda3b75b19d", topic, "Help", nil))
contact.SetTicket(flows.NewTicket("7f44b065-ec28-4d7a-bbb4-0bda3b75b19d", topic, nil))
}

// and switch their language
Expand Down
13 changes: 8 additions & 5 deletions flows/actions/open_ticket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"strings"

"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
Expand Down Expand Up @@ -34,7 +36,7 @@ type OpenTicketAction struct {
onlineAction

Topic *assets.TopicReference `json:"topic" validate:"omitempty"`
Body string `json:"body" engine:"evaluated"`
Body string `json:"body" engine:"evaluated"` // TODO will become "note" in future migration
Assignee *assets.UserReference `json:"assignee" validate:"omitempty"`
ResultName string `json:"result_name" validate:"required"`
}
Expand Down Expand Up @@ -67,9 +69,10 @@ func (a *OpenTicketAction) Execute(run flows.Run, step flows.Step, logModifier f
assignee = resolveUser(run, a.Assignee, logEvent)
}

evaluatedBody, _ := run.EvaluateTemplate(a.Body, logEvent)
evaluatedNote, _ := run.EvaluateTemplate(a.Body, logEvent)
evaluatedNote = strings.TrimSpace(evaluatedNote)

ticket := a.open(run, topic, evaluatedBody, assignee, logModifier, logEvent)
ticket := a.open(run, topic, assignee, evaluatedNote, logModifier, logEvent)
if ticket != nil {
a.saveResult(run, step, a.ResultName, string(ticket.UUID()), CategorySuccess, "", "", nil, logEvent)
} else {
Expand All @@ -79,7 +82,7 @@ func (a *OpenTicketAction) Execute(run flows.Run, step flows.Step, logModifier f
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 {
func (a *OpenTicketAction) open(run flows.Run, topic *flows.Topic, assignee *flows.User, note string, logModifier flows.ModifierCallback, logEvent flows.EventCallback) *flows.Ticket {
if run.Session().BatchStart() {
logEvent(events.NewErrorf("can't open tickets during batch starts"))
return nil
Expand All @@ -90,7 +93,7 @@ func (a *OpenTicketAction) open(run flows.Run, topic *flows.Topic, body string,
return nil
}

mod := modifiers.NewTicket(topic, body, assignee)
mod := modifiers.NewTicket(topic, assignee, note)

if a.applyModifier(run, mod, logModifier, logEvent) {
// if we were able to open a ticket, return it
Expand Down
49 changes: 32 additions & 17 deletions flows/actions/testdata/open_ticket.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,18 @@
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Last message: Hi everybody",
"assignee": {
"email": "[email protected]",
"name": "Bob"
}
}
},
{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"note": "Last message: Hi everybody"
},
{
"type": "contact_groups_changed",
"created_on": "2018-10-18T14:20:30.000123456Z",
Expand Down Expand Up @@ -180,7 +185,6 @@
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Last message: Hi everybody",
"assignee": {
"email": "[email protected]",
"name": "Bob"
Expand Down Expand Up @@ -229,10 +233,15 @@
"topic": {
"uuid": "0d9a2c56-6fc2-4f27-93c5-a6322e26b740",
"name": "General"
},
"body": "Last message: Hi everybody"
}
}
},
{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"note": "Last message: Hi everybody"
},
{
"type": "contact_groups_changed",
"created_on": "2018-10-18T14:20:30.000123456Z",
Expand Down Expand Up @@ -289,8 +298,7 @@
"topic": {
"uuid": "0d9a2c56-6fc2-4f27-93c5-a6322e26b740",
"name": "General"
},
"body": "Last message: Hi everybody"
}
}
},
"templates": [
Expand Down Expand Up @@ -330,13 +338,18 @@
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Last message: Hi everybody",
"assignee": {
"email": "[email protected]",
"name": "Jim"
}
}
},
{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"note": "Last message: Hi everybody"
},
{
"type": "contact_groups_changed",
"created_on": "2018-10-18T14:20:30.000123456Z",
Expand Down Expand Up @@ -394,7 +407,6 @@
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Last message: Hi everybody",
"assignee": {
"email": "[email protected]",
"name": "Jim"
Expand Down Expand Up @@ -450,10 +462,15 @@
"topic": {
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Last message: Hi everybody"
}
}
},
{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"note": "Last message: Hi everybody"
},
{
"type": "contact_groups_changed",
"created_on": "2018-10-18T14:20:30.000123456Z",
Expand Down Expand Up @@ -510,8 +527,7 @@
"topic": {
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Last message: Hi everybody"
}
}
},
"templates": [
Expand All @@ -533,12 +549,12 @@
}
},
{
"description": "Error events if subject or body contain expression errors",
"description": "Error events if note contains expression errors",
"action": {
"type": "open_ticket",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"topic": null,
"body": "Where are my cookies? @(1/ 0)",
"body": "@(1/ 0)",
"assignee": null,
"result_name": "Ticket"
},
Expand All @@ -558,8 +574,7 @@
"topic": {
"uuid": "0d9a2c56-6fc2-4f27-93c5-a6322e26b740",
"name": "General"
},
"body": "Where are my cookies? "
}
}
},
{
Expand All @@ -583,7 +598,7 @@
}
],
"templates": [
"Where are my cookies? @(1/ 0)"
"@(1/ 0)"
],
"inspection": {
"dependencies": [],
Expand Down
2 changes: 1 addition & 1 deletion flows/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestContact(t *testing.T) {
assert.Nil(t, contact.Ticket())

weather := sa.Topics().Get("472a7a73-96cb-4736-b567-056d987cc5b4")
ticket := flows.OpenTicket(weather, "I have issues", nil)
ticket := flows.OpenTicket(weather, nil)
contact.SetTicket(ticket)

assert.NotNil(t, contact.Ticket())
Expand Down
9 changes: 3 additions & 6 deletions flows/engine/testdata/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@
},
{
"template": "@(json(contact.tickets))",
"output": "[{\"assignee\":{\"email\":\"[email protected]\",\"first_name\":\"Bob\",\"name\":\"Bob\"},\"body\":\"What day is it?\",\"topic\":{\"name\":\"Weather\",\"uuid\":\"472a7a73-96cb-4736-b567-056d987cc5b4\"},\"uuid\":\"78d1fe0d-7e39-461e-81c3-a6a25f15ed69\"}]"
"output": "[{\"assignee\":{\"email\":\"[email protected]\",\"first_name\":\"Bob\",\"name\":\"Bob\"},\"topic\":{\"name\":\"Weather\",\"uuid\":\"472a7a73-96cb-4736-b567-056d987cc5b4\"},\"uuid\":\"78d1fe0d-7e39-461e-81c3-a6a25f15ed69\"}]"
},
{
"template": "@ticket",
"output": "{assignee: Bob, body: What day is it?, topic: Weather, uuid: 78d1fe0d-7e39-461e-81c3-a6a25f15ed69}"
"output": "{assignee: Bob, topic: Weather, uuid: 78d1fe0d-7e39-461e-81c3-a6a25f15ed69}"
},
{
"template": "@(json(ticket))",
"output": "{\"assignee\":{\"email\":\"[email protected]\",\"first_name\":\"Bob\",\"name\":\"Bob\"},\"body\":\"What day is it?\",\"topic\":{\"name\":\"Weather\",\"uuid\":\"472a7a73-96cb-4736-b567-056d987cc5b4\"},\"uuid\":\"78d1fe0d-7e39-461e-81c3-a6a25f15ed69\"}"
"output": "{\"assignee\":{\"email\":\"[email protected]\",\"first_name\":\"Bob\",\"name\":\"Bob\"},\"topic\":{\"name\":\"Weather\",\"uuid\":\"472a7a73-96cb-4736-b567-056d987cc5b4\"},\"uuid\":\"78d1fe0d-7e39-461e-81c3-a6a25f15ed69\"}"
},
{
"template": "@(json(contact))",
Expand Down Expand Up @@ -449,7 +449,6 @@
"first_name": "Bob",
"name": "Bob"
},
"body": "What day is it?",
"topic": {
"name": "Weather",
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4"
Expand Down Expand Up @@ -529,7 +528,6 @@
"first_name": "Bob",
"name": "Bob"
},
"body": "What day is it?",
"topic": {
"name": "Weather",
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4"
Expand Down Expand Up @@ -750,7 +748,6 @@
"first_name": "Bob",
"name": "Bob"
},
"body": "What day is it?",
"topic": {
"name": "Weather",
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4"
Expand Down
22 changes: 14 additions & 8 deletions flows/events/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestEventMarshaling(t *testing.T) {
weather := session.Assets().Topics().Get("472a7a73-96cb-4736-b567-056d987cc5b4")
user := session.Assets().Users().Get("[email protected]")
facebook := session.Assets().Channels().Get("4bb288a0-7fca-4da1-abe8-59a593aff648")
ticket := flows.NewTicket("7481888c-07dd-47dc-bf22-ef7448696ffe", weather, "Where are my cookies?", user)
ticket := flows.NewTicket("7481888c-07dd-47dc-bf22-ef7448696ffe", weather, user)

eventTests := []struct {
event flows.Event
Expand Down Expand Up @@ -309,16 +309,15 @@ func TestEventMarshaling(t *testing.T) {
"name": "Ryan Lewis",
"status": "active",
"ticket": {
"assignee": {
"email": "[email protected]",
"name": "Bob"
},
"body": "What day is it?",
"uuid": "78d1fe0d-7e39-461e-81c3-a6a25f15ed69",
"topic": {
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"uuid": "78d1fe0d-7e39-461e-81c3-a6a25f15ed69"
"assignee": {
"email": "[email protected]",
"name": "Bob"
}
},
"timezone": "America/Guayaquil",
"urns": [
Expand Down Expand Up @@ -611,6 +610,14 @@ func TestEventMarshaling(t *testing.T) {
}
}`,
},
{
events.NewTicketNoteAdded("this is weird"),
`{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"note": "this is weird"
}`,
},
{
events.NewTicketOpened(ticket),
`{
Expand All @@ -622,7 +629,6 @@ func TestEventMarshaling(t *testing.T) {
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"body": "Where are my cookies?",
"assignee": {
"email": "[email protected]",
"name": "Bob"
Expand Down
35 changes: 35 additions & 0 deletions flows/events/ticket_note_added.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package events

import (
"github.com/nyaruka/goflow/flows"
)

func init() {
registerType(TypeTicketNoteAdded, func() flows.Event { return &TicketNoteAddedEvent{} })
}

// TypeTicketNoteAdded is the type for our ticket note added events
const TypeTicketNoteAdded string = "ticket_note_added"

// TicketNoteAddedEvent events are created when a note is added to the currently open ticket.
//
// {
// "type": "ticket_note_added",
// "created_on": "2006-01-02T15:04:05Z",
// "note": "this is weird"
// }
//
// @event ticket_note_added
type TicketNoteAddedEvent struct {
BaseEvent

Note string `json:"note"`
}

// NewTicketNoteAdded returns a new ticket note added event
func NewTicketNoteAdded(note string) *TicketNoteAddedEvent {
return &TicketNoteAddedEvent{
BaseEvent: NewBaseEvent(TypeTicketNoteAdded),
Note: note,
}
}
3 changes: 0 additions & 3 deletions flows/events/ticket_opened.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const TypeTicketOpened string = "ticket_opened"
type Ticket struct {
UUID flows.TicketUUID `json:"uuid" validate:"required,uuid4"`
Topic *assets.TopicReference `json:"topic" validate:"omitempty"`
Body string `json:"body"`
Assignee *assets.UserReference `json:"assignee,omitempty" validate:"omitempty"`
}

Expand All @@ -30,7 +29,6 @@ type Ticket struct {
// "uuid": "add17edf-0b6e-4311-bcd7-a64b2a459157",
// "name": "Weather"
// },
// "body": "Where are my cookies?",
// "assignee": {"email": "[email protected]", "name": "Bob"}
// }
// }
Expand All @@ -49,7 +47,6 @@ func NewTicketOpened(ticket *flows.Ticket) *TicketOpenedEvent {
Ticket: &Ticket{
UUID: ticket.UUID(),
Topic: ticket.Topic().Reference(),
Body: ticket.Body(),
Assignee: ticket.Assignee().Reference(),
},
}
Expand Down
4 changes: 2 additions & 2 deletions flows/inspect/issues/testdata/legacy_vars.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
},
"issues": [
{
"type": "legacy_vars",
"node_uuid": "a58be63b-907d-4a1a-856b-0bb5579d7507",
"action_uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
"description": "use of expressions instead of contact query",
"node_uuid": "a58be63b-907d-4a1a-856b-0bb5579d7507",
"type": "legacy_vars",
"vars": [
"@contact.uuid",
"@fields.friend_tel"
Expand Down
Loading

0 comments on commit c95e757

Please sign in to comment.