Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martintajur/d5b1daee305c4e6538e7b22dfcc83bb9 to your computer and use it in GitHub Desktop.
Save martintajur/d5b1daee305c4e6538e7b22dfcc83bb9 to your computer and use it in GitHub Desktop.
var Pipedrive = require('pipedrive'),
pipedrive = new Pipedrive.Client('YOUR_API_TOKEN_HERE'),
async = require('async'),
q = [],
pageSize = 500,
transformDeal = function(deal) {
// Only update deals in BYR currency that have 0 products attached:
if (deal.currency === 'BYR' && !deal.products_count) {
console.log('Updating ' + deal.title + '...');
q.push(function(next) {
// And update them to have 1/10000th of the original value,
// and to have BYN as the currency:
deal
.set('value', parseInt(deal.value, 10) / 10000)
.set('currency', 'BYN')
.save(next);
});
}
};
fetchDeals(0);
function fetchDeals(start) {
if (!start) {
start = 0;
}
// Additionally, a filter_id can be set here to only process certain deals:
pipedrive.Deals.getAll({ start: start, limit: pageSize }, function(err, deals, additionalData) {
if (err) {
throw err;
}
console.log('Fetched deals from ' + start + ' to ' + (start + pageSize) + '...');
deals.map(transformDeal);
if (additionalData.pagination && additionalData.pagination.more_items_in_collection) {
fetchDeals(start + pageSize);
} else {
async.series(q, function(err) {
if (err) {
throw err;
}
console.log(q.length + ' deals processed!');
process.exit();
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment