Created
March 20, 2014 09:58
-
-
Save torpio/9660583 to your computer and use it in GitHub Desktop.
Send opportunities with a specific tag to WebMerge, along with contact, organisation and user merge data.
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 settings = {}; | |
// Torpio will only merge opportunities that have this tag. | |
settings.INSIGHTLY_TAG = 'merge' | |
// The full merge URL copied from your WebMerge account | |
settings.MERGE_URL = 'https://www.webmerge.me/merge/12345/iqtabg' | |
function fetch(url, key, n, data, ok) { | |
n--; | |
service.get('insightly', url, function (error, status, result) { | |
if (error) { | |
log.error(error); | |
} else if (status >= 400) { | |
log.error(result); | |
} else { | |
var newData = {}; | |
newData[key] = result; | |
data = _.extend(data, newData); | |
if (n === 0) { | |
merge(data, ok); | |
} | |
} | |
}); | |
} | |
function merge(data, ok) { | |
service.post('webmerge', settings.MERGE_URL.replace('https://www.webmerge.me/', ''), data, function (error, status, result) { | |
log.info(data); | |
if (error) { | |
log.error(error); | |
} else { | |
ok(); | |
if (status >= 400) { | |
log.error(result); | |
} else { | |
log.info("Sent opportunity " + data.OPPORTUNITY_ID + " to WebMerge!"); | |
} | |
} | |
}); | |
} | |
service.get('insightly', 'Opportunities?tag=' + settings.INSIGHTLY_TAG, function (error, status, result) { | |
if (error) { | |
log.error(error); | |
} else if (status >= 400) { | |
log.error(result); | |
} else { | |
helper.iterate(result, 'OPPORTUNITY_ID', function (error, item, ok) { | |
if (error) { | |
log.error(error); | |
} else { | |
var data = { opportunity: item }; | |
var n = 0; | |
var contacts = 0; | |
var orgs = 0; | |
if (item.RESPONSIBLE_USER_ID !== null) { | |
n++; | |
fetch('Users/' + item.RESPONSIBLE_USER_ID, 'user', n, data, ok); | |
} | |
_.each(item.LINKS, function (link) { | |
if (link.CONTACT_ID !== null) { | |
n++; | |
contacts++; | |
fetch('Contacts/' + link.CONTACT_ID, 'contact' + contacts, n, data, ok); | |
} | |
if (link.ORGANISATION_ID !== null) { | |
n++; | |
orgs++; | |
fetch('Organisations/' + link.ORGANISATION_ID, 'org' + orgs, n, data, ok); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33234 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment