diff --git a/flows/contact_test.go b/flows/contact_test.go index 2166ee631..95c287959 100644 --- a/flows/contact_test.go +++ b/flows/contact_test.go @@ -2,6 +2,7 @@ package flows_test import ( "encoding/json" + "fmt" "io/ioutil" "testing" "time" @@ -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) @@ -128,7 +136,7 @@ func TestContact(t *testing.T) { assert.Equal(t, 0, contact.Tickets().Count()) - ticket := flows.OpenTicket(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()) @@ -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) { diff --git a/flows/tickets.go b/flows/tickets.go index d2ca09055..4f803d1ae 100644 --- a/flows/tickets.go +++ b/flows/tickets.go @@ -7,7 +7,6 @@ import ( "github.com/nyaruka/goflow/envs" "github.com/nyaruka/goflow/excellent/types" "github.com/nyaruka/goflow/utils" - "github.com/pkg/errors" ) // TicketUUID is the UUID of a ticket @@ -66,7 +65,7 @@ func (t *Ticket) Context(env envs.Environment) map[string]types.XValue { type ticketEnvelope struct { UUID TicketUUID `json:"uuid" validate:"required,uuid4"` - Ticketer *assets.TicketerReference `json:"ticketer" validate:"required,dive"` + Ticketer *assets.TicketerReference `json:"ticketer" validate:"omitempty,dive"` Subject string `json:"subject"` Body string `json:"body"` ExternalID string `json:"external_id,omitempty"` @@ -78,7 +77,7 @@ func ReadTicket(sa SessionAssets, data []byte, missing assets.MissingCallback) ( e := &ticketEnvelope{} if err := utils.UnmarshalAndValidate(data, e); err != nil { - return nil, errors.Wrap(err, "unable to read ticket") + return nil, err } ticketer := sa.Ticketers().Get(e.Ticketer.UUID) diff --git a/flows/tickets_test.go b/flows/tickets_test.go index c1073a111..c33e7076b 100644 --- a/flows/tickets_test.go +++ b/flows/tickets_test.go @@ -50,6 +50,9 @@ func TestTickets(t *testing.T) { missingRefs = append(missingRefs, ref) } + _, err = flows.ReadTicket(sa, []byte(`{}`), missing) + assert.EqualError(t, err, "field 'uuid' is required") + ticket1, err := flows.ReadTicket(sa, []byte(`{ "uuid": "349c851f-3f8e-4353-8bf2-8e90b6d73530", "ticketer": {"uuid": "0a0b5ce4-35c9-47b7-b124-40258f0a5b53", "name": "Deleted"},