From 18ac0a0b61f7680d9b8e3598caeb0d7cd455a6a0 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 16 Sep 2022 23:13:37 +0200 Subject: [PATCH] Export Email and Report classes --- loopback/server/boot/print.js | 4 +- .../back/methods/ticket/deliveryNote.js | 47 ++++++++++++++++ modules/ticket/back/models/ticket.js | 1 + .../report-footer/assets/css/import.js | 13 +++-- .../report-header/assets/css/import.js | 11 ++-- print/core/database.js | 39 +++++--------- print/core/stylesheet.js | 4 +- print/index.js | 53 +++++++++++++++++++ .../delivery-note/assets/css/import.js | 13 +++-- .../reports/delivery-note/delivery-note.js | 4 +- 10 files changed, 143 insertions(+), 46 deletions(-) create mode 100644 modules/ticket/back/methods/ticket/deliveryNote.js create mode 100644 print/index.js diff --git a/loopback/server/boot/print.js b/loopback/server/boot/print.js index 0f6af4d56..c2ef243b7 100644 --- a/loopback/server/boot/print.js +++ b/loopback/server/boot/print.js @@ -1,3 +1,3 @@ module.exports = function(app) { - require('../../../print/boot.js')(app); -}; + require('vn-print').boot(app); +}; \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/deliveryNote.js b/modules/ticket/back/methods/ticket/deliveryNote.js new file mode 100644 index 000000000..b4049b4c6 --- /dev/null +++ b/modules/ticket/back/methods/ticket/deliveryNote.js @@ -0,0 +1,47 @@ +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"`]; + }; +}; diff --git a/modules/ticket/back/models/ticket.js b/modules/ticket/back/models/ticket.js index 47d105824..85f7be12b 100644 --- a/modules/ticket/back/models/ticket.js +++ b/modules/ticket/back/models/ticket.js @@ -28,6 +28,7 @@ module.exports = Self => { require('../methods/ticket/freightCost')(Self); require('../methods/ticket/getComponentsSum')(Self); require('../methods/ticket/refund')(Self); + require('../methods/ticket/deliveryNote')(Self); Self.observe('before save', async function(ctx) { const loopBackContext = LoopBackContext.getCurrentContext(); diff --git a/print/core/components/report-footer/assets/css/import.js b/print/core/components/report-footer/assets/css/import.js index a2a9334cb..71fa00f4b 100644 --- a/print/core/components/report-footer/assets/css/import.js +++ b/print/core/components/report-footer/assets/css/import.js @@ -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/layout.css`, - `${appPath}/common/css/report.css`, - `${appPath}/common/css/misc.css`, + `${vnPrintPath}/common/css/layout.css`, + `${vnPrintPath}/common/css/report.css`, + `${vnPrintPath}/common/css/misc.css`, `${__dirname}/style.css`]) - .mergeStyles(); + .mergeStyles(); \ No newline at end of file diff --git a/print/core/components/report-header/assets/css/import.js b/print/core/components/report-header/assets/css/import.js index a2a9334cb..e21d3821a 100644 --- a/print/core/components/report-header/assets/css/import.js +++ b/print/core/components/report-header/assets/css/import.js @@ -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/layout.css`, - `${appPath}/common/css/report.css`, - `${appPath}/common/css/misc.css`, + `${vnPrintPath}/common/css/layout.css`, + `${vnPrintPath}/common/css/report.css`, + `${vnPrintPath}/common/css/misc.css`, `${__dirname}/style.css`]) .mergeStyles(); diff --git a/print/core/database.js b/print/core/database.js index 6c690afc6..f442cd4ed 100644 --- a/print/core/database.js +++ b/print/core/database.js @@ -1,16 +1,17 @@ const mysql = require('mysql2'); const config = require('./config.js'); const fs = require('fs-extra'); +const path = require('path'); const PoolConnection = mysql.PoolConnection; module.exports = { - init() { + init(pool) { if (!this.pool) { - const datasources = config.datasources; - const pool = mysql.createPoolCluster(); + // const datasources = config.datasources; + // const pool = mysql.createPoolCluster(); - for (let datasource of datasources) - pool.add(datasource.name, datasource.options); + // for (let datasource of datasources) + // pool.add(datasource.name, datasource.options); this.pool = pool; } @@ -43,29 +44,12 @@ module.exports = { * * @return {Object} - Result promise */ - rawSql(query, params, connection) { - let pool = this.pool; - if (params instanceof PoolConnection) - connection = params; - if (connection) pool = connection; - + rawSql(query, params) { return new Promise((resolve, reject) => { - if (!connection) { - pool.getConnection('default', function(error, conn) { - if (error) return reject(error); - - conn.query(query, params, (error, rows) => { - if (error) return reject(error); - conn.release(); - resolve(rows); - }); - }); - } else { - connection.query(query, params, (error, rows) => { - if (error) return reject(error); - resolve(rows); - }); - } + this.pool.query(query, params, (error, rows) => { + if (error) return reject(error); + resolve(rows); + }); }); }, @@ -102,6 +86,7 @@ module.exports = { * @return {Object} - Result promise */ findOneFromDef(queryName, params) { + console.log(path.resolve('queryName')); return this.rawSqlFromDef(queryName, params) .then(([row]) => row); }, diff --git a/print/core/stylesheet.js b/print/core/stylesheet.js index 42a44fb57..195044f02 100644 --- a/print/core/stylesheet.js +++ b/print/core/stylesheet.js @@ -1,4 +1,5 @@ const fs = require('fs-extra'); +const path = require('path'); class Stylesheet { constructor(files) { @@ -7,8 +8,9 @@ class Stylesheet { } mergeStyles() { - for (const file of this.files) + for (const file of this.files) { this.css.push(fs.readFileSync(file)); + } return this.css.join('\n'); } diff --git a/print/index.js b/print/index.js new file mode 100644 index 000000000..c25df6ae1 --- /dev/null +++ b/print/index.js @@ -0,0 +1,53 @@ +const express = require('express'); +const path = require('path'); +const fs = require('fs'); + +const templatesPath = path.resolve(__dirname, './templates'); +const componentsPath = path.resolve(__dirname, './core/components'); + +module.exports = { + async boot(app) { + // Init database instance + const conn = app.dataSources.vn.connector.client; + conn.query('SELECT 1', function(error, rows) { + if (error) return error; + console.log(rows); + }); + // console.log(app.dataSource.vn.connector.executeStmt('SELECT 1')); + require('./core/database').init(conn); + require('./core/smtp').init(); + require('./core/mixins'); + require('./core/filters'); + require('./core/directives'); + + const componentsDir = fs.readdirSync(componentsPath); + componentsDir.forEach(componentName => { + const componentDir = path.join(componentsPath, '/', componentName); + const assetsDir = `${componentDir}/assets`; + + app.use(`/api/${componentName}/assets`, express.static(assetsDir)); + }); + + /** + * Serve static files + */ + const templatesDir = fs.readdirSync(templatesPath); + templatesDir.forEach(directory => { + const templateTypeDir = path.join(templatesPath, '/', directory); + const templates = fs.readdirSync(templateTypeDir); + + templates.forEach(templateName => { + const templateDir = path.join(templatesPath, '/', directory, '/', templateName); + const assetsDir = `${templateDir}/assets`; + + app.use(`/api/${templateName}/assets`, express.static(assetsDir)); + }); + }); + + return true; + }, + smtp: require('./core/smtp'), + db: require('./core/database'), + Email: require('./core/email'), + Report: require('./core/report') +}; diff --git a/print/templates/reports/delivery-note/assets/css/import.js b/print/templates/reports/delivery-note/assets/css/import.js index fd8796c2b..37a98dfdd 100644 --- a/print/templates/reports/delivery-note/assets/css/import.js +++ b/print/templates/reports/delivery-note/assets/css/import.js @@ -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/report.css`, + `${vnPrintPath}/common/css/spacing.css`, + `${vnPrintPath}/common/css/misc.css`, + `${vnPrintPath}/common/css/layout.css`, + `${vnPrintPath}/common/css/report.css`, `${__dirname}/style.css`]) .mergeStyles(); diff --git a/print/templates/reports/delivery-note/delivery-note.js b/print/templates/reports/delivery-note/delivery-note.js index f9dba0578..a5ff69560 100755 --- a/print/templates/reports/delivery-note/delivery-note.js +++ b/print/templates/reports/delivery-note/delivery-note.js @@ -1,5 +1,5 @@ -const config = require(`${appPath}/core/config`); -const Component = require(`${appPath}/core/component`); +const config = require(`vn-print/core/config`); +const Component = require(`vn-print/core/component`); const reportHeader = new Component('report-header'); const reportFooter = new Component('report-footer'); const md5 = require('md5');