const smtp = require('vn-print/core/smtp');
const config = require('vn-print/core/config');
module.exports = Self => {
Self.remoteMethodCtx('sendToSupport', {
description: 'Send mail to support',
accessType: 'WRITE',
accepts: [
{
arg: 'reason',
type: 'string',
description: 'The reason'
},
{
arg: 'additionalData',
type: 'object',
required: true,
description: 'The additional data'
}
],
returns: {
type: 'object',
root: true
},
http: {
path: `/send-to-support`,
verb: 'POST'
}
});
Self.sendToSupport = async(ctx, reason, additionalData) => {
const userId = ctx.req.accessToken.userId;
const emailUser =
await Self.app.models.EmailUser.findById(userId, {fields: ['email']});
let html = `
Motivo: ${reason}
`;
html += `Usuario: ${userId} ${emailUser.email}
`;
html += `Additional Data:
`;
html += '';
for (const [key, val] of Object.entries(additionalData)) {
if (key !== 'config') html += `- ${key}: ${parse(val)}
`;
else {
html += `- ${key}:
`;
for (const [confKey, confVal] of Object.entries(val))
html += `- ${confKey}: ${parse(confVal)}
`;
html += '
';
}
}
html += '
';
const {message, path, name} = additionalData;
await smtp.send({
to: `${config.app.reportEmail}, ${emailUser.email}`,
subject: `[Support-Salix] ${path} ${name}: ${message}`,
html
});
};
function parse(value) {
try {
try {
value = JSON.parse(value);
} catch {}
return JSON.stringify(value, null, ' ').split('\n').join('
');
} catch {
return value;
}
}
};