Skip to content

Commit

Permalink
Merge pull request #59 from weni-ai/feat/seller-id-vtex
Browse files Browse the repository at this point in the history
Vtex search support with sellerId
  • Loading branch information
Robi9 authored Dec 7, 2023
2 parents 6d25cbe + f965259 commit 903c65d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 24 deletions.
6 changes: 4 additions & 2 deletions core/handlers/msg_catalog_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestMsgCatalogCreated(t *testing.T) {
false,
"",
"",
"",
true,
),
},
Expand All @@ -57,11 +58,12 @@ func TestMsgCatalogCreated(t *testing.T) {
false,
"",
"",
"",
true,
),
},
testdata.Bob: []flows.Action{
actions.NewSendMsgCatalog(handlers.NewActionUUID(), "No URNs", "", "", "View Products", "i want a water bottle", nil, false, "", "", false),
actions.NewSendMsgCatalog(handlers.NewActionUUID(), "No URNs", "", "", "View Products", "i want a water bottle", nil, false, "", "", "", false),
},
},
Msgs: handlers.ContactMsgMap{
Expand All @@ -70,7 +72,7 @@ func TestMsgCatalogCreated(t *testing.T) {
SQLAssertions: []handlers.SQLAssertion{
{
SQL: "SELECT COUNT(*) FROM msgs_msg WHERE contact_id = $1 AND metadata = $2 AND high_priority = TRUE",
Args: []interface{}{testdata.Cathy.ID, `{"action":"View Products","body":"Some products","products":["9f526c6f-b2cb-4457-8048-a7f1dc101e50","eb2305cc-bf39-43ad-a069-bbbfb6401acc"]}`},
Args: []interface{}{testdata.Cathy.ID, `{"action":"View Products","body":"Some products","products":["9f526c6f-b2cb-4457-8048-a7f1dc101e50","eb2305cc-bf39-43ad-a069-bbbfb6401acc"],"send_catalog":false}`},
Count: 2,
},
{
Expand Down
8 changes: 4 additions & 4 deletions core/models/catalog_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ func loadCatalog(ctx context.Context, db *sqlx.DB, orgID OrgID) ([]assets.MsgCat
}
defer rows.Close()

if err == sql.ErrNoRows || !rows.Next() {
return nil, nil
}

catalog := make([]assets.MsgCatalog, 0)
for rows.Next() {
msgCatalog := &MsgCatalog{}
Expand All @@ -175,6 +171,10 @@ func loadCatalog(ctx context.Context, db *sqlx.DB, orgID OrgID) ([]assets.MsgCat
catalog = append(catalog, msgCatalog)
}

if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "error iterating through rows")
}

logrus.WithField("elapsed", time.Since(start)).WithField("org_id", orgID).WithField("count", len(catalog)).Debug("loaded catalog")

return catalog, nil
Expand Down
2 changes: 0 additions & 2 deletions core/models/catalog_products_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models_test

import (
"fmt"
"testing"

"github.com/nyaruka/mailroom/core/models"
Expand Down Expand Up @@ -30,7 +29,6 @@ func TestCatalogProducts(t *testing.T) {
(uuid, facebook_catalog_id, "name", created_on, modified_on, is_active, channel_id, org_id)
VALUES('9bbe354d-cea6-408b-ba89-9ce28999da3f', '1234567891', 'Catalog Dummy2', now(), now(), false, $1, $2);
`, 123, testdata.Org2.ID)
fmt.Println(err)
assert.NoError(t, err)

ctpn, err := models.GetActiveCatalogFromChannel(ctx, *db, 123)
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.4.0-goflow-0.144.3
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.4.1-goflow-0.144.3
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.4.0-goflow-0.144.3 h1:CqWVO7qwOrTeZGdhuWTcYy2RvjUU9sxzMV7f25RIk6c=
github.com/weni-ai/goflow v0.4.0-goflow-0.144.3/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v0.4.1-goflow-0.144.3 h1:T4M8Unl869ouIFl39B7yOOIvL62cu0cyABdLwEvizzA=
github.com/weni-ai/goflow v0.4.1-goflow-0.144.3/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
102 changes: 89 additions & 13 deletions services/external/weni/service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package catalogs

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -105,14 +107,15 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log
productRetailerIDMap := make(map[string]struct{})
searchResult := []string{}
var trace *httpx.Trace
var traces []*httpx.Trace

for _, product := range productList {
if params.SearchType == "default" {
searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig)
callResult.Traces = append(callResult.Traces, trace)
} else if params.SearchType == "vtex" {
searchResult, trace, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType)
callResult.Traces = append(callResult.Traces, trace)
searchResult, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, params.SellerId)
callResult.Traces = append(callResult.Traces, traces...)
if searchResult == nil {
continue
}
Expand Down Expand Up @@ -227,39 +230,53 @@ func GetProductListFromChatGPT(ctx context.Context, rtConfig *runtime.Config, co
return products["products"], trace, nil
}

func GetProductListFromVtex(productSearch string, searchUrl string, apiType string) ([]string, *httpx.Trace, error) {
func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, sellerID string) ([]string, []*httpx.Trace, error) {
var result []string
var traces []*httpx.Trace
var trace *httpx.Trace
var err error

if apiType == "legacy" {
result, trace, err = VtexLegacySearch(searchUrl, productSearch)
result, traces, err = VtexLegacySearch(searchUrl, productSearch, sellerID)
if err != nil {
return nil, trace, err
return nil, traces, err
}
} else if apiType == "intelligent" {
result, trace, err = VtexIntelligentSearch(searchUrl, productSearch)
traces = append(traces, trace)
if err != nil {
return nil, trace, err
return nil, traces, err
}
}

return result, trace, nil
return result, traces, nil
}

func VtexLegacySearch(searchUrl string, productSearch string) ([]string, *httpx.Trace, error) {
type SearchSeller struct {
Items []struct {
ID string `json:"id"`
Quantity int `json:"quantity"`
Seller string `json:"seller"`
Availability string `json:"availability,omitempty"`
} `json:"items"`
}

func VtexLegacySearch(searchUrl string, productSearch string, sellerId string) ([]string, []*httpx.Trace, error) {
urlAfter := strings.TrimSuffix(searchUrl, "/")
url := fmt.Sprintf("%s/%s", urlAfter, productSearch)

traces := []*httpx.Trace{}

req, err := httpx.NewRequest("GET", url, nil, nil)
if err != nil {
return nil, nil, err
}

client := &http.Client{}
trace, err := httpx.DoTrace(client, req, nil, nil, -1)
traces = append(traces, trace)
if err != nil {
return nil, trace, err
return nil, traces, err
}

response := []struct {
Expand All @@ -270,24 +287,83 @@ func VtexLegacySearch(searchUrl string, productSearch string) ([]string, *httpx.

err = jsonx.Unmarshal(trace.ResponseBody, &response)
if err != nil {
return nil, trace, err
return nil, traces, err
}

result := []string{}

if len(response) == 0 {
return result, trace, nil
return result, traces, nil
}

var body SearchSeller

for i, product := range response {
if i == 5 {
break
}
product_retailer_id := product.Items[0].ItemId
result = append(result, product_retailer_id)

if sellerId != "" {
result = append(result, product_retailer_id)
body.Items = append(body.Items, struct {
ID string "json:\"id\""
Quantity int "json:\"quantity\""
Seller string "json:\"seller\""
Availability string "json:\"availability,omitempty\""
}{ID: product_retailer_id, Quantity: 1, Seller: sellerId})
} else {
result = append(result, product_retailer_id+"#1")
}
}

return result, trace, nil
if len(body.Items) > 0 {

urlSplit := strings.Split(urlAfter, "api")

urlSimulation := urlSplit[0] + "api/checkout/pub/orderForms/simulation"

headers := map[string]string{
"Accept": "application/json",
}
var b io.Reader
data, err := jsonx.Marshal(body)
if err != nil {
return nil, traces, err
}
b = bytes.NewReader(data)
headers["Content-Type"] = "application/json"
req, err := httpx.NewRequest("POST", urlSimulation, b, headers)
if err != nil {
return nil, traces, err
}

trace, err := httpx.DoTrace(client, req, nil, nil, -1)
traces = append(traces, trace)
if err != nil {
return nil, traces, err
}

if trace.Response.StatusCode >= 400 {
return nil, traces, fmt.Errorf("error when searching with seller: status code %d", trace.Response.StatusCode)
}

response := &SearchSeller{}

err = json.Unmarshal(trace.ResponseBody, response)
if err != nil {
return nil, traces, err
}

availableProducts := []string{}
for _, item := range response.Items {
if item.Availability == "available" {
availableProducts = append(availableProducts, item.ID+"#"+sellerId)
}
}
return availableProducts, traces, nil
}
return result, traces, nil
}

func VtexIntelligentSearch(searchUrl string, productSearch string) ([]string, *httpx.Trace, error) {
Expand Down
3 changes: 3 additions & 0 deletions services/external/weni/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestService(t *testing.T) {
SearchType: "default",
SearchUrl: "",
ApiType: "",
SellerId: "",
}
call, err := svc.Call(session, params, logger.Log)
assert.NoError(t, err)
Expand All @@ -127,6 +128,7 @@ func TestService(t *testing.T) {
SearchType: "vtex",
SearchUrl: "https://vtex.com.br/legacy/search",
ApiType: "legacy",
SellerId: "",
}
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
Expand All @@ -140,6 +142,7 @@ func TestService(t *testing.T) {
SearchType: "vtex",
SearchUrl: "https://vtex.com.br/intelligent/search",
ApiType: "intelligent",
SellerId: "",
}
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
Expand Down

0 comments on commit 903c65d

Please sign in to comment.