52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('createPdf', {
|
|
description: 'Creates an invoice PDF',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'The invoice id',
|
|
http: {source: 'path'}
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/createPdf`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.createPdf = async function(ctx, id, options) {
|
|
const models = Self.app.models;
|
|
options = typeof options == 'object'
|
|
? Object.assign({}, options) : {};
|
|
|
|
let tx;
|
|
if (!options.transaction)
|
|
tx = options.transaction = await Self.beginTransaction({});
|
|
|
|
try {
|
|
const invoiceOut = await Self.findById(id, {fields: ['hasPdf']}, options);
|
|
|
|
if (invoiceOut.hasPdf) {
|
|
const canCreatePdf = await models.ACL.checkAccessAcl(ctx, 'InvoiceOut', 'canCreatePdf', 'WRITE');
|
|
if (!canCreatePdf)
|
|
throw new UserError(`You don't have enough privileges`);
|
|
}
|
|
|
|
await Self.makePdf(id, options);
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (err) {
|
|
if (tx) await tx.rollback();
|
|
throw err;
|
|
}
|
|
};
|
|
};
|