31 lines
1.4 KiB
JavaScript
31 lines
1.4 KiB
JavaScript
module.exports = function(Self) {
|
|
Self.observe('after save', async function(ctx) {
|
|
const instance = ctx.instance;
|
|
const models = Self.app.models;
|
|
const options = ctx.options;
|
|
|
|
if (!instance.sectorFk || !instance.labelerFk) return;
|
|
const sector = await models.Sector.findById(instance.sectorFk, {
|
|
fields: ['description', 'mainPrinterFk']
|
|
}, options);
|
|
if (sector.mainPrinterFk && sector.mainPrinterFk != instance.labelerFk) {
|
|
const userId = ctx.options.accessToken.userId;
|
|
const user = await models.VnUser.findById(userId, {fields: ['nickname']}, options);
|
|
const labeler = await models.Printer.findById(instance.labelerFk, {fields: ['name']}, options);
|
|
const mainPrinter = await models.Printer.findById(sector.mainPrinterFk, {fields: ['name']}, options);
|
|
await models.NotificationQueue.create({
|
|
notificationFk: 'not-main-printer-configured',
|
|
authorFk: userId,
|
|
params: JSON.stringify(
|
|
{
|
|
'labeler': `#${instance.labelerFk} ${labeler.name}`,
|
|
'sector': `#${instance.sectorFk} ${sector.description}`,
|
|
'mainPrinter': `#${sector.mainPrinterFk} ${mainPrinter.name}`,
|
|
'worker': `#${userId} ${user.nickname}`
|
|
}
|
|
)
|
|
}, options);
|
|
}
|
|
});
|
|
};
|