Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // require('dotenv').config();
- const nodemailer = require('nodemailer');
- const fsm = require('fsm-sdk');
- const emailClient = nodemailer.createTransport({
- service: 'gmail',
- auth: {
- user: process.env.email_from,
- pass: process.env.email_password // set a app password for gmail
- }
- });
- const fsmClient = new fsm.CoreAPIClient({
- debug: false,
- clientVersion: '0.0.1',
- clientIdentifier: process.env.fsm_client_identifier,
- clientSecret: process.env.fsm_client_secret,
- authAccountName: process.env.fsm_auth_accountname,
- authUserName: process.env.fsm_auth_username,
- authPassword: process.env.fsm_auth_password
- });
- async function getById(resourceName, resourceId) {
- return fsmClient.getById(resourceName, resourceId)
- .then(r => {
- const alias = resourceName.split('')
- .reduce((word, char, idx) => `${word}${(idx === 0 ? char.toLowerCase() : char)}`, '');
- const it = r.data[0][alias];
- // console.log(alias, JSON.stringify(it, null, 2));
- return it;
- });
- }
- async function sendMail(params) {
- return new Promise((ok, fail) => {
- try {
- // console.log(`sendMail, ${JSON.stringify(params)}`);
- const { to, subject, html } = params;
- emailClient.sendMail({ to, subject, html, from: process.env.email_from }, (error, info) => {
- if (error)
- fail(error);
- else
- ok(info.response);
- });
- } catch (error) {
- fail(error);
- }
- });
- }
- async function main(id) {
- const activity = await getById('Activity', id);
- console.log('activity', activity.subject, activity.remarks)
- const bp = await getById('BusinessPartner', activity.businessPartner);
- console.log('businessPartner', bp.emailAddress, bp.name);
- const technician = await getById('UnifiedPerson', activity.responsibles[0]);
- console.log('technician', technician.firstName, technician.lastName)
- await sendMail({
- to: bp.emailAddress,
- subject: '[Edufy] Response from the helper crowd',
- html: [
- `Hi <strong>${bp.name}</strong>`,
- ``,
- `your question:`,
- `<pre>"${activity.subject}"</pre>`,
- ``,
- `${technician.firstName} ${technician.lastName} answered:`,
- `<pre>"${activity.remarks}"</pre>`
- ].join('<BR/>')
- });
- }
- // add trigger CS BR that triggers
- // when [ServiceCheckout] is created
- // context:
- // activity => activity.id IN ${serviceCheckout.activities}
- // conditions:
- // ${activity.remarks} != null
- // trigger web-hook with
- // body => { "id":"${activity.id}" }
- // read event.data.id
- (async (event, context) => {
- try {
- await main('148774D1B48C4294B689F12FE6C98CBE'); // event.data.id here
- } catch (error) {
- console.error(error);
- }
- })()
Add Comment
Please, Sign In to add comment