Skip to content

Commit

Permalink
Add channelUUID to MsgCatalog assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Oct 26, 2023
1 parent 41558fd commit b464f65
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
7 changes: 4 additions & 3 deletions core/models/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type OrgAssets struct {

msgCatalogs []assets.MsgCatalog
msgCatalogsByID map[CatalogID]*MsgCatalog
msgCatalogsByUUID map[assets.MsgCatalogUUID]*MsgCatalog
msgCatalogsByUUID map[assets.ChannelUUID]*MsgCatalog
}

var ErrNotFound = errors.New("not found")
Expand Down Expand Up @@ -391,10 +391,11 @@ func NewOrgAssets(ctx context.Context, rt *runtime.Runtime, orgID OrgID, prev *O
return nil, errors.Wrapf(err, "error loading catalogs for org %d", orgID)
}
oa.msgCatalogsByID = make(map[CatalogID]*MsgCatalog)
oa.msgCatalogsByUUID = make(map[assets.MsgCatalogUUID]*MsgCatalog)
oa.msgCatalogsByUUID = make(map[assets.ChannelUUID]*MsgCatalog)

for _, a := range oa.msgCatalogs {
oa.msgCatalogsByID[a.(*MsgCatalog).c.ID] = a.(*MsgCatalog)
oa.msgCatalogsByUUID[a.UUID()] = a.(*MsgCatalog)
oa.msgCatalogsByUUID[a.(*MsgCatalog).c.ChannelUUID] = a.(*MsgCatalog)
}
} else {
oa.msgCatalogs = prev.msgCatalogs
Expand Down
54 changes: 28 additions & 26 deletions core/models/catalog_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,22 @@ type MsgCatalog struct {
IsActive bool `db:"is_active"`
ChannelID ChannelID `db:"channel_id"`
OrgID OrgID `db:"org_id"`
ChannelUUID assets.ChannelUUID
Type string
}
}

func (c *MsgCatalog) ID() CatalogID { return c.c.ID }
func (c *MsgCatalog) UUID() assets.MsgCatalogUUID { return c.c.UUID }
func (c *MsgCatalog) FacebookCatalogID() string { return c.c.FacebookCatalogID }
func (c *MsgCatalog) Name() string { return c.c.Name }
func (c *MsgCatalog) CreatedOn() time.Time { return c.c.CreatedOn }
func (c *MsgCatalog) ModifiedOn() time.Time { return c.c.ModifiedOn }
func (c *MsgCatalog) IsActive() bool { return c.c.IsActive }
func (c *MsgCatalog) ChannelID() ChannelID { return c.c.ChannelID }
func (c *MsgCatalog) OrgID() OrgID { return c.c.OrgID }
func (c *MsgCatalog) Type() string { return c.c.Type }

// type MsgCatalog struct {
// e struct {
// ID CatalogID `json:"id,omitempty"`
// ChannelUUID uuids.UUID `json:"uuid,omitempty"`
// OrgID OrgID `json:"org_id,omitempty"`
// Name string `json:"name,omitempty"`
// Config map[string]string `json:"config,omitempty"`
// Type string `json:"type,omitempty"`
// }
// }

// func (c *MsgCatalog) ChannelUUID() uuids.UUID { return c.e.ChannelUUID }
// func (c *MsgCatalog) Name() string { return c.e.Name }
// func (c *MsgCatalog) Type() string { return c.e.Type }
func (c *MsgCatalog) ID() CatalogID { return c.c.ID }
func (c *MsgCatalog) UUID() assets.MsgCatalogUUID { return c.c.UUID }
func (c *MsgCatalog) FacebookCatalogID() string { return c.c.FacebookCatalogID }
func (c *MsgCatalog) Name() string { return c.c.Name }
func (c *MsgCatalog) CreatedOn() time.Time { return c.c.CreatedOn }
func (c *MsgCatalog) ModifiedOn() time.Time { return c.c.ModifiedOn }
func (c *MsgCatalog) IsActive() bool { return c.c.IsActive }
func (c *MsgCatalog) ChannelID() ChannelID { return c.c.ChannelID }
func (c *MsgCatalog) OrgID() OrgID { return c.c.OrgID }
func (c *MsgCatalog) Type() string { return c.c.Type }
func (c *MsgCatalog) ChannelUUID() assets.ChannelUUID { return c.c.ChannelUUID }

func init() {
goflow.RegisterMsgCatalogServiceFactory(msgCatalogServiceFactory)
Expand Down Expand Up @@ -135,7 +122,7 @@ func GetActiveCatalogFromChannel(ctx context.Context, db sqlx.DB, channelID Chan
return &catalog, nil
}

func loadCatalog(ctx context.Context, db sqlx.Queryer, orgID OrgID) ([]assets.MsgCatalog, error) {
func loadCatalog(ctx context.Context, db *sqlx.DB, orgID OrgID) ([]assets.MsgCatalog, error) {
start := time.Now()

rows, err := db.Queryx(selectOrgCatalogSQL, orgID)
Expand All @@ -151,6 +138,11 @@ func loadCatalog(ctx context.Context, db sqlx.Queryer, orgID OrgID) ([]assets.Ms
if err != nil {
return nil, errors.Wrapf(err, "error unmarshalling external service")
}
channelUUID, err := ChannelUUIDForChannelID(ctx, db, msgCatalog.ChannelID())
if err != nil {
return nil, err
}
msgCatalog.c.ChannelUUID = channelUUID
catalog = append(catalog, msgCatalog)
}

Expand Down Expand Up @@ -179,3 +171,13 @@ ORDER BY
c.created_on ASC
) r;
`

// ChannelForChannelID returns the channel for the passed in channel ID if any
func ChannelUUIDForChannelID(ctx context.Context, db *sqlx.DB, channelID ChannelID) (assets.ChannelUUID, error) {
var channelUUID assets.ChannelUUID
err := db.GetContext(ctx, &channelUUID, `SELECT uuid FROM channels_channel WHERE id = $1 AND is_active = TRUE`, channelID)
if err != nil {
return assets.ChannelUUID(""), errors.Wrapf(err, "no channel found with id: %s", channelID)
}
return channelUUID, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ go 1.17

replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni

replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-5-develop
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-6-develop
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-5-develop h1:kw/+ePhpVFBiL7siH6RjoSCXKEDP6Yu0vkucwfy57wU=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-5-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-6-develop h1:wVyTQJBcqePvIKZBZBxQVFiM7CabkZNFFtst6y19n4w=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-6-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down

0 comments on commit b464f65

Please sign in to comment.