This commit is contained in:
parent
c9f11830a2
commit
665339c8d2
|
@ -42,42 +42,42 @@ module.exports = Self => {
|
|||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, 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 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,
|
||||
contentType: 'application/pdf',
|
||||
name: fileName
|
||||
};
|
||||
|
||||
try {
|
||||
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 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,
|
||||
contentType: 'application/pdf',
|
||||
name: fileName
|
||||
};
|
||||
|
||||
try {
|
||||
await fs.access(file.path);
|
||||
} catch (error) {
|
||||
await Self.createPdf(ctx, id, myOptions);
|
||||
}
|
||||
|
||||
const stream = fs.createReadStream(file.path);
|
||||
|
||||
return [stream, file.contentType, `filename="${file.name}"`];
|
||||
await fs.access(file.path);
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT')
|
||||
throw new UserError('The PDF document does not exist');
|
||||
|
||||
throw error;
|
||||
await Self.createPdf(ctx, id, myOptions);
|
||||
}
|
||||
|
||||
const 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 => {
|
||||
const e = new Error(err.message);
|
||||
err.stack = e.stack;
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
return [stream, file.contentType, `filename="${file.name}"`];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -63,12 +63,12 @@ class Email extends Component {
|
|||
await getAttachments(componentPath, component.attachments);
|
||||
|
||||
if (component.components)
|
||||
await getSubcomponentAttachments(component)
|
||||
await getSubcomponentAttachments(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await getSubcomponentAttachments(instance)
|
||||
await getSubcomponentAttachments(instance);
|
||||
|
||||
if (this.attachments)
|
||||
await getAttachments(this.path, this.attachments);
|
||||
|
|
Loading…
Reference in New Issue