Created
February 27, 2017 21:20
-
-
Save CiscoKidxx/7f40ff9e475f1ed4eed0714e44fad040 to your computer and use it in GitHub Desktop.
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
const mysql = require('mysql'); | |
const request = require('request'); | |
const async = require('async'); | |
const fs = require('fs'); | |
const log = require('simple-node-logger').createSimpleFileLogger('project.log'); | |
const count = 1; // How many offices to change? | |
const connection = mysql.createConnection({ | |
host: '10.20.28.26', | |
user: 'admin', | |
password: 'changeme', | |
database: 'shoreware', | |
port: '4308' | |
}); | |
// Begin async execution | |
async.waterfall([ | |
getApiData, | |
changeSTConfigDB | |
], function(err, result) { | |
// console.log(err); | |
}); | |
function getApiData(callback) { | |
request('https://dev-api.smilegeneration.com/callcenterinfo', function(error, response, body) { | |
if (error) { | |
log.fatal('Error requesting info from API: ', error, new Date().toJSON()); // Print the error if one occurred | |
} else if (response && response.statusCode) { | |
log.info('The server returned: ', response.statusCode, new Date().toJSON()); // Print the response status code if a response was received | |
} | |
let apiData = JSON.parse(body); // Parse the response body into JSON | |
callback(null, apiData); // Pass result into next async function | |
}); | |
} | |
function changeSTConfigDB(apiData, callback) { | |
let shoretel_offices = []; | |
let changed = []; | |
let counter = 0; | |
apiData.forEach(function(element, i) { | |
if (Number(element.PhoneNumber) === element.DnisId && counter < count) { | |
connection.query("SELECT DN FROM diddigitmap WHERE TrunkGroupID = 7 && Digits LIKE ?", "%" + element.DnisId, function(error, results1) { | |
handleError(error); | |
log.trace('ShoreTel Office ' + element.Name + ' with number: ' + element.PhoneNumber + ' : Changing "In a meeting" from : ' + results[0].CFAlways, new Date().toJSON()); | |
let dn_to_change = results1[0].DN; | |
if (dn) { | |
new_dest = "9+1" + element.Five9DnisID; | |
connection.query("SELECT CFAlways, UserDN FROM callhandlingmodes WHERE CHMTypeID = 2 && UserDN = ?", function(error, results2) { | |
handleError(error); | |
}); | |
connection.query("UPDATE callhandlingmodes SET CFAlways = ? WHERE CHMTypeID = 2 && UserDN = ?", new_dest, dn_to_change, function(error, results3) { | |
handleError(error); | |
log.info('Changed ', results, ' accepted at ', new Date().toJSON()); | |
}); | |
} | |
counter++; | |
shoretel_offices.push(element); | |
}); | |
callback(null, apiData, shoretel_offices); | |
} else { | |
log.trace('Skipping ' + element.PhoneNumber + ' - ' + element.Name + 'because it does not appear to be a ShoreTel office'); | |
} | |
}); | |
} | |
function handleError(error) { | |
if (error) { | |
log.fatal('Error quering the ShoreTel config database: ' + error, new Date().toJSON()); | |
throw error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment