Ok i tried something else with set interval ! I think it works, because i do get back more queries (if there’s a limit, i don’t see it!)
import { fetch } from 'wix-fetch'
import wixStoresBackend from 'wix-stores-backend';
import wixData from 'wix-data';
export async function refreshShop() {
let index = 0
let MaxIndex = 100
wixData.query("Stores/Variants").limit(MaxIndex).skip(index).find().then((queryResults) => {
console.log(queryResults)
toFetch(queryResults)
setInterval(function () {
if (queryResults.hasNext()) {
index = index + 100
toFetch(queryResults)
}
}, 1000)
})
}
/*
export async function retrieveVariantItems(page) {
let results = await wixData.query("Stores/Variants").limit(100).find();
let allItems = results.items;
while (results.hasNext()) {
results = await results.next();
allItems = allItems.concat(results.items);
}
return allItems;
}*/
export function toFetch(queryResults) {
const credentials = Buffer.from("XXXXXX", 'binary').toString('base64')
const auth = { "Authorization": 'Basic ' + credentials, "Content-type": "application/x-www-form-urlencoded" }
console.log(auth)
let variants = queryResults.items.map(a => ({ productId: a.productId, variant: a.variantName, variantId: a._id, productName: a.productName }))
console.log(variants)
variants.forEach((element) => {
fetch("https://affaires.hiboutik.com/api/products/search/XXXXX=" + element.productName, {
"method": "GET",
"headers": auth,
}).then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
}).then(json => {
if (json.length > 0) {
console.log(json)
let productId = json[0].product_id
let sizeType = json[0].product_size_type
fetch("https://affaires.hiboutik.com/api/sizes/" + sizeType, {
"method": "GET",
"headers": auth,
}).then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
}).then(it => {
console.log(it)
let size_Id = it.map(a => a.size_id)
if (size_Id !== undefined) {
size_Id.array.forEach(sizeId => {
fetch("https://affaires.hiboutik.com/api/stock_available/product_id_size/" + productId + '/' + sizeId, {
"method": "GET",
"headers": auth,
}).then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
}).then(thing => {
if (thing !== undefined) {
if (thing.length !== 0) {
let item = {
"trackQuantity": true,
"variants": [{
"quantity": thing[0].stock_available,
"variantId": element.variantId,
"inStock": true
}]
};
wixStoresBackend.updateInventoryVariantFieldsByProductId(element.productId, item);
}
}
}).catch(err => console.log(err));
})
}
}).catch(err => console.log(err));
}
}).catch(err => console.log(err));
}).catch(err => console.log(err));
}