Guest User

Untitled

a guest
Apr 26th, 2018
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import * as nodemailer from 'nodemailer';
  2. import * as passport from 'koa-passport';
  3. import * as Router from 'koa-router';
  4. import * as Pipedrive from 'pipedrive';
  5. import { STATUS_CODES } from 'http';
  6. import { InvalidParameters } from './auth/errors';
  7. import { countBy, find, propEq, indexBy, prop } from 'ramda';
  8. import { getFiltered, getAll } from './pipeDrives';
  9.  
  10. const reMap = {
  11. "deals": "pipeline",
  12. "people": "persons"
  13. }
  14.  
  15. const from = "[email protected]";
  16.  
  17. const htmlEmail = (count, url) =>( `<img src="https://image.ibb.co/g48o3c/pipedrive1.png" style="width:100%"/><br />
  18. <h3>PIPEDRIVE DATA need your action</h3><br/> <br/> There are ${count} items that require your update in PIPEDRIVE <br /> <br />
  19. ${url} <br /><br />Automatic Mail Robot for PIPEDRIVE made in V1`
  20. )
  21.  
  22. const createUrl = (type, id) => `https://directpeople.pipedrive.com/${reMap[type]}/filter/${id}`
  23.  
  24.  
  25. const transporter = nodemailer.createTransport({
  26. host: 'email-smtp.eu-west-1.amazonaws.com',
  27. port: 465,
  28. secure: true,
  29. auth: {
  30. user: 'AKIAICUMWZ5GBOJETY4A',
  31. pass: 'AhfLkkXlJX17GK6ernJFlXiQ4XrQVzo931Idj97b2hSS',
  32. }
  33. });
  34.  
  35. const sendEmail = async (ids, emails, url) => {
  36. Object.keys(ids).map((item, index) => {
  37. let mailOptions = {
  38. from, // sender address
  39. subject: emails[item].email, // Subject line
  40. html: htmlEmail(ids[item], url) // html body
  41. };
  42. transporter.sendMail(mailOptions, (error, info) => {
  43. if (error) {
  44. return console.log(error);
  45. }
  46. console.log('Message sent: %s', info.messageId);
  47. console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
  48. });
  49.  
  50. })
  51. }
  52.  
  53. const router = new Router();
  54.  
  55. // get entities
  56. router.get('send', async ctx => {
  57. const { pipedrive } = ctx.state.authInfo;
  58. const { filterId, type } = ctx.query;
  59. const body = await getFiltered(pipedrive, type, filterId);
  60. const names : any = await getAll(pipedrive, "Users", null)
  61. const emails = [];
  62. const countArr = countBy(person => body[person].owner_name, Object.keys(body));
  63. const nameIndex = indexBy(prop("name"), names);
  64. const url = createUrl(type, filterId);
  65. sendEmail(countArr, nameIndex, url );
  66. ctx.body = countArr;
  67. ctx.status = 200;
  68. });
  69.  
  70. export default router;
Advertisement
Add Comment
Please, Sign In to add comment