From 62222efb5cbddfdad16b657c895ac5c37a6ced5b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 21 Sep 2023 12:12:57 -0500 Subject: [PATCH] Rename send_optin to request_optin and optin_created to optin_requested --- CHANGELOG.md | 4 +- flows/actions/base_test.go | 4 +- flows/actions/request_optin.go | 60 +++++++++++++++++++ flows/actions/send_optin.go | 60 ------------------- .../{send_optin.json => request_optin.json} | 6 +- flows/events/base_test.go | 4 +- .../{optin_created.go => optin_requested.go} | 22 +++---- 7 files changed, 80 insertions(+), 80 deletions(-) create mode 100644 flows/actions/request_optin.go delete mode 100644 flows/actions/send_optin.go rename flows/actions/testdata/{send_optin.json => request_optin.json} (93%) rename flows/events/{optin_created.go => optin_requested.go} (56%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02ff47740..7b456a2e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ v0.195.2 (2023-09-18) ------------------------- - * Rename optin_sent to optin_created for consistency with msg_created and add channel/URN properties + * Rename optin_sent to optin_requested for consistency with msg_created and add channel/URN properties v0.195.1 (2023-09-18) ------------------------- @@ -8,7 +8,7 @@ v0.195.1 (2023-09-18) v0.195.0 (2023-09-14) ------------------------- - * Add send_optin action and optin_sent event + * Add request_optin action and optin_sent event * Add features to channel assets and define optins as a feature * Add trigger type for optins * Add OptIn as new asset type diff --git a/flows/actions/base_test.go b/flows/actions/base_test.go index 659c6a684..319d8d3d0 100644 --- a/flows/actions/base_test.go +++ b/flows/actions/base_test.go @@ -571,12 +571,12 @@ func TestConstructors(t *testing.T) { }`, }, { - actions.NewSendOptIn( + actions.NewRequestOptIn( actionUUID, assets.NewOptInReference("248be71d-78e9-4d71-a6c4-9981d369e5cb", "Joke Of The Day"), ), `{ - "type": "send_optin", + "type": "request_optin", "uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", "optin": { "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", diff --git a/flows/actions/request_optin.go b/flows/actions/request_optin.go new file mode 100644 index 000000000..183e17088 --- /dev/null +++ b/flows/actions/request_optin.go @@ -0,0 +1,60 @@ +package actions + +import ( + "github.com/nyaruka/goflow/assets" + "github.com/nyaruka/goflow/flows" + "github.com/nyaruka/goflow/flows/events" +) + +func init() { + registerType(TypeRequestOptIn, func() flows.Action { return &RequestOptInAction{} }) +} + +// TypeRequestOptIn is the type for the send optin action +const TypeRequestOptIn string = "request_optin" + +// RequestOptInAction can be used to send an optin to the contact if the channel supports that. +// +// An [event:optin_requested] event will be created if the optin was requested. +// +// { +// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9", +// "type": "request_optin", +// "optin": { +// "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", +// "name": "Joke Of The Day" +// } +// } +// +// @action request_optin +type RequestOptInAction struct { + baseAction + onlineAction + + OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` +} + +// NewRequestOptIn creates a new request optin action +func NewRequestOptIn(uuid flows.ActionUUID, optIn *assets.OptInReference) *RequestOptInAction { + return &RequestOptInAction{ + baseAction: newBaseAction(TypeRequestOptIn, uuid), + OptIn: optIn, + } +} + +// Execute creates the optin events +func (a *RequestOptInAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error { + optIn := run.Session().Assets().OptIns().Get(a.OptIn.UUID) + destinations := run.Contact().ResolveDestinations(false) + + if len(destinations) > 0 { + ch := destinations[0].Channel + urn := destinations[0].URN + + if ch.HasFeature(assets.ChannelFeatureOptIns) { + logEvent(events.NewOptInRequested(optIn, ch, urn.URN())) + } + } + + return nil +} diff --git a/flows/actions/send_optin.go b/flows/actions/send_optin.go deleted file mode 100644 index d18e1f959..000000000 --- a/flows/actions/send_optin.go +++ /dev/null @@ -1,60 +0,0 @@ -package actions - -import ( - "github.com/nyaruka/goflow/assets" - "github.com/nyaruka/goflow/flows" - "github.com/nyaruka/goflow/flows/events" -) - -func init() { - registerType(TypeSendOptIn, func() flows.Action { return &SendOptInAction{} }) -} - -// TypeSendOptIn is the type for the send optin action -const TypeSendOptIn string = "send_optin" - -// SendOptInAction can be used to send an optin to the contact if the channel supports that. -// -// An [event:optin_created] event will be created if the optin was sent. -// -// { -// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9", -// "type": "send_optin", -// "optin": { -// "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", -// "name": "Joke Of The Day" -// } -// } -// -// @action send_optin -type SendOptInAction struct { - baseAction - onlineAction - - OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` -} - -// NewSendOptIn creates a new send optin action -func NewSendOptIn(uuid flows.ActionUUID, optIn *assets.OptInReference) *SendOptInAction { - return &SendOptInAction{ - baseAction: newBaseAction(TypeSendOptIn, uuid), - OptIn: optIn, - } -} - -// Execute creates the optin events -func (a *SendOptInAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error { - optIn := run.Session().Assets().OptIns().Get(a.OptIn.UUID) - destinations := run.Contact().ResolveDestinations(false) - - 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/request_optin.json similarity index 93% rename from flows/actions/testdata/send_optin.json rename to flows/actions/testdata/request_optin.json index 4b7cfc898..697528555 100644 --- a/flows/actions/testdata/send_optin.json +++ b/flows/actions/testdata/request_optin.json @@ -2,7 +2,7 @@ { "description": "NOOP when channel doesn't support optins feature", "action": { - "type": "send_optin", + "type": "request_optin", "uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", "optin": { "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", @@ -26,7 +26,7 @@ "created_on": "2018-06-20T11:40:30.123456789-00:00" }, "action": { - "type": "send_optin", + "type": "request_optin", "uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", "optin": { "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", @@ -35,7 +35,7 @@ }, "events": [ { - "type": "optin_created", + "type": "optin_requested", "created_on": "2018-10-18T14:20:30.000123456Z", "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", "optin": { diff --git a/flows/events/base_test.go b/flows/events/base_test.go index dd379a152..dcf99c665 100644 --- a/flows/events/base_test.go +++ b/flows/events/base_test.go @@ -546,9 +546,9 @@ func TestEventMarshaling(t *testing.T) { }`, }, { - events.NewOptInCreated(jotd, facebook, urns.URN("facebook:1234567890")), + events.NewOptInRequested(jotd, facebook, urns.URN("facebook:1234567890")), `{ - "type": "optin_created", + "type": "optin_requested", "created_on": "2018-10-18T14:20:30.000123456Z", "optin": { "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", diff --git a/flows/events/optin_created.go b/flows/events/optin_requested.go similarity index 56% rename from flows/events/optin_created.go rename to flows/events/optin_requested.go index ff8dca5ef..1ae2f956f 100644 --- a/flows/events/optin_created.go +++ b/flows/events/optin_requested.go @@ -7,16 +7,16 @@ import ( ) func init() { - registerType(TypeOptInCreated, func() flows.Event { return &OptInCreatedEvent{} }) + registerType(TypeOptInRequested, func() flows.Event { return &OptInRequestedEvent{} }) } -// TypeOptInCreated is our type for the optin event -const TypeOptInCreated string = "optin_created" +// TypeOptInRequested is our type for the optin event +const TypeOptInRequested string = "optin_requested" -// OptInCreatedEvent events are created when an action has created an optin to be sent. +// OptInRequestedEvent events are created when an action has created an optin to be sent. // // { -// "type": "optin_created", +// "type": "optin_requested", // "created_on": "2006-01-02T15:04:05Z", // "optin": { // "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", @@ -29,8 +29,8 @@ const TypeOptInCreated string = "optin_created" // "urn": "tel:+12065551212" // } // -// @event optin_created -type OptInCreatedEvent struct { +// @event optin_requested +type OptInRequestedEvent struct { BaseEvent OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` @@ -38,10 +38,10 @@ type OptInCreatedEvent struct { 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), +// NewOptInRequested returns a new optin requested event +func NewOptInRequested(optIn *flows.OptIn, ch *flows.Channel, urn urns.URN) *OptInRequestedEvent { + return &OptInRequestedEvent{ + BaseEvent: NewBaseEvent(TypeOptInRequested), OptIn: optIn.Reference(), Channel: ch.Reference(), URN: urn,