diff --git a/back/methods/collection/previousLabel.js b/back/methods/collection/previousLabel.js index fb2df8133..04e833c20 100644 --- a/back/methods/collection/previousLabel.js +++ b/back/methods/collection/previousLabel.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('previousLabel', { description: 'Returns the previa label pdf', @@ -33,17 +31,5 @@ module.exports = Self => { } }); - Self.previousLabel = async(ctx, id) => { - 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('previa-label', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="previa-${id}.pdf"`]; - }; + Self.previousLabel = (ctx, id) => Self.printPdf(ctx, id, 'previa-label'); }; diff --git a/loopback/common/methods/vn-model/printService.js b/loopback/common/methods/vn-model/printService.js new file mode 100644 index 000000000..40be35a5c --- /dev/null +++ b/loopback/common/methods/vn-model/printService.js @@ -0,0 +1,33 @@ +const {Report, Email} = require('vn-print'); + +module.exports = Self => { + Self.printPdf = async function(ctx, id, reportName) { + 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(reportName, params); + const stream = await report.toPdfStream(); + + return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; + }; + + Self.sendTemplate = async function(ctx, reportName) { + 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(reportName, params); + + return email.send(); + }; +}; diff --git a/loopback/common/models/vn-model.js b/loopback/common/models/vn-model.js index cc3eede8e..cebd37eac 100644 --- a/loopback/common/models/vn-model.js +++ b/loopback/common/models/vn-model.js @@ -7,6 +7,7 @@ module.exports = function(Self) { require('../methods/vn-model/getSetValues')(Self); require('../methods/vn-model/getEnumValues')(Self); + require('../methods/vn-model/printService')(Self); Object.assign(Self, { setup() { diff --git a/modules/claim/back/methods/claim/claimPickupPdf.js b/modules/claim/back/methods/claim/claimPickupPdf.js index 0e3abe908..c1059ab7e 100644 --- a/modules/claim/back/methods/claim/claimPickupPdf.js +++ b/modules/claim/back/methods/claim/claimPickupPdf.js @@ -1,5 +1,3 @@ -const { Report } = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('claimPickupPdf', { description: 'Returns the claim pickup order pdf', @@ -39,17 +37,5 @@ module.exports = Self => { } }); - Self.claimPickupPdf = async(ctx, id) => { - 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('claim-pickup-order', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.claimPickupPdf = (ctx, id) => Self.printPdf(ctx, id, 'claim-pickup-order'); }; diff --git a/modules/client/back/methods/client/campaignMetricsEmail.js b/modules/client/back/methods/client/campaignMetricsEmail.js index bb57f90a0..3a1bac5e6 100644 --- a/modules/client/back/methods/client/campaignMetricsEmail.js +++ b/modules/client/back/methods/client/campaignMetricsEmail.js @@ -51,19 +51,5 @@ module.exports = Self => { } }); - Self.campaignMetricsEmail = async ctx => { - 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('campaign-metrics', params); - - return email.send(); - }; + Self.campaignMetricsEmail = ctx => Self.sendTemplate(ctx, 'campaign-metrics'); }; diff --git a/modules/client/back/methods/client/campaignMetricsPdf.js b/modules/client/back/methods/client/campaignMetricsPdf.js index 14194d62b..1bae261ee 100644 --- a/modules/client/back/methods/client/campaignMetricsPdf.js +++ b/modules/client/back/methods/client/campaignMetricsPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('campaignMetricsPdf', { description: 'Returns the campaign metrics pdf', @@ -50,17 +48,5 @@ module.exports = Self => { } }); - Self.campaignMetricsPdf = async(ctx, id) => { - 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('campaign-metrics', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.campaignMetricsPdf = (ctx, id) => Self.sendPdf(ctx, id, 'campaign-metrics'); }; diff --git a/modules/client/back/methods/client/clientDebtStatementEmail.js b/modules/client/back/methods/client/clientDebtStatementEmail.js index 8bcdc900f..1b3ab13d8 100644 --- a/modules/client/back/methods/client/clientDebtStatementEmail.js +++ b/modules/client/back/methods/client/clientDebtStatementEmail.js @@ -46,19 +46,5 @@ module.exports = Self => { } }); - Self.clientDebtStatementEmail = async ctx => { - 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('client-debt-statement', params); - - return email.send(); - }; + Self.clientDebtStatementEmail = ctx => Self.sendTemplate(ctx, 'client-debt-statement'); }; diff --git a/modules/client/back/methods/client/clientDebtStatementPdf.js b/modules/client/back/methods/client/clientDebtStatementPdf.js index 8e2dca314..882aa5a5f 100644 --- a/modules/client/back/methods/client/clientDebtStatementPdf.js +++ b/modules/client/back/methods/client/clientDebtStatementPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('clientDebtStatementPdf', { description: 'Returns the client debt statement pdf', @@ -45,17 +43,5 @@ module.exports = Self => { } }); - Self.clientDebtStatementPdf = async(ctx, id) => { - 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('client-debt-statement', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.clientDebtStatementPdf = (ctx, id) => Self.sendPdf(ctx, id, 'client-debt-statement'); }; diff --git a/modules/client/back/methods/client/clientWelcomeEmail.js b/modules/client/back/methods/client/clientWelcomeEmail.js index 318ee18e6..accf12bb8 100644 --- a/modules/client/back/methods/client/clientWelcomeEmail.js +++ b/modules/client/back/methods/client/clientWelcomeEmail.js @@ -1,5 +1,3 @@ -const {Email} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('clientWelcomeEmail', { description: 'Sends the client welcome email with an attached PDF', @@ -41,19 +39,5 @@ module.exports = Self => { } }); - Self.clientWelcomeEmail = async ctx => { - 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('client-welcome', params); - - return email.send(); - }; + Self.clientWelcomeEmail = ctx => Self.sendTemplate(ctx, 'client-welcome'); }; diff --git a/modules/client/back/methods/client/creditRequestEmail.js b/modules/client/back/methods/client/creditRequestEmail.js index b6a60e971..3af415ed8 100644 --- a/modules/client/back/methods/client/creditRequestEmail.js +++ b/modules/client/back/methods/client/creditRequestEmail.js @@ -41,19 +41,5 @@ module.exports = Self => { } }); - Self.clientCreditEmail = async ctx => { - 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('credit-request', params); - - return email.send(); - }; + Self.clientCreditEmail = async ctx => Self.sendTemplate(ctx, 'client-welcome'); }; diff --git a/modules/client/back/methods/client/creditRequestPdf.js b/modules/client/back/methods/client/creditRequestPdf.js index 2e3dc0619..632ce467f 100644 --- a/modules/client/back/methods/client/creditRequestPdf.js +++ b/modules/client/back/methods/client/creditRequestPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('creditRequestPdf', { description: 'Returns the credit request pdf', @@ -40,17 +38,5 @@ module.exports = Self => { } }); - Self.creditRequestPdf = async(ctx, id) => { - 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('credit-request', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.creditRequestPdf = (ctx, id) => Self.sendPdf(ctx, id, 'credit-request'); }; diff --git a/modules/client/back/methods/client/incotermsAuthorizationEmail.js b/modules/client/back/methods/client/incotermsAuthorizationEmail.js index 2a4fe593a..0252ceb08 100644 --- a/modules/client/back/methods/client/incotermsAuthorizationEmail.js +++ b/modules/client/back/methods/client/incotermsAuthorizationEmail.js @@ -47,19 +47,5 @@ module.exports = Self => { } }); - Self.incotermsAuthorizationEmail = async ctx => { - 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('incoterms-authorization', params); - - return email.send(); - }; + Self.incotermsAuthorizationEmail = ctx => Self.sendTemplate(ctx, 'incoterms-authorization'); }; diff --git a/modules/client/back/methods/client/incotermsAuthorizationPdf.js b/modules/client/back/methods/client/incotermsAuthorizationPdf.js index 9a8a8d296..c64531a87 100644 --- a/modules/client/back/methods/client/incotermsAuthorizationPdf.js +++ b/modules/client/back/methods/client/incotermsAuthorizationPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('incotermsAuthorizationPdf', { description: 'Returns the incoterms authorization pdf', @@ -46,17 +44,5 @@ module.exports = Self => { } }); - Self.incotermsAuthorizationPdf = async(ctx, id) => { - 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('incoterms-authorization', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.incotermsAuthorizationPdf = (ctx, id) => Self.sendPdf(ctx, id, 'incoterms-authorization'); }; diff --git a/modules/client/back/methods/client/letterDebtorNdEmail.js b/modules/client/back/methods/client/letterDebtorNdEmail.js index e188c6e0a..396acdb97 100644 --- a/modules/client/back/methods/client/letterDebtorNdEmail.js +++ b/modules/client/back/methods/client/letterDebtorNdEmail.js @@ -1,5 +1,3 @@ -const {Email} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('letterDebtorNdEmail', { description: 'Sends the second debtor letter email with an attached PDF', @@ -47,19 +45,5 @@ module.exports = Self => { } }); - Self.letterDebtorNdEmail = async ctx => { - 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('letter-debtor-nd', params); - - return email.send(); - }; + Self.letterDebtorNdEmail = ctx => Self.sendTemplate(ctx, 'letter-debtor-nd'); }; diff --git a/modules/client/back/methods/client/letterDebtorPdf.js b/modules/client/back/methods/client/letterDebtorPdf.js index 421d531e6..a3c0cb08b 100644 --- a/modules/client/back/methods/client/letterDebtorPdf.js +++ b/modules/client/back/methods/client/letterDebtorPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('letterDebtorPdf', { description: 'Returns the letter debtor pdf', @@ -46,17 +44,5 @@ module.exports = Self => { } }); - Self.letterDebtorPdf = async(ctx, id) => { - 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('letter-debtor', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.letterDebtorPdf = (ctx, id) => Self.sendTemplate(ctx, id, 'letter-debtor'); }; diff --git a/modules/client/back/methods/client/letterDebtorStEmail.js b/modules/client/back/methods/client/letterDebtorStEmail.js index ee39a101b..c76204fbc 100644 --- a/modules/client/back/methods/client/letterDebtorStEmail.js +++ b/modules/client/back/methods/client/letterDebtorStEmail.js @@ -1,5 +1,3 @@ -const {Email} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('letterDebtorStEmail', { description: 'Sends the printer setup email with an attached PDF', @@ -47,19 +45,5 @@ module.exports = Self => { } }); - Self.letterDebtorStEmail = async ctx => { - 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('letter-debtor-st', params); - - return email.send(); - }; + Self.letterDebtorStEmail = ctx => Self.sendTemplate(ctx, 'letter-debtor-st'); }; diff --git a/modules/client/back/methods/client/printerSetupEmail.js b/modules/client/back/methods/client/printerSetupEmail.js index 254948659..2a0903e90 100644 --- a/modules/client/back/methods/client/printerSetupEmail.js +++ b/modules/client/back/methods/client/printerSetupEmail.js @@ -1,5 +1,3 @@ -const {Email} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('printerSetupEmail', { description: 'Sends the printer setup email with an attached PDF', @@ -41,19 +39,5 @@ module.exports = Self => { } }); - Self.printerSetupEmail = async ctx => { - 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('printer-setup', params); - - return email.send(); - }; + Self.printerSetupEmail = ctx => Self.sendTemplate(ctx, 'printer-setup'); }; diff --git a/modules/client/back/methods/client/sepaCoreEmail.js b/modules/client/back/methods/client/sepaCoreEmail.js index 93c9d4302..20931eb03 100644 --- a/modules/client/back/methods/client/sepaCoreEmail.js +++ b/modules/client/back/methods/client/sepaCoreEmail.js @@ -1,5 +1,3 @@ -const {Email} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('sepaCoreEmail', { description: 'Sends the campaign metrics email with an attached PDF', @@ -47,19 +45,5 @@ module.exports = Self => { } }); - Self.sepaCoreEmail = async ctx => { - 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('sepa-core', params); - - return email.send(); - }; + Self.sepaCoreEmail = ctx => Self.sendTemplate(ctx, 'sepa-core'); }; diff --git a/modules/client/back/methods/receipt/balanceCompensationPdf.js b/modules/client/back/methods/receipt/balanceCompensationPdf.js index ff8713253..9c8abb6cd 100644 --- a/modules/client/back/methods/receipt/balanceCompensationPdf.js +++ b/modules/client/back/methods/receipt/balanceCompensationPdf.js @@ -1,5 +1,3 @@ -const { Report } = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('balanceCompensationPdf', { description: 'Returns the the debit balances compensation pdf', @@ -10,7 +8,7 @@ module.exports = Self => { type: 'number', required: true, description: 'The receipt id', - http: { source: 'path' } + http: {source: 'path'} } ], returns: [ @@ -34,17 +32,5 @@ module.exports = Self => { } }); - Self.balanceCompensationPdf = async(ctx, id) => { - 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('balance-compensation', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.balanceCompensationPdf = (ctx, id) => Self.sendPdf(ctx, id, 'balance-compensation'); }; diff --git a/modules/client/back/methods/receipt/receiptPdf.js b/modules/client/back/methods/receipt/receiptPdf.js index f55e05040..ea962b017 100644 --- a/modules/client/back/methods/receipt/receiptPdf.js +++ b/modules/client/back/methods/receipt/receiptPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('receiptPdf', { description: 'Returns the receipt pdf', @@ -39,17 +37,5 @@ module.exports = Self => { } }); - Self.receiptPdf = async(ctx, id) => { - 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('receipt', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.receiptPdf = (ctx, id) => Self.sendPdf(ctx, id, 'receipt'); }; diff --git a/modules/entry/back/methods/entry/entryOrderPdf.js b/modules/entry/back/methods/entry/entryOrderPdf.js index e6d37fdb6..ca5940d6e 100644 --- a/modules/entry/back/methods/entry/entryOrderPdf.js +++ b/modules/entry/back/methods/entry/entryOrderPdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('entryOrderPdf', { description: 'Returns the entry order pdf', @@ -38,17 +36,5 @@ module.exports = Self => { } }); - Self.entryOrderPdf = async(ctx, id) => { - 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('entry-order', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.entryOrderPdf = (ctx, id) => Self.sendPdf(ctx, id, 'entry-order'); }; diff --git a/modules/ticket/back/methods/ticket/deliveryNoteEmail.js b/modules/ticket/back/methods/ticket/deliveryNoteEmail.js index cf9f5dd62..473eb4091 100644 --- a/modules/ticket/back/methods/ticket/deliveryNoteEmail.js +++ b/modules/ticket/back/methods/ticket/deliveryNoteEmail.js @@ -1,5 +1,3 @@ -const {Email} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('deliveryNoteEmail', { description: 'Sends the delivery note email with an attached PDF', @@ -47,19 +45,5 @@ module.exports = Self => { } }); - Self.deliveryNoteEmail = async ctx => { - 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('delivery-note', params); - - return email.send(); - }; + Self.deliveryNoteEmail = ctx => Self.sendPdf(ctx, 'delivery-note'); }; diff --git a/modules/ticket/back/methods/ticket/deliveryNotePdf.js b/modules/ticket/back/methods/ticket/deliveryNotePdf.js index b2b3f7198..29c8e64e3 100644 --- a/modules/ticket/back/methods/ticket/deliveryNotePdf.js +++ b/modules/ticket/back/methods/ticket/deliveryNotePdf.js @@ -1,5 +1,3 @@ -const {Report} = require('vn-print'); - module.exports = Self => { Self.remoteMethodCtx('deliveryNotePdf', { description: 'Returns the delivery note pdf', @@ -46,17 +44,5 @@ module.exports = Self => { } }); - Self.deliveryNotePdf = async(ctx, id) => { - 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('delivery-note', params); - const stream = await report.toPdfStream(); - - return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; - }; + Self.deliveryNotePdf = (ctx, id) => Self.printPdf(ctx, id, 'delivery-note'); };