fix(brightpearl): chunked product availabilities in sync
This commit is contained in:
1 parent
91a48174b0
commit
fd82de6ef9
1 file changed
+24
-6
@@ -140,13 +140,31 @@ class BrightpearlService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bpProducts.length) {
|
if (bpProducts.length) {
|
||||||
const productRange = bpProducts
|
let availabilities = {}
|
||||||
.map(({ productId }) => productId)
|
const perChunk = 100
|
||||||
.join(",")
|
const chunkedProducts = bpProducts.reduce((resultArray, item, index) => {
|
||||||
|
const chunkIndex = Math.floor(index / perChunk)
|
||||||
|
|
||||||
const availabilities = await client.products.retrieveAvailability(
|
if (!resultArray[chunkIndex]) {
|
||||||
productRange
|
resultArray[chunkIndex] = [] // start a new chunk
|
||||||
)
|
}
|
||||||
|
|
||||||
|
resultArray[chunkIndex].push(item)
|
||||||
|
|
||||||
|
return resultArray
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// For large product catalogues we get 414 Too long URI so to avoid this
|
||||||
|
// we chunk things up
|
||||||
|
for (const chunk of chunkedProducts) {
|
||||||
|
const productRange = chunk.map(({ productId }) => productId).join(",")
|
||||||
|
|
||||||
|
const chunkAvails = await client.products.retrieveAvailability(
|
||||||
|
productRange
|
||||||
|
)
|
||||||
|
|
||||||
|
availabilities = Object.assign(availabilities, chunkAvails)
|
||||||
|
}
|
||||||
|
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
bpProducts.map(async (bpProduct) => {
|
bpProducts.map(async (bpProduct) => {
|
||||||
|
|||||||
Reference in new issue
Block a user