Skip to content

Commit

Permalink
Fix queries for channels and catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Oct 27, 2023
1 parent e89d6d9 commit 0e15e47
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
29 changes: 24 additions & 5 deletions core/models/catalog_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,42 @@ func RegisterMsgCatalogService(name string, initFunc MsgCatalogServiceFunc) {
}

const getActiveCatalogSQL = `
SELECT
id, uuid, facebook_catalog_id, name, created_on, modified_on, is_active, channel_id, org_id
FROM public.wpp_products_catalog
WHERE channel_id = $1 AND is_active = true
SELECT ROW_TO_JSON(r) FROM (SELECT
c.id as id,
c.uuid as uuid,
c.facebook_catalog_id as facebook_catalog_id,
c.name as name,
c.created_on as created_on,
c.modified_on as modified_on,
c.is_active as is_active,
c.channel_id as channel_id,
c.org_id as org_id
FROM
public.wpp_products_catalog c
WHERE
channel_id = $1 AND is_active = true
) r;
`

// GetActiveCatalogFromChannel returns the active catalog from the given channel
func GetActiveCatalogFromChannel(ctx context.Context, db sqlx.DB, channelID ChannelID) (*MsgCatalog, error) {
var catalog MsgCatalog

err := db.GetContext(ctx, &catalog.c, getActiveCatalogSQL, channelID)
rows, err := db.QueryxContext(ctx, getActiveCatalogSQL, channelID)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, errors.Wrapf(err, "error getting active catalog for channelID: %d", channelID)
}
defer rows.Close()

for rows.Next() {
err = dbutil.ReadJSONRow(rows, &catalog.c)
if err != nil {
return nil, err
}
}

return &catalog, nil
}
Expand Down
35 changes: 35 additions & 0 deletions core/models/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/lib/pq"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/mailroom/utils/dbutil"
Expand Down Expand Up @@ -146,6 +147,40 @@ func GetChannelsByID(ctx context.Context, db Queryer, ids []ChannelID) ([]*Chann
return channels, nil
}

const selectActiveChannelByUUIDSQL = `
SELECT ROW_TO_JSON(r) FROM (SELECT
c.id as id,
c.uuid as uuid,
c.name as name,
c.channel_type as channel_type,
COALESCE(c.tps, 10) as tps,
COALESCE(c.config, '{}')::json as config
FROM
channels_channel c
WHERE
c.uuid = $1
and c.is_active = TRUE
) r;
`

func GetActiveChannelByUUID(ctx context.Context, db Queryer, channelUUID uuids.UUID) (*Channel, error) {
rows, err := db.QueryxContext(ctx, selectActiveChannelByUUIDSQL, channelUUID)
if err != nil {
return nil, errors.Wrapf(err, "error querying channel by uuid")
}
defer rows.Close()

channel := &Channel{}
for rows.Next() {
err := dbutil.ReadJSONRow(rows, &channel.c)
if err != nil {
return nil, errors.Wrapf(err, "error unmarshalling channel")
}
}

return channel, nil
}

const selectChannelsByIDSQL = `
SELECT ROW_TO_JSON(r) FROM (SELECT
c.id as id,
Expand Down
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-7-develop
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-8-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-7-develop h1:Pw2ezzff85NDT8nqc0MhEyMIjZZuEjZivqGWr5pappc=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-7-develop/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-8-develop h1:wTZxMvM7bczn9lyUQEgatV16F2WXy4HYI0A/Q6qUKVI=
github.com/weni-ai/goflow v0.3.0-goflow-0.144.3-catalog-8-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
23 changes: 5 additions & 18 deletions services/external/weni/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/jmoiron/sqlx"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
Expand Down Expand Up @@ -74,20 +73,20 @@ func NewService(rtCfg *runtime.Config, httpClient *http.Client, httpRetries *htt
func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, logHTTP flows.HTTPLogCallback) (*flows.MsgCatalogCall, error) {
callResult := &flows.MsgCatalogCall{}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

content := params.ProductSearch
productList, err := GetProductListFromWeniGPT(s.rtConfig, content)
if err != nil {
return nil, err
}
channelUUID := params.ChannelUUID
channel, err := ChannelForChannelUUID(db, channelUUID)
channel, err := models.GetActiveChannelByUUID(ctx, db, channelUUID)
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

catalog, err := models.GetActiveCatalogFromChannel(ctx, *db, channel.ID())
if err != nil {
return nil, err
Expand Down Expand Up @@ -115,18 +114,6 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log
return callResult, nil
}

// ChannelForChannelUUID returns the channel id for the passed in channel UUID if any
func ChannelForChannelUUID(db *sqlx.DB, channelUUID uuids.UUID) (models.Channel, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
var channel models.Channel
err := db.GetContext(ctx, &channel, `SELECT id, uuid, parent_id, name, address, channel_type, tps, country, scheme, roles, match_prefixes, allow_international, machine_detection, config FROM channels_channel WHERE uuid = $1 AND is_active = TRUE`, channelUUID)
if err != nil {
return models.Channel{}, errors.Wrapf(err, "no channel found with uuid: %s", channelUUID)
}
return channel, nil
}

func GetProductListFromWeniGPT(rtConfig *runtime.Config, content string) ([]string, error) {
httpClient, httpRetries, _ := goflow.HTTP(rtConfig)
weniGPTClient := wenigpt.NewClient(httpClient, httpRetries, rtConfig.WeniGPTBaseURL, rtConfig.WeniGPTAuthToken, rtConfig.WeniGPTCookie)
Expand All @@ -144,7 +131,7 @@ func GetProductListFromWeniGPT(rtConfig *runtime.Config, content string) ([]stri

response, _, err := weniGPTClient.WeniGPTRequest(dr)
if err != nil {
return nil, errors.Wrapf(err, "error on wewnigpt call fot list products")
return nil, errors.Wrapf(err, "error on wenigpt call fot list products")
}

productsJson := response.Output.Text[0]
Expand Down

0 comments on commit 0e15e47

Please sign in to comment.