app/services/stockUpdate.js
/**
 * Inserts the new articles for the given stockDatum
 * @param {stock} stockDatum - The existing stock datum
 * @param {articles[]} newArticles - The article to be added
 * @param {} updatedPriceHistory - The latest retrieved stock prices
 * @param {function} callback - the function to call on completion
 */
Inserts the new articles for the given stockDatum
@param {stock} stockDatum - The existing stock datum
@param {articles[]} newArticles - The article to be added
@param {} updatedPriceHistory - The latest retrieved stock prices
@param {function} callback - the function to call on completion
Inserts the new articles for the given stockDatum @param { stock } stockDatum - The existing stock datum @param { articles [] } newArticles - The article to be added @param {} updatedPriceHistory - The latest retrieved stock prices @param { function } callback - the function to call on completion
function insertNewData(stockDatum, newArticles, updatedPriceHistory, callback) {
  getImages(newArticles).then((imgResults) => {
    for (var x=0; x<imgResults.length; x++) {
      var imgResult = imgResults[x];
      for (var y=0; y<newArticles.length; y++) {
        var newArtic = newArticles[y];
        if (newArtic.url === imgResult.url) {
          newArtic.imageURL = imgResult.imageURL;
          break;
        }
      }
    }
    var company = stockDatum.company;
    if (newArticles.length > 0) {
      var existingArticles = stockDatum.history || [];
      var updatedArticles = sortArticles(existingArticles.concat(newArticles));
      if (updatedArticles.length > config.MAX_ARTICLES_PER_COMPANY) {
        console.log('"' + company + '" has exceeded article max of ' + config.MAX_ARTICLES_PER_COMPANY + '...');
        console.log('Removing ' + (updatedArticles.length - config.MAX_ARTICLES_PER_COMPANY) + ' oldest article(s) from "' + company + '" history...');
        updatedArticles = updatedArticles.slice(0, config.MAX_ARTICLES_PER_COMPANY);
      }
      stockDatum.history = updatedArticles;
      console.log('Inserting into company "' + company + '" articles: ' );
      console.log(newArticles);
    } else {
      console.log('No new articles to insert into "' + company + '"');
    }
    var newHistory = stockDatum.price_history || {};
    var pricesUpdated = false;
    for (var date in updatedPriceHistory) {
      if (updatedPriceHistory.hasOwnProperty(date)) {
        if (!newHistory[date]) {
          pricesUpdated = true;
          newHistory[date] = updatedPriceHistory[date];
        }
      }
    }
    stockDatum.price_history = newHistory;
    if (pricesUpdated || newArticles.length > 0) {
      stock_db.insertOrUpdate(stockDatum).catch((error) => {
        console.log(error);
      });
    }
    callback();
  }).catch(() => {
    callback();
  });
}
function insertNewData ( stockDatum , newArticles , updatedPriceHistory , callback ) { getImages ( newArticles ) . then ( ( imgResults ) => { for ( var x = 0 ; x < imgResults . length ; x ++ ) { var imgResult = imgResults [ x ] ; for ( var y = 0 ; y < newArticles . length ; y ++ ) { var newArtic = newArticles [ y ] ; if ( newArtic . url === imgResult . url ) { newArtic . imageURL = imgResult . imageURL ; break ; } } } var company = stockDatum . company ; if ( newArticles . length > 0 ) { var existingArticles = stockDatum . history || [ ] ; var updatedArticles = sortArticles ( existingArticles . concat ( newArticles ) ) ; if ( updatedArticles . length > config . MAX_ARTICLES_PER_COMPANY ) { console . log ( '"' + company + '" has exceeded article max of ' + config . MAX_ARTICLES_PER_COMPANY + '...' ) ; console . log ( 'Removing ' + ( updatedArticles . length - config . MAX_ARTICLES_PER_COMPANY ) + ' oldest article(s) from "' + company + '" history...' ) ; updatedArticles = updatedArticles . slice ( 0 , config . MAX_ARTICLES_PER_COMPANY ) ; } stockDatum . history = updatedArticles ; console . log ( 'Inserting into company "' + company + '" articles: ' ) ; console . log ( newArticles ) ; } else { console . log ( 'No new articles to insert into "' + company + '"' ) ; } var newHistory = stockDatum . price_history || { } ; var pricesUpdated = false ; for ( var date in updatedPriceHistory ) { if ( updatedPriceHistory . hasOwnProperty ( date ) ) { if ( ! newHistory [ date ] ) { pricesUpdated = true ; newHistory [ date ] = updatedPriceHistory [ date ] ; } } } stockDatum . price_history = newHistory ; if ( pricesUpdated || newArticles . length > 0 ) { stock_db . insertOrUpdate ( stockDatum ) . catch ( ( error ) => { console . log ( error ) ; } ) ; } callback ( ) ; } ) . catch ( ( ) => { callback ( ) ; } ) ; }
Inserts the new articles for the given stockDatum
 @param {stock} stockDatum - The existing stock datum
 @param {articles[]} newArticles - The article to be added
 @param {} updatedPriceHistory - The latest retrieved stock prices
 @param {function} callback - the function to call on completion
Inserts the new articles for the given stockDatum @param { stock } stockDatum - The existing stock datum @param { articles [] } newArticles - The article to be added @param {} updatedPriceHistory - The latest retrieved stock prices @param { function } callback - the function to call on completion
//combine the existing price history with the newly retrieved history //TODO batch insert?