feat: refs #7025 notify changes on save
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-04-23 13:36:25 +02:00
parent 2291a92a43
commit d94f37f1f9
2 changed files with 20 additions and 2 deletions

View File

@ -11,6 +11,8 @@ module.exports = Self => {
require('../methods/entry/addFromBuy')(Self);
Self.observe('before save', async function(ctx, options) {
const models = Self.app.models;
if (ctx.isNewInstance) return;
const changes = ctx.data || ctx.instance;
@ -21,6 +23,21 @@ module.exports = Self => {
const observationChanged = hasChanges
&& orgData.observation != observation;
const loopbackContext = LoopBackContext.getCurrentContext();
const userId = loopbackContext.active.accessToken.userId;
if (orgData.isConfirmed && orgData.editorFk) {
const {name} = await models.VnUser.findById(orgData.editorFk, null);
const to = `@${name}`;
let message = `<h1>Changes</h1><ul>`;
for (const [key, value] of Object.entries(changes)) message += `<li>${key}: ${value}</li>`;
message += '</ul>';
ctx.req = loopbackContext.active;
await models.Chat.send(ctx, to, message);
}
if (observationChanged) {
let tx;
const myOptions = {};
@ -34,8 +51,6 @@ module.exports = Self => {
}
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);

View File

@ -74,6 +74,9 @@
},
"observationEditorFk": {
"type": "number"
},
"editorFk": {
"type": "number"
}
},
"relations": {