2024-10-25 14:15:13 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-02-14 07:08:48 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2020-01-10 10:52:35 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
require('../methods/entry/filter')(Self);
|
2020-02-21 11:48:34 +00:00
|
|
|
require('../methods/entry/getEntry')(Self);
|
2020-09-09 09:39:18 +00:00
|
|
|
require('../methods/entry/getBuys')(Self);
|
2024-08-29 10:19:06 +00:00
|
|
|
require('../methods/entry/getBuysCsv')(Self);
|
2021-03-10 09:29:58 +00:00
|
|
|
require('../methods/entry/importBuys')(Self);
|
|
|
|
require('../methods/entry/importBuysPreview')(Self);
|
2022-01-28 08:35:30 +00:00
|
|
|
require('../methods/entry/lastItemBuys')(Self);
|
2022-09-26 11:33:27 +00:00
|
|
|
require('../methods/entry/entryOrderPdf')(Self);
|
2023-05-04 10:03:47 +00:00
|
|
|
require('../methods/entry/addFromPackaging')(Self);
|
2023-06-05 07:10:19 +00:00
|
|
|
require('../methods/entry/addFromBuy')(Self);
|
2024-07-01 07:07:36 +00:00
|
|
|
require('../methods/entry/buyLabel')(Self);
|
2024-11-28 07:59:08 +00:00
|
|
|
require('../methods/entry/labelSupplier')(Self);
|
2024-11-27 14:55:30 +00:00
|
|
|
require('../methods/entry/buyLabelSupplier')(Self);
|
2023-02-14 07:08:48 +00:00
|
|
|
|
|
|
|
Self.observe('before save', async function(ctx, options) {
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
|
|
|
|
const changes = ctx.data || ctx.instance;
|
|
|
|
const orgData = ctx.currentInstance;
|
2024-10-25 14:15:13 +00:00
|
|
|
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');
|
|
|
|
}
|
2023-02-14 07:08:48 +00:00
|
|
|
|
|
|
|
const observation = changes.observation || orgData.observation;
|
2024-10-25 14:15:13 +00:00
|
|
|
const observationChanged = hasChanges && orgData.observation != observation;
|
2023-02-14 07:08:48 +00:00
|
|
|
|
|
|
|
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 {
|
2024-10-25 14:15:13 +00:00
|
|
|
const userId = loopBackContext.active.accessToken.userId;
|
2023-02-14 07:08:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-01-10 10:52:35 +00:00
|
|
|
};
|