diff --git a/common/models/stock-order-lineitem-model.js b/common/models/stock-order-lineitem-model.js index e6a36aa4..45c701ca 100644 --- a/common/models/stock-order-lineitem-model.js +++ b/common/models/stock-order-lineitem-model.js @@ -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) { @@ -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', @@ -177,7 +177,7 @@ module.exports = function (StockOrderLineitemModel) { receivedQuantity: 1 } }; - }else { + } else { logger.debug({ functionName: 'scanBarcodeStockOrder', message: 'UnKnown ScanType Encountered', @@ -220,7 +220,6 @@ module.exports = function (StockOrderLineitemModel) { }); return Promise.reject(error); }) - .then(function ([obj, stockOrderLineItemId]) { logger.debug({ functionName: 'scanBarcodeStockOrder', @@ -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())); + // }); }; }; diff --git a/common/utils/workers.js b/common/utils/workers.js index b8cea52d..fe6e3191 100644 --- a/common/utils/workers.js +++ b/common/utils/workers.js @@ -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) diff --git a/workers/sqsWorker.js b/workers/sqsWorker.js index edbacc75..d17172cb 100755 --- a/workers/sqsWorker.js +++ b/workers/sqsWorker.js @@ -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, diff --git a/workers/workers-v2/receive-consignment-async/receive-consignment-async-vend.js b/workers/workers-v2/receive-consignment-async/receive-consignment-async-vend.js new file mode 100644 index 00000000..06536175 --- /dev/null +++ b/workers/workers-v2/receive-consignment-async/receive-consignment-async-vend.js @@ -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 +}; \ No newline at end of file