daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Nov 9th, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Connection, Request } = require('tedious');
  2.  
  3. const options = {
  4.   weekday: 'long',
  5.   year: 'numeric',
  6.   month: 'long',
  7.   day: 'numeric'
  8. };
  9.  
  10. const config = {
  11.   userName: process.env.SQL_USERNAME,
  12.   password: process.env.SQL_PASSWORD,
  13.   server: process.env.SQL_SERVER,
  14.   options: {
  15.     encrypt: true,
  16.     database: process.env.SQL_DATABASE
  17.   }
  18. };
  19.  
  20. module.exports = function(context, myTimer) {
  21.   getChangedSkus()
  22.     .then(data => {
  23.       if (data.length > 0) {
  24.         sendEmail(context, data);
  25.       } else {
  26.         context.done();
  27.       }
  28.     })
  29.     .catch(err => {
  30.       context.log(`ERROR: ${err}`);
  31.     });
  32. };
  33.  
  34. /**
  35.  * Executes a query against the database for SKU's changed in the last 24 hours
  36.  * @returns {Promise} Promise object contains result of query
  37.  */
  38. function getChangedSkus() {
  39.   return new Promise((resolve, reject) => {
  40.     const connection = new Connection(config);
  41.     const query = `SELECT Sku, Quantity, CONVERT(varchar, Modified, 0) as Modified
  42.                    FROM Inventory
  43.                    WHERE Modified >= dateadd(day, -1, getdate())`;
  44.  
  45.     connection.on('connect', err => {
  46.       if (err) reject(err);
  47.  
  48.       let request = new Request(query, err => {
  49.         if (err) {
  50.           reject(err);
  51.         }
  52.       });
  53.  
  54.       const results = [];
  55.       request.on('row', columns => {
  56.         let result = {};
  57.         columns.forEach(column => {
  58.           result[column.metadata.colName] = column.value;
  59.         });
  60.  
  61.         results.push(result);
  62.       });
  63.  
  64.       request.on('doneProc', (rowCount, more) => {
  65.         resolve(results);
  66.       });
  67.  
  68.       connection.execSql(request);
  69.     });
  70.   });
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top