refs #5772 Test environment fix
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2023-06-23 22:52:03 +02:00
parent 665339c8d2
commit 7660d76850
5 changed files with 49 additions and 54 deletions

View File

@ -24,20 +24,15 @@ module.exports = Self => {
Self.createPdf = async function(ctx, id, options) {
const models = Self.app.models;
options = typeof options == 'object'
? Object.assign({}, options) : {};
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
if (!options.transaction)
tx = options.transaction = await Self.beginTransaction({});
try {
const invoiceOut = await Self.findById(id, {fields: ['hasPdf']}, myOptions);
const invoiceOut = await Self.findById(id, {fields: ['hasPdf']}, options);
if (invoiceOut.hasPdf) {
const canCreatePdf = await models.ACL.checkAccessAcl(ctx, 'InvoiceOut', 'canCreatePdf', 'WRITE');
@ -45,7 +40,7 @@ module.exports = Self => {
throw new UserError(`You don't have enough privileges`);
}
await Self.makePdf(id, myOptions);
await Self.makePdf(id, options);
if (tx) await tx.commit();
} catch (err) {

View File

@ -1,6 +1,5 @@
const fs = require('fs-extra');
const path = require('path');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('download', {
@ -37,39 +36,27 @@ module.exports = Self => {
Self.download = async function(ctx, id, options) {
const models = Self.app.models;
const myOptions = {};
options = typeof options == 'object'
? Object.assign({}, options) : {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const pdfFile = await Self.filePath(id, options);
const invoiceOut = await models.InvoiceOut.findById(id, {
fields: ['ref', 'issued']
}, myOptions);
const issued = invoiceOut.issued;
const year = issued.getFullYear().toString();
const month = (issued.getMonth() + 1).toString();
const day = issued.getDate().toString();
const container = await models.InvoiceContainer.container(year);
const container = await models.InvoiceContainer.container(pdfFile.year);
const rootPath = container.client.root;
const src = path.join(rootPath, year, month, day);
const fileName = `${year}${invoiceOut.ref}.pdf`;
const fileSrc = path.join(src, fileName);
const file = {
path: fileSrc,
path: path.join(rootPath, pdfFile.path, pdfFile.name),
contentType: 'application/pdf',
name: fileName
name: pdfFile.name
};
try {
await fs.access(file.path);
} catch (error) {
await Self.createPdf(ctx, id, myOptions);
await Self.createPdf(ctx, id, options);
}
const stream = await fs.createReadStream(file.path);
let stream = await fs.createReadStream(file.path);
// XXX: To prevent unhandled ENOENT error
// https://stackoverflow.com/questions/17136536/is-enoent-from-fs-createreadstream-uncatchable
stream.on('error', err => {
@ -78,6 +65,14 @@ module.exports = Self => {
console.error(err);
});
return [stream, file.contentType, `filename="${file.name}"`];
if (process.env.NODE_ENV == 'test') {
try {
await fs.access(file.path);
} catch (error) {
stream = null;
}
}
return [stream, file.contentType, `filename="${pdfFile.name}"`];
};
};

View File

@ -45,7 +45,6 @@ module.exports = Self => {
Self.invoiceClient = async(ctx, options) => {
const args = ctx.args;
const models = Self.app.models;
options = typeof options == 'object'
? Object.assign({}, options) : {};
options.userId = ctx.req.accessToken.userId;

View File

@ -11,20 +11,17 @@ module.exports = Self => {
type: 'string',
required: true,
http: {source: 'path'}
},
{
}, {
arg: 'recipient',
type: 'string',
description: 'The recipient email',
required: true,
},
{
}, {
arg: 'replyTo',
type: 'string',
description: 'The sender email to reply to',
required: false
},
{
}, {
arg: 'recipientId',
type: 'number',
description: 'The recipient id to send to the recipient preferred language',
@ -43,16 +40,13 @@ module.exports = Self => {
Self.invoiceEmail = async(ctx, reference) => {
const args = Object.assign({}, ctx.args);
const {InvoiceOut} = Self.app.models;
const params = {
recipient: args.recipient,
lang: ctx.req.getLocale()
};
const invoiceOut = await InvoiceOut.findOne({
where: {
ref: reference
}
const invoiceOut = await Self.findOne({
where: {ref: reference}
});
delete args.ctx;

View File

@ -1,4 +1,5 @@
const print = require('vn-print');
const path = require('path');
module.exports = Self => {
require('../methods/invoiceOut/filter')(Self);
@ -23,20 +24,31 @@ module.exports = Self => {
require('../methods/invoiceOut/negativeBases')(Self);
require('../methods/invoiceOut/negativeBasesCsv')(Self);
Self.makePdf = async function(id, options) {
const fields = ['id', 'hasPdf', 'ref', 'issued'];
Self.filePath = async function(id, options) {
const fields = ['ref', 'issued'];
const invoiceOut = await Self.findById(id, {fields}, options);
const invoiceReport = new print.Report('invoice', {
reference: invoiceOut.ref
});
const buffer = await invoiceReport.toPdfStream();
const issued = invoiceOut.issued;
const year = issued.getFullYear().toString();
const month = (issued.getMonth() + 1).toString();
const day = issued.getDate().toString();
const fileName = `${year}${invoiceOut.ref}.pdf`;
return {
path: path.join(year, month, day),
name: `${year}${invoiceOut.ref}.pdf`,
year
};
};
Self.makePdf = async function(id, options) {
const fields = ['id', 'hasPdf', 'ref'];
const invoiceOut = await Self.findById(id, {fields}, options);
const invoiceReport = new print.Report('invoice', {
reference: invoiceOut.ref
});
const buffer = await invoiceReport.toPdfStream();
const pdfFile = await Self.filePath(id, options);
// Store invoice
@ -47,8 +59,8 @@ module.exports = Self => {
if (process.env.NODE_ENV !== 'test') {
await print.storage.write(buffer, {
type: 'invoice',
path: `${year}/${month}/${day}`,
fileName: fileName
path: pdfFile.path,
fileName: pdfFile.name
});
}
};