fix(medusa-plugin-brightpearl): more gentle inventory syncs
This commit is contained in:
1 parent
70e8fcde41
commit
e25478034b
4 files changed
+72
-24
No files matched your search
@@ -18,38 +18,39 @@ var inventorySync = /*#__PURE__*/function () {
|
|||||||
case 0:
|
case 0:
|
||||||
brightpearlService = container.resolve("brightpearlService");
|
brightpearlService = container.resolve("brightpearlService");
|
||||||
eventBus = container.resolve("eventBusService");
|
eventBus = container.resolve("eventBusService");
|
||||||
_context.prev = 2;
|
console.log("hi inventory");
|
||||||
_context.next = 5;
|
_context.prev = 3;
|
||||||
|
_context.next = 6;
|
||||||
return brightpearlService.getClient();
|
return brightpearlService.getClient();
|
||||||
|
|
||||||
case 5:
|
case 6:
|
||||||
client = _context.sent;
|
client = _context.sent;
|
||||||
pattern = "43 4,10,14,20 * * *"; // nice for tests "*/10 * * * * *"
|
pattern = "43 4,10,14,20 * * *"; // nice for tests "*/10 * * * * *"
|
||||||
|
|
||||||
eventBus.createCronJob("inventory-sync", {}, pattern, brightpearlService.syncInventory());
|
eventBus.createCronJob("inventory-sync", {}, pattern, brightpearlService.syncInventory());
|
||||||
_context.next = 15;
|
_context.next = 16;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 10:
|
case 11:
|
||||||
_context.prev = 10;
|
_context.prev = 11;
|
||||||
_context.t0 = _context["catch"](2);
|
_context.t0 = _context["catch"](3);
|
||||||
|
|
||||||
if (!(_context.t0.name === "not_allowed")) {
|
if (!(_context.t0.name === "not_allowed")) {
|
||||||
_context.next = 14;
|
_context.next = 15;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _context.abrupt("return");
|
return _context.abrupt("return");
|
||||||
|
|
||||||
case 14:
|
case 15:
|
||||||
throw _context.t0;
|
throw _context.t0;
|
||||||
|
|
||||||
case 15:
|
case 16:
|
||||||
case "end":
|
case "end":
|
||||||
return _context.stop();
|
return _context.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, _callee, null, [[2, 10]]);
|
}, _callee, null, [[3, 11]]);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return function inventorySync(_x) {
|
return function inventorySync(_x) {
|
||||||
|
|||||||
@@ -125,18 +125,40 @@ class BrightpearlService extends BaseService {
|
|||||||
async syncInventory() {
|
async syncInventory() {
|
||||||
const client = await this.getClient()
|
const client = await this.getClient()
|
||||||
const variants = await this.productVariantService_.list()
|
const variants = await this.productVariantService_.list()
|
||||||
|
|
||||||
|
let search = true
|
||||||
|
let bpProducts = []
|
||||||
|
while (search) {
|
||||||
|
const { products, metadata } = await client.products.search(search)
|
||||||
|
bpProducts = [...bpProducts, ...products]
|
||||||
|
if (metadata.morePagesAvailable) {
|
||||||
|
search = `firstResult=${metadata.lastResult + 1}`
|
||||||
|
} else {
|
||||||
|
search = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bpProducts.length) {
|
||||||
|
const productRange = `${bpProducts[0].productId}-${
|
||||||
|
bpProducts[bpProducts.length - 1].productId
|
||||||
|
}`
|
||||||
|
|
||||||
|
const availabilities = await client.products.retrieveAvailability(
|
||||||
|
productRange
|
||||||
|
)
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
variants.map(async (v) => {
|
variants.map(async (v) => {
|
||||||
const brightpearlProduct = await this.retrieveProductBySKU(v.sku)
|
const brightpearlProduct = bpProducts.find(
|
||||||
|
(prod) => prod.SKU === v.sku
|
||||||
|
)
|
||||||
if (!brightpearlProduct) {
|
if (!brightpearlProduct) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { productId } = brightpearlProduct
|
const { productId } = brightpearlProduct
|
||||||
const availability = await client.products.retrieveAvailability(
|
const availability = availabilities[productId]
|
||||||
productId
|
if (availability) {
|
||||||
)
|
const onHand = availability.total.onHand
|
||||||
const onHand = availability[productId].total.onHand
|
|
||||||
|
|
||||||
// Only update if the inventory levels have changed
|
// Only update if the inventory levels have changed
|
||||||
if (parseInt(v.inventory_quantity) !== parseInt(onHand)) {
|
if (parseInt(v.inventory_quantity) !== parseInt(onHand)) {
|
||||||
@@ -144,9 +166,11 @@ class BrightpearlService extends BaseService {
|
|||||||
inventory_quantity: onHand,
|
inventory_quantity: onHand,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async updateInventory(productId) {
|
async updateInventory(productId) {
|
||||||
const client = await this.getClient()
|
const client = await this.getClient()
|
||||||
|
|||||||
@@ -289,6 +289,18 @@ class BrightpearlClient {
|
|||||||
})
|
})
|
||||||
.then(({ data }) => data.response && data.response[0])
|
.then(({ data }) => data.response && data.response[0])
|
||||||
},
|
},
|
||||||
|
search: (search) => {
|
||||||
|
return this.client_
|
||||||
|
.request({
|
||||||
|
url: `/product-service/product-search?${search}`,
|
||||||
|
})
|
||||||
|
.then(({ data }) => {
|
||||||
|
return {
|
||||||
|
products: this.buildSearchResults_(data.response),
|
||||||
|
metadata: data.response.metaData,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
retrieveBySKU: (sku) => {
|
retrieveBySKU: (sku) => {
|
||||||
return this.client_
|
return this.client_
|
||||||
.request({
|
.request({
|
||||||
|
|||||||
@@ -254,11 +254,22 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
|||||||
return data.response && data.response[0];
|
return data.response && data.response[0];
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
search: function search(_search) {
|
||||||
|
return _this.client_.request({
|
||||||
|
url: "/product-service/product-search?".concat(_search)
|
||||||
|
}).then(function (_ref15) {
|
||||||
|
var data = _ref15.data;
|
||||||
|
return {
|
||||||
|
products: _this.buildSearchResults_(data.response),
|
||||||
|
metadata: data.response.metaData
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
retrieveBySKU: function retrieveBySKU(sku) {
|
retrieveBySKU: function retrieveBySKU(sku) {
|
||||||
return _this.client_.request({
|
return _this.client_.request({
|
||||||
url: "/product-service/product-search?SKU=".concat(sku)
|
url: "/product-service/product-search?SKU=".concat(sku)
|
||||||
}).then(function (_ref15) {
|
}).then(function (_ref16) {
|
||||||
var data = _ref15.data;
|
var data = _ref16.data;
|
||||||
return _this.buildSearchResults_(data.response);
|
return _this.buildSearchResults_(data.response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -270,8 +281,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
|||||||
retrieveByEmail: function retrieveByEmail(email) {
|
retrieveByEmail: function retrieveByEmail(email) {
|
||||||
return _this.client_.request({
|
return _this.client_.request({
|
||||||
url: "/contact-service/contact-search?primaryEmail=".concat(email)
|
url: "/contact-service/contact-search?primaryEmail=".concat(email)
|
||||||
}).then(function (_ref16) {
|
}).then(function (_ref17) {
|
||||||
var data = _ref16.data;
|
var data = _ref17.data;
|
||||||
return _this.buildSearchResults_(data.response);
|
return _this.buildSearchResults_(data.response);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -280,8 +291,8 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
|||||||
url: "/contact-service/contact",
|
url: "/contact-service/contact",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
data: customerData
|
data: customerData
|
||||||
}).then(function (_ref17) {
|
}).then(function (_ref18) {
|
||||||
var data = _ref17.data;
|
var data = _ref18.data;
|
||||||
return data.response;
|
return data.response;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -337,7 +348,7 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
|||||||
return request;
|
return request;
|
||||||
});
|
});
|
||||||
this.client_.interceptors.response.use(undefined, /*#__PURE__*/function () {
|
this.client_.interceptors.response.use(undefined, /*#__PURE__*/function () {
|
||||||
var _ref18 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(error) {
|
var _ref19 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(error) {
|
||||||
var response;
|
var response;
|
||||||
return regeneratorRuntime.wrap(function _callee$(_context) {
|
return regeneratorRuntime.wrap(function _callee$(_context) {
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -388,7 +399,7 @@ var BrightpearlClient = /*#__PURE__*/function () {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
return function (_x) {
|
return function (_x) {
|
||||||
return _ref18.apply(this, arguments);
|
return _ref19.apply(this, arguments);
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user