From d94f37f1f95de7e05e2c87fcfe2d30ecfc6c4a2c Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 23 Apr 2024 13:36:25 +0200 Subject: [PATCH 1/5] feat: refs #7025 notify changes on save --- modules/entry/back/models/entry.js | 19 +++++++++++++++++-- modules/entry/back/models/entry.json | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index 6148ae559..d251634c4 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -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 = `

Changes

'; + + 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); diff --git a/modules/entry/back/models/entry.json b/modules/entry/back/models/entry.json index ab451219e..f29960b6f 100644 --- a/modules/entry/back/models/entry.json +++ b/modules/entry/back/models/entry.json @@ -74,6 +74,9 @@ }, "observationEditorFk": { "type": "number" + }, + "editorFk": { + "type": "number" } }, "relations": { -- 2.40.1 From f087a2c3182bfe73b37a47774f2d2eea9ec01ebf Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 23 Apr 2024 16:59:22 +0200 Subject: [PATCH 2/5] fix: refs #7025 rollback --- modules/entry/back/models/entry.js | 19 ++----------------- modules/entry/back/models/entry.json | 3 --- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index d251634c4..6148ae559 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -11,8 +11,6 @@ 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; @@ -23,21 +21,6 @@ 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 = `

Changes

'; - - ctx.req = loopbackContext.active; - await models.Chat.send(ctx, to, message); - } - if (observationChanged) { let tx; const myOptions = {}; @@ -51,6 +34,8 @@ 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); diff --git a/modules/entry/back/models/entry.json b/modules/entry/back/models/entry.json index f29960b6f..ab451219e 100644 --- a/modules/entry/back/models/entry.json +++ b/modules/entry/back/models/entry.json @@ -74,9 +74,6 @@ }, "observationEditorFk": { "type": "number" - }, - "editorFk": { - "type": "number" } }, "relations": { -- 2.40.1 From aad69d9a24c151908b8fc77f0a306daea244b6dd Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 23 Apr 2024 17:27:09 +0200 Subject: [PATCH 3/5] feat: refs #7025 observe buy changes --- loopback/locale/es.json | 3 ++- modules/entry/back/models/buy.js | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index d7f9564fe..e769c8402 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -353,5 +353,6 @@ "This password can only be changed by the user themselves": "Esta contraseña solo puede ser modificada por el propio usuario", "They're not your subordinate": "No es tu subordinado/a.", "No results found": "No se han encontrado resultados", - "InvoiceIn is already booked": "La factura recibida está contabilizada" + "InvoiceIn is already booked": "La factura recibida está contabilizada", + "Grouping cannot be zero": "Grouping cannot be zero" } \ No newline at end of file diff --git a/modules/entry/back/models/buy.js b/modules/entry/back/models/buy.js index 34f19e765..16bee1b99 100644 --- a/modules/entry/back/models/buy.js +++ b/modules/entry/back/models/buy.js @@ -1,5 +1,49 @@ +const LoopBackContext = require('loopback-context'); +const yaml = require('js-yaml'); +const fs = require('fs'); +const path = require('path'); +const locale = { + es: yaml.load(fs.readFileSync(path.join(__dirname, '../locale/buy/es.yml'))), + en: yaml.load(fs.readFileSync(path.join(__dirname, '../locale/buy/en.yml'))), + +}; + module.exports = Self => { require('../methods/entry/editLatestBuys')(Self); require('../methods/entry/latestBuysFilter')(Self); require('../methods/entry/deleteBuys')(Self); + + Self.observe('before save', async function(ctx) { + const models = Self.app.models; + const loopbackContext = LoopBackContext.getCurrentContext(); + + if (ctx.isNewInstance) return; + + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; + const {isConfirmed} = await models.Entry.findById(orgData.entryFk, {fields: ['isConfirmed']}); + + if (isConfirmed && orgData.editorFk) { + const {name, lang} = await models.VnUser.findById(orgData.editorFk, {fields: ['name', 'lang']}); + const to = `@${name}`; + + let message = `*${locale[lang].name} changes*:\n`; + const datePattern = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(.\d{3})?Z)?$/; // ISO Date + + for (let [key, value] of Object.entries(changes)) { + if (key === 'printedStickers') continue; + + if (datePattern.test(value)) { + const date = new Date(value); + value = date.toLocaleDateString('es-ES'); + value += ` ${date.toLocaleTimeString('es-ES')}`; + } + + const field = lang ? locale[lang].columns[key] : key; + message += `${field}: ${value}\n`; + } + ctx.req = loopbackContext.active; + await models.Chat.send(ctx, to, message); + } + }); }; -- 2.40.1 From 63993a7c654d2695dd4af98088ef86419dd9625a Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 24 Apr 2024 10:21:53 +0200 Subject: [PATCH 4/5] fix: refs #7025 bold title --- modules/entry/back/models/buy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/entry/back/models/buy.js b/modules/entry/back/models/buy.js index 16bee1b99..4990fedf2 100644 --- a/modules/entry/back/models/buy.js +++ b/modules/entry/back/models/buy.js @@ -27,7 +27,7 @@ module.exports = Self => { const {name, lang} = await models.VnUser.findById(orgData.editorFk, {fields: ['name', 'lang']}); const to = `@${name}`; - let message = `*${locale[lang].name} changes*:\n`; + let message = `**${locale[lang].name} changes**:\n`; const datePattern = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(.\d{3})?Z)?$/; // ISO Date for (let [key, value] of Object.entries(changes)) { -- 2.40.1 From f33641eb2cb352703fd6d63335941b5eaaaae221 Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 24 Apr 2024 13:14:24 +0200 Subject: [PATCH 5/5] fix: refs #7025 check if has the same value & gets userFk --- db/dump/fixtures.before.sql | 4 ++++ modules/entry/back/models/buy.js | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index ff58af2e2..664d28c9c 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -3787,3 +3787,7 @@ INSERT INTO `vn`.`accountReconciliationConfig`(currencyFk, warehouseFk) INSERT INTO vn.workerTeam(id, team, workerFk) VALUES (8, 1, 19); + +INSERT INTO vn.entryLog (id, originFk, userFk, `action`, creationDate, description, changedModel, oldInstance, newInstance, changedModelId, changedModelValue) + VALUES + (1, 1, 1, 'insert', '2024-02-23 04:35:40.000', NULL, 'Entry', NULL, '{"id":1,"supplierFk":1,"dated":null,"invoiceNumber":"IN2001","isBooked":false,"isExcludedFromAvailable":false,"isConfirmed":true,"isOrdered":false,"isRaid":false,"commission":0.0,"created":"2000-12-01T00:00:00.000Z","evaNotes":"","travelFk":1,"currencyFk":1,"companyFk":442,"gestDocFk":null,"invoiceInFk":null,"isBlocked__":false,"loadPriority":null,"kop":null,"sub":null,"pro":null,"auction":null,"invoiceAmount":null,"buyerFk":null,"typeFk":null,"reference":"Movement 1","observationEditorFk":null,"clonedFrom":null,"editorFk":100,"lockerUserFk":null,"locked":"2024-04-24T12:20:01.000Z"}', 325789, NULL); \ No newline at end of file diff --git a/modules/entry/back/models/buy.js b/modules/entry/back/models/buy.js index 4990fedf2..77acc15df 100644 --- a/modules/entry/back/models/buy.js +++ b/modules/entry/back/models/buy.js @@ -22,16 +22,20 @@ module.exports = Self => { const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; const {isConfirmed} = await models.Entry.findById(orgData.entryFk, {fields: ['isConfirmed']}); + const entryLog = await models.EntryLog.findOne({ + fields: ['userFk'], + where: {originFk: orgData.entryFk, changedModel: 'Entry', action: 'insert'} + }); - if (isConfirmed && orgData.editorFk) { - const {name, lang} = await models.VnUser.findById(orgData.editorFk, {fields: ['name', 'lang']}); + if (isConfirmed && entryLog?.userFk) { + const {name, lang} = await models.VnUser.findById(entryLog.userFk, {fields: ['name', 'lang']}); const to = `@${name}`; - let message = `**${locale[lang].name} changes**:\n`; + let message = ''; const datePattern = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(.\d{3})?Z)?$/; // ISO Date for (let [key, value] of Object.entries(changes)) { - if (key === 'printedStickers') continue; + if (key === 'printedStickers' || orgData[key] === value) continue; if (datePattern.test(value)) { const date = new Date(value); @@ -39,9 +43,12 @@ module.exports = Self => { value += ` ${date.toLocaleTimeString('es-ES')}`; } - const field = lang ? locale[lang].columns[key] : key; + const field = lang ? locale[lang].columns[key] ?? key : key; message += `${field}: ${value}\n`; } + if (!message) return; + + message = `**${locale[lang].name} changes**:\n${message}`; ctx.req = loopbackContext.active; await models.Chat.send(ctx, to, message); } -- 2.40.1