Skip to content

Commit

Permalink
Refactor msg_catalog card
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Oct 25, 2023
1 parent f8564b2 commit d6d5fd0
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 110 deletions.
51 changes: 51 additions & 0 deletions assets/msg_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package assets

import (
"fmt"

"github.com/nyaruka/gocommon/uuids"
)

//type MsgCatalogUUID uuids.UUID

type MsgCatalog interface {
ChannelUUID() uuids.UUID
Name() string
Type() string
}

type MsgCatalogReference struct {
UUID uuids.UUID `json:"uuid" validate:"required,uuid"`
Name string `json:"name"`
}

type MsgCatalogCallAction struct {
Name string `json:"name"`
Value string `json:"value"`
}

func NewMsgCatalogReference(uuid uuids.UUID, name string) *MsgCatalogReference {
return &MsgCatalogReference{UUID: uuid, Name: name}
}

func (r *MsgCatalogReference) Type() string {
return "msg_catalog"
}

func (r *MsgCatalogReference) GenericUUID() uuids.UUID {
return uuids.UUID(r.UUID)
}

func (r *MsgCatalogReference) Identity() string {
return string(r.UUID)
}

func (r *MsgCatalogReference) Variable() bool {
return false
}

func (r *MsgCatalogReference) String() string {
return fmt.Sprintf("%s[uuid=%s,name=%s]", r.Type(), r.Identity(), r.Name)
}

var _ UUIDReference = (*MsgCatalogReference)(nil)
1 change: 1 addition & 0 deletions assets/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ type Source interface {
Ticketers() ([]Ticketer, error)
Topics() ([]Topic, error)
Users() ([]User, error)
MsgCatalogs() ([]MsgCatalog, error)
}
26 changes: 26 additions & 0 deletions assets/static/msg_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package static

import (
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
)

type MsgCatalog struct {
ChannelUUID_ uuids.UUID `json:"uuid" validate:"required,uuid`
Name_ string `json:"name"`
Type_ string `json:"type"`
}

func NewMsgCatalog(uuid uuids.UUID, name string, type_ string) assets.MsgCatalog {
return &MsgCatalog{
ChannelUUID_: uuid,
Name_: name,
Type_: type_,
}
}

func (mc *MsgCatalog) ChannelUUID() uuids.UUID { return mc.ChannelUUID_ }

func (mc *MsgCatalog) Name() string { return mc.Name_ }

func (mc *MsgCatalog) Type() string { return mc.Type_ }
9 changes: 9 additions & 0 deletions assets/static/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type StaticSource struct {
Groups []*Group `json:"groups" validate:"omitempty,dive"`
Labels []*Label `json:"labels" validate:"omitempty,dive"`
Locations []*envs.LocationHierarchy `json:"locations"`
MsgCatalogs []*MsgCatalog `json:"msgCatalogs" validate:"omitempty"`
Resthooks []*Resthook `json:"resthooks" validate:"omitempty,dive"`
Templates []*Template `json:"templates" validate:"omitempty,dive"`
Ticketers []*Ticketer `json:"ticketers" validate:"omitempty,dive"`
Expand Down Expand Up @@ -138,6 +139,14 @@ func (s *StaticSource) Locations() ([]assets.LocationHierarchy, error) {
return set, nil
}

func (s *StaticSource) MsgCatalogs() ([]assets.MsgCatalog, error) {
set := make([]assets.MsgCatalog, len(s.s.MsgCatalogs))
for i := range s.s.MsgCatalogs {
set[i] = s.s.MsgCatalogs[i]
}
return set, nil
}

// Resthooks returns all resthook assets
func (s *StaticSource) Resthooks() ([]assets.Resthook, error) {
set := make([]assets.Resthook, len(s.s.Resthooks))
Expand Down
2 changes: 1 addition & 1 deletion cmd/flowrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func PrintEvent(event flows.Event, out io.Writer) {
case *events.MsgCreatedEvent:
msg = fmt.Sprintf("💬 message created \"%s\"", typed.Msg.Text())
case *events.MsgCatalogCreatedEvent:
msg = fmt.Sprintf("🛒 message catalog created with products \"%s\"", strings.Join(typed.Msg.Products(), ", "))
msg = fmt.Sprintf("🛒 message catalog created with products \"%s\"", typed.Msg.Products())
case *events.MsgReceivedEvent:
msg = fmt.Sprintf("📥 message received \"%s\"", typed.Msg.Text())
case *events.MsgWaitEvent:
Expand Down
57 changes: 47 additions & 10 deletions flows/actions/send_msg_catalog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package actions

import (
"fmt"

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
Expand Down Expand Up @@ -46,10 +49,11 @@ type SendMsgCatalogAction struct {
universalAction
createMsgCatalogAction

AllURNs bool `json:"all_urns,omitempty"`
Templating *Templating `json:"templating,omitempty" validate:"omitempty,dive"`
Topic flows.MsgTopic `json:"topic,omitempty" validate:"omitempty,msg_topic"`
ResultName string `json:"result_name,omitempty"`
MsgCatalog *assets.MsgCatalogReference `json:"msg_catalog,omitempty"`
AllURNs bool `json:"all_urns,omitempty"`
Templating *Templating `json:"templating,omitempty" validate:"omitempty,dive"`
Topic flows.MsgTopic `json:"topic,omitempty" validate:"omitempty,msg_topic"`
ResultName string `json:"result_name,omitempty"`
}

type createMsgCatalogAction struct {
Expand Down Expand Up @@ -119,20 +123,30 @@ func (a *SendMsgCatalogAction) Execute(run flows.FlowRun, step flows.Step, logMo
channelRef = assets.NewChannelReference(dest.Channel.UUID(), dest.Channel.Name())
}

msg := flows.NewMsgCatalog(dest.URN.URN(), channelRef, evaluatedHeader, evaluatedBody, evaluatedFooter, a.ProductViewSettings.Action, evaluatedSearch, products, a.AutomaticSearch, a.Topic)
/////////////////////

msgCatalog := run.Session().Assets().MsgCatalog()
mc := msgCatalog.Get(uuids.UUID(channelRef.UUID))
c, err := a.call(run, step, mc, logEvent)
if err != nil {
a.saveResult(run, step, a.ResultName, fmt.Sprintf("%s", err), CategoryFailure, "", "", nil, logEvent)
}

input := fmt.Sprintf("%s %s", c.RequestMethod, c.RequestURL)
a.saveResult(run, step, a.ResultName, string(c.ResponseJSON), CategorySuccess, "", input, c.ResponseJSON, logEvent)

////////////////////

msg := flows.NewMsgCatalogOut(dest.URN.URN(), channelRef, evaluatedHeader, evaluatedBody, evaluatedFooter, a.ProductViewSettings.Action, evaluatedSearch, products, a.AutomaticSearch, a.Topic)
logEvent(events.NewMsgCatalogCreated(msg))
}

// if we couldn't find a destination, create a msg without a URN or channel and it's up to the caller
// to handle that as they want
if len(destinations) == 0 {
msg := flows.NewMsgCatalog(urns.NilURN, nil, evaluatedHeader, evaluatedBody, evaluatedFooter, a.ProductViewSettings.Action, evaluatedSearch, products, a.AutomaticSearch, a.Topic)
msg := flows.NewMsgCatalogOut(urns.NilURN, nil, evaluatedHeader, evaluatedBody, evaluatedFooter, a.ProductViewSettings.Action, evaluatedSearch, products, a.AutomaticSearch, a.Topic)
logEvent(events.NewMsgCatalogCreated(msg))
}

// TODO: saveResult with CategoryFailure
a.saveResult(run, step, a.ResultName, "SUCCESS RESULT", CategorySuccess, "", "SUCCESS RESULT", nil, logEvent)

return nil
}

Expand All @@ -141,3 +155,26 @@ var msgCatalogCategories = []string{CategorySuccess, CategoryFailure}
func (a *SendMsgCatalogAction) Results(include func(*flows.ResultInfo)) {
include(flows.NewResultInfo(a.ResultName, msgCatalogCategories))
}

func (a *SendMsgCatalogAction) call(run flows.FlowRun, step flows.Step, msgCatalog *flows.MsgCatalog, logEvent flows.EventCallback) (*flows.MsgCatalogCall, error) {
if msgCatalog == nil {
logEvent(events.NewDependencyError(a.MsgCatalog))
return nil, nil
}

svc, err := run.Session().Engine().Services().MsgCatalog(run.Session(), msgCatalog)
if err != nil {
logEvent(events.NewError(err))
return nil, nil
}

httpLogger := &flows.HTTPLogger{}

call, err := svc.Call(run.Session(), httpLogger.Log)
if err != nil {
logEvent(events.NewError(err))
return nil, err
}

return call, nil
}
7 changes: 7 additions & 0 deletions flows/engine/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type sessionAssets struct {
groups *flows.GroupAssets
labels *flows.LabelAssets
locations *flows.LocationAssets
msgCatalog *flows.MsgCatalogAssets
resthooks *flows.ResthookAssets
templates *flows.TemplateAssets
ticketers *flows.TicketerAssets
Expand Down Expand Up @@ -64,6 +65,10 @@ func NewSessionAssets(env envs.Environment, source assets.Source, migrationConfi
if err != nil {
return nil, err
}
msgCatalog, err := source.MsgCatalogs()
if err != nil {
return nil, err
}
resthooks, err := source.Resthooks()
if err != nil {
return nil, err
Expand Down Expand Up @@ -99,6 +104,7 @@ func NewSessionAssets(env envs.Environment, source assets.Source, migrationConfi
groups: groupAssets,
labels: flows.NewLabelAssets(labels),
locations: flows.NewLocationAssets(locations),
msgCatalog: flows.NewMsgCatalogAssets(msgCatalog),
resthooks: flows.NewResthookAssets(resthooks),
templates: flows.NewTemplateAssets(templates),
ticketers: flows.NewTicketerAssets(ticketers),
Expand All @@ -122,6 +128,7 @@ func (s *sessionAssets) Templates() *flows.TemplateAssets { return
func (s *sessionAssets) Ticketers() *flows.TicketerAssets { return s.ticketers }
func (s *sessionAssets) Topics() *flows.TopicAssets { return s.topics }
func (s *sessionAssets) Users() *flows.UserAssets { return s.users }
func (s *sessionAssets) MsgCatalog() *flows.MsgCatalogAssets { return s.msgCatalog }

func (s *sessionAssets) ResolveField(key string) assets.Field {
f := s.Fields().Get(key)
Expand Down
4 changes: 4 additions & 0 deletions flows/engine/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func (s *testSource) Locations() ([]assets.LocationHierarchy, error) {
return nil, s.err("locations")
}

func (s *testSource) MsgCatalogs() ([]assets.MsgCatalog, error) {
return nil, s.err("msgCatalog")
}

func (s *testSource) Resthooks() ([]assets.Resthook, error) {
return nil, s.err("resthooks")
}
Expand Down
5 changes: 5 additions & 0 deletions flows/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (b *Builder) WithExternalServiceServiceFactory(f ExternalServiceServiceFact
return b
}

func (b *Builder) WithMsgCatalogServiceFactory(f MsgCatalogServiceFactory) *Builder {
b.eng.services.msgCatalog = f
return b
}

// WithMaxStepsPerSprint sets the maximum number of steps allowed in a single sprint
func (b *Builder) WithMaxStepsPerSprint(max int) *Builder {
b.eng.maxStepsPerSprint = max
Expand Down
10 changes: 10 additions & 0 deletions flows/engine/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ type AirtimeServiceFactory func(flows.Session) (flows.AirtimeService, error)
// ExternalServiceServiceFactory resolves a session to an external service service
type ExternalServiceServiceFactory func(flows.Session, *flows.ExternalService) (flows.ExternalServiceService, error)

type MsgCatalogServiceFactory func(flows.Session, *flows.MsgCatalog) (flows.MsgCatalogService, error)

type services struct {
email EmailServiceFactory
webhook WebhookServiceFactory
classification ClassificationServiceFactory
ticket TicketServiceFactory
airtime AirtimeServiceFactory
externalService ExternalServiceServiceFactory
msgCatalog MsgCatalogServiceFactory
}

func newEmptyServices() *services {
Expand All @@ -53,6 +56,9 @@ func newEmptyServices() *services {
externalService: func(flows.Session, *flows.ExternalService) (flows.ExternalServiceService, error) {
return nil, errors.New("no external service factory configured")
},
msgCatalog: func(flows.Session, *flows.MsgCatalog) (flows.MsgCatalogService, error) {
return nil, errors.New("no external service factory configured")
},
}
}

Expand All @@ -79,3 +85,7 @@ func (s *services) Airtime(session flows.Session) (flows.AirtimeService, error)
func (s *services) ExternalService(session flows.Session, externalService *flows.ExternalService) (flows.ExternalServiceService, error) {
return s.externalService(session, externalService)
}

func (s *services) MsgCatalog(session flows.Session, msgCatalog *flows.MsgCatalog) (flows.MsgCatalogService, error) {
return s.msgCatalog(session, msgCatalog)
}
4 changes: 2 additions & 2 deletions flows/events/msg_catalog_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const TypeMsgCatalogCreated string = "msg_catalog_created"
type MsgCatalogCreatedEvent struct {
baseEvent

Msg *flows.MsgCatalog `json:"msg" validate:"required,dive"`
Msg *flows.MsgCatalogOut `json:"msg" validate:"required,dive"`
}

// NewMsgCreated creates a new outgoing msg event to a single contact
func NewMsgCatalogCreated(msg *flows.MsgCatalog) *MsgCatalogCreatedEvent {
func NewMsgCatalogCreated(msg *flows.MsgCatalogOut) *MsgCatalogCreatedEvent {
return &MsgCatalogCreatedEvent{
baseEvent: newBaseEvent(TypeMsgCatalogCreated),
Msg: msg,
Expand Down
1 change: 1 addition & 0 deletions flows/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type SessionAssets interface {
Groups() *GroupAssets
Labels() *LabelAssets
Locations() *LocationAssets
MsgCatalog() *MsgCatalogAssets
Resthooks() *ResthookAssets
Templates() *TemplateAssets
Ticketers() *TicketerAssets
Expand Down
Loading

0 comments on commit d6d5fd0

Please sign in to comment.