diff --git a/services/external/weni/service.go b/services/external/weni/service.go index d07e220d4..c98b28e0b 100644 --- a/services/external/weni/service.go +++ b/services/external/weni/service.go @@ -192,6 +192,21 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log } } + 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 @@ -199,7 +214,7 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log finalResult.ProductRetailerIDS = allProductsSponsored } - for _, productEntry := range callResult.ProductRetailerIDS { + for _, productEntry := range productSections { newEntry := productEntry newEntry.ProductRetailerIDs = []string{} for _, productRetailerID := range productEntry.ProductRetailerIDs { @@ -223,22 +238,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 } @@ -740,39 +739,44 @@ func fetchProducts(url string) (*Response, *httpx.Trace, error) { func ProductsSearchMeta(productEntryList []flows.ProductEntry, catalog string, whatsappSystemUserToken string) ([]flows.ProductEntry, []*httpx.Trace, error) { traces := []*httpx.Trace{} - for i, productEntry := range productEntryList { - filter, err := createFilter(productEntry.ProductRetailerIDs) - if err != nil { - return nil, nil, err - } - - params := url.Values{} - params.Add("fields", "[\"category\",\"name\",\"retailer_id\",\"availability\"]") - 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()) + allIds := []string{} + for _, productEntry := range productEntryList { + allIds = append(allIds, productEntry.ProductRetailerIDs...) + } - response, trace, err := fetchProducts(url_) - traces = append(traces, trace) - if err != nil { - return nil, traces, err - } + filter, err := createFilter(allIds) + if err != nil { + return nil, nil, err + } - var productRetailerIDs []string + params := url.Values{} + params.Add("fields", "[\"category\",\"name\",\"retailer_id\",\"availability\"]") + 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()) - // Process the data - for _, product := range response.Data { - productRetailerIDs = append(productRetailerIDs, product.RetailerID) - } - productEntryList[i].ProductRetailerIDs = productRetailerIDs + response, trace, err := fetchProducts(url_) + traces = append(traces, trace) + if err != nil { + return nil, traces, err } + newProductEntryList := []flows.ProductEntry{} for _, productEntry := range productEntryList { - if len(productEntry.ProductRetailerIDs) > 0 { - newProductEntryList = append(newProductEntryList, productEntry) + 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 }