feat(print): added reusable pdf print functions #1390

Merged
joan merged 7 commits from 5182-print_publish into dev 2023-03-16 06:22:59 +00:00
32 changed files with 58 additions and 318 deletions
Showing only changes of commit 6495e016a8 - Show all commits

View File

@ -31,5 +31,5 @@ module.exports = Self => {
} }
}); });
Self.previousLabel = (ctx, id) => Self.printPdf(ctx, id, 'previa-label'); Self.previousLabel = (ctx, id) => Self.printReport(ctx, id, 'previa-label');
}; };

View File

@ -1,7 +1,7 @@
const {Report, Email} = require('vn-print'); const {Report, Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.printPdf = async function(ctx, id, reportName) { Self.printReport = async function(ctx, id, reportName) {
const args = Object.assign({}, ctx.args); const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()}; const params = {lang: ctx.req.getLocale()};
@ -12,10 +12,34 @@ module.exports = Self => {
const report = new Report(reportName, params); const report = new Report(reportName, params);
const stream = await report.toPdfStream(); const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; let fileName = `${reportName}`;
if (id) fileName += `-${id}`;
return [stream, 'application/pdf', `filename="${fileName}.pdf"`];
}; };
Self.sendTemplate = async function(ctx, reportName) { Self.printEmail = async function(ctx, id, templateName) {
const {accessToken} = ctx.req;
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
params.access_token = accessToken.id;
const report = new Email(templateName, params);
const html = await report.render();
let fileName = `${templateName}`;
if (id) fileName += `-${id}`;
return [html, 'text/html', `filename=${fileName}.pdf"`];
};
Self.sendTemplate = async function(ctx, templateName) {
const args = Object.assign({}, ctx.args); const args = Object.assign({}, ctx.args);
const params = { const params = {
recipient: args.recipient, recipient: args.recipient,
@ -26,7 +50,7 @@ module.exports = Self => {
for (const param in args) for (const param in args)
params[param] = args[param]; params[param] = args[param];
const email = new Email(reportName, params); const email = new Email(templateName, params);
return email.send(); return email.send();
}; };

View File

@ -37,5 +37,5 @@ module.exports = Self => {
} }
}); });
Self.claimPickupPdf = (ctx, id) => Self.printPdf(ctx, id, 'claim-pickup-order'); Self.claimPickupPdf = (ctx, id) => Self.printReport(ctx, id, 'claim-pickup-order');
}; };

View File

@ -48,5 +48,5 @@ module.exports = Self => {
} }
}); });
Self.campaignMetricsPdf = (ctx, id) => Self.sendPdf(ctx, id, 'campaign-metrics'); Self.campaignMetricsPdf = (ctx, id) => Self.printReport(ctx, id, 'campaign-metrics');
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('clientDebtStatementHtml', { Self.remoteMethodCtx('clientDebtStatementHtml', {
description: 'Returns the client debt statement email preview', description: 'Returns the client debt statement email preview',
@ -45,21 +43,5 @@ module.exports = Self => {
} }
}); });
Self.clientDebtStatementHtml = async(ctx, id) => { Self.clientDebtStatementHtml = (ctx, id) => Self.printEmail(ctx, id, 'client-debt-statement');
const {accessToken} = ctx.req;
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
params.access_token = accessToken.id;
const report = new Email('client-debt-statement', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -43,5 +43,5 @@ module.exports = Self => {
} }
}); });
Self.clientDebtStatementPdf = (ctx, id) => Self.sendPdf(ctx, id, 'client-debt-statement'); Self.clientDebtStatementPdf = (ctx, id) => Self.printReport(ctx, id, 'client-debt-statement');
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('clientWelcomeHtml', { Self.remoteMethodCtx('clientWelcomeHtml', {
description: 'Returns the client welcome email preview', description: 'Returns the client welcome email preview',
@ -40,19 +38,5 @@ module.exports = Self => {
} }
}); });
Self.clientWelcomeHtml = async(ctx, id) => { Self.clientWelcomeHtml = (ctx, id) => Self.printEmail(ctx, id, 'client-welcome');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
const report = new Email('client-welcome', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('creditRequestHtml', { Self.remoteMethodCtx('creditRequestHtml', {
description: 'Returns the credit request email preview', description: 'Returns the credit request email preview',
@ -40,21 +38,5 @@ module.exports = Self => {
} }
}); });
Self.creditRequestHtml = async(ctx, id) => { Self.creditRequestHtml = (ctx, id) => Self.printEmail(ctx, id, 'credit-request');
const {accessToken} = ctx.req;
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
params.access_token = accessToken.id;
const report = new Email('credit-request', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -38,5 +38,5 @@ module.exports = Self => {
} }
}); });
Self.creditRequestPdf = (ctx, id) => Self.sendPdf(ctx, id, 'credit-request'); Self.creditRequestPdf = (ctx, id) => Self.printReport(ctx, id, 'credit-request');
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('incotermsAuthorizationEmail', { Self.remoteMethodCtx('incotermsAuthorizationEmail', {
description: 'Sends the incoterms authorization email with an attached PDF', description: 'Sends the incoterms authorization email with an attached PDF',

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('incotermsAuthorizationHtml', { Self.remoteMethodCtx('incotermsAuthorizationHtml', {
description: 'Returns the incoterms authorization email preview', description: 'Returns the incoterms authorization email preview',
@ -46,21 +44,5 @@ module.exports = Self => {
} }
}); });
Self.incotermsAuthorizationHtml = async(ctx, id) => { Self.incotermsAuthorizationHtml = (ctx, id) => Self.printEmail(ctx, id, 'incoterms-authorization');
const {accessToken} = ctx.req;
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
params.access_token = accessToken.id;
const report = new Email('incoterms-authorization', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -44,5 +44,5 @@ module.exports = Self => {
} }
}); });
Self.incotermsAuthorizationPdf = (ctx, id) => Self.sendPdf(ctx, id, 'incoterms-authorization'); Self.incotermsAuthorizationPdf = (ctx, id) => Self.printReport(ctx, id, 'incoterms-authorization');
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('letterDebtorNdHtml', { Self.remoteMethodCtx('letterDebtorNdHtml', {
description: 'Returns the second letter debtor email preview', description: 'Returns the second letter debtor email preview',
@ -46,21 +44,5 @@ module.exports = Self => {
} }
}); });
Self.letterDebtorNdHtml = async(ctx, id) => { Self.letterDebtorNdHtml = (ctx, id) => Self.printEmail(ctx, id, 'letter-debtor-nd');
const {accessToken} = ctx.req;
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
params.access_token = accessToken.id;
const report = new Email('letter-debtor-nd', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -44,5 +44,5 @@ module.exports = Self => {
} }
}); });
Self.letterDebtorPdf = (ctx, id) => Self.sendTemplate(ctx, id, 'letter-debtor'); Self.letterDebtorPdf = (ctx, id) => Self.printReport(ctx, id, 'letter-debtor');
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('letterDebtorStHtml', { Self.remoteMethodCtx('letterDebtorStHtml', {
description: 'Returns the letter debtor email preview', description: 'Returns the letter debtor email preview',
@ -46,21 +44,5 @@ module.exports = Self => {
} }
}); });
Self.letterDebtorStHtml = async(ctx, id) => { Self.letterDebtorStHtml = (ctx, id) => Self.printEmail(ctx, id, 'letter-debtor-st');
const {accessToken} = ctx.req;
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
params.access_token = accessToken.id;
const report = new Email('letter-debtor-st', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('printerSetupHtml', { Self.remoteMethodCtx('printerSetupHtml', {
description: 'Returns the printer setup email preview', description: 'Returns the printer setup email preview',
@ -40,19 +38,5 @@ module.exports = Self => {
} }
}); });
Self.printerSetupHtml = async(ctx, id) => { Self.printerSetupHtml = (ctx, id) => Self.printEmail(ctx, id, 'printer-setup');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
params.isPreview = true;
const report = new Email('printer-setup', params);
const html = await report.render();
return [html, 'text/html', `filename="mail-${id}.pdf"`];
};
}; };

View File

@ -32,5 +32,5 @@ module.exports = Self => {
} }
}); });
Self.balanceCompensationPdf = (ctx, id) => Self.sendPdf(ctx, id, 'balance-compensation'); Self.balanceCompensationPdf = (ctx, id) => Self.printReport(ctx, id, 'balance-compensation');
}; };

View File

@ -37,5 +37,5 @@ module.exports = Self => {
} }
}); });
Self.receiptPdf = (ctx, id) => Self.sendPdf(ctx, id, 'receipt'); Self.receiptPdf = (ctx, id) => Self.printReport(ctx, id, 'receipt');
}; };

View File

@ -36,5 +36,5 @@ module.exports = Self => {
} }
}); });
Self.entryOrderPdf = (ctx, id) => Self.sendPdf(ctx, id, 'entry-order'); Self.entryOrderPdf = (ctx, id) => Self.printReport(ctx, id, 'entry-order');
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('invoiceInEmail', { Self.remoteMethodCtx('invoiceInEmail', {
description: 'Sends the invoice in email with an attached PDF', description: 'Sends the invoice in email with an attached PDF',
@ -35,19 +33,5 @@ module.exports = Self => {
} }
}); });
Self.invoiceInEmail = async ctx => { Self.invoiceInEmail = ctx => Self.sendTemplate(ctx, 'invoiceIn');
const args = Object.assign({}, ctx.args);
const params = {
recipient: args.recipient,
lang: ctx.req.getLocale()
};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const email = new Email('invoiceIn', params);
return email.send();
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('invoiceInPdf', { Self.remoteMethodCtx('invoiceInPdf', {
description: 'Returns the invoiceIn pdf', description: 'Returns the invoiceIn pdf',
@ -34,17 +32,5 @@ module.exports = Self => {
} }
}); });
Self.invoiceInPdf = async(ctx, id) => { Self.invoiceInPdf = (ctx, id) => Self.printReport(ctx, id, 'invoiceIn');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('invoiceIn', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('exportationPdf', { Self.remoteMethodCtx('exportationPdf', {
description: 'Returns the exportation pdf', description: 'Returns the exportation pdf',
@ -39,17 +37,5 @@ module.exports = Self => {
} }
}); });
Self.exportationPdf = async(ctx, reference) => { Self.exportationPdf = (ctx, reference) => Self.printReport(ctx, reference, 'exportation');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('exportation', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${reference}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('labelPdf', { Self.remoteMethodCtx('labelPdf', {
description: 'Returns the item label pdf', description: 'Returns the item label pdf',
@ -56,17 +54,5 @@ module.exports = Self => {
} }
}); });
Self.labelPdf = async(ctx, id) => { Self.labelPdf = (ctx, id) => Self.printReport(ctx, id, 'item-label');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('item-label', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="item-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('driverRouteEmail', { Self.remoteMethodCtx('driverRouteEmail', {
description: 'Sends the driver route email with an attached PDF', description: 'Sends the driver route email with an attached PDF',
@ -41,19 +39,5 @@ module.exports = Self => {
} }
}); });
Self.driverRouteEmail = async ctx => { Self.driverRouteEmail = ctx => Self.sendTemplate(ctx, 'driver-route');
const args = Object.assign({}, ctx.args);
const params = {
recipient: args.recipient,
lang: ctx.req.getLocale()
};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const email = new Email('driver-route', params);
return email.send();
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('driverRoutePdf', { Self.remoteMethodCtx('driverRoutePdf', {
description: 'Returns the driver route pdf', description: 'Returns the driver route pdf',
@ -39,17 +37,5 @@ module.exports = Self => {
} }
}); });
Self.driverRoutePdf = async(ctx, id) => { Self.driverRoutePdf = (ctx, id) => Self.printReport(ctx, id, 'driver-route');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('driver-route', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('campaignMetricsPdf', { Self.remoteMethodCtx('campaignMetricsPdf', {
description: 'Returns the campaign metrics pdf', description: 'Returns the campaign metrics pdf',
@ -49,17 +47,5 @@ module.exports = Self => {
} }
}); });
Self.campaignMetricsPdf = async(ctx, id) => { Self.campaignMetricsPdf = (ctx, id) => Self.printReport(ctx, id, 'supplier-campaign-metrics');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('supplier-campaign-metrics', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('collectionLabel', { Self.remoteMethodCtx('collectionLabel', {
description: 'Returns the collection label', description: 'Returns the collection label',
@ -39,17 +37,5 @@ module.exports = Self => {
} }
}); });
Self.collectionLabel = async(ctx, id) => { Self.collectionLabel = (ctx, id) => Self.printReport(ctx, id, 'collection-label');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('collection-label', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${id}.pdf"`];
};
}; };

View File

@ -45,5 +45,5 @@ module.exports = Self => {
} }
}); });
Self.deliveryNoteEmail = ctx => Self.sendPdf(ctx, 'delivery-note'); Self.deliveryNoteEmail = ctx => Self.sendTemplate(ctx, 'delivery-note');
}; };

View File

@ -44,5 +44,5 @@ module.exports = Self => {
} }
}); });
Self.deliveryNotePdf = (ctx, id) => Self.printPdf(ctx, id, 'delivery-note'); Self.deliveryNotePdf = (ctx, id) => Self.printReport(ctx, id, 'delivery-note');
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('expeditionPalletLabel', { Self.remoteMethodCtx('expeditionPalletLabel', {
description: 'Returns the expedition pallet label', description: 'Returns the expedition pallet label',
@ -39,17 +37,5 @@ module.exports = Self => {
} }
}); });
Self.expeditionPalletLabel = async(ctx, id) => { Self.expeditionPalletLabel = (ctx, id) => Self.printReport(ctx, id, 'expedition-pallet-label');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('expedition-pallet-label', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="doc-${id}.pdf"`];
};
}; };

View File

@ -1,5 +1,3 @@
const {Email} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('extraCommunityEmail', { Self.remoteMethodCtx('extraCommunityEmail', {
description: 'Sends the extra community email with an attached PDF', description: 'Sends the extra community email with an attached PDF',
@ -74,19 +72,5 @@ module.exports = Self => {
} }
}); });
Self.extraCommunityEmail = async ctx => { Self.extraCommunityEmail = ctx => Self.sendTemplate(ctx, 'extra-community');
const args = Object.assign({}, ctx.args);
const params = {
recipient: args.recipient,
lang: ctx.req.getLocale()
};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const email = new Email('extra-community', params);
return email.send();
};
}; };

View File

@ -1,5 +1,3 @@
const {Report} = require('vn-print');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('extraCommunityPdf', { Self.remoteMethodCtx('extraCommunityPdf', {
description: 'Returns the extra community pdf', description: 'Returns the extra community pdf',
@ -73,17 +71,5 @@ module.exports = Self => {
} }
}); });
Self.extraCommunityPdf = async ctx => { Self.extraCommunityPdf = ctx => Self.printReport(ctx, null, 'extra-community');
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report('extra-community', params);
const stream = await report.toPdfStream();
return [stream, 'application/pdf', `filename="extra-community.pdf"`];
};
}; };