76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('download', {
|
|
description: 'Download an invoice PDF',
|
|
accessType: 'READ',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'String',
|
|
description: 'The invoice id',
|
|
http: {source: 'path'}
|
|
}
|
|
],
|
|
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: `/:id/download`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.download = async function(ctx, id, options) {
|
|
const models = Self.app.models;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
try {
|
|
const invoiceOut = await models.InvoiceOut.findById(id, null, 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
|
|
};
|
|
|
|
await fs.access(file.path);
|
|
let stream = fs.createReadStream(file.path);
|
|
return [stream, file.contentType, `filename="${file.name}"`];
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT')
|
|
throw new UserError('The PDF document does not exists');
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
};
|