salix/modules/entry/back/models/entry.js

62 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-10-25 14:15:13 +00:00
const UserError = require('vn-loopback/util/user-error');
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);
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);
require('../methods/entry/buyLabel')(Self);
require('../methods/entry/print')(Self);
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');
}
const observation = changes.observation || orgData.observation;
2024-10-25 14:15:13 +00:00
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 {
2024-10-25 14:15:13 +00:00
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;
}
}
});
2020-01-10 10:52:35 +00:00
};