From 1aa888b1a30f20adac3372a9428281710be8d113 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 9 Sep 2024 15:11:09 +0200 Subject: [PATCH 1/4] 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; }; }; -- 2.40.1 From 8dc83e244473b172cac65892ceef5b6de2183d3e Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 11 Sep 2024 08:16:21 +0200 Subject: [PATCH 2/4] 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; + } }; }; -- 2.40.1 From fbfbf21e06f044ee929820e19d79279e8dd9433f Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 11 Sep 2024 08:17:18 +0200 Subject: [PATCH 3/4] 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; -}; -- 2.40.1 From c21da8d258196466544549fadad1c7cd7b54c823 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 11 Sep 2024 10:31:43 +0200 Subject: [PATCH 4/4] 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'} -- 2.40.1