63 lines
2.5 KiB
JavaScript
63 lines
2.5 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
const LoopBackContext = require('loopback-context');
|
|
module.exports = Self => {
|
|
require('../methods/entry/filter')(Self);
|
|
require('../methods/entry/getEntry')(Self);
|
|
require('../methods/entry/getBuys')(Self);
|
|
require('../methods/entry/getBuysCsv')(Self);
|
|
require('../methods/entry/importBuys')(Self);
|
|
require('../methods/entry/importBuysPreview')(Self);
|
|
require('../methods/entry/lastItemBuys')(Self);
|
|
require('../methods/entry/entryOrderPdf')(Self);
|
|
require('../methods/entry/addFromPackaging')(Self);
|
|
require('../methods/entry/addFromBuy')(Self);
|
|
require('../methods/entry/buyLabel')(Self);
|
|
require('../methods/entry/labelSupplier')(Self);
|
|
require('../methods/entry/buyLabelSupplier')(Self);
|
|
|
|
Self.observe('before save', async function(ctx, options) {
|
|
if (ctx.isNewInstance) return;
|
|
|
|
const changes = ctx.data || ctx.instance;
|
|
const orgData = ctx.currentInstance;
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
const accessToken = {req: loopBackContext.active};
|
|
const hasChanges = orgData && changes;
|
|
|
|
const isBookedChanged = changes.isBooked !== undefined && orgData.isBooked !== changes.isBooked;
|
|
|
|
if (isBookedChanged) {
|
|
const canEditIsBooked = await Self.app.models.ACL.checkAccessAcl(accessToken, 'Entry', 'isBooked', 'READ');
|
|
if (!canEditIsBooked)
|
|
throw new UserError('You do not have permission to modify the booked field');
|
|
}
|
|
|
|
const observation = changes.observation || orgData.observation;
|
|
const observationChanged = hasChanges && orgData.observation != observation;
|
|
|
|
if (observationChanged) {
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const userId = loopBackContext.active.accessToken.userId;
|
|
const id = changes.id || orgData.id;
|
|
const entry = await Self.app.models.Entry.findById(id, null, myOptions);
|
|
await entry.updateAttribute('observationEditorFk', userId, myOptions);
|
|
if (tx) await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
}
|
|
});
|
|
};
|