'use strict';

const Promise = require('promise');
const azure = require('azure-sb'); 
const asyncEachLimit = require('async/eachLimit');
const trackDependency = require('../appinsights/AppInsightsClient').trackDependency;
const SERVICE_BUS_CONNECTION_STRING = process.env.FORTIS_SB_CONN_STR;
const MAX_CONCURRENT_BATCHES = process.env.MAX_CONCURRENT_BATCHES || 50;

let client = azure.createServiceBusService(SERVICE_BUS_CONNECTION_STRING);

/**
 * @param {string} queue
 * @param {Array<string>} messages
 * @returns {Promise}
 */
function sendMessages(queue, messages) {
  return new Promise((resolve, reject) => {
    if (!client) return reject('Failed to create service bus service. No service bus connection string provided.');

    if (!messages || !messages.length) {
      return reject('No messages to be sent to service bus.');
    }

    asyncEachLimit(messages, MAX_CONCURRENT_BATCHES, (message, asyncCallback) => {
      try {
        message => ({ body: JSON.stringify(message) });
      } catch (exception) {
        asyncCallback(exception);
      }

      try {
        client.sendQueueMessage(queue, message, (error) => {
          if (error) asyncCallback(error);
          else asyncCallback();
        });
      } catch (exception) {
        asyncCallback(exception);
      }
    },
    (error) => {
      if (error) reject(error);
      else resolve();
    });
  });
}

module.exports = {
  sendMessages: trackDependency(sendMessages, 'ServiceBus', 'send')
};
@@ -84,59 +84,8 @@
 ); %0A
-const asyncEachLimit = require('async/eachLimit');%0A
 cons
@@ -238,81 +238,8 @@
 STR;
-%0Aconst MAX_CONCURRENT_BATCHES = process.env.MAX_CONCURRENT_BATCHES %7C%7C 50;
 %0A%0Ale
@@ -356,21 +356,14 @@
 am %7B
-Array%3C
 string
-%3E
 %7D me
@@ -367,17 +367,16 @@
  message
-s
 %0A * @ret
@@ -414,17 +414,16 @@
 dMessage
-s
 (queue, 
@@ -429,17 +429,16 @@
  message
-s
 ) %7B%0A  re
@@ -610,17 +610,16 @@
 !message
-s
  %7C%7C !mes
@@ -622,17 +622,16 @@
 !message
-s
 .length)
@@ -664,17 +664,16 @@
  message
-s
  to be s
@@ -706,93 +706,8 @@
  %7D%0A%0A
-    asyncEachLimit(messages, MAX_CONCURRENT_BATCHES, (message, asyncCallback) =%3E %7B%0A  
     
@@ -718,18 +718,16 @@
 %7B%0A      
-  
 message 
@@ -762,34 +762,32 @@
 essage) %7D);%0A    
-  
 %7D catch (excepti
@@ -794,39 +794,37 @@
 on) %7B%0A      
-  asyncCallback
+return reject
 (exception);
@@ -820,26 +820,24 @@
 exception);%0A
-  
     %7D%0A%0A     
@@ -835,26 +835,22 @@
  %7D%0A%0A    
-  
 try %7B%0A
-  
       cl
@@ -904,26 +904,24 @@
 %3E %7B%0A        
-  
 if (error) a
@@ -919,29 +919,22 @@
 (error) 
-asyncCallback
+reject
 (error);
@@ -946,34 +946,31 @@
     
-  
 else 
-asyncCallback(
+resolve(message
 );%0A
-  
     
@@ -975,18 +975,16 @@
     %7D);%0A
-  
     %7D ca
@@ -1011,23 +1011,14 @@
     
-  asyncCallback
+reject
 (exc
@@ -1034,97 +1034,9 @@
     
-  %7D%0A    %7D,%0A    (error) =%3E %7B%0A      if (error) reject(error);%0A      else resolve();%0A    %7D);
+%7D
 %0A  %7D
@@ -1103,17 +1103,16 @@
 dMessage
-s
 , 'Servi

Change to send one message.