Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor product search #184

Merged
merged 15 commits into from
Nov 22, 2024
Prev Previous commit
Next Next commit
Refactor meta search
  • Loading branch information
Robi9 committed Nov 21, 2024
commit a6cd65addcb1ee801fd88d121e01a93fe0256d8c
27 changes: 17 additions & 10 deletions services/external/weni/service.go
Original file line number Diff line number Diff line change
@@ -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})
}
}
}

Loading