salix/back/methods/osticket/sendToSupport.js

97 lines
3.4 KiB
JavaScript

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']});
const tableStyle = 'width:100%; border-collapse: collapse; text-align: left;';
const thStyle = 'padding: 8px; border: 1px solid #ddd; background-color: #f4f4f4;';
const tdStyle = 'padding: 8px; border: 1px solid #ddd;';
const tdBoldStyle = 'padding: 8px; border: 1px solid #ddd; font-weight: bold;';
const subTdStyle = 'padding: 6px; border: 1px solid #ddd;';
const subTdBoldStyle = 'padding: 6px; border: 1px solid #ddd; font-weight: bold;';
let html = `
<h2>Motivo: ${reason}</h2>
<h3>Usuario: ${userId} ${emailUser.email}</h3>
<h3>Additional Data:</h3>
<table style="${tableStyle}">
<thead>
<tr>
<th style="${thStyle}">Clave</th><th style="${thStyle}">Valor</th></tr>
</thead>
<tbody>`;
for (const [key, val] of Object.entries(additionalData)) {
if (key !== 'config') {
html += `<tr>
<td style="${tdBoldStyle}">${key}</td>
<td style="${tdStyle}">${parse(val)}</td>
</tr>`;
} else {
html += `<tr>
<td style="${tdBoldStyle}">${key}</td>
<td style="${tdStyle}">
<table style="${tableStyle}">
<tbody>`;
for (const [confKey, confVal] of Object.entries(val)) {
html += `<tr>
<td style="${subTdBoldStyle}">${confKey}</td>
<td style="${subTdStyle}">${parse(confVal)}</td>
</tr>`;
}
html += `</tbody></table></td></tr>`;
}
}
html += `</tbody></table>`;
const {message, path, name} = additionalData;
const err = name && message ? `${name}: ${message}` : name || message || '';
await smtp.send({
to: `${config.app.reportEmail}, ${emailUser.email}`,
subject: `[Support-Salix] ${path.split('?')[0]} ${err}`,
html
});
};
function parse(value) {
try {
try {
value = JSON.parse(value);
} catch {}
return JSON.stringify(value, null, '&nbsp;').split('\n').join('<br>');
} catch {
return value;
}
}
};