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; + } }; };