From 47c4f9cb79167e25b7ff2298231e7b446b9ca19b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 18 Sep 2023 16:39:02 -0500 Subject: [PATCH] Rename optin_sent to optin_created for consistency with msg_created and add channel/URN properties --- flows/actions/send_optin.go | 11 ++++-- flows/actions/testdata/send_optin.json | 9 +++-- flows/events/base_test.go | 12 +++++-- flows/events/optin_created.go | 49 ++++++++++++++++++++++++++ flows/events/optin_sent.go | 39 -------------------- 5 files changed, 73 insertions(+), 47 deletions(-) create mode 100644 flows/events/optin_created.go delete mode 100644 flows/events/optin_sent.go diff --git a/flows/actions/send_optin.go b/flows/actions/send_optin.go index d44e31740..d18e1f959 100644 --- a/flows/actions/send_optin.go +++ b/flows/actions/send_optin.go @@ -15,7 +15,7 @@ const TypeSendOptIn string = "send_optin" // SendOptInAction can be used to send an optin to the contact if the channel supports that. // -// An [event:optin_sent] event will be created if the optin was sent. +// An [event:optin_created] event will be created if the optin was sent. // // { // "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9", @@ -47,8 +47,13 @@ func (a *SendOptInAction) Execute(run flows.Run, step flows.Step, logModifier fl optIn := run.Session().Assets().OptIns().Get(a.OptIn.UUID) destinations := run.Contact().ResolveDestinations(false) - if len(destinations) > 0 && destinations[0].Channel.HasFeature(assets.ChannelFeatureOptIns) { - logEvent(events.NewOptInSent(optIn)) + if len(destinations) > 0 { + ch := destinations[0].Channel + urn := destinations[0].URN + + if ch.HasFeature(assets.ChannelFeatureOptIns) { + logEvent(events.NewOptInCreated(optIn, ch, urn.URN())) + } } return nil diff --git a/flows/actions/testdata/send_optin.json b/flows/actions/testdata/send_optin.json index 49020a8c7..4b7cfc898 100644 --- a/flows/actions/testdata/send_optin.json +++ b/flows/actions/testdata/send_optin.json @@ -35,13 +35,18 @@ }, "events": [ { - "type": "optin_sent", + "type": "optin_created", "created_on": "2018-10-18T14:20:30.000123456Z", "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", "optin": { "name": "Joke Of The Day", "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb" - } + }, + "channel": { + "uuid": "4bb288a0-7fca-4da1-abe8-59a593aff648", + "name": "Facebook Channel" + }, + "urn": "facebook:1234567890" } ] } diff --git a/flows/events/base_test.go b/flows/events/base_test.go index 5e6e82f9f..dd379a152 100644 --- a/flows/events/base_test.go +++ b/flows/events/base_test.go @@ -49,6 +49,7 @@ func TestEventMarshaling(t *testing.T) { mailgun := session.Assets().Ticketers().Get("19dc6346-9623-4fe4-be80-538d493ecdf5") weather := session.Assets().Topics().Get("472a7a73-96cb-4736-b567-056d987cc5b4") user := session.Assets().Users().Get("bob@nyaruka.com") + facebook := session.Assets().Channels().Get("4bb288a0-7fca-4da1-abe8-59a593aff648") ticket := flows.NewTicket("7481888c-07dd-47dc-bf22-ef7448696ffe", mailgun, weather, "Where are my cookies?", "1243252", user) eventTests := []struct { @@ -545,14 +546,19 @@ func TestEventMarshaling(t *testing.T) { }`, }, { - events.NewOptInSent(jotd), + events.NewOptInCreated(jotd, facebook, urns.URN("facebook:1234567890")), `{ - "type": "optin_sent", + "type": "optin_created", "created_on": "2018-10-18T14:20:30.000123456Z", "optin": { "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", "name": "Joke Of The Day" - } + }, + "channel": { + "uuid": "4bb288a0-7fca-4da1-abe8-59a593aff648", + "name": "Facebook Channel" + }, + "urn": "facebook:1234567890" }`, }, { diff --git a/flows/events/optin_created.go b/flows/events/optin_created.go new file mode 100644 index 000000000..ff8dca5ef --- /dev/null +++ b/flows/events/optin_created.go @@ -0,0 +1,49 @@ +package events + +import ( + "github.com/nyaruka/gocommon/urns" + "github.com/nyaruka/goflow/assets" + "github.com/nyaruka/goflow/flows" +) + +func init() { + registerType(TypeOptInCreated, func() flows.Event { return &OptInCreatedEvent{} }) +} + +// TypeOptInCreated is our type for the optin event +const TypeOptInCreated string = "optin_created" + +// OptInCreatedEvent events are created when an action has created an optin to be sent. +// +// { +// "type": "optin_created", +// "created_on": "2006-01-02T15:04:05Z", +// "optin": { +// "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", +// "name": "Joke Of The Day" +// }, +// "channel": { +// "uuid": "4bb288a0-7fca-4da1-abe8-59a593aff648", +// "name": "Facebook" +// }, +// "urn": "tel:+12065551212" +// } +// +// @event optin_created +type OptInCreatedEvent struct { + BaseEvent + + OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` + Channel *assets.ChannelReference `json:"channel" validate:"required,dive"` + URN urns.URN `json:"urn" validate:"required"` +} + +// NewOptInCreated returns a new optin sent event +func NewOptInCreated(optIn *flows.OptIn, ch *flows.Channel, urn urns.URN) *OptInCreatedEvent { + return &OptInCreatedEvent{ + BaseEvent: NewBaseEvent(TypeOptInCreated), + OptIn: optIn.Reference(), + Channel: ch.Reference(), + URN: urn, + } +} diff --git a/flows/events/optin_sent.go b/flows/events/optin_sent.go deleted file mode 100644 index bad9ff89a..000000000 --- a/flows/events/optin_sent.go +++ /dev/null @@ -1,39 +0,0 @@ -package events - -import ( - "github.com/nyaruka/goflow/assets" - "github.com/nyaruka/goflow/flows" -) - -func init() { - registerType(TypeOptInSent, func() flows.Event { return &OptInSentEvent{} }) -} - -// TypeOptInSent is our type for the optin event -const TypeOptInSent string = "optin_sent" - -// OptInSentEvent events are created when an action has sent an optin. -// -// { -// "type": "optin_sent", -// "created_on": "2006-01-02T15:04:05Z", -// "optin": { -// "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", -// "name": "Joke Of The Day" -// } -// } -// -// @event optin_sent -type OptInSentEvent struct { - BaseEvent - - OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` -} - -// NewOptInSent returns a new optin sent event -func NewOptInSent(optIn *flows.OptIn) *OptInSentEvent { - return &OptInSentEvent{ - BaseEvent: NewBaseEvent(TypeOptInSent), - OptIn: optIn.Reference(), - } -}