Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
574
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 = "PipeDriveAMS@viableone.cz";
  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. to: 'uragecz@gmail.com, otto.kalivoda@viableone.cz, frunz@viableone.cz', // list of receivers
  40. subject: emails[item].email, // Subject line
  41. html: htmlEmail(ids[item], url) // html body
  42. };
  43. transporter.sendMail(mailOptions, (error, info) => {
  44. if (error) {
  45. return console.log(error);
  46. }
  47. console.log('Message sent: %s', info.messageId);
  48. console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
  49. });
  50.  
  51. })
  52. }
  53.  
  54. const router = new Router();
  55.  
  56. // get entities
  57. router.get('send', async ctx => {
  58. const { pipedrive } = ctx.state.authInfo;
  59. const { filterId, type } = ctx.query;
  60. const body = await getFiltered(pipedrive, type, filterId);
  61. const names : any = await getAll(pipedrive, "Users", null)
  62. const emails = [];
  63. const countArr = countBy(person => body[person].owner_name, Object.keys(body));
  64. const nameIndex = indexBy(prop("name"), names);
  65. const url = createUrl(type, filterId);
  66. sendEmail(countArr, nameIndex, url );
  67. ctx.body = countArr;
  68. ctx.status = 200;
  69. });
  70.  
  71. export default router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement