From 4058c21fc84642d15b20f5f0d8b3435e29878c66 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Wed, 13 Nov 2024 16:26:33 -0300 Subject: [PATCH 01/14] Split items into batches for cart simulation --- services/external/weni/service.go | 127 ++++++++++++++++-------------- 1 file changed, 66 insertions(+), 61 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 0ee2cb2a8..6de421de3 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "fmt" - "io" "net/http" "net/url" "strconv" @@ -185,9 +184,10 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log hasSimulation := false if postalCode_ != "" && sellerID != "1" { + var tracesSimulation []*httpx.Trace hasSimulation = true - existingProductsIds, trace, err = CartSimulation(allProducts, sellerID, params.SearchUrl, postalCode_) - callResult.Traces = append(callResult.Traces, trace) + existingProductsIds, tracesSimulation, err = CartSimulation(allProducts, sellerID, params.SearchUrl, postalCode_) + callResult.Traces = append(callResult.Traces, tracesSimulation...) if err != nil { return callResult, err } @@ -406,16 +406,18 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri } type SearchSeller struct { - Items []struct { - ID string `json:"id"` - Quantity int `json:"quantity"` - Seller string `json:"seller"` - Availability string `json:"availability,omitempty"` - } `json:"items"` + Items []Item `json:"items"` PostalCode string `json:"postalCode"` Country string `json:"country"` } +type Item struct { + ID string `json:"id"` + Quantity int `json:"quantity"` + Seller string `json:"seller"` + Availability string `json:"availability,omitempty"` +} + type VtexProduct struct { ItemId string `json:"itemId"` } @@ -600,75 +602,78 @@ func VtexSponsoredSearch(searchUrl string, productSearch string, hideUnavailable } -func CartSimulation(products []string, sellerID string, url string, postalCode string) ([]string, *httpx.Trace, error) { - var trace *httpx.Trace - var body SearchSeller - result := []string{} +func CartSimulation(products []string, sellerID string, url string, postalCode string) ([]string, []*httpx.Trace, error) { + var traces []*httpx.Trace + var searchSeller SearchSeller + batchSize := 300 + availableProducts := []string{} + + if postalCode != "" { + searchSeller.PostalCode = postalCode + searchSeller.Country = "BRA" + } + + urlSplit := strings.Split(url, "api") + urlSimulation := urlSplit[0] + "api/checkout/pub/orderForms/simulation" for _, product := range products { product_retailer_id := product + searchSeller.Items = append(searchSeller.Items, Item{ID: product_retailer_id, Quantity: 1, Seller: sellerID}) - 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}) + if len(searchSeller.Items) == batchSize { + batchAvailableProducts, trace, err := sendBatchRequest(searchSeller, urlSimulation) + traces = append(traces, trace) + if err != nil { + return nil, traces, err + } + availableProducts = append(availableProducts, batchAvailableProducts...) + searchSeller.Items = []Item{} + } } - if len(body.Items) > 0 { - - if postalCode != "" { - body.PostalCode = postalCode - body.Country = "BRA" - } + return availableProducts, traces, nil +} - urlSplit := strings.Split(url, "api") +func sendBatchRequest(body SearchSeller, url string) ([]string, *httpx.Trace, error) { + client := &http.Client{} - urlSimulation := urlSplit[0] + "api/checkout/pub/orderForms/simulation" + headers := map[string]string{ + "Accept": "application/json", + } - headers := map[string]string{ - "Accept": "application/json", - } - var b io.Reader - data, err := jsonx.Marshal(body) - if err != nil { - return nil, trace, err - } - b = bytes.NewReader(data) - headers["Content-Type"] = "application/json" - req, err := httpx.NewRequest("POST", urlSimulation, b, headers) - if err != nil { - return nil, trace, err - } + data, err := jsonx.Marshal(body) + if err != nil { + return nil, nil, err + } - client := &http.Client{} - trace, err := httpx.DoTrace(client, req, nil, nil, -1) - if err != nil { - return nil, trace, err - } + req, err := httpx.NewRequest("POST", url, bytes.NewReader(data), headers) + if err != nil { + return nil, nil, err + } - if trace.Response.StatusCode >= 400 { - return nil, trace, fmt.Errorf("error when searching with seller: status code %d", trace.Response.StatusCode) - } + trace, err := httpx.DoTrace(client, req, nil, nil, -1) + if err != nil { + return nil, trace, err + } - response := &SearchSeller{} + if trace.Response.StatusCode >= 400 { + return nil, trace, fmt.Errorf("error when searching with seller: status code %d", trace.Response.StatusCode) + } - err = json.Unmarshal(trace.ResponseBody, response) - if err != nil { - return nil, trace, err - } + response := &SearchSeller{} + err = json.Unmarshal(trace.ResponseBody, response) + if err != nil { + return nil, trace, err + } - availableProducts := []string{} - for _, item := range response.Items { - if item.Availability == "available" { - availableProducts = append(availableProducts, item.ID) - } + availableProducts := []string{} + for _, item := range response.Items { + if item.Availability == "available" { + availableProducts = append(availableProducts, item.ID) } - return availableProducts, trace, nil } - return result, trace, nil + return availableProducts, trace, nil } // Filter represents the structure of the filter for the API request From c6e8ce04332a943acb9be1c86d2a50b7c12cbbdc Mon Sep 17 00:00:00 2001 From: Robi9 Date: Tue, 19 Nov 2024 18:04:50 -0300 Subject: [PATCH 02/14] Fix bugs and refactor product search --- services/external/weni/service.go | 107 ++++++++++++++++++------------ 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 3e2372c82..52b9ead5e 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -114,6 +114,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log var traces []*httpx.Trace var sellerID string var allProducts []string + existingProductsIds := []string{} qttProducts := 5 postalCode_ := strings.TrimSpace(params.PostalCode) @@ -142,7 +143,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log searchResult, trace, err = GetProductListFromSentenX(product, catalog.FacebookCatalogID(), searchThreshold, s.rtConfig) callResult.Traces = append(callResult.Traces, trace) } else if params.SearchType == "vtex" { - searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig, hasVtexAds, hideUnavailableItems) + searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig, hasVtexAds, hideUnavailableItems, sellerID) callResult.Traces = append(callResult.Traces, traces...) allProducts = append(allProducts, searchResult...) if searchResult == nil { @@ -173,15 +174,13 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log if len(searchResultSponsored) > 0 { hasSponsored = true - allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored+"#"+sellerID) + allProductsSponsored[0].ProductRetailerIDs = append(allProductsSponsored[0].ProductRetailerIDs, searchResultSponsored) } - } callResult.ProductRetailerIDS = productEntries - existingProductsIds := []string{} - + // simulates cart in VTEX with all products hasSimulation := false if postalCode_ != "" && sellerID != "1" { var tracesSimulation []*httpx.Trace @@ -193,6 +192,29 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } } + // adds '#sellerID' formatting to the end of all retailer IDs + for _, productEntry := range callResult.ProductRetailerIDS { + for i, retailerID := range productEntry.ProductRetailerIDs { + productEntry.ProductRetailerIDs[i] = retailerID + "#" + sellerID + } + } + + // search for products in Meta + retries := 2 + var productSections []flows.ProductEntry + var tracesMeta []*httpx.Trace + for i := 0; i < retries; i++ { + productSections, tracesMeta, err = ProductsSearchMeta(callResult.ProductRetailerIDS, fmt.Sprint(catalog.FacebookCatalogID()), s.rtConfig.WhatsappSystemUserToken) + callResult.Traces = append(callResult.Traces, tracesMeta...) + if err != nil { + continue + } + break + } + if err != nil { + return callResult, err + } + finalResult := &flows.MsgCatalogCall{} finalResult.Traces = callResult.Traces finalResult.ResponseJSON = callResult.ResponseJSON @@ -200,7 +222,8 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log finalResult.ProductRetailerIDS = allProductsSponsored } - for _, productEntry := range callResult.ProductRetailerIDS { + // checks available products and limits to 5 per section + for _, productEntry := range productSections { newEntry := productEntry newEntry.ProductRetailerIDs = []string{} for _, productRetailerID := range productEntry.ProductRetailerIDs { @@ -208,13 +231,13 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log for _, existingProductId := range existingProductsIds { if productRetailerID == existingProductId { if len(newEntry.ProductRetailerIDs) < qttProducts { - newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID+"#"+sellerID) + newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID) } } } } else { if len(newEntry.ProductRetailerIDs) < qttProducts { - newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID+"#"+sellerID) + newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID) } } } @@ -224,22 +247,6 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } } - retries := 2 - var newProductRetailerIDS []flows.ProductEntry - var tracesMeta []*httpx.Trace - for i := 0; i < retries; i++ { - newProductRetailerIDS, tracesMeta, err = ProductsSearchMeta(finalResult.ProductRetailerIDS, fmt.Sprint(catalog.FacebookCatalogID()), s.rtConfig.WhatsappSystemUserToken) - finalResult.Traces = append(finalResult.Traces, tracesMeta...) - if err != nil { - continue - } - break - } - if err != nil { - return finalResult, err - } - finalResult.ProductRetailerIDS = newProductRetailerIDS - return finalResult, nil } @@ -343,7 +350,7 @@ func GetProductListFromChatGPT(ctx context.Context, rtConfig *runtime.Config, co return products["products"], trace, nil } -func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config, hasVtexAds bool, hideUnavailableItems bool) ([]string, string, []*httpx.Trace, error) { +func GetProductListFromVtex(productSearch string, searchUrl string, apiType string, catalog string, rt *runtime.Config, hasVtexAds bool, hideUnavailableItems bool, sellerID string) ([]string, string, []*httpx.Trace, error) { var result []string var traces []*httpx.Trace var err error @@ -368,13 +375,11 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri } productRetailerIDS := []string{} - productRetailerIDMap := make(map[string]struct{}) var productEntries []flows.ProductEntry var productEntry flows.ProductEntry for _, productRetailerID := range resultSponsored { - productRetailerIDS = append(productRetailerIDS, productRetailerID) - productRetailerIDMap[productRetailerID] = struct{}{} + productRetailerIDS = append(productRetailerIDS, productRetailerID+"#"+sellerID) } if len(productRetailerIDS) > 0 { @@ -383,7 +388,6 @@ func GetProductListFromVtex(productSearch string, searchUrl string, apiType stri ProductRetailerIDs: productRetailerIDS, } productEntries = append(productEntries, productEntry) - productRetailerIDS = nil retries := 2 var newProductRetailerIDS []flows.ProductEntry @@ -757,9 +761,23 @@ func fetchProducts(url string) (*Response, *httpx.Trace, error) { } func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, whatsappSystemUserToken string) ([]flows.ProductEntry, []*httpx.Trace, error) { + const batchSize = 15 traces := []*httpx.Trace{} - for i, productEntry := range productEntryList { - filter, err := createFilter(productEntry.ProductRetailerIDs) + allIds := []string{} + for _, productEntry := range productEntryList { + allIds = append(allIds, productEntry.ProductRetailerIDs...) + } + + newProductEntryList := []flows.ProductEntry{} + + for i := 0; i < len(allIds); i += batchSize { + end := i + batchSize + if end > len(allIds) { + end = len(allIds) + } + + batchIds := allIds[i:end] + filter, err := createFilter(batchIds) if err != nil { return nil, nil, err } @@ -769,6 +787,7 @@ func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, w params.Add("summary", "true") params.Add("access_token", whatsappSystemUserToken) params.Add("filter", filter) + url_ := fmt.Sprintf("https://graph.facebook.com/v14.0/%s/products?%s", catalog, params.Encode()) response, trace, err := fetchProducts(url_) @@ -777,22 +796,22 @@ func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, w return nil, traces, err } - var productRetailerIDs []string - - // Process the data - for _, product := range response.Data { - productRetailerIDs = append(productRetailerIDs, product.RetailerID) - } - productEntryList[i].ProductRetailerIDs = productRetailerIDs - } - newProductEntryList := []flows.ProductEntry{} - for _, productEntry := range productEntryList { - if len(productEntry.ProductRetailerIDs) > 0 { - newProductEntryList = append(newProductEntryList, productEntry) + for _, productEntry := range productEntryList { + validProductIds := []string{} + for _, retailerId := range productEntry.ProductRetailerIDs { + for _, id := range response.Data { + if retailerId == id.RetailerID { + validProductIds = append(validProductIds, id.RetailerID) + } + } + } + if len(validProductIds) > 0 { + newProductEntryList = append(newProductEntryList, flows.ProductEntry{Product: productEntry.Product, ProductRetailerIDs: validProductIds}) + } } } - return newProductEntryList, traces, nil + return newProductEntryList, traces, nil } var languages = map[string]string{ From 1a0c50b375daaf84d6bbe1a96a30c18fde21132b Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 21 Nov 2024 10:10:04 -0300 Subject: [PATCH 03/14] Truncate url for Meta search --- services/external/weni/service.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 52b9ead5e..8dbf5687b 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -742,6 +742,7 @@ func fetchProducts(url string) (*Response, *httpx.Trace, error) { } t, err := httpx.DoTrace(client, req, nil, nil, -1) + t.Request.URL = truncateURL(t.Request.URL) if err != nil { return nil, t, err } @@ -819,3 +820,16 @@ var languages = map[string]string{ "por": "Você também pode gostar:", "spa": "También te puede interesar:", } + +func truncateURL(u *url.URL) *url.URL { + const maxLength = 2048 + if len(u.String()) > maxLength { + excessLength := len(u.String()) - maxLength + if excessLength < len(u.RawQuery) { + u.RawQuery = u.RawQuery[:len(u.RawQuery)-excessLength] + } else { + u.RawQuery = "" + } + } + return u +} From a6cd65addcb1ee801fd88d121e01a93fe0256d8c Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 21 Nov 2024 12:35:35 -0300 Subject: [PATCH 04/14] Refactor meta search --- services/external/weni/service.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 8dbf5687b..3eaa9a5a5 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -180,6 +180,8 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log callResult.ProductRetailerIDS = productEntries + fmt.Println("SIZE ProductRetailerIDS:", len(callResult.ProductRetailerIDS)) + // simulates cart in VTEX with all products hasSimulation := false if postalCode_ != "" && sellerID != "1" { @@ -763,12 +765,15 @@ func fetchProducts(url string) (*Response, *httpx.Trace, error) { func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, whatsappSystemUserToken string) ([]flows.ProductEntry, []*httpx.Trace, error) { const batchSize = 15 + validProductIds := []string{} traces := []*httpx.Trace{} allIds := []string{} for _, productEntry := range productEntryList { allIds = append(allIds, productEntry.ProductRetailerIDs...) } + fmt.Println("SIZE ALLIDS: ", len(allIds)) + newProductEntryList := []flows.ProductEntry{} for i := 0; i < len(allIds); i += batchSize { @@ -797,18 +802,20 @@ func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, w return nil, traces, err } - for _, productEntry := range productEntryList { - validProductIds := []string{} - for _, retailerId := range productEntry.ProductRetailerIDs { - for _, id := range response.Data { - if retailerId == id.RetailerID { - validProductIds = append(validProductIds, id.RetailerID) - } + for _, id := range response.Data { + validProductIds = append(validProductIds, id.RetailerID) + } + } + + for i, productEntry := range productEntryList { + newProductEntryList[i].Product = productEntry.Product + for _, retailerId := range productEntry.ProductRetailerIDs { + for _, id := range validProductIds { + if retailerId == id { + newProductEntryList[i].ProductRetailerIDs = append(newProductEntryList[i].ProductRetailerIDs, id) + break } } - if len(validProductIds) > 0 { - newProductEntryList = append(newProductEntryList, flows.ProductEntry{Product: productEntry.Product, ProductRetailerIDs: validProductIds}) - } } } From 912fda6c2fd327b78fbd77147f0cb0f59cfff498 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 21 Nov 2024 12:55:45 -0300 Subject: [PATCH 05/14] Correct final product list --- services/external/weni/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 3eaa9a5a5..78c489464 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -808,7 +808,7 @@ func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, w } for i, productEntry := range productEntryList { - newProductEntryList[i].Product = productEntry.Product + newProductEntryList = append(newProductEntryList, flows.ProductEntry{Product: productEntry.Product, ProductRetailerIDs: []string{}}) for _, retailerId := range productEntry.ProductRetailerIDs { for _, id := range validProductIds { if retailerId == id { From 4215d2379998e3dc29fec61fcc6bb1c10a48ea8a Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 21 Nov 2024 17:28:16 -0300 Subject: [PATCH 06/14] Fix cart simulation --- services/external/weni/service.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 78c489464..0f4f02be8 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -638,6 +638,15 @@ func CartSimulation(products []string, sellerID string, url string, postalCode s } } + if len(searchSeller.Items) > 0 { + batchAvailableProducts, trace, err := sendBatchRequest(searchSeller, urlSimulation) + traces = append(traces, trace) + if err != nil { + return nil, traces, err + } + availableProducts = append(availableProducts, batchAvailableProducts...) + } + return availableProducts, traces, nil } From c0c6d1bbbb1203e14065ca61a5b13fb8ea775a8d Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 21 Nov 2024 17:55:36 -0300 Subject: [PATCH 07/14] Add sellerID to cart products --- services/external/weni/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 0f4f02be8..4fc6f0378 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -231,7 +231,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log for _, productRetailerID := range productEntry.ProductRetailerIDs { if hasSimulation { for _, existingProductId := range existingProductsIds { - if productRetailerID == existingProductId { + if productRetailerID == existingProductId+"#"+sellerID { if len(newEntry.ProductRetailerIDs) < qttProducts { newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID) } From 950b28d4ebb70d30357035fba49ecb6d4909ce81 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 21 Nov 2024 18:59:39 -0300 Subject: [PATCH 08/14] Refactor the cart simulation --- services/external/weni/service.go | 37 +++++++++++++------------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 4fc6f0378..d5f395d77 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -610,40 +610,35 @@ func VtexSponsoredSearch(searchUrl string, productSearch string, hideUnavailable } func CartSimulation(products []string, sellerID string, url string, postalCode string) ([]string, []*httpx.Trace, error) { + const batchSize = 300 var traces []*httpx.Trace - var searchSeller SearchSeller - batchSize := 300 - availableProducts := []string{} - - if postalCode != "" { - searchSeller.PostalCode = postalCode - searchSeller.Country = "BRA" - } + var availableProducts []string urlSplit := strings.Split(url, "api") urlSimulation := urlSplit[0] + "api/checkout/pub/orderForms/simulation" - for _, product := range products { - product_retailer_id := product - searchSeller.Items = append(searchSeller.Items, Item{ID: product_retailer_id, Quantity: 1, Seller: sellerID}) + for i := 0; i < len(products); i += batchSize { + end := i + batchSize + if end > len(products) { + end = len(products) + } + batchProducts := products[i:end] - if len(searchSeller.Items) == batchSize { - batchAvailableProducts, trace, err := sendBatchRequest(searchSeller, urlSimulation) - traces = append(traces, trace) - if err != nil { - return nil, traces, err - } - availableProducts = append(availableProducts, batchAvailableProducts...) - searchSeller.Items = []Item{} + var searchSeller SearchSeller + if postalCode != "" { + searchSeller.PostalCode = postalCode + searchSeller.Country = "BRA" + } + for _, product := range batchProducts { + searchSeller.Items = append(searchSeller.Items, Item{ID: product, Quantity: 1, Seller: sellerID}) } - } - if len(searchSeller.Items) > 0 { batchAvailableProducts, trace, err := sendBatchRequest(searchSeller, urlSimulation) traces = append(traces, trace) if err != nil { return nil, traces, err } + availableProducts = append(availableProducts, batchAvailableProducts...) } From 9c8d4af8d19e7226d44e9cc9aff91a538e1f0941 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 22 Nov 2024 11:27:50 -0300 Subject: [PATCH 09/14] Remove duplicate products --- services/external/weni/service.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index d5f395d77..b6a4b5bcd 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -145,7 +145,6 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } else if params.SearchType == "vtex" { searchResult, searchResultSponsored, traces, err = GetProductListFromVtex(product, params.SearchUrl, params.ApiType, catalog.FacebookCatalogID(), s.rtConfig, hasVtexAds, hideUnavailableItems, sellerID) callResult.Traces = append(callResult.Traces, traces...) - allProducts = append(allProducts, searchResult...) if searchResult == nil { continue } @@ -187,7 +186,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log if postalCode_ != "" && sellerID != "1" { var tracesSimulation []*httpx.Trace hasSimulation = true - existingProductsIds, tracesSimulation, err = CartSimulation(allProducts, sellerID, params.SearchUrl, postalCode_) + existingProductsIds, tracesSimulation, err = CartSimulation(callResult.ProductRetailerIDS, sellerID, params.SearchUrl, postalCode_) callResult.Traces = append(callResult.Traces, tracesSimulation...) if err != nil { return callResult, err @@ -609,14 +608,19 @@ func VtexSponsoredSearch(searchUrl string, productSearch string, hideUnavailable } -func CartSimulation(products []string, sellerID string, url string, postalCode string) ([]string, []*httpx.Trace, error) { +func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, url string, postalCode string) ([]string, []*httpx.Trace, error) { const batchSize = 300 var traces []*httpx.Trace var availableProducts []string + var products []string urlSplit := strings.Split(url, "api") urlSimulation := urlSplit[0] + "api/checkout/pub/orderForms/simulation" + for _, p := range ProductRetailerIDS { + products = append(products, p.ProductRetailerIDs...) + } + for i := 0; i < len(products); i += batchSize { end := i + batchSize if end > len(products) { From ddc2805c9df1aad269dae52f76faabe2ee3bd23d Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 22 Nov 2024 11:37:45 -0300 Subject: [PATCH 10/14] Remove allProducts --- services/external/weni/service.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index b6a4b5bcd..ba6d5ba56 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -113,7 +113,6 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log var trace *httpx.Trace var traces []*httpx.Trace var sellerID string - var allProducts []string existingProductsIds := []string{} qttProducts := 5 From 0333ddee9ddcfede657f5ae86179dd3e483d15a9 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 22 Nov 2024 12:08:10 -0300 Subject: [PATCH 11/14] Empty batch to avoid duplications --- services/external/weni/service.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index ba6d5ba56..a9274fd28 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -620,6 +620,12 @@ func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, ur products = append(products, p.ProductRetailerIDs...) } + var searchSeller SearchSeller + if postalCode != "" { + searchSeller.PostalCode = postalCode + searchSeller.Country = "BRA" + } + for i := 0; i < len(products); i += batchSize { end := i + batchSize if end > len(products) { @@ -627,11 +633,6 @@ func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, ur } batchProducts := products[i:end] - var searchSeller SearchSeller - if postalCode != "" { - searchSeller.PostalCode = postalCode - searchSeller.Country = "BRA" - } for _, product := range batchProducts { searchSeller.Items = append(searchSeller.Items, Item{ID: product, Quantity: 1, Seller: sellerID}) } @@ -643,6 +644,7 @@ func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, ur } availableProducts = append(availableProducts, batchAvailableProducts...) + searchSeller.Items = []Item{} } return availableProducts, traces, nil From b7cfd4b8cd3dca661829a98f27d7228aa073f2ba Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 22 Nov 2024 12:26:02 -0300 Subject: [PATCH 12/14] Isolate batches --- services/external/weni/service.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index a9274fd28..56a10ac7a 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -620,12 +620,6 @@ func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, ur products = append(products, p.ProductRetailerIDs...) } - var searchSeller SearchSeller - if postalCode != "" { - searchSeller.PostalCode = postalCode - searchSeller.Country = "BRA" - } - for i := 0; i < len(products); i += batchSize { end := i + batchSize if end > len(products) { @@ -633,6 +627,14 @@ func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, ur } batchProducts := products[i:end] + searchSeller := SearchSeller{ + Items: []Item{}, + } + if postalCode != "" { + searchSeller.PostalCode = postalCode + searchSeller.Country = "BRA" + } + for _, product := range batchProducts { searchSeller.Items = append(searchSeller.Items, Item{ID: product, Quantity: 1, Seller: sellerID}) } @@ -644,7 +646,6 @@ func CartSimulation(ProductRetailerIDS []flows.ProductEntry, sellerID string, ur } availableProducts = append(availableProducts, batchAvailableProducts...) - searchSeller.Items = []Item{} } return availableProducts, traces, nil From 0f910e1e6b56474cdef34837cb0c1878872f37ec Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 22 Nov 2024 13:06:45 -0300 Subject: [PATCH 13/14] Remove duplicate products --- services/external/weni/service.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index 56a10ac7a..e3a2d584f 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -230,8 +230,12 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log if hasSimulation { for _, existingProductId := range existingProductsIds { if productRetailerID == existingProductId+"#"+sellerID { - if len(newEntry.ProductRetailerIDs) < qttProducts { - newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID) + _, exists := productRetailerIDMap[productRetailerID] + if !exists { + if len(newEntry.ProductRetailerIDs) < qttProducts { + newEntry.ProductRetailerIDs = append(newEntry.ProductRetailerIDs, productRetailerID) + } + productRetailerIDMap[productRetailerID] = struct{}{} } } } From 64d0e61d571cfd28c64e506a84e1d525697222b0 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Fri, 22 Nov 2024 15:14:49 -0300 Subject: [PATCH 14/14] Remove prints --- services/external/weni/service.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/services/external/weni/service.go b/services/external/weni/service.go index e3a2d584f..02e93c06c 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -178,8 +178,6 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log callResult.ProductRetailerIDS = productEntries - fmt.Println("SIZE ProductRetailerIDS:", len(callResult.ProductRetailerIDS)) - // simulates cart in VTEX with all products hasSimulation := false if postalCode_ != "" && sellerID != "1" { @@ -786,8 +784,6 @@ func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, w allIds = append(allIds, productEntry.ProductRetailerIDs...) } - fmt.Println("SIZE ALLIDS: ", len(allIds)) - newProductEntryList := []flows.ProductEntry{} for i := 0; i < len(allIds); i += batchSize {