Skip to content

Instantly share code, notes, and snippets.

@tracker1
Forked from joetemp/newW2.js
Last active June 8, 2016 06:39
Show Gist options
  • Save tracker1/65a0e62c4aedc742685b6d42390a12d7 to your computer and use it in GitHub Desktop.
Save tracker1/65a0e62c4aedc742685b6d42390a12d7 to your computer and use it in GitHub Desktop.

Revisions

  1. tracker1 revised this gist Jun 8, 2016. 1 changed file with 56 additions and 47 deletions.
    103 changes: 56 additions & 47 deletions newW2.js
    Original file line number Diff line number Diff line change
    @@ -1,63 +1,72 @@
    const request = require('request-promise');
    const moment = require('moment');

    module.exports = function (API_KEY, deals, activities, app, type, pbl, address, test){
    const getId = url => request(url).then(b => JSON.parse(b).data);

    //moment().format();

    var soonPart = {};
    var laterPart = {};

    let soon = deals.filter(deal => (
    function getSoonItems(deal, type) {
    return deals.filter(deal => (
    deal.stage_id === app && deal[type.key] === type.refi ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.no
    )).reduce((obj, deal) => { ...obj, [deal.id]:deal });

    let later = deals.filter(deal => (
    !soon[deal.id] && (
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === '' ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === null
    )
    )).reduce((obj, deal) => { ...obj, [deal.id]:deal });

    let have = activities.filter(a => a.subject === 'W-2')
    }

    function getLaterItems(deal, type, pbl, address, soon) {
    deals.filter(deal => (
    !soon[deal.id] && (
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === '' ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === null
    )
    )).reduce((obj, deal) => { ...obj, [deal.id]:deal });
    }

    function getHaveItems(activities) {
    return activities.filter(a => a.subject === 'W-2')
    .reduce((obj, a) => { ...obj, [`${a.deal_id}${deal.person_id}`]: a });
    }

    export default async function(API_KEY, deals, activities, app, type, pbl, address, test) {
    try {
    let soon = getSoonItems();
    let later = getLaterItems(deal, type, pbl, address, soon)
    let have = getHavItems(activities);

    console.log('Here are the people who already have a W-2 activity:');
    console.log(Object.keys(have));

    let soonArr = Object.keys(soon).map(d => +d);

    console.log('Here are the deals that could potentially need a W-2 soon:');
    console.log(soonArr);

    const results = await Promise.all(soonArr.map(d =>
    getId(`https://api.pipedrive.com/v1/deals/${d}/participants?start=0&api_token=${API_KEY}`)
    ));
    const soonPart = results.reduce((obj, part) => { ...obj, [`${part.related_item_id}${part.person.id}`]:part }, {});

    console.log('Here are the people associated with those deals that are EMPLOYED:');
    console.log(Object.keys(soonPart));

    let soonQueue = Object.keys(soonPart).filter(part => Object.keys(have).indexOf(part) === -1);

    console.log('Here is the soon queue:');
    console.log(soonQueue);

    const getId = url => request(url).then(b => JSON.parse(b).data)

    Promise.all(soonArr.map(d => getId(`https://api.pipedrive.com/v1/deals/${d}/participants?start=0&api_token=${API_KEY}`)))
    .then(results => results.reduce((obj, part) => { ...obj, [`${part.related_item_id}${part.person.id}`]:part }, {}))
    .then(soonPart => {
    console.log('Here are the people associated with those deals that are EMPLOYED:');
    console.log(Object.keys(soonPart));

    let soonQueue = Object.keys(soonPart).filter(part => Object.keys(have).indexOf(part) === -1);

    console.log('Here is the soon queue:');
    console.log(soonQueue);

    return Promise.all(soonQueue.map(cat => request.post(
    `https://api.pipedrive.com/v1/activities?api_token=${API_KEY}`,
    {
    form: {
    subject : 'W-2',
    deal_id : soonPart[cat].related_item_id,
    person_id: soonPart[cat].person.id,
    type : 'task',
    note : `Sign and scan 1003 for ${soonPart[cat].person_id.name}.`,
    due_date : moment().add(3, 'days').format('YYYY-MM-DD')}
    }
    )));
    })
    .then(function() {
    console.log("Do moar shit heeeeaaaaare!!!");
    });
    }
    await Promise.all(soonQueue.map(cat => request.post(
    `https://api.pipedrive.com/v1/activities?api_token=${API_KEY}`,
    {
    form: {
    subject : 'W-2',
    deal_id : soonPart[cat].related_item_id,
    person_id: soonPart[cat].person.id,
    type : 'task',
    note : `Sign and scan 1003 for ${soonPart[cat].person_id.name}.`,
    due_date : moment().add(3, 'days').format('YYYY-MM-DD')}
    }
    )));

    console.log("Do moar shit heeeeaaaaare!!!");

    } catch(err) {
    console.error('ERROR', err);
    }
    }
  2. tracker1 revised this gist Jun 8, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion newW2.js
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,7 @@ module.exports = function (API_KEY, deals, activities, app, type, pbl, address,
    deal_id : soonPart[cat].related_item_id,
    person_id: soonPart[cat].person.id,
    type : 'task',
    note : 1Sign and scan 1003 for ${soonPart[cat].person_id.name}.1,
    note : `Sign and scan 1003 for ${soonPart[cat].person_id.name}.`,
    due_date : moment().add(3, 'days').format('YYYY-MM-DD')}
    }
    )));
  3. tracker1 revised this gist Jun 8, 2016. 1 changed file with 43 additions and 76 deletions.
    119 changes: 43 additions & 76 deletions newW2.js
    Original file line number Diff line number Diff line change
    @@ -1,96 +1,63 @@
    const request = require('request-promise');
    const moment = require('moment');

    module.exports = function (API_KEY, deals, activities, app, type, pbl, address, test){
    const request = require('request-promise');
    const moment = require('moment');
    moment().format();

    var soon = {};
    //moment().format();

    var soonPart = {};
    var later = {};
    var laterPart = {};
    var have = {};

    deals.map(function(deal) {
    if (deal.stage_id === app && deal[type.key] === type.refi ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.no) {
    soon[deal.id] = deal;
    } else if (deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === '' ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === null) {
    later[deal.id] = deal;
    }
    });

    activities.map(function(activity) {
    if (activity.subject === 'W-2') {
    var dealID = activity.deal_id.toString();
    var personID = activity.person_id.toString();
    var cat = dealID + personID;

    console.log(cat);
    have[cat] = activity;
    }
    });
    let soon = deals.filter(deal => (
    deal.stage_id === app && deal[type.key] === type.refi ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.no
    )).reduce((obj, deal) => { ...obj, [deal.id]:deal });

    let later = deals.filter(deal => (
    !soon[deal.id] && (
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === '' ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === null
    )
    )).reduce((obj, deal) => { ...obj, [deal.id]:deal });

    let have = activities.filter(a => a.subject === 'W-2')
    .reduce((obj, a) => { ...obj, [`${a.deal_id}${deal.person_id}`]: a });

    console.log('Here are the people who already have a W-2 activity:');
    console.log(Object.keys(have));

    var soonArr = (Object.keys(soon)).map(function(deal) {
    return Number(deal);
    });
    let soonArr = Object.keys(soon).map(d => +d);

    console.log('Here are the deals that could potentially need a W-2 soon:');
    console.log(soonArr);

    function getIt(url) {
    return request(url).then(function(body) {
    return JSON.parse(body).data;
    });
    }

    return Promise.all([]).then(function() {
    var promises = soonArr.map(function(deal) {
    var url = 'https://api.pipedrive.com/v1/deals/' + deal + '/participants?start=0&api_token=' + API_KEY;

    return Promise.all([getIt(url)]).then(function(results){

    results[0].map(function(part){

    if (part.person[test.key] === '14') {
    const getId = url => request(url).then(b => JSON.parse(b).data)

    var relatedItemID = part.related_item_id.toString();
    var personID = part.person.id.toString();
    var cat = relatedItemID + personID;

    soonPart[cat] = part;
    }

    });
    });
    });
    return Promise.all(promises);

    }).then(function() {
    Promise.all(soonArr.map(d => getId(`https://api.pipedrive.com/v1/deals/${d}/participants?start=0&api_token=${API_KEY}`)))
    .then(results => results.reduce((obj, part) => { ...obj, [`${part.related_item_id}${part.person.id}`]:part }, {}))
    .then(soonPart => {
    console.log('Here are the people associated with those deals that are EMPLOYED:');
    console.log(Object.keys(soonPart));

    var soonQueue = Object.keys(soonPart).filter(function(part) {
    return Object.keys(have).indexOf(part) === -1;
    });


    let soonQueue = Object.keys(soonPart).filter(part => Object.keys(have).indexOf(part) === -1);

    console.log('Here is the soon queue:');
    console.log(soonQueue);

    soonQueue.map(function(cat){
    request.post('https://api.pipedrive.com/v1/activities?api_token=' + API_KEY, {
    form: {'subject': 'W-2',
    'deal_id': soonPart[cat].related_item_id,
    'person_id': soonPart[cat].person.id,
    'type' : 'task',
    'note' : 'Sign and scan 1003 for ' + soonPart[cat].person_id.name + '.',
    'due_date' : moment().add(3, 'days').format('YYYY-MM-DD')}});
    });

    }).then(function() {

    return Promise.all(soonQueue.map(cat => request.post(
    `https://api.pipedrive.com/v1/activities?api_token=${API_KEY}`,
    {
    form: {
    subject : 'W-2',
    deal_id : soonPart[cat].related_item_id,
    person_id: soonPart[cat].person.id,
    type : 'task',
    note : 1Sign and scan 1003 for ${soonPart[cat].person_id.name}.1,
    due_date : moment().add(3, 'days').format('YYYY-MM-DD')}
    }
    )));
    })
    .then(function() {
    console.log("Do moar shit heeeeaaaaare!!!");
    });

    });
    }
  4. @joetemp joetemp created this gist Jun 8, 2016.
    96 changes: 96 additions & 0 deletions newW2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    module.exports = function (API_KEY, deals, activities, app, type, pbl, address, test){
    const request = require('request-promise');
    const moment = require('moment');
    moment().format();

    var soon = {};
    var soonPart = {};
    var later = {};
    var laterPart = {};
    var have = {};

    deals.map(function(deal) {
    if (deal.stage_id === app && deal[type.key] === type.refi ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.no) {
    soon[deal.id] = deal;
    } else if (deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === '' ||
    deal.stage_id === app && deal[type.key] === type.purchase && deal[pbl.key] === pbl.yes && deal[address.key] === null) {
    later[deal.id] = deal;
    }
    });

    activities.map(function(activity) {
    if (activity.subject === 'W-2') {
    var dealID = activity.deal_id.toString();
    var personID = activity.person_id.toString();
    var cat = dealID + personID;

    console.log(cat);
    have[cat] = activity;
    }
    });

    console.log('Here are the people who already have a W-2 activity:');
    console.log(Object.keys(have));

    var soonArr = (Object.keys(soon)).map(function(deal) {
    return Number(deal);
    });

    console.log('Here are the deals that could potentially need a W-2 soon:');
    console.log(soonArr);

    function getIt(url) {
    return request(url).then(function(body) {
    return JSON.parse(body).data;
    });
    }

    return Promise.all([]).then(function() {
    var promises = soonArr.map(function(deal) {
    var url = 'https://api.pipedrive.com/v1/deals/' + deal + '/participants?start=0&api_token=' + API_KEY;

    return Promise.all([getIt(url)]).then(function(results){

    results[0].map(function(part){

    if (part.person[test.key] === '14') {

    var relatedItemID = part.related_item_id.toString();
    var personID = part.person.id.toString();
    var cat = relatedItemID + personID;

    soonPart[cat] = part;
    }

    });
    });
    });
    return Promise.all(promises);

    }).then(function() {
    console.log('Here are the people associated with those deals that are EMPLOYED:');
    console.log(Object.keys(soonPart));

    var soonQueue = Object.keys(soonPart).filter(function(part) {
    return Object.keys(have).indexOf(part) === -1;
    });

    console.log('Here is the soon queue:');
    console.log(soonQueue);

    soonQueue.map(function(cat){
    request.post('https://api.pipedrive.com/v1/activities?api_token=' + API_KEY, {
    form: {'subject': 'W-2',
    'deal_id': soonPart[cat].related_item_id,
    'person_id': soonPart[cat].person.id,
    'type' : 'task',
    'note' : 'Sign and scan 1003 for ' + soonPart[cat].person_id.name + '.',
    'due_date' : moment().add(3, 'days').format('YYYY-MM-DD')}});
    });

    }).then(function() {
    console.log("Do moar shit heeeeaaaaare!!!");
    });

    }