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

50 lines
1.8 KiB
JavaScript
Raw Normal View History

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);
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);
Self.observe('before save', async function(ctx, options) {
if (ctx.isNewInstance) return;
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
const observation = changes.observation || orgData.observation;
const hasChanges = orgData && changes;
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 loopbackContext = LoopBackContext.getCurrentContext();
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
};