Export Email and Report classes
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-09-16 23:13:37 +02:00
parent fe71e485aa
commit 18ac0a0b61
10 changed files with 143 additions and 46 deletions

View File

@ -1,3 +1,3 @@
module.exports = function(app) {
require('../../../print/boot.js')(app);
require('vn-print').boot(app);
};

View File

@ -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"`];
};
};

View File

@ -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();

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/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();

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/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();

View File

@ -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) => {
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);
},

View File

@ -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');
}

53
print/index.js Normal file
View File

@ -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')
};

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/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();

View File

@ -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');