Merge remote-tracking branch 'origin/hotfix/bp-stock-sync' into develop
This commit is contained in:
@@ -5,13 +5,9 @@ const inventorySync = async (container, options) => {
|
|||||||
const brightpearlService = container.resolve("brightpearlService")
|
const brightpearlService = container.resolve("brightpearlService")
|
||||||
const eventBus = container.resolve("eventBusService")
|
const eventBus = container.resolve("eventBusService")
|
||||||
try {
|
try {
|
||||||
const client = await brightpearlService.getClient()
|
|
||||||
const pattern = options.inventory_sync_cron
|
const pattern = options.inventory_sync_cron
|
||||||
eventBus.createCronJob(
|
eventBus.createCronJob("inventory-sync", {}, pattern, () =>
|
||||||
"inventory-sync",
|
brightpearlService.syncInventory()
|
||||||
{},
|
|
||||||
pattern,
|
|
||||||
brightpearlService.syncInventory
|
|
||||||
)
|
)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.name === "not_allowed") {
|
if (err.name === "not_allowed") {
|
||||||
|
|||||||
@@ -126,7 +126,6 @@ class BrightpearlService extends BaseService {
|
|||||||
|
|
||||||
async syncInventory() {
|
async syncInventory() {
|
||||||
const client = await this.getClient()
|
const client = await this.getClient()
|
||||||
const variants = await this.productVariantService_.list()
|
|
||||||
|
|
||||||
let search = true
|
let search = true
|
||||||
let bpProducts = []
|
let bpProducts = []
|
||||||
@@ -141,31 +140,39 @@ class BrightpearlService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bpProducts.length) {
|
if (bpProducts.length) {
|
||||||
const productRange = `${bpProducts[0].productId}-${
|
const productRange = bpProducts
|
||||||
bpProducts[bpProducts.length - 1].productId
|
.map(({ productId }) => productId)
|
||||||
}`
|
.join(",")
|
||||||
|
|
||||||
const availabilities = await client.products.retrieveAvailability(
|
const availabilities = await client.products.retrieveAvailability(
|
||||||
productRange
|
productRange
|
||||||
)
|
)
|
||||||
|
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
variants.map(async (v) => {
|
bpProducts.map(async (bpProduct) => {
|
||||||
const brightpearlProduct = bpProducts.find(
|
const { SKU: sku, productId } = bpProduct
|
||||||
(prod) => prod.SKU === v.sku
|
|
||||||
)
|
const variant = await this.productVariantService_
|
||||||
if (!brightpearlProduct) {
|
.retrieveBySKU(sku, {
|
||||||
return
|
select: ["id", "manage_inventory", "inventory_quantity"],
|
||||||
|
})
|
||||||
|
.catch((_) => undefined)
|
||||||
|
|
||||||
|
const prodAvail = availabilities[productId]
|
||||||
|
|
||||||
|
let onHand = 0
|
||||||
|
if (
|
||||||
|
prodAvail &&
|
||||||
|
prodAvail.warehouses &&
|
||||||
|
prodAvail.warehouses[`${this.options.warehouse}`]
|
||||||
|
) {
|
||||||
|
onHand = prodAvail.warehouses[`${this.options.warehouse}`].onHand
|
||||||
}
|
}
|
||||||
|
|
||||||
const { productId } = brightpearlProduct
|
if (variant && variant.manage_inventory) {
|
||||||
const availability = availabilities[productId]
|
if (parseInt(variant.inventory_quantity) !== parseInt(onHand)) {
|
||||||
if (availability) {
|
return this.productVariantService_.update(variant.id, {
|
||||||
const onHand = availability.total.onHand
|
inventory_quantity: parseInt(onHand),
|
||||||
|
|
||||||
// Only update if the inventory levels have changed
|
|
||||||
if (parseInt(v.inventory_quantity) !== parseInt(onHand)) {
|
|
||||||
return this.productVariantService_.update(v.id, {
|
|
||||||
inventory_quantity: onHand,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user