diff --git a/loopback/util/not-found-error.js b/loopback/util/not-found-error.js new file mode 100644 index 000000000..7a05254f9 --- /dev/null +++ b/loopback/util/not-found-error.js @@ -0,0 +1,9 @@ +module.exports = class NotFoundError extends Error { + constructor(message = 'Not found', code = 'NOT_FOUND', ...translateArgs) { + super(message); + this.name = 'NotFoundError'; + this.statusCode = 404; + this.code = code; + this.translateArgs = translateArgs; + } +}; diff --git a/print/core/mixins/vn-report.js b/print/core/mixins/vn-report.js new file mode 100644 index 000000000..66b9704e7 --- /dev/null +++ b/print/core/mixins/vn-report.js @@ -0,0 +1,22 @@ +const Component = require(`vn-print/core/component`); +const reportHeader = new Component('report-header'); +const reportFooter = new Component('report-footer'); +const reportBody = new Component('report-body'); +const NotFoundError = require('vn-loopback/util/not-found-error'); + +module.exports = { + components: { + 'report-body': reportBody.build(), + 'report-header': reportHeader.build(), + 'report-footer': reportFooter.build() + }, + methods: { + checkMainEntity: function(entity) { + if (entity == null) + throw new NotFoundError(); + }, + dated: function(filters, date, format) { + return filters.date(date, format); + } + }, +}; diff --git a/print/core/report.js b/print/core/report.js index d37b79c6d..c5182d1a8 100644 --- a/print/core/report.js +++ b/print/core/report.js @@ -23,43 +23,39 @@ class Report extends Component { } async toPdfStream() { - try { - const template = await this.render(); - const defaultOptions = Object.assign({}, config.pdf); + const template = await this.render(); + const defaultOptions = Object.assign({}, config.pdf); - const optionsPath = `${this.path}/options.json`; - const fullPath = path.resolve(__dirname, optionsPath); - let options = defaultOptions; - if (fs.existsSync(fullPath)) - options = require(optionsPath); + const optionsPath = `${this.path}/options.json`; + const fullPath = path.resolve(__dirname, optionsPath); + let options = defaultOptions; + if (fs.existsSync(fullPath)) + options = require(optionsPath); - return new Promise(resolve => { - Cluster.pool.queue({}, async({page}) => { - await page.emulateMediaType('screen'); - await page.setContent(template); + return new Promise(resolve => { + Cluster.pool.queue({}, async({page}) => { + await page.emulateMediaType('screen'); + await page.setContent(template); - const element = await page.$('#pageFooter'); + const element = await page.$('#pageFooter'); - let footer = '\n'; - if (element) { - footer = await page.evaluate(el => { - const html = el.innerHTML; - el.remove(); - return html; - }, element); - } + let footer = '\n'; + if (element) { + footer = await page.evaluate(el => { + const html = el.innerHTML; + el.remove(); + return html; + }, element); + } - options.headerTemplate = '\n'; - options.footerTemplate = footer; + options.headerTemplate = '\n'; + options.footerTemplate = footer; - const stream = await page.pdf(options); + const stream = await page.pdf(options); - resolve(stream); - }); + resolve(stream); }); - } catch (e) { - console.log('ERROR CATCHED:', e); - } + }); } /**