Skip to content

Commit

Permalink
Merge pull request #1018 from nyaruka/no_more_ticket_refs
Browse files Browse the repository at this point in the history
Get rid of ticket references
  • Loading branch information
rowanseymour authored Jun 9, 2021
2 parents d69e984 + e55ba07 commit 650cc05
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 151 deletions.
2 changes: 1 addition & 1 deletion flows/actions/open_ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (a *OpenTicketAction) Execute(run flows.FlowRun, step flows.Step, logModifi

ticket := a.open(run, step, ticketer, evaluatedSubject, evaluatedBody, logEvent)
if ticket != nil {
a.saveResult(run, step, a.ResultName, string(ticket.UUID), CategorySuccess, "", "", nil, logEvent)
a.saveResult(run, step, a.ResultName, string(ticket.UUID()), CategorySuccess, "", "", nil, logEvent)
} else {
a.saveResult(run, step, a.ResultName, "", CategoryFailure, "", "", nil, logEvent)
}
Expand Down
29 changes: 23 additions & 6 deletions flows/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewContact(
urns []urns.URN,
groups []*assets.GroupReference,
fields map[string]*Value,
tickets []*TicketReference,
tickets []*Ticket,
missing assets.MissingCallback) (*Contact, error) {

urnList, err := ReadURNList(sa, urns, missing)
Expand All @@ -86,7 +86,7 @@ func NewContact(

groupList := NewGroupList(sa, groups, missing)
fieldValues := NewFieldValues(sa, fields, missing)
ticketList := NewTicketList(sa, tickets, missing)
ticketList := NewTicketList(tickets)

return &Contact{
uuid: uuid,
Expand Down Expand Up @@ -118,7 +118,7 @@ func NewEmptyContact(sa SessionAssets, name string, language envs.Language, time
urns: URNList{},
groups: NewGroupList(sa, nil, assets.IgnoreMissing),
fields: make(FieldValues),
tickets: NewTicketList(sa, nil, assets.IgnoreMissing),
tickets: NewTicketList([]*Ticket{}),
assets: sa,
}
}
Expand Down Expand Up @@ -582,7 +582,7 @@ type contactEnvelope struct {
URNs []urns.URN `json:"urns,omitempty" validate:"dive,urn"`
Groups []*assets.GroupReference `json:"groups,omitempty" validate:"dive"`
Fields map[string]*Value `json:"fields,omitempty"`
Tickets []*TicketReference `json:"tickets,omitempty" validate:"dive"`
Tickets []json.RawMessage `json:"tickets,omitempty"`
}

// ReadContact decodes a contact from the passed in JSON
Expand Down Expand Up @@ -626,13 +626,30 @@ func ReadContact(sa SessionAssets, data json.RawMessage, missing assets.MissingC

c.groups = NewGroupList(sa, envelope.Groups, missing)
c.fields = NewFieldValues(sa, envelope.Fields, missing)
c.tickets = NewTicketList(sa, envelope.Tickets, missing)

tickets := make([]*Ticket, len(envelope.Tickets))
for i := range envelope.Tickets {
tickets[i], err = ReadTicket(sa, envelope.Tickets[i], missing)
if err != nil {
return nil, errors.Wrap(err, "unable to read ticket")
}
}
c.tickets = NewTicketList(tickets)

return c, nil
}

// MarshalJSON marshals this contact into JSON
func (c *Contact) MarshalJSON() ([]byte, error) {
var err error
tickets := make([]json.RawMessage, len(c.tickets.tickets))
for i, ticket := range c.tickets.tickets {
tickets[i], err = jsonx.Marshal(ticket)
if err != nil {
return nil, err
}
}

ce := &contactEnvelope{
Name: c.name,
UUID: c.uuid,
Expand All @@ -643,7 +660,7 @@ func (c *Contact) MarshalJSON() ([]byte, error) {
LastSeenOn: c.lastSeenOn,
URNs: c.urns.RawURNs(),
Groups: c.groups.references(),
Tickets: c.tickets.references(),
Tickets: tickets,
}

if c.timezone != nil {
Expand Down
20 changes: 19 additions & 1 deletion flows/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flows_test

import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"time"
Expand Down Expand Up @@ -34,6 +35,13 @@ func TestContact(t *testing.T) {
"roles": ["send", "receive"],
"country": "US"
}
],
"ticketers": [
{
"uuid": "d605bb96-258d-4097-ad0a-080937db2212",
"name": "Support Tickets",
"type": "mailgun"
}
]
}`))
require.NoError(t, err)
Expand Down Expand Up @@ -128,7 +136,7 @@ func TestContact(t *testing.T) {

assert.Equal(t, 0, contact.Tickets().Count())

ticket := flows.NewTicket(sa.Ticketers().Get("19dc6346-9623-4fe4-be80-538d493ecdf5"), "New ticket", "I have issues")
ticket := flows.OpenTicket(sa.Ticketers().Get("d605bb96-258d-4097-ad0a-080937db2212"), "New ticket", "I have issues")
contact.Tickets().Add(ticket)

assert.Equal(t, 1, contact.Tickets().Count())
Expand Down Expand Up @@ -166,6 +174,16 @@ func TestContact(t *testing.T) {
assert.True(t, contact.ClearURNs()) // did have URNs
assert.False(t, contact.ClearURNs())
assert.Equal(t, flows.URNList{}, contact.URNs())

marshaled, err := jsonx.Marshal(contact)
require.NoError(t, err)

fmt.Println(string(marshaled))

unmarshaled, err := flows.ReadContact(sa, marshaled, assets.PanicOnMissing)
require.NoError(t, err)

assert.True(t, contact.Equal(unmarshaled))
}

func TestReadContact(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions flows/events/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func TestEventMarshaling(t *testing.T) {
timeout := 500
gender := session.Assets().Fields().Get("gender")
mailgun := session.Assets().Ticketers().Get("19dc6346-9623-4fe4-be80-538d493ecdf5")
ticket := flows.NewTicket(mailgun, "Need help", "Where are my cookies?")
ticket.ExternalID = "1243252"
ticket := flows.NewTicket("7481888c-07dd-47dc-bf22-ef7448696ffe", mailgun, "Need help", "Where are my cookies?", "1243252")

eventTests := []struct {
event flows.Event
Expand Down Expand Up @@ -424,7 +423,7 @@ func TestEventMarshaling(t *testing.T) {
},
"text": "Hi there",
"urn": "tel:+12345678900",
"uuid": "04e910a5-d2e3-448b-958a-630e35c62431"
"uuid": "20cc4181-48cf-4344-9751-99419796decd"
},
"type": "ivr_created"
}`,
Expand Down Expand Up @@ -518,7 +517,7 @@ func TestEventMarshaling(t *testing.T) {
"type": "ticket_opened",
"created_on": "2018-10-18T14:20:30.000123456Z",
"ticket": {
"uuid": "20cc4181-48cf-4344-9751-99419796decd",
"uuid": "7481888c-07dd-47dc-bf22-ef7448696ffe",
"ticketer": {
"uuid": "19dc6346-9623-4fe4-be80-538d493ecdf5",
"name": "Support Tickets"
Expand Down
19 changes: 17 additions & 2 deletions flows/events/ticket_opened.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package events

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

Expand All @@ -11,6 +12,14 @@ func init() {
// TypeTicketOpened is the type for our ticket opened events
const TypeTicketOpened string = "ticket_opened"

type Ticket struct {
UUID flows.TicketUUID `json:"uuid" validate:"required,uuid4"`
Ticketer *assets.TicketerReference `json:"ticketer" validate:"required,dive"`
Subject string `json:"subject"`
Body string `json:"body"`
ExternalID string `json:"external_id,omitempty"`
}

// TicketOpenedEvent events are created when a new ticket is opened.
//
// {
Expand All @@ -32,13 +41,19 @@ const TypeTicketOpened string = "ticket_opened"
type TicketOpenedEvent struct {
baseEvent

Ticket *flows.TicketReference `json:"ticket"`
Ticket *Ticket `json:"ticket"`
}

// NewTicketOpened returns a new ticket opened event
func NewTicketOpened(ticket *flows.Ticket) *TicketOpenedEvent {
return &TicketOpenedEvent{
baseEvent: newBaseEvent(TypeTicketOpened),
Ticket: ticket.Reference(),
Ticket: &Ticket{
UUID: ticket.UUID(),
Ticketer: ticket.Ticketer().Reference(),
Subject: ticket.Subject(),
Body: ticket.Body(),
ExternalID: ticket.ExternalID(),
},
}
}
Loading

0 comments on commit 650cc05

Please sign in to comment.