-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add async calls to Vend on Approving/Receiving per item in order #377
- Loading branch information
1 parent
c666841
commit d1625a0
Showing
4 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
workers/workers-v2/receive-consignment-async/receive-consignment-async-vend.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |