Last active
March 31, 2017 22:06
-
-
Save CiscoKidxx/df65eff21740811e8c470747531e1b52 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 request = require('request'); // To hit the API | |
const fs = require('fs'); | |
const csv = require('csv-write-stream'); // To output a .csv | |
const jsonfile = require('jsonfile'); // To parse a .json file | |
const async = require('async'); // To make sure the program excutes top down | |
newArray = []; | |
// Begin async execution | |
async.waterfall([ | |
consumeJsonFile, | |
consumeApi, | |
buildNewObject, | |
outputCSV | |
], function(err, result) { | |
if (err) { | |
throw err; | |
} else { | |
// console.log(result); | |
} | |
}); | |
function consumeJsonFile(callback) { | |
var file = 'dnis.json'; | |
jsonfile.readFile(file, function(err, obj) { | |
let jsonData = obj; | |
callback(null, jsonData); | |
}); | |
} | |
function consumeApi(jsonData, callback) { | |
var file = 'db.json'; | |
jsonfile.readFile(file, function(err, obj) { | |
let apiData = obj; | |
callback(null, jsonData, apiData); | |
}); | |
} | |
function buildNewObject(jsonData, apiData, callback) { | |
jsonData.forEach(function(element, i) { | |
let result = apiData.filter(function(obj) { | |
return obj.DnisId == element.dnis; | |
}); | |
newArray.push(result[0]); | |
}); | |
callback(null, newArray); | |
} | |
function outputCSV(newArray, callback) { | |
let writer = csv(); | |
writer.pipe(fs.createWriteStream('truantOffices.csv')); | |
newArray.forEach(function(element, i) { | |
if (element !== undefined) { | |
writer.write({ | |
DnisId: element.DnisId, | |
InternalName: element.InternalName, | |
Name: element.Name, | |
PhoneNumber: element.PhoneNumber, | |
Five9DnisID: element.Five9DnisID, | |
OfficeId: element.OfficeId | |
}); | |
} | |
}); | |
writer.end(); | |
callback(null, newArray); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment