Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

2310 msi #86

Merged
merged 5 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/adapters/magento/magento2-rest-client/lib/stock_items.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ module.exports = function (restClient) {
return restClient.get(endpointUrl);
}

// MSI
module.getSalableQty = function (sku, stockId) {
var endpointUrl = util.format(
'/inventory/get-product-salable-quantity/%s/%d',
encodeURIComponent(sku),
encodeURIComponent(stockId)
);
return restClient.get(endpointUrl);
}

// MSI
module.isSalable = function (sku, stockId) {
var endpointUrl = util.format(
'/inventory/is-product-salable/%s/%d',
encodeURIComponent(sku),
encodeURIComponent(stockId)
);
return restClient.get(endpointUrl);
}

return module;
}
33 changes: 25 additions & 8 deletions src/adapters/magento/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,46 @@ class ProductAdapter extends AbstractMagentoAdapter {
}
item[customAttribute.attribute_code] = attrValue;
}
item.slug = _slugify(item.name + '-' + item.id)
item.slug = _slugify(item.name + '-' + item.id);
item.custom_attributes = null;

return new Promise((done, reject) => {
// TODO: add denormalization of productcategories into product categories
// DO NOT use "productcategories" type but rather do search categories with assigned products

let subSyncPromises = []
const config = this.config
let subSyncPromises = [];
const config = this.config;

// TODO: Refactor the following to "Chain of responsibility"
// STOCK SYNC
if (this.stock_sync) {
logger.info(`Product sub-stage 1: Getting stock items for ${item.sku}`);
subSyncPromises.push(() => {
return this.api.stockItems.list(item.sku).then((result) => {
item.stock = result
item.stock = result;

const key = util.format(CacheKeys.CACHE_KEY_STOCKITEM, item.id);
logger.debug(`Storing stock data to cache under: ${key}`);
this.cache.set(key, JSON.stringify(result));
if (this.config.magento.msi.enabled) {
return this.api.stockItems.getSalableQty(item.sku, this.config.magento.msi.stockId).then((salableQty) => {
item.stock.qty = salableQty;
return item;
}).then((item) => {
return this.api.stockItems.isSalable(item.sku, this.config.magento.msi.stockId).then((isSalable) => {
item.stock.is_in_stock = isSalable;

return item
const key = util.format(CacheKeys.CACHE_KEY_STOCKITEM, item.id);
logger.debug(`Storing stock data to cache under: ${key}`);
this.cache.set(key, JSON.stringify(item.stock));

return item;
})
})
} else {
const key = util.format(CacheKeys.CACHE_KEY_STOCKITEM, item.id);
logger.debug(`Storing stock data to cache under: ${key}`);
this.cache.set(key, JSON.stringify(result));

return item;
}
})
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports = {
accessToken: process.env.MAGENTO_ACCESS_TOKEN || 'rw5w0si9imbu45h3m9hkyrfr4gjina8q',
accessTokenSecret: process.env.MAGENTO_ACCESS_TOKEN_SECRET || '00y9dl4vpxgcef3gn5mntbxtylowjcc9',
storeId: process.env.MAGENTO_STORE_ID || 1,
currencyCode: process.env.MAGENTO_CURRENCY_CODE || 'USD'
currencyCode: process.env.MAGENTO_CURRENCY_CODE || 'USD',
msi: { enabled: process.env.MAGENTO_MSI_ENABLED || false, stockId: process.env.MAGENTO_MSI_STOCK_ID || 1 }
},

vuestorefront: {
Expand Down
16 changes: 16 additions & 0 deletions src/test_product_msi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

export TIME_TO_EXIT=2000
export VS_INVALIDATE_CACHE_URL=http://localhost:3000/invalidate?key=aeSu7aip&tag=
export VS_INVALIDATE_CACHE=1
export PRODUCTS_SPECIAL_PRICES=true
export MAGENTO_MSI_ENABLED=true
export MAGENTO_MSI_STOCK_ID=1
export MAGENTO_CONSUMER_KEY=byv3730rhoulpopcq64don8ukb8lf2gq
export MAGENTO_CONSUMER_SECRET=u9q4fcobv7vfx9td80oupa6uhexc27rb
export MAGENTO_ACCESS_TOKEN=040xx3qy7s0j28o3q0exrfop579cy20m
export MAGENTO_ACCESS_TOKEN_SECRET=7qunl3p505rubmr7u1ijt7odyialnih9

echo 'Default store - in our case United States / en'
export MAGENTO_URL=http://demo-magento2.vuestorefront.io/rest

node --harmony cli.js products --removeNonExistent=true --partitions=1