Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports.opalInventoryWF = ()=>{
- return new CronJob('0 33 * * * *', function() {
- console.log('Started Opal Inventory CronJob');
- const endpoint = process.env.ENDPOINT,
- warehouse = process.env.WAREHOUSE,
- user = process.env.USER,
- password = process.env.PASS;
- let itemArray = [],
- array = [],
- queryString = "redacted due to db schema exposure";
- pool.getConnection((err,connection)=>{
- if(err){
- console.error(err);
- throw err;
- }
- console.log('querying');
- let query = connection.query(queryString);
- query.on('error',(err)=>{
- console.error(err);
- throw err;
- }).on('result',(row,index)=>{
- let sku = row.PRODUCT_ID,
- quantity = row.AVAILABLE_TO_PROMISE_TOTAL,
- disc = row.SUPPLIER_DISCONTINUATION_DATE,
- etd = row.ETD,
- osInv = row.AVAILABLE_TO_PROMISE_TOTAL,
- boq = row.SATP;
- if(quantity >= 50){
- quantity = 50;
- }else if(quantity < 3){
- quantity = 0;
- }
- if(osInv < 3){
- osInv = 0;
- }
- if(quantity !== 0 && quantity >= 3 && !sku.includes('-606SW') && sku !== '200ACR201B-76096'){
- quantity = quantity - 3;
- }
- if(osInv !== 0 && osInv >= 3 && !sku.includes('-606SW') && sku !== '200ACR201B-76096'){
- osInv = osInv - 3;
- }
- if(quantity > 0){
- etd = '';
- boq = 0;
- }
- if(disc === 'TRUE' && quantity > 5){
- disc = false;
- }else if(disc === 'TRUE' && quantity < 5){
- disc = true;
- }else{
- disc = false;
- }
- if(quantity >= 0 && osInv >= 0 && disc === false){
- itemArray.push({sku,quantity,disc,etd,boq,osInv})
- }
- }).on('end',()=>{
- connection.release();
- writeArray = itemArray.slice(0),
- alteredArray = [];
- var csv = json2csv({data: writeArray,fields:fields}),
- timestamp = new Date(Date.now());
- timestamp = timestamp.getFullYear() + '-' +(timestamp.getMonth() + 1) + '-' + timestamp.getDate()+ ' '+timestamp.getHours() +':'+timestamp.getMinutes()+':'+timestamp.getSeconds();
- let fpath = './public/assets/archives/opalEdiInventory-'+timestamp+'.csv';
- fs.writeFile(fpath,csv,(err)=>{
- if(err){
- console.error('Error writing output csv: '+err);
- throw err;
- }
- console.log('output saved');
- let recipients = [redacted]
- send({
- to: recipients,
- subject: 'Inventory Export '+timestamp,
- files: [fpath]
- },
- function (err, res) {
- if(err){
- console.error('Error sending email: '+ err);
- throw err;
- }
- console.log('Email successfully sent response: '+res);
- });
- });
- console.log('Item Array Length: ' + itemArray.length);
- while(itemArray.length > 0){
- alteredArray = itemArray.splice(0,999);
- for(let i = 0; i < alteredArray.length; i++){
- jsonObjectArray.push({
- sku: alteredArray[i]['sku'],
- quantity: alteredArray[i]["quantity"],
- overstockquantity: alteredArray[i]["osInv"],
- warehouse: warehouse,
- isdiscontinued: alteredArray[i]["disc"],
- backorderdate: alteredArray[i]["etd"],
- backorderavailability: alteredArray[i]["boq"]
- });
- }
- var jsonObject = {
- login: user,
- password: password,
- items: jsonObjectArray
- };
- postOptions.url = endpoint;
- postOptions.body = JSON.stringify(jsonObject);
- funcArray.push({func:function(postOptions){request(postOptions,(err,res,body)=>{if(err){console.error(err);throw err;}console.log(body);})},vars:postOptions});
- jsonObjectArray.length = 0;
- }
- var mili = 180000;
- for(let i = 0;i < funcArray.length; i++){
- setTimeout(()=>{
- var d = JSON.parse(funcArray[i]['vars'].body);
- console.log(d);
- console.log('request '+ i);
- //funcArray[i]['func'](funcArray[i]['vars']);
- }, mili * i);
- }
- });
- });
- }, null, true, 'America/Los_Angeles');
- }
- while(itemArray.length > 0){
- alteredArray = itemArray.splice(0,999);
- for(let i = 0; i < alteredArray.length; i++){
- jsonObjectArray.push({
- sku: alteredArray[i]['sku'],
- quantity: alteredArray[i]["quantity"],
- overstockquantity: alteredArray[i]["osInv"],
- warehouse: warehouse,
- isdiscontinued: alteredArray[i]["disc"],
- backorderdate: alteredArray[i]["etd"],
- backorderavailability: alteredArray[i]["boq"]
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement