Skip to content

Commit

Permalink
Implement opening ticket with note as two events
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 6, 2024
1 parent 04805d8 commit ad19d5d
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 64 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, nil, "Help"))
contact.SetTicket(flows.NewTicket("7f44b065-ec28-4d7a-bbb4-0bda3b75b19d", topic, nil))
}

// and switch their language
Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -68,6 +70,7 @@ func (a *OpenTicketAction) Execute(run flows.Run, step flows.Step, logModifier f
}

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

ticket := a.open(run, topic, assignee, evaluatedNote, logModifier, logEvent)
if ticket != nil {
Expand Down
57 changes: 39 additions & 18 deletions flows/actions/testdata/open_ticket.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@
"assignee": {
"email": "[email protected]",
"name": "Bob"
},
"note": "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 @@ -183,8 +188,7 @@
"assignee": {
"email": "[email protected]",
"name": "Bob"
},
"note": "Last message: Hi everybody"
}
}
},
"templates": [
Expand Down Expand Up @@ -229,10 +233,15 @@
"topic": {
"uuid": "0d9a2c56-6fc2-4f27-93c5-a6322e26b740",
"name": "General"
},
"note": "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"
},
"note": "Last message: Hi everybody"
}
}
},
"templates": [
Expand Down Expand Up @@ -333,10 +341,15 @@
"assignee": {
"email": "[email protected]",
"name": "Jim"
},
"note": "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 @@ -397,8 +410,7 @@
"assignee": {
"email": "[email protected]",
"name": "Jim"
},
"note": "Last message: Hi everybody"
}
}
},
"templates": [
Expand Down Expand Up @@ -450,10 +462,15 @@
"topic": {
"uuid": "472a7a73-96cb-4736-b567-056d987cc5b4",
"name": "Weather"
},
"note": "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"
},
"note": "Last message: Hi everybody"
}
}
},
"templates": [
Expand Down Expand Up @@ -558,10 +574,15 @@
"topic": {
"uuid": "0d9a2c56-6fc2-4f27-93c5-a6322e26b740",
"name": "General"
},
"note": "Where are my cookies? "
}
}
},
{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"note": "Where are my cookies? "
},
{
"type": "contact_groups_changed",
"created_on": "2018-10-18T14:20:30.000123456Z",
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, nil, "spam?")
ticket := flows.OpenTicket(weather, nil)
contact.SetTicket(ticket)

assert.NotNil(t, contact.Ticket())
Expand Down
13 changes: 10 additions & 3 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, user, "this is weird")
ticket := flows.NewTicket("7481888c-07dd-47dc-bf22-ef7448696ffe", weather, user)

eventTests := []struct {
event flows.Event
Expand Down Expand Up @@ -610,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 @@ -624,8 +632,7 @@ func TestEventMarshaling(t *testing.T) {
"assignee": {
"email": "[email protected]",
"name": "Bob"
},
"note": "this is weird"
}
}
}`,
},
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,
}
}
5 changes: 1 addition & 4 deletions flows/events/ticket_opened.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Ticket struct {
UUID flows.TicketUUID `json:"uuid" validate:"required,uuid4"`
Topic *assets.TopicReference `json:"topic" validate:"omitempty"`
Assignee *assets.UserReference `json:"assignee,omitempty" validate:"omitempty"`
Note string `json:"note,omitempty"`
}

// TicketOpenedEvent events are created when a new ticket is opened.
Expand All @@ -30,8 +29,7 @@ type Ticket struct {
// "uuid": "add17edf-0b6e-4311-bcd7-a64b2a459157",
// "name": "Weather"
// },
// "assignee": {"email": "[email protected]", "name": "Bob"},
// "note": "this is weird"
// "assignee": {"email": "[email protected]", "name": "Bob"}
// }
// }
//
Expand All @@ -50,7 +48,6 @@ func NewTicketOpened(ticket *flows.Ticket) *TicketOpenedEvent {
UUID: ticket.UUID(),
Topic: ticket.Topic().Reference(),
Assignee: ticket.Assignee().Reference(),
Note: ticket.Note(),
},
}
}
11 changes: 7 additions & 4 deletions flows/modifiers/testdata/ticket.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"assignee": {
"email": "[email protected]",
"name": "Bob"
},
"note": "this is a note"
}
}
},
"events": [
Expand All @@ -52,9 +51,13 @@
"assignee": {
"email": "[email protected]",
"name": "Bob"
},
"note": "this is a note"
}
}
},
{
"type": "ticket_note_added",
"created_on": "2018-10-18T14:20:30.000123456Z",
"note": "this is a note"
}
]
},
Expand Down
5 changes: 4 additions & 1 deletion flows/modifiers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ func (m *TicketModifier) Apply(eng flows.Engine, env envs.Environment, sa flows.
return false
}

ticket := flows.OpenTicket(m.topic, m.assignee, m.note)
ticket := flows.OpenTicket(m.topic, m.assignee)
log(events.NewTicketOpened(ticket))
if m.note != "" {
log(events.NewTicketNoteAdded(m.note))
}

contact.SetTicket(ticket)
return true
Expand Down
13 changes: 4 additions & 9 deletions flows/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@ type Ticket struct {
uuid TicketUUID
topic *Topic
assignee *User
note string
}

// NewTicket creates a new ticket
func NewTicket(uuid TicketUUID, topic *Topic, assignee *User, note string) *Ticket {
func NewTicket(uuid TicketUUID, topic *Topic, assignee *User) *Ticket {
return &Ticket{
uuid: uuid,
topic: topic,
assignee: assignee,
note: note,
}
}

// OpenTicket creates a new ticket. Used by ticketing services to open a new ticket.
func OpenTicket(topic *Topic, assignee *User, note string) *Ticket {
return NewTicket(TicketUUID(uuids.NewV4()), topic, assignee, note)
func OpenTicket(topic *Topic, assignee *User) *Ticket {
return NewTicket(TicketUUID(uuids.NewV4()), topic, assignee)
}

func (t *Ticket) UUID() TicketUUID { return t.uuid }
func (t *Ticket) Topic() *Topic { return t.topic }
func (t *Ticket) Assignee() *User { return t.assignee }
func (t *Ticket) Note() string { return t.note }

// Context returns the properties available in expressions
//
Expand All @@ -63,7 +60,6 @@ type ticketEnvelope struct {
UUID TicketUUID `json:"uuid" validate:"required,uuid4"`
Topic *assets.TopicReference `json:"topic" validate:"omitempty"`
Assignee *assets.UserReference `json:"assignee,omitempty" validate:"omitempty"`
Note string `json:"note,omitempty"`
}

// ReadTicket decodes a contact from the passed in JSON. If the topic or assigned user can't
Expand Down Expand Up @@ -91,7 +87,7 @@ func ReadTicket(sa SessionAssets, data []byte, missing assets.MissingCallback) (
}
}

return &Ticket{uuid: e.UUID, topic: topic, assignee: assignee, note: e.Note}, nil
return &Ticket{uuid: e.UUID, topic: topic, assignee: assignee}, nil
}

// MarshalJSON marshals this ticket into JSON
Expand All @@ -110,6 +106,5 @@ func (t *Ticket) MarshalJSON() ([]byte, error) {
UUID: t.uuid,
Topic: topicRef,
Assignee: assigneeRef,
Note: t.note,
})
}
Loading

0 comments on commit ad19d5d

Please sign in to comment.