Skip to content

Commit

Permalink
add async calls to Vend on Approving/Receiving per item in order #377
Browse files Browse the repository at this point in the history
  • Loading branch information
panic-coder committed Jan 19, 2020
1 parent c666841 commit d1625a0
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 6 deletions.
41 changes: 35 additions & 6 deletions common/models/stock-order-lineitem-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var _ = require('underscore');
var path = require('path');
var fileName = path.basename(__filename, '.js'); // gives the filename without the .js extension
const logger = require('sp-json-logger')({fileName: 'common:models:' + fileName});

var workerUtils = require('../utils/workers');

module.exports = function (StockOrderLineitemModel) {

Expand Down Expand Up @@ -95,7 +95,7 @@ module.exports = function (StockOrderLineitemModel) {
options
});
return StockOrderLineitemModel.findOne(filter);
}else {
} else {
logger.debug({
functionName: 'scanBarcodeStockOrder',
message: 'No Product Found Associated with the entered SKU, Will Exit',
Expand Down Expand Up @@ -177,7 +177,7 @@ module.exports = function (StockOrderLineitemModel) {
receivedQuantity: 1
}
};
}else {
} else {
logger.debug({
functionName: 'scanBarcodeStockOrder',
message: 'UnKnown ScanType Encountered',
Expand Down Expand Up @@ -220,7 +220,6 @@ module.exports = function (StockOrderLineitemModel) {
});
return Promise.reject(error);
})

.then(function ([obj, stockOrderLineItemId]) {
logger.debug({
functionName: 'scanBarcodeStockOrder',
Expand Down Expand Up @@ -250,9 +249,39 @@ module.exports = function (StockOrderLineitemModel) {
});
return Promise.reject(error);
})

.then(function ([obj, stockLineItem]) {
return Promise.resolve(Object.assign({}, obj, stockLineItem.toJSON()));
var payload = {
stockOrderLineItemId: stockLineItem.id,
// reportModelId: stockLineItem.orgModelId,
op: 'receiveConsignmentAsyncVend',
eventType: workerUtils.messageFor.MESSAGE_FOR_CLIENT,
callId: options.accessToken.userId,
};
// console.log("check stockLineItem : ",stockLineItem);
// console.log("check payload stockLineItem :",payload);
return workerUtils.sendPayLoad(payload)
.then(function (response) {
logger.debug({
message: 'Sent receiveConsignmentAsyncVend operation to worker',
options,
response,
functionName: 'receiveConsignmentAsyncVend'
});
return Promise.resolve(Object.assign({}, obj, stockLineItem.toJSON()));
})
.catch(function (error) {
logger.error({
message: 'Could not send receiveConsignmentAsyncVend to worker',
options,
error,
functionName: 'receiveConsignmentAsyncVend'
});
// return Promise.reject('Error in creating stock order');
return Promise.resolve(Object.assign({}, obj, stockLineItem.toJSON()));
});
});
// .then(function ([obj, stockLineItem]) {
// return Promise.resolve(Object.assign({}, obj, stockLineItem.toJSON()));
// });
};
};
1 change: 1 addition & 0 deletions common/utils/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var sendPayLoad = function (payload) {
MessageBody: JSON.stringify(payload),
QueueUrl: app.get('awsQueueUrl2')
};
// console.log("check payload : ",sqsParams);
var sendMessageAsync = Promise.promisify(sqs.sendMessage, sqs);

return sendMessageAsync(sqsParams)
Expand Down
16 changes: 16 additions & 0 deletions workers/sqsWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,22 @@ function routeToWorker(payload, config, taskId, messageId, receiptHandle) {
return Promise.reject('Internal Server Error');
});
}
else if (payload.op === 'receiveConsignmentAsyncVend') {
logger.tag('Routed').info({
messageId: messageId,
message: 'routed to receiveConsignmentAsyncVend'
});
var receiveConsignment = require('./workers-v2/receive-consignment-async/receive-consignment-async-vend');
return receiveConsignment.run(payload, config, taskId, messageId)
.then(function () {
logger.debug({messageId: messageId, message: 'received purchase order in Vend successfully'});
return Promise.resolve(receiptHandle);
})
.catch(function (error) {
logger.error({err: error, messageId: messageId});
return Promise.reject('Internal Server Error');
});
}
else if (payload.op === 'importVendOrderFromFile') {
logger.tag('Routed').info({
messageId: messageId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const path = require('path');
const commandName = path.basename(__filename, '.js'); // gives the filename without the .js extension
const logger = require('sp-json-logger')({fileName: 'workers:workers-v2:' + commandName});
const dbUrl = process.env.DB_URL;
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
var db = null; //database connected
const utils = require('./../../jobs/utils/utils.js');
const _ = require('underscore');
const Promise = require('bluebird');
const TODAYS_DATE = new Date();
const rp = require('request-promise');

var runMe = function (payload, config, taskId, messageId) {
var stockOrderLineItemId = payload.stockOrderLineItemId;
// var reportModelId = payload.reportModelId;
var stockOrderLineItemModels;
var reportModelInstance;
try {
logger.debug({
commandName: commandName,
argv: process.argv,
stockOrderLineItemId,
messageId,
payload
});
return Promise.resolve()
.then(function () {
logger.debug({
message: 'Will connect to Mongo DB',
commandName,
messageId
});
return MongoClient.connect(dbUrl, {promiseLibrary: Promise});
})
.catch(function (error) {
logger.error({
message: 'Could not connect to Mongo DB',
error,
commandName,
messageId
});
return Promise.reject('Could not connect to Mongo DB');
})
.then(function (dbInstance) {
db = dbInstance;
logger.debug({
message: 'Connected to Mongo DB, will look for stockLineItem model',
commandName,
messageId
});
return db.collection('StockOrderLineitemModel').findOne({
_id: ObjectId(stockOrderLineItemId)
});
})
.catch(function (error) {
logger.error({
message: 'Could not find report model instance',
stockOrderLineItemId,
error,
commandName,
messageId
});
return Promise.reject('Could not find report, store, supplier instances');
})
.then(function (response){
logger.debug({
message: 'Updated report model status to receiving',
response,
messageId
});
// return function(){
console.log("check response.receivedQuantity : ",response.receivedQuantity);
if (response.receivedQuantity) {
return utils.updateStockOrderLineitemForVend(db, response, response, messageId);
}
else {
if (response.vendConsignmentProductId && !response.vendDeletedAt) {
return utils.deleteStockOrderLineitemForVend(db, response, messageId)
.then(function (response) {
logger.debug({
message: 'Deleted line item from Vend, will update vend deleted status in DB',
response,
messageId,
response
});
return db.collection('StockOrderLineitemModel').updateOne({
_id: ObjectId(response._id)
}, {
$set: {
vendDeletedAt: new Date()
}
})
.catch(function (error) {
logger.error({
message: 'Could not update vend deleted status in DB',
error,
messageId,
response
});
return Promise.reject('Could not update vend deleted status in DB');
})
.then(function (response) {
logger.debug({
message: 'Updated vend deleted status in DB',
eachLineItem,
messageId,
response
});
return Promise.resolve('Updated vend deleted status in DB');
});
})
}
else {
return Promise.resolve();
}
}
// }
})
.catch(function (error) {
logger.error({
commandName,
error,
reason: error,
message: 'Could not update receiving quantities for line item',
messageId
});
return Promise.reject('Could not update receiving quantities for line item');
})
// return Promise.resolve();
} catch (e) {
logger.error({
message: 'last catch block', err: e,
messageId
});
throw e;
}
};

module.exports = {
run: runMe
};

0 comments on commit d1625a0

Please sign in to comment.