From 1aa888b1a30f20adac3372a9428281710be8d113 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 9 Sep 2024 15:11:09 +0200 Subject: [PATCH 1/5] feat(WorkerDms_filter): refs #7182 split code. fix: filter --- .../back/methods/worker-dms/docuwareFilter.js | 74 +++++++++++++++ .../worker/back/methods/worker-dms/filter.js | 90 ++++++------------- 2 files changed, 99 insertions(+), 65 deletions(-) create mode 100644 modules/worker/back/methods/worker-dms/docuwareFilter.js diff --git a/modules/worker/back/methods/worker-dms/docuwareFilter.js b/modules/worker/back/methods/worker-dms/docuwareFilter.js new file mode 100644 index 000000000..0311153b0 --- /dev/null +++ b/modules/worker/back/methods/worker-dms/docuwareFilter.js @@ -0,0 +1,74 @@ +module.exports = Self => { + Self.remoteMethodCtx('filter', { + description: 'Find docuware documents', + accessType: 'READ', + accepts: [ + { + arg: 'id', + type: 'number', + description: 'The worker id', + http: {source: 'path'} + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/:id/filter`, + verb: 'GET' + } + }); + + Self.filter = async(ctx, id) => { + const models = Self.app.models; + + const {dmsTypeFk} = await models.Docuware.findOne({ + fields: ['dmsTypeFk'], + where: {code: 'hr', action: 'find'} + }); + + if (!await models.DmsType.hasReadRole(ctx, dmsTypeFk)) return []; + + let workerDocuware = []; + const worker = await models.Worker.findById(id, {fields: ['fi', 'firstName', 'lastName']}); + const docuwareParse = { + 'Filename': 'dmsFk', + 'Tipo Documento': 'description', + 'Stored on': 'created', + 'Document ID': 'id', + 'URL': 'download', + 'Stored by': 'name', + 'Estado': 'state' + }; + + workerDocuware = + await models.Docuware.getById('hr', worker.lastName + ' ' + worker.firstName, docuwareParse) ?? []; + const url = (await Self.app.models.Url.getUrl('docuware')) + 'WebClient'; + for (document of workerDocuware) { + const docuwareId = document.id; + const defaultData = { + id: docuwareId, + workerFk: id, + dmsFk: docuwareId, + dms: { + id: docuwareId, + file: docuwareId + '.pdf', + isDocuware: true, + hasFile: false, + reference: worker.fi, + dmsFk: docuwareId, + url, + description: document.description + ' - ' + document.state, + download: document.download, + created: document.created, + dmsType: {name: 'Docuware'}, + worker: {id: null, user: {name: document.name}}, + } + }; + Object.assign(document, defaultData); + } + }; + + return workerDocuware; +}; diff --git a/modules/worker/back/methods/worker-dms/filter.js b/modules/worker/back/methods/worker-dms/filter.js index b7802a689..b92ba139b 100644 --- a/modules/worker/back/methods/worker-dms/filter.js +++ b/modules/worker/back/methods/worker-dms/filter.js @@ -8,19 +8,19 @@ module.exports = Self => { accepts: [ { arg: 'id', - type: 'Number', + type: 'number', description: 'The worker id', http: {source: 'path'} }, { arg: 'filter', - type: 'Object', + type: 'object', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', http: {source: 'query'} } ], returns: { - type: ['Object'], + type: ['object'], root: true }, http: { @@ -36,6 +36,7 @@ module.exports = Self => { // Get ids alloweds const account = await models.VnUser.findById(userId); + const stmt = new ParameterizedSQL( `SELECT d.id, d.id dmsFk FROM workerDocument wd @@ -44,68 +45,27 @@ module.exports = Self => { LEFT JOIN account.roleRole rr ON rr.inheritsFrom = dt.readRoleFk AND rr.role = ? `, [account.roleFk] ); - const yourOwnDms = {and: [{isReadableByWorker: true}, {worker: userId}]}; - const where = { - or: [yourOwnDms, { - role: { - neq: null - } - }] - }; - stmt.merge(conn.makeSuffix(mergeWhere(filter.where, where))); - - // Get workerDms alloweds - const dmsIds = await conn.executeStmt(stmt); - const allowedIds = dmsIds.map(dms => dms.id); - const allowedFilter = mergeFilters(filter, {where: {dmsFk: {inq: allowedIds}, workerFk: id}}); - let workerDms = await models.WorkerDms.find(allowedFilter); - - // Get docuware info - const docuware = await models.Docuware.findOne({ - fields: ['dmsTypeFk'], - where: {code: 'hr', action: 'find'} - }); - const docuwareDmsType = docuware.dmsTypeFk; - let workerDocuware = []; - if (!filter.skip && (!docuwareDmsType || (docuwareDmsType && await models.DmsType.hasReadRole(ctx, docuwareDmsType)))) { - const worker = await models.Worker.findById(id, {fields: ['fi', 'firstName', 'lastName']}); - const docuwareParse = { - 'Filename': 'dmsFk', - 'Tipo Documento': 'description', - 'Stored on': 'created', - 'Document ID': 'id', - 'URL': 'download', - 'Stored by': 'name', - 'Estado': 'state' - }; - - workerDocuware = - await models.Docuware.getById('hr', worker.lastName + ' ' + worker.firstName, docuwareParse) ?? []; - const url = (await Self.app.models.Url.getUrl('docuware')) + 'WebClient'; - for (document of workerDocuware) { - const docuwareId = document.id; - const defaultData = { - id: docuwareId, - workerFk: id, - dmsFk: docuwareId, - dms: { - id: docuwareId, - file: docuwareId + '.pdf', - isDocuware: true, - hasFile: false, - reference: worker.fi, - dmsFk: docuwareId, - url, - description: document.description + ' - ' + document.state, - download: document.download, - created: document.created, - dmsType: {name: 'Docuware'}, - worker: {id: null, user: {name: document.name}}, + const yourOwnDms = { + or: [ + {and: [ + {isReadableByWorker: true}, + {worker: userId} + ]}, + { + role: { + neq: null } - }; - Object.assign(document, defaultData); - } - } - return workerDms.concat(workerDocuware); + }] + }; + const where = mergeWhere(filter.where, yourOwnDms); + stmt.merge(conn.makeSuffix({where})); + const dmsIds = await conn.executeStmt(stmt); + + const allowedIds = dmsIds.map(dms => dms.id); + const allowedFilter = mergeFilters(filter, {where: {dmsFk: {inq: allowedIds}}}); + + const find = await models.WorkerDms.find(allowedFilter); + + return find; }; }; From 8dc83e244473b172cac65892ceef5b6de2183d3e Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 11 Sep 2024 08:16:21 +0200 Subject: [PATCH 2/5] feat: refs #7182 optimize workerDms filter --- .../worker/back/methods/worker-dms/filter.js | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/modules/worker/back/methods/worker-dms/filter.js b/modules/worker/back/methods/worker-dms/filter.js index b92ba139b..ed0490f54 100644 --- a/modules/worker/back/methods/worker-dms/filter.js +++ b/modules/worker/back/methods/worker-dms/filter.js @@ -57,15 +57,66 @@ module.exports = Self => { } }] }; + const where = mergeWhere(filter.where, yourOwnDms); stmt.merge(conn.makeSuffix({where})); const dmsIds = await conn.executeStmt(stmt); const allowedIds = dmsIds.map(dms => dms.id); - const allowedFilter = mergeFilters(filter, {where: {dmsFk: {inq: allowedIds}}}); + const allowedFilter = mergeFilters(filter, {where: {dmsFk: {inq: allowedIds}, workerFk: id}}); - const find = await models.WorkerDms.find(allowedFilter); + const workerDms = await models.WorkerDms.find(allowedFilter); - return find; + const workerDocuware = filter.skip ? [] : await getDocuwareasync(ctx, id); + return workerDms.concat(workerDocuware); + + async function getDocuwareasync(ctx, id) { + const {dmsTypeFk} = await models.Docuware.findOne({ + fields: ['dmsTypeFk'], + where: {code: 'hr', action: 'find'} + }); + + if (!await models.DmsType.hasReadRole(ctx, dmsTypeFk)) return []; + + let workerDocuware = []; + const worker = await models.Worker.findById(id, {fields: ['fi', 'firstName', 'lastName']}); + const docuwareParse = { + 'Filename': 'dmsFk', + 'Tipo Documento': 'description', + 'Stored on': 'created', + 'Document ID': 'id', + 'URL': 'download', + 'Stored by': 'name', + 'Estado': 'state' + }; + + workerDocuware = + await models.Docuware.getById('hr', worker.lastName + ' ' + worker.firstName, docuwareParse) ?? []; + const url = (await Self.app.models.Url.getUrl('docuware')) + 'WebClient'; + for (document of workerDocuware) { + const docuwareId = document.id; + const defaultData = { + id: docuwareId, + workerFk: id, + dmsFk: docuwareId, + dms: { + id: docuwareId, + file: docuwareId + '.pdf', + isDocuware: true, + hasFile: false, + reference: worker.fi, + dmsFk: docuwareId, + url, + description: document.description + ' - ' + document.state, + download: document.download, + created: document.created, + dmsType: {name: 'Docuware'}, + worker: {id: null, user: {name: document.name}}, + } + }; + Object.assign(document, defaultData); + } + return workerDocuware; + } }; }; From fbfbf21e06f044ee929820e19d79279e8dd9433f Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 11 Sep 2024 08:17:18 +0200 Subject: [PATCH 3/5] fix: refs #7182 unnesesary file --- .../back/methods/worker-dms/docuwareFilter.js | 74 ------------------- 1 file changed, 74 deletions(-) delete mode 100644 modules/worker/back/methods/worker-dms/docuwareFilter.js diff --git a/modules/worker/back/methods/worker-dms/docuwareFilter.js b/modules/worker/back/methods/worker-dms/docuwareFilter.js deleted file mode 100644 index 0311153b0..000000000 --- a/modules/worker/back/methods/worker-dms/docuwareFilter.js +++ /dev/null @@ -1,74 +0,0 @@ -module.exports = Self => { - Self.remoteMethodCtx('filter', { - description: 'Find docuware documents', - accessType: 'READ', - accepts: [ - { - arg: 'id', - type: 'number', - description: 'The worker id', - http: {source: 'path'} - } - ], - returns: { - type: ['object'], - root: true - }, - http: { - path: `/:id/filter`, - verb: 'GET' - } - }); - - Self.filter = async(ctx, id) => { - const models = Self.app.models; - - const {dmsTypeFk} = await models.Docuware.findOne({ - fields: ['dmsTypeFk'], - where: {code: 'hr', action: 'find'} - }); - - if (!await models.DmsType.hasReadRole(ctx, dmsTypeFk)) return []; - - let workerDocuware = []; - const worker = await models.Worker.findById(id, {fields: ['fi', 'firstName', 'lastName']}); - const docuwareParse = { - 'Filename': 'dmsFk', - 'Tipo Documento': 'description', - 'Stored on': 'created', - 'Document ID': 'id', - 'URL': 'download', - 'Stored by': 'name', - 'Estado': 'state' - }; - - workerDocuware = - await models.Docuware.getById('hr', worker.lastName + ' ' + worker.firstName, docuwareParse) ?? []; - const url = (await Self.app.models.Url.getUrl('docuware')) + 'WebClient'; - for (document of workerDocuware) { - const docuwareId = document.id; - const defaultData = { - id: docuwareId, - workerFk: id, - dmsFk: docuwareId, - dms: { - id: docuwareId, - file: docuwareId + '.pdf', - isDocuware: true, - hasFile: false, - reference: worker.fi, - dmsFk: docuwareId, - url, - description: document.description + ' - ' + document.state, - download: document.download, - created: document.created, - dmsType: {name: 'Docuware'}, - worker: {id: null, user: {name: document.name}}, - } - }; - Object.assign(document, defaultData); - } - }; - - return workerDocuware; -}; From c21da8d258196466544549fadad1c7cd7b54c823 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 11 Sep 2024 10:31:43 +0200 Subject: [PATCH 4/5] fix: refs #7182 function name --- modules/worker/back/methods/worker-dms/filter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/worker/back/methods/worker-dms/filter.js b/modules/worker/back/methods/worker-dms/filter.js index ed0490f54..394988f9e 100644 --- a/modules/worker/back/methods/worker-dms/filter.js +++ b/modules/worker/back/methods/worker-dms/filter.js @@ -67,10 +67,10 @@ module.exports = Self => { const workerDms = await models.WorkerDms.find(allowedFilter); - const workerDocuware = filter.skip ? [] : await getDocuwareasync(ctx, id); + const workerDocuware = filter.skip ? [] : await getDocuware(ctx, id); return workerDms.concat(workerDocuware); - async function getDocuwareasync(ctx, id) { + async function getDocuware(ctx, id) { const {dmsTypeFk} = await models.Docuware.findOne({ fields: ['dmsTypeFk'], where: {code: 'hr', action: 'find'} From 728c40916c4dc759afe2a0e56bb8ffb5e1acb7fd Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 11 Sep 2024 14:38:58 +0200 Subject: [PATCH 5/5] refactor: refs #7895 workerDocument to workerDms --- db/dump/fixtures.before.sql | 2 +- ...ent_afterDelete.sql => workerDms_afterDelete.sql} | 6 +++--- ...t_beforeInsert.sql => workerDms_beforeInsert.sql} | 4 ++-- ...t_beforeUpdate.sql => workerDms_beforeUpdate.sql} | 4 ++-- db/versions/11225-goldenLaurel/00-firstScript.sql | 1 + db/versions/11225-goldenLaurel/01-firstScript.sql | 1 + db/versions/11225-goldenLaurel/02-firstScript.sql | 1 + modules/worker/back/methods/worker-dms/filter.js | 7 ++++--- modules/worker/back/models/worker-dms.json | 12 +++--------- myt.config.yml | 2 +- 10 files changed, 19 insertions(+), 21 deletions(-) rename db/routines/vn/triggers/{workerDocument_afterDelete.sql => workerDms_afterDelete.sql} (71%) rename db/routines/vn/triggers/{workerDocument_beforeInsert.sql => workerDms_beforeInsert.sql} (73%) rename db/routines/vn/triggers/{workerDocument_beforeUpdate.sql => workerDms_beforeUpdate.sql} (73%) create mode 100644 db/versions/11225-goldenLaurel/00-firstScript.sql create mode 100644 db/versions/11225-goldenLaurel/01-firstScript.sql create mode 100644 db/versions/11225-goldenLaurel/02-firstScript.sql diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index ea689c609..0a797f7ec 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -2488,7 +2488,7 @@ INSERT INTO `vn`.`clientDms`(`clientFk`, `dmsFk`) (1104, 2), (1104, 3); -INSERT INTO `vn`.`workerDocument`(`id`, `worker`, `document`,`isReadableByWorker`) +INSERT INTO `vn`.`workerDms`(`id`, `workerFk`, `dmsFk`,`isReadableByWorker`) VALUES (1, 1106, 4, TRUE), (2, 1107, 3, FALSE); diff --git a/db/routines/vn/triggers/workerDocument_afterDelete.sql b/db/routines/vn/triggers/workerDms_afterDelete.sql similarity index 71% rename from db/routines/vn/triggers/workerDocument_afterDelete.sql rename to db/routines/vn/triggers/workerDms_afterDelete.sql index 8d4878248..bfb7d481e 100644 --- a/db/routines/vn/triggers/workerDocument_afterDelete.sql +++ b/db/routines/vn/triggers/workerDms_afterDelete.sql @@ -1,11 +1,11 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`workerDocument_afterDelete` - AFTER DELETE ON `workerDocument` +CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`workerDms_afterDelete` + AFTER DELETE ON `workerDms` FOR EACH ROW BEGIN INSERT INTO workerLog SET `action` = 'delete', - `changedModel` = 'WorkerDocument', + `changedModel` = 'WorkerDms', `changedModelId` = OLD.id, `userFk` = account.myUser_getId(); END$$ diff --git a/db/routines/vn/triggers/workerDocument_beforeInsert.sql b/db/routines/vn/triggers/workerDms_beforeInsert.sql similarity index 73% rename from db/routines/vn/triggers/workerDocument_beforeInsert.sql rename to db/routines/vn/triggers/workerDms_beforeInsert.sql index f0675e68f..a52c60961 100644 --- a/db/routines/vn/triggers/workerDocument_beforeInsert.sql +++ b/db/routines/vn/triggers/workerDms_beforeInsert.sql @@ -1,6 +1,6 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`workerDocument_beforeInsert` - BEFORE INSERT ON `workerDocument` +CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`workerDms_beforeInsert` + BEFORE INSERT ON `workerDms` FOR EACH ROW BEGIN SET NEW.editorFk = account.myUser_getId(); diff --git a/db/routines/vn/triggers/workerDocument_beforeUpdate.sql b/db/routines/vn/triggers/workerDms_beforeUpdate.sql similarity index 73% rename from db/routines/vn/triggers/workerDocument_beforeUpdate.sql rename to db/routines/vn/triggers/workerDms_beforeUpdate.sql index ffb6efd74..564064444 100644 --- a/db/routines/vn/triggers/workerDocument_beforeUpdate.sql +++ b/db/routines/vn/triggers/workerDms_beforeUpdate.sql @@ -1,6 +1,6 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`workerDocument_beforeUpdate` - BEFORE UPDATE ON `workerDocument` +CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`workerDms_beforeUpdate` + BEFORE UPDATE ON `workerDms` FOR EACH ROW BEGIN SET NEW.editorFk = account.myUser_getId(); diff --git a/db/versions/11225-goldenLaurel/00-firstScript.sql b/db/versions/11225-goldenLaurel/00-firstScript.sql new file mode 100644 index 000000000..87f521a03 --- /dev/null +++ b/db/versions/11225-goldenLaurel/00-firstScript.sql @@ -0,0 +1 @@ +ALTER TABLE vn.workerDocument CHANGE worker workerFk int(10) unsigned DEFAULT NULL NULL; diff --git a/db/versions/11225-goldenLaurel/01-firstScript.sql b/db/versions/11225-goldenLaurel/01-firstScript.sql new file mode 100644 index 000000000..3aa3a1876 --- /dev/null +++ b/db/versions/11225-goldenLaurel/01-firstScript.sql @@ -0,0 +1 @@ +ALTER TABLE vn.workerDocument CHANGE document dmsFk int(11) DEFAULT NULL NULL; diff --git a/db/versions/11225-goldenLaurel/02-firstScript.sql b/db/versions/11225-goldenLaurel/02-firstScript.sql new file mode 100644 index 000000000..d0e789534 --- /dev/null +++ b/db/versions/11225-goldenLaurel/02-firstScript.sql @@ -0,0 +1 @@ +RENAME TABLE vn.workerDocument TO vn.workerDms; diff --git a/modules/worker/back/methods/worker-dms/filter.js b/modules/worker/back/methods/worker-dms/filter.js index b7802a689..8feac3658 100644 --- a/modules/worker/back/methods/worker-dms/filter.js +++ b/modules/worker/back/methods/worker-dms/filter.js @@ -38,10 +38,11 @@ module.exports = Self => { const account = await models.VnUser.findById(userId); const stmt = new ParameterizedSQL( `SELECT d.id, d.id dmsFk - FROM workerDocument wd - JOIN dms d ON d.id = wd.document + FROM workerDms wd + JOIN dms d ON d.id = wd.dmsFk JOIN dmsType dt ON dt.id = d.dmsTypeFk - LEFT JOIN account.roleRole rr ON rr.inheritsFrom = dt.readRoleFk AND rr.role = ? + LEFT JOIN account.roleRole rr ON rr.inheritsFrom = dt.readRoleFk + AND rr.role = ? `, [account.roleFk] ); const yourOwnDms = {and: [{isReadableByWorker: true}, {worker: userId}]}; diff --git a/modules/worker/back/models/worker-dms.json b/modules/worker/back/models/worker-dms.json index a5c0f30b2..ef5684025 100644 --- a/modules/worker/back/models/worker-dms.json +++ b/modules/worker/back/models/worker-dms.json @@ -6,7 +6,7 @@ }, "options": { "mysql": { - "table": "workerDocument" + "table": "workerDms" } }, "properties": { @@ -16,17 +16,11 @@ }, "dmsFk": { "type": "number", - "required": true, - "mysql": { - "columnName": "document" - } + "required": true }, "workerFk": { "type": "number", - "required": true, - "mysql": { - "columnName": "worker" - } + "required": true }, "isReadableByWorker": { "type": "boolean" diff --git a/myt.config.yml b/myt.config.yml index ffa4188b2..1a0a138f0 100755 --- a/myt.config.yml +++ b/myt.config.yml @@ -375,7 +375,7 @@ localFixtures: - workerBosses - workerBusinessType - workerConfig - - workerDocument + - workerDms - workerLog - workerMana - workerManaExcluded