View & send PDF from loopback endpoint
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2022-09-19 12:59:32 +02:00
parent 18ac0a0b61
commit 09bdee8b34
15 changed files with 181 additions and 95 deletions

View File

@ -14,8 +14,8 @@ class Email {
* @param {Object} params The email parameters
* @return {Promise} Promise resolved when it's sent
*/
send(template, params) {
return this.$http.get(`email/${template}`, {params})
send(url, params) {
return this.$http.post(url, params)
.then(() => this.vnApp.showMessage(this.$t('Notification sent!')));
}

View File

@ -13,12 +13,12 @@ class Report {
* @param {String} report The report name
* @param {Object} params The report parameters
*/
show(report, params) {
show(url, params) {
params = Object.assign({
authorization: this.vnToken.token
access_token: this.vnToken.token
}, params);
const serializedParams = this.$httpParamSerializer(params);
window.open(`api/report/${report}?${serializedParams}`);
window.open(`api/${url}?${serializedParams}`);
}
/**

View File

@ -130,5 +130,6 @@
"Descanso diario 12h.": "Daily rest 12h.",
"Fichadas impares": "Odd signs",
"Descanso diario 9h.": "Daily rest 9h.",
"Descanso semanal 36h. / 72h.": "Weekly rest 36h. / 72h."
"Descanso semanal 36h. / 72h.": "Weekly rest 36h. / 72h.",
"deliveryNote": "deliveryNote-{{ticketId}}"
}

View File

@ -232,5 +232,6 @@
"Fichadas impares": "Fichadas impares",
"Descanso diario 12h.": "Descanso diario 12h.",
"Descanso semanal 36h. / 72h.": "Descanso semanal 36h. / 72h.",
"Dirección incorrecta": "Dirección incorrecta"
"Dirección incorrecta": "Dirección incorrecta",
"deliveryNote": "albaran-{{ticketId}}"
}

View File

@ -1,47 +0,0 @@
const {Report, smtp} = require('vn-print');
module.exports = Self => {
Self.remoteMethod('deliveryNote', {
description: '',
accepts: [
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: '/delivery-note',
verb: 'GET'
}
});
Self.deliveryNote = async() => {
const params = {ticketId: 1};
const rpt = new Report('delivery-note', params, {
module: 'ticket',
lang: 'es'
});
const stream = await rpt.render();
// await smtp.send({
// from: 'joan@verdnatura.es',
// to: 'joan@verdnatura.es',
// subject: 'test123',
// body: rptTpl
// });
return [stream, 'text/html', `filename="test.pdf"`];
};
};

View File

@ -0,0 +1,63 @@
const {Report, Email, smtp} = require('vn-print');
module.exports = Self => {
Self.remoteMethodCtx('deliveryNoteEmail', {
description: '',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'recipient',
type: 'string',
description: 'The recipient email',
required: true,
},
{
arg: 'replyTo',
type: 'string',
description: 'The sender email to reply to',
required: false
},
{
arg: 'recipientId',
type: 'number',
description: 'The recipient id to send to the recipient preferred language',
required: false
},
],
returns: {
type: ['object'],
root: true
},
http: {
path: '/:id/delivery-note-email',
verb: 'POST'
}
});
Self.deliveryNoteEmail = async(ctx, id) => {
const args = ctx.args;
const params = {
ticketId: id,
recipient: args.recipient,
lang: ctx.req.getLocale()
};
if (args.recipientId)
params.recipientId = args.recipientId;
if (args.replyTo)
params.replyTo = args.replyTo;
const email = new Email('delivery-note', params);
await email.send();
return true;
};
};

View File

@ -0,0 +1,61 @@
const {Report, Email, smtp} = require('vn-print');
module.exports = Self => {
Self.remoteMethodCtx('deliveryNotePdf', {
description: '',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'recipientId',
type: 'number',
description: 'The client id',
required: false
},
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: '/:id/delivery-note-pdf',
verb: 'GET'
}
});
Self.deliveryNotePdf = async(ctx, id) => {
const args = ctx.args;
const $t = ctx.req.__;
const params = {
ticketId: id,
lang: ctx.req.getLocale()
};
if (args.recipientId)
params.recipientId = args.recipientId;
const report = new Report('delivery-note', params);
const stream = await report.toPdfStream();
const fileName = $t('deliveryNote', {ticketId: id});
return [stream, 'application/pdf', `filename="${fileName}.pdf"`];
};
};

View File

@ -28,7 +28,8 @@ module.exports = Self => {
require('../methods/ticket/freightCost')(Self);
require('../methods/ticket/getComponentsSum')(Self);
require('../methods/ticket/refund')(Self);
require('../methods/ticket/deliveryNote')(Self);
require('../methods/ticket/deliveryNotePdf')(Self);
require('../methods/ticket/deliveryNoteEmail')(Self);
Self.observe('before save', async function(ctx) {
const loopBackContext = LoopBackContext.getCurrentContext();

View File

@ -116,14 +116,6 @@ class Controller extends Section {
});
}
showPdfDeliveryNote(type) {
this.vnReport.show('delivery-note', {
recipientId: this.ticket.client.id,
ticketId: this.id,
type: type
});
}
hasDocuware() {
const params = {
fileCabinet: 'deliveryClient',
@ -133,18 +125,24 @@ class Controller extends Section {
.then(res => this.hasDocuwareFile = res.data);
}
showCsvDeliveryNote() {
this.vnReport.showCsv('delivery-note', {
showPdfDeliveryNote(type) {
this.vnReport.show(`tickets/${this.id}/delivery-note-pdf`, {
recipientId: this.ticket.client.id,
ticketId: this.id,
type: type
});
}
sendPdfDeliveryNote($data) {
return this.vnEmail.send('delivery-note', {
return this.vnEmail.send(`tickets/${this.id}/delivery-note-email`, {
recipientId: this.ticket.client.id,
recipient: $data.email,
ticketId: this.id
recipient: $data.email
});
}
showCsvDeliveryNote() {
this.vnReport.showCsv('delivery-note', {
recipientId: this.ticket.client.id,
ticketId: this.id,
});
}

View File

@ -53,21 +53,21 @@ class Component {
}
async getUserLocale() {
let locale = this.args.auth.locale;
let lang = this.args.lang;
// Fetches user locale from mixing method getLocale()
if (this.args.recipientId) {
const component = await this.component();
locale = await component.getLocale(this.args.recipientId);
lang = await component.getLocale(this.args.recipientId);
}
const messages = this.locale.messages;
const userTranslations = messages[locale];
const userTranslations = messages[lang];
if (!userTranslations) {
const fallbackLocale = config.i18n.fallbackLocale;
const fallbackLang = config.i18n.fallbackLocale;
return messages[fallbackLocale];
return messages[fallbackLang];
}
return userTranslations;

View File

@ -1,9 +1,12 @@
const Stylesheet = require(`${appPath}/core/stylesheet`);
const Stylesheet = require(`vn-print/core/stylesheet`);
const path = require('path');
const vnPrintPath = path.resolve('print');
module.exports = new Stylesheet([
`${appPath}/common/css/spacing.css`,
`${appPath}/common/css/misc.css`,
`${appPath}/common/css/layout.css`,
`${appPath}/common/css/email.css`,
`${vnPrintPath}/common/css/spacing.css`,
`${vnPrintPath}/common/css/misc.css`,
`${vnPrintPath}/common/css/layout.css`,
`${vnPrintPath}/common/css/email.css`,
`${__dirname}/style.css`])
.mergeStyles();
.mergeStyles();

View File

@ -1,9 +1,12 @@
const Stylesheet = require(`${appPath}/core/stylesheet`);
const Stylesheet = require(`vn-print/core/stylesheet`);
const path = require('path');
const vnPrintPath = path.resolve('print');
module.exports = new Stylesheet([
`${appPath}/common/css/spacing.css`,
`${appPath}/common/css/misc.css`,
`${appPath}/common/css/layout.css`,
`${appPath}/common/css/email.css`,
`${vnPrintPath}/common/css/spacing.css`,
`${vnPrintPath}/common/css/misc.css`,
`${vnPrintPath}/common/css/layout.css`,
`${vnPrintPath}/common/css/email.css`,
`${__dirname}/style.css`])
.mergeStyles();
.mergeStyles();

View File

@ -73,10 +73,9 @@ class Email extends Component {
}
const localeSubject = await this.getSubject();
const replyTo = this.args.replyTo || this.args.auth.email;
const mailOptions = {
to: this.args.recipient,
replyTo: replyTo,
replyTo: this.args.replyTo || '',
subject: localeSubject,
html: rendered,
attachments: attachments

View File

@ -1,8 +1,11 @@
const Stylesheet = require(`${appPath}/core/stylesheet`);
const Stylesheet = require(`vn-print/core/stylesheet`);
const path = require('path');
const vnPrintPath = path.resolve('print');
module.exports = new Stylesheet([
`${appPath}/common/css/spacing.css`,
`${appPath}/common/css/misc.css`,
`${appPath}/common/css/layout.css`,
`${appPath}/common/css/email.css`])
.mergeStyles();
`${vnPrintPath}/common/css/spacing.css`,
`${vnPrintPath}/common/css/misc.css`,
`${vnPrintPath}/common/css/layout.css`,
`${vnPrintPath}/common/css/email.css`])
.mergeStyles();

View File

@ -1,4 +1,4 @@
const Component = require(`${appPath}/core/component`);
const Component = require(`vn-print/core/component`);
const emailHeader = new Component('email-header');
const emailFooter = new Component('email-footer');