Skip to content

Commit

Permalink
Merge pull request #1187 from nyaruka/optin_created
Browse files Browse the repository at this point in the history
Rename `optin_sent` to `optin_created` for consistency with `msg_created` and add channel/URN properties
  • Loading branch information
rowanseymour authored Sep 18, 2023
2 parents 78562f6 + 47c4f9c commit 93ebb98
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 47 deletions.
11 changes: 8 additions & 3 deletions flows/actions/send_optin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions flows/actions/testdata/send_optin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
12 changes: 9 additions & 3 deletions flows/events/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]")
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 {
Expand Down Expand Up @@ -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"
}`,
},
{
Expand Down
49 changes: 49 additions & 0 deletions flows/events/optin_created.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
39 changes: 0 additions & 39 deletions flows/events/optin_sent.go

This file was deleted.

0 comments on commit 93ebb98

Please sign in to comment.