This commit is contained in:
Joan Sanchez 2021-10-01 08:18:56 +02:00
commit c80457142f
3 changed files with 10 additions and 19 deletions

View File

@ -28,7 +28,7 @@ module.exports = Self => {
Self.createPdf = async function(ctx, id, options) {
const models = Self.app.models;
const headers = ctx.req.headers;
const origin = headers.origin || headers.referer;
const origin = headers.origin;
const authorization = ctx.req.accessToken.id;
if (process.env.NODE_ENV == 'test')
@ -61,12 +61,12 @@ module.exports = Self => {
const created = invoiceOut.created;
const year = created.getFullYear().toString();
const month = created.getMonth().toString();
const month = (created.getMonth() + 1).toString();
const day = created.getDate().toString();
const container = await models.InvoiceContainer.container(year);
const rootPath = container.client.root;
const fileName = `${invoiceOut.ref}.pdf`;
const fileName = `${year}${invoiceOut.ref}.pdf`;
const src = path.join(rootPath, year, month, day);
fileSrc = path.join(src, fileName);
@ -75,16 +75,11 @@ module.exports = Self => {
if (tx) await tx.commit();
const writeStream = fs.createWriteStream(fileSrc);
writeStream.on('open', () => {
response.pipe(writeStream);
});
writeStream.on('open', () => response.pipe(writeStream));
writeStream.on('finish', () => writeStream.end());
return new Promise(resolve => {
writeStream.on('finish', () => {
writeStream.end();
resolve(invoiceOut);
});
writeStream.on('close', () => resolve(invoiceOut));
});
} catch (e) {
if (tx) await tx.rollback();

View File

@ -47,23 +47,19 @@ module.exports = Self => {
const created = invoiceOut.created;
const year = created.getFullYear().toString();
const month = created.getMonth().toString();
const month = (created.getMonth() + 1).toString();
const day = created.getDate().toString();
const container = await models.InvoiceContainer.container(year);
const rootPath = container.client.root;
const src = path.join(rootPath, year, month, day);
const fileName = `${invoiceOut.ref}.pdf`;
const fileName = `${year}${invoiceOut.ref}.pdf`;
const fileSrc = path.join(src, fileName);
// Creates PDF document if it has not been created
if (!invoiceOut.hasPdf || !fs.existsSync(fileSrc))
await Self.createPdf(ctx, invoiceOut.id, myOptions);
const file = {
path: fileSrc,
contentType: 'application/pdf',
name: `${id}.pdf`
name: fileName
};
await fs.access(file.path);

View File

@ -23,6 +23,6 @@ describe('InvoiceOut download()', () => {
const result = await models.InvoiceOut.download(ctx, invoiceId);
expect(result[1]).toEqual('application/pdf');
expect(result[2]).toEqual('filename="1.pdf"');
expect(result[2]).toEqual('filename="2021T1111111.pdf"');
});
});