52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('regenerate', {
|
|
description: 'Sends an invoice to a regeneration queue',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The invoiceOut id',
|
|
http: {source: 'path'}
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: '/:id/regenerate',
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.regenerate = async(ctx, id) => {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const models = Self.app.models;
|
|
const invoiceReportFk = 30; // Should be deprecated
|
|
const worker = await models.Worker.findOne({where: {userFk: userId}});
|
|
const tx = await Self.beginTransaction({});
|
|
|
|
try {
|
|
let options = {transaction: tx};
|
|
|
|
// Remove all invoice references from tickets
|
|
const invoiceOut = await models.InvoiceOut.findById(id, null, options);
|
|
await invoiceOut.updateAttributes({
|
|
hasPdf: false
|
|
});
|
|
|
|
// Send to print queue
|
|
await Self.rawSql(`
|
|
INSERT INTO vn.printServerQueue (reportFk, param1, workerFk)
|
|
VALUES (?, ?, ?)`, [invoiceReportFk, id, worker.id], options);
|
|
|
|
await tx.commit();
|
|
|
|
return invoiceOut;
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|