Guest User

Untitled

a guest
Oct 5th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. // require('dotenv').config();
  2.  
  3. const nodemailer = require('nodemailer');
  4. const fsm = require('fsm-sdk');
  5.  
  6.  
  7. const emailClient = nodemailer.createTransport({
  8. service: 'gmail',
  9. auth: {
  10. user: process.env.email_from,
  11. pass: process.env.email_password // set a app password for gmail
  12. }
  13. });
  14.  
  15. const fsmClient = new fsm.CoreAPIClient({
  16. debug: false,
  17. clientVersion: '0.0.1',
  18. clientIdentifier: process.env.fsm_client_identifier,
  19. clientSecret: process.env.fsm_client_secret,
  20. authAccountName: process.env.fsm_auth_accountname,
  21. authUserName: process.env.fsm_auth_username,
  22. authPassword: process.env.fsm_auth_password
  23. });
  24.  
  25. async function getById(resourceName, resourceId) {
  26. return fsmClient.getById(resourceName, resourceId)
  27. .then(r => {
  28. const alias = resourceName.split('')
  29. .reduce((word, char, idx) => `${word}${(idx === 0 ? char.toLowerCase() : char)}`, '');
  30. const it = r.data[0][alias];
  31. // console.log(alias, JSON.stringify(it, null, 2));
  32. return it;
  33. });
  34. }
  35.  
  36. async function sendMail(params) {
  37. return new Promise((ok, fail) => {
  38. try {
  39. // console.log(`sendMail, ${JSON.stringify(params)}`);
  40. const { to, subject, html } = params;
  41. emailClient.sendMail({ to, subject, html, from: process.env.email_from }, (error, info) => {
  42. if (error)
  43. fail(error);
  44. else
  45. ok(info.response);
  46. });
  47. } catch (error) {
  48. fail(error);
  49. }
  50. });
  51. }
  52.  
  53. async function main(id) {
  54.  
  55. const activity = await getById('Activity', id);
  56. console.log('activity', activity.subject, activity.remarks)
  57.  
  58. const bp = await getById('BusinessPartner', activity.businessPartner);
  59. console.log('businessPartner', bp.emailAddress, bp.name);
  60.  
  61. const technician = await getById('UnifiedPerson', activity.responsibles[0]);
  62. console.log('technician', technician.firstName, technician.lastName)
  63.  
  64.  
  65. await sendMail({
  66. to: bp.emailAddress,
  67. subject: '[Edufy] Response from the helper crowd',
  68. html: [
  69. `Hi <strong>${bp.name}</strong>`,
  70. ``,
  71. `your question:`,
  72. `<pre>"${activity.subject}"</pre>`,
  73. ``,
  74. `${technician.firstName} ${technician.lastName} answered:`,
  75. `<pre>"${activity.remarks}"</pre>`
  76. ].join('<BR/>')
  77. });
  78.  
  79. }
  80.  
  81. // add trigger CS BR that triggers
  82. // when [ServiceCheckout] is created
  83. // context:
  84. // activity => activity.id IN ${serviceCheckout.activities}
  85. // conditions:
  86. // ${activity.remarks} != null
  87. // trigger web-hook with
  88. // body => { "id":"${activity.id}" }
  89. // read event.data.id
  90. (async (event, context) => {
  91.  
  92. try {
  93. await main('148774D1B48C4294B689F12FE6C98CBE'); // event.data.id here
  94. } catch (error) {
  95. console.error(error);
  96. }
  97.  
  98. })()
Add Comment
Please, Sign In to add comment