Last active
August 5, 2016 18:15
-
-
Save jsullivanlive/c9c4198f1f2a75747d1488ade9493b10 to your computer and use it in GitHub Desktop.
Load HipChat History to Salesforce.com
This file contains hidden or 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
var jsforce = require('jsforce'); | |
var fs = require('fs'); | |
var Promise = require('bluebird'); | |
var conn = new jsforce.Connection({}); | |
var p = conn.login(process.env.SALESFORCE_USERNAME, process.env.SALESFORCE_PASSWORD) | |
.then(getLastSyncedMessage) | |
.then(getData) | |
.then(loadDataToSalesforce) | |
.catch(console.error) | |
function getLastSyncedMessage () { | |
return conn.query('select max(Date__c) from HipChat_History__c') | |
.then(function(res){ | |
return res.records[0].expr0; | |
}); | |
} | |
function getData (since) { | |
var rooms = fs.readdirSync('rooms') | |
var combined = []; | |
for (var i in rooms) { | |
console.log([i, rooms[i]]); | |
var roomPath = 'rooms/' + rooms[i]; | |
if (!fs.lstatSync(roomPath).isDirectory()) continue; | |
var days = fs.readdirSync(roomPath); | |
for (var j in days) { | |
if (days[j] == '.DS_Store') continue; | |
// console.log([i, rooms[i], j, days[j]]); | |
var dayData = require('./' + roomPath + '/' + days[j]); | |
for (k in dayData) { | |
if (k == []) continue; // some days are just ""[[]]"" like AdvisorAssist 2015-10-01 (maybe when archived?) | |
if (!dayData[k].message) continue; | |
if (since > dayData[k].date) continue; | |
combined.push({ | |
Room__c: rooms[i], | |
Message__c: dayData[k].message, | |
From_Name__c: dayData[k].from ? dayData[k].from.name : '', | |
Date__c: dayData[k].date | |
}) | |
} | |
} | |
} | |
return combined; | |
} | |
function loadDataToSalesforce (records) { | |
console.log('loading records: ' + records.length); | |
var job = conn.bulk.createJob("HipChat_History__c", "insert"); | |
while (records.length) { | |
var batch = job.createBatch(); | |
batch.execute(records.splice(0, 10000)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment