Skip to content

Commit

Permalink
Merge branch 'refactor/meta' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Oct 11, 2024
2 parents 086533a + 7132f8b commit f223b55
Showing 1 changed file with 46 additions and 42 deletions.
88 changes: 46 additions & 42 deletions services/external/weni/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,29 @@ 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
if hasSponsored {
finalResult.ProductRetailerIDS = allProductsSponsored
}

for _, productEntry := range callResult.ProductRetailerIDS {
for _, productEntry := range productSections {
newEntry := productEntry
newEntry.ProductRetailerIDs = []string{}
for _, productRetailerID := range productEntry.ProductRetailerIDs {
Expand All @@ -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
}

Expand Down Expand Up @@ -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

}
Expand Down

0 comments on commit f223b55

Please sign in to comment.