Skip to content

Commit

Permalink
Change optins to be channel agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 14, 2023
1 parent 233276a commit a0eee0b
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 97 deletions.
9 changes: 2 additions & 7 deletions assets/optin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ import (
// OptInUUID is the UUID of an opt in
type OptInUUID uuids.UUID

// OptIn are channel specific opt-ins
// OptIn are opt-ins for messaging campaign.
//
// {
// "uuid": "8925c76f-926b-4a63-a6eb-ab69e7a6b79b",
// "name": "Joke Of The Day",
// "channel": {
// "uuid": "204e5af9-42c3-4d46-8aab-ce204dff25b4",
// "name": "Facebook"
// }
// "name": "Joke Of The Day"
// }
//
// @asset optin
type OptIn interface {
UUID() OptInUUID
Name() string
Channel() *ChannelReference
}

// OptInReference is used to reference an opt in
Expand Down
13 changes: 4 additions & 9 deletions assets/static/optin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import (

// OptIn is a JSON serializable implementation of an optin asset
type OptIn struct {
UUID_ assets.OptInUUID `json:"uuid" validate:"required,uuid"`
Name_ string `json:"name" validate:"required"`
Channel_ *assets.ChannelReference `json:"channel" validate:"required,dive"`
UUID_ assets.OptInUUID `json:"uuid" validate:"required,uuid"`
Name_ string `json:"name" validate:"required"`
}

// NewOptIn creates a new topic
func NewOptIn(uuid assets.OptInUUID, name string, channel *assets.ChannelReference) assets.OptIn {
return &OptIn{
UUID_: uuid,
Name_: name,
Channel_: channel,
UUID_: uuid,
Name_: name,
}
}

Expand All @@ -25,6 +23,3 @@ func (t *OptIn) UUID() assets.OptInUUID { return t.UUID_ }

// Name returns the name of this ticketer
func (t *OptIn) Name() string { return t.Name_ }

// Channel returns a reference to this optin's channel
func (c *OptIn) Channel() *assets.ChannelReference { return c.Channel_ }
1 change: 0 additions & 1 deletion assets/static/optin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ func TestOptIn(t *testing.T) {
)
assert.Equal(t, assets.OptInUUID("37657cf7-5eab-4286-9cb0-bbf270587bad"), optin.UUID())
assert.Equal(t, "Weather Updates", optin.Name())
assert.Equal(t, assets.NewChannelReference("f4366920-cb05-47b9-a974-29be2d528984", "Facebook"), optin.Channel())
}
6 changes: 1 addition & 5 deletions assets/static/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ var assetsJSON = `{
"optins": [
{
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb",
"name": "Joke Of The Day",
"channel": {
"uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe",
"name": "Facebook"
}
"name": "Joke Of The Day"
}
],
"resthooks": [
Expand Down
4 changes: 1 addition & 3 deletions flows/engine/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ func NewSessionAssets(env envs.Environment, source assets.Source, migrationConfi

fieldAssets := flows.NewFieldAssets(fields)
groupAssets, _ := flows.NewGroupAssets(env, fieldAssets, groups)
channelAssets := flows.NewChannelAssets(channels)
optInAssets, _ := flows.NewOptInAssets(channelAssets, optIns)

return &sessionAssets{
source: source,
Expand All @@ -100,7 +98,7 @@ func NewSessionAssets(env envs.Environment, source assets.Source, migrationConfi
groups: groupAssets,
labels: flows.NewLabelAssets(labels),
locations: flows.NewLocationAssets(locations),
optIns: optInAssets,
optIns: flows.NewOptInAssets(optIns),
resthooks: flows.NewResthookAssets(resthooks),
templates: flows.NewTemplateAssets(templates),
ticketers: flows.NewTicketerAssets(ticketers),
Expand Down
6 changes: 1 addition & 5 deletions flows/engine/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ var assetsJSON = `{
"optins": [
{
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb",
"name": "Joke Of The Day",
"channel": {
"uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe",
"name": "Facebook"
}
"name": "Joke Of The Day"
}
],
"resthooks": [
Expand Down
32 changes: 7 additions & 25 deletions flows/optin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,21 @@ import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
"github.com/pkg/errors"
)

// OptIn adds some functionality to optin assets.
type OptIn struct {
assets.OptIn

channel *Channel
}

// NewOptIn returns a new optin object from the given optin asset
func NewOptIn(channels *ChannelAssets, asset assets.OptIn) (*OptIn, error) {
ch := channels.Get(asset.Channel().UUID)
if ch == nil {
return nil, errors.Errorf("no such channel with UUID %s", asset.Channel().UUID)
}

return &OptIn{OptIn: asset, channel: ch}, nil
func NewOptIn(asset assets.OptIn) *OptIn {
return &OptIn{OptIn: asset}
}

// Asset returns the underlying asset
func (o *OptIn) Asset() assets.OptIn { return o.OptIn }

// Channel returns the associated channel
func (o *OptIn) Channel() *Channel { return o.channel }

// Reference returns a reference to this optin
func (o *OptIn) Reference() *assets.OptInReference {
if o == nil {
Expand All @@ -42,15 +31,13 @@ func (o *OptIn) Reference() *assets.OptInReference {
//
// uuid:text -> the UUID of the optin
// name:text -> the name of the optin
// channel:channel -> the channel of the optin
//
// @context ticket
// @context optin
func (o *OptIn) Context(env envs.Environment) map[string]types.XValue {
return map[string]types.XValue{
"__default__": types.NewXText(string(o.Name())),
"uuid": types.NewXText(string(o.UUID())),
"name": types.NewXText(string(o.Name())),
"channel": Context(env, o.channel),
}
}

Expand All @@ -60,20 +47,15 @@ type OptInAssets struct {
}

// NewOptInAssets creates a new set of optin assets
func NewOptInAssets(channels *ChannelAssets, optins []assets.OptIn) (*OptInAssets, []assets.OptIn) {
broken := make([]assets.OptIn, 0)
func NewOptInAssets(optins []assets.OptIn) *OptInAssets {
s := &OptInAssets{
byUUID: make(map[assets.OptInUUID]*OptIn, len(optins)),
}
for _, asset := range optins {
optin, err := NewOptIn(channels, asset)
if err != nil {
broken = append(broken, asset)
} else {
s.byUUID[asset.UUID()] = optin
}
optin := NewOptIn(asset)
s.byUUID[asset.UUID()] = optin
}
return s, broken
return s
}

// Get finds the optin with the given UUID
Expand Down
27 changes: 1 addition & 26 deletions flows/optin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,10 @@ func TestOptIns(t *testing.T) {
env := envs.NewBuilder().Build()

source, err := static.NewSource([]byte(`{
"channels": [
{
"uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe",
"name": "Facebook",
"address": "457547478475",
"schemes": [
"facebook"
],
"roles": [
"send",
"receive"
]
}
],
"optins": [
{
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb",
"name": "Joke Of The Day",
"channel": {
"uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe",
"name": "Facebook"
}
"name": "Joke Of The Day"
}
]
}`))
Expand All @@ -55,20 +37,13 @@ func TestOptIns(t *testing.T) {
jotd := sa.OptIns().Get("248be71d-78e9-4d71-a6c4-9981d369e5cb")
assert.Equal(t, assets.OptInUUID("248be71d-78e9-4d71-a6c4-9981d369e5cb"), jotd.UUID())
assert.Equal(t, "Joke Of The Day", jotd.Name())
assert.Equal(t, "Facebook", jotd.Channel().Name())
assert.Equal(t, assets.NewOptInReference("248be71d-78e9-4d71-a6c4-9981d369e5cb", "Joke Of The Day"), jotd.Reference())

// check use in expressions
test.AssertXEqual(t, types.NewXObject(map[string]types.XValue{
"__default__": types.NewXText("Joke Of The Day"),
"uuid": types.NewXText("248be71d-78e9-4d71-a6c4-9981d369e5cb"),
"name": types.NewXText("Joke Of The Day"),
"channel": types.NewXObject(map[string]types.XValue{
"__default__": types.NewXText("Facebook"),
"uuid": types.NewXText("58e9b092-fe42-4173-876c-ff45a14a24fe"),
"name": types.NewXText("Facebook"),
"address": types.NewXText("457547478475"),
}),
}), flows.Context(env, jotd))

assert.Nil(t, (*flows.OptIn)(nil).Reference())
Expand Down
6 changes: 1 addition & 5 deletions flows/triggers/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
"optins": [
{
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb",
"name": "Joke Of The Day",
"channel": {
"uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe",
"name": "Facebook"
}
"name": "Joke Of The Day"
}
],
"ticketers": [
Expand Down
7 changes: 1 addition & 6 deletions flows/triggers/testdata/optin.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@
"keyword": "",
"optin": {
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb",
"name": "Joke Of The Day",
"channel": {
"uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe",
"name": "Facebook",
"address": "457547478475"
}
"name": "Joke Of The Day"
},
"origin": "",
"params": {},
Expand Down
6 changes: 1 addition & 5 deletions test/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ var sessionAssets = `{
"optins": [
{
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb",
"name": "Joke Of The Day",
"channel": {
"uuid": "4bb288a0-7fca-4da1-abe8-59a593aff648",
"name": "Facebook"
}
"name": "Joke Of The Day"
}
],
"ticketers": [
Expand Down

0 comments on commit a0eee0b

Please sign in to comment.