Last active
April 15, 2016 21:59
-
-
Save joetemp/7c1f7a307c7240ba317b9cf4755e843a to your computer and use it in GitHub Desktop.
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
| const request = require('request-promise'); | |
| const moment = require('moment'); | |
| moment().format(); | |
| var API_KEY = process.env.API_KEY; | |
| var urls = { deals: 'https://api.pipedrive.com/v1/deals?start=0&api_token=' + API_KEY, | |
| activities : 'https://api.pipedrive.com/v1/activities?start=0&api_token=' + API_KEY }; | |
| function getIt (url) { | |
| return request(url).then(function (body) { | |
| return JSON.parse(body).data; | |
| }); | |
| } | |
| // I need to set a proper due date in here. | |
| function add (i) { | |
| return request.post('https://api.pipedrive.com/v1/activities?api_token=' + API_KEY, { | |
| form: {'subject': '4506-T', | |
| 'deal_id': i}}); | |
| } | |
| Promise.all([getIt(urls.deals), getIt(urls.activities)]).then(function(results) { | |
| var deals = results[0]; | |
| var activities = results[1]; | |
| var applications = deals.filter(function(deal) { | |
| return (deal.stage_id === 2); | |
| }); | |
| var alreadyHave4506T = activities.filter(function(activity) { | |
| return (activity.subject === '4506-T'); | |
| }); | |
| var stillNeeds4506T = applications.filter(function(application) { | |
| return (alreadyHave4506T.indexOf(application) === -1); | |
| }); | |
| return Promise.all(stillNeeds4506T.map(function(item) { | |
| //console.log(stillNeeds4506T); | |
| return add(item.id); | |
| })); | |
| }).then(function(responses) { | |
| // all have now been added | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment