Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for handling line items not fetched via IronCache #7

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
59 changes: 52 additions & 7 deletions wh.order.import.cached.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ try {
var path = require('path');
var Promise = require('bluebird');
var _ = require('underscore');

var vendSdk = require('vend-nodejs-sdk')({});
// Global variable for logging
var commandName = path.basename(__filename, '.js'); // gives the filename without the .js extension

Expand Down Expand Up @@ -228,7 +228,7 @@ try {
for (var i=0; i<totalPages; i++) {
pseudoArrayToIterateOverPagesSerially[i] = i+1;
}

var itemsFailedFromIronCache = [];
// constraint Promise.map with concurrency of 1 around pseudoArrayIterateAllPages
return Promise.map(
pseudoArrayToIterateOverPagesSerially,
Expand Down Expand Up @@ -268,6 +268,8 @@ try {
})
.catch(function (error) {
console.error('failed to lookup vend data from cache, maybe it expired or maybe it was never placed there');
itemsFailedFromIronCache.push(lineitem.sku);
console.log(commandName,'Pushed failed item : ',lineitem);
console.log(commandName, 'ERROR', error);
console.log('ignoring this ERROR, so that we may finish the rest of the process');
return Promise.resolve();
Expand All @@ -276,10 +278,53 @@ try {
{concurrency: 1}
)
.then(function(){
console.log('cross-referenced and filled out lineitems against data from IronCache');
console.log('will send update(s) to loopback');
return client.models.ReportModel.updateRowsAsync(params.reportId, lineitems);
});
var connectionInfo = utils.loadOauthTokens();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pulkitsinghal just now ShoppinPal member
you could blackbox this into a file ... like the logic is compressed earlier in:
var prepStockOrder = require('./jobs/cache-vend-products-for-stock-order.js');


var failedFromCache = function(sku,failedItems){
var i = null;
for (i = 0; failedItems.length > i; i += 1) {
if (failedItems[i] === sku) {
return true;
}
}
return false;
};

return Promise.map(lineitems,
function(lineitem) {
if (failedFromCache(lineitem.sku, itemsFailedFromIronCache)) {

return vendSdk.products.fetchBySku({sku: {value: lineitem.sku}}, connectionInfo)
.then(function (response) {
var product = response.products[0];
var neoProduct = _.pick(product,'name','supply_price','id','sku','type');
neoProduct.inventory = _.find(product.inventory, function(inv){
return inv.outlet_id === params.outletId;
});

lineitem.productId = neoProduct.id;
lineitem.name = neoProduct.name;
lineitem.quantityOnHand = Number(neoProduct.inventory.count);
lineitem.desiredStockLevel = Number(neoProduct.inventory['reorder_point']);
lineitem.fulfilledQuantity = lineitem.orderQuantity;
lineitem.type = neoProduct.type;
if (lineitem.type) { // warehouse folks can choose to box those lacking department/product-type, manually
lineitem.state = BOXED; // boxed by default
lineitem.boxNumber = 1; // boxed together by default
}
console.log(lineitem);
return Promise.resolve();
});
}
},
{concurrency:1}
)
.then(function(){
console.log('cross-referenced and filled out lineitems against data from IronCache');
console.log('will send update(s) to loopback');
return client.models.ReportModel.updateRowsAsync(params.reportId, lineitems);
});
})
});
},
{concurrency: 1}
Expand Down Expand Up @@ -332,4 +377,4 @@ catch (e) {
console.error('last catch block');
console.error(e);
process.exit(FAILURE);
}
}