refs #5712 feat(workerDms): docuware integration
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2023-05-29 15:21:37 +02:00
parent fad28dc711
commit b60cd2539a
9 changed files with 219 additions and 52 deletions

View File

@ -1,7 +1,7 @@
const axios = require('axios');
module.exports = Self => {
Self.remoteMethodCtx('checkFile', {
Self.remoteMethod('checkFile', {
description: 'Check if exist docuware file',
accessType: 'READ',
accepts: [
@ -20,8 +20,14 @@ module.exports = Self => {
{
arg: 'signed',
type: 'boolean',
required: true,
required: false,
description: 'If pdf is necessary to be signed'
},
{
arg: 'filter',
type: 'object',
required: false,
description: 'The filter'
}
],
returns: {
@ -34,7 +40,7 @@ module.exports = Self => {
}
});
Self.checkFile = async function(ctx, id, fileCabinet, signed) {
Self.checkFile = async function(id, fileCabinet, signed, filter) {
const models = Self.app.models;
const action = 'find';
@ -44,12 +50,12 @@ module.exports = Self => {
action: action
}
});
const searchFilter = {
console.log(id, fileCabinet, signed, filter);
if (!filter) {
filter = {
condition: [
{
DBName: docuwareInfo.findById,
Value: [id]
}
],
@ -60,19 +66,23 @@ module.exports = Self => {
}
]
};
}
try {
const options = await Self.getOptions();
const fileCabinetId = await Self.getFileCabinet(fileCabinet);
const dialogId = await Self.getDialog(fileCabinet, action, fileCabinetId);
console.log('FILTER', filter);
const response = await axios.post(
`${options.url}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`,
searchFilter,
filter,
options.headers
);
const [documents] = response.data.Items;
console.log(response);
console.log(response.data);
if (!documents) return false;
const state = documents.Fields.find(field => field.FieldName == 'ESTADO');

View File

@ -1,6 +1,28 @@
const axios = require('axios');
module.exports = Self => {
/**
* Returns basic headers
*
* @param {string} cookie - The docuware cookie
* @return {object} - The headers
*/
Self.getOptions = async() => {
const docuwareConfig = await Self.app.models.DocuwareConfig.findOne();
const headers = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Cookie': docuwareConfig.cookie
}
};
return {
url: docuwareConfig.url,
headers
};
};
/**
* Returns the dialog id
*
@ -20,9 +42,6 @@ module.exports = Self => {
const options = await Self.getOptions();
if (!process.env.NODE_ENV)
return Math.round();
const response = await axios.get(`${options.url}/FileCabinets/${fileCabinetId}/dialogs`, options.headers);
const dialogs = response.data.Dialog;
const dialogId = dialogs.find(dialogs => dialogs.DisplayName === docuwareInfo.dialogName).Id;
@ -44,9 +63,6 @@ module.exports = Self => {
}
});
if (!process.env.NODE_ENV)
return Math.round();
const fileCabinetResponse = await axios.get(`${options.url}/FileCabinets`, options.headers);
const fileCabinets = fileCabinetResponse.data.FileCabinet;
const fileCabinetId = fileCabinets.find(fileCabinet => fileCabinet.Name === docuwareInfo.fileCabinetName).Id;
@ -55,24 +71,81 @@ module.exports = Self => {
};
/**
* Returns basic headers
* Returns docuware data
*
* @param {string} cookie - The docuware cookie
* @return {object} - The headers
* @param {string} code - The fileCabinet code
* @param {object} filter - The filter for docuware
* @param {object} parse - The fields parsed
* @return {object} - The data
*/
Self.getOptions = async() => {
const docuwareConfig = await Self.app.models.DocuwareConfig.findOne();
const headers = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Cookie': docuwareConfig.cookie
}
Self.get = async(code, filter, parse) => {
const options = await Self.getOptions();
const fileCabinetId = await Self.getFileCabinet(code);
const dialogId = await Self.getDialog(code, 'find', fileCabinetId);
const {data} = await axios.post(
`${options.url}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`,
filter,
options.headers
);
return parser(data, parse);
};
return {
url: docuwareConfig.url,
headers
/**
* Returns docuware data
*
* @param {string} code - The fileCabinet code
* @param {any} id - The id of docuware
* @param {object} parse - The fields parsed
* @return {object} - The data
*/
Self.getById = async(code, id, parse) => {
const docuwareInfo = await Self.app.models.Docuware.findOne({
fields: ['findById'],
where: {
code: code,
action: 'find'
}
});
const filter = {
condition: [
{
DBName: docuwareInfo.findById,
Value: [id]
}
]
};
return Self.get(code, filter, parse);
};
/**
* Returns docuware data filtered
*
* @param {array} data - The data
* @param {object} parse - The fields parsed
* @return {object} - The data parsed
*/
function parser(data, parse) {
if (!(data && data.Items)) return data;
const parsed = [];
for (item of data.Items) {
const itemParsed = {};
item.Fields.map(field => {
if (field.ItemElementName.includes('Date')) field.Item = toDate(field.Item);
if (!parse) return itemParsed[field.FieldLabel] = field.Item;
if (parse[field.FieldLabel])
itemParsed[parse[field.FieldLabel]] = field.Item;
});
parsed.push(itemParsed);
}
return parsed;
}
function toDate(value) {
if (!value) return;
return new Date(Number(value.substring(6, 19)));
}
};

View File

@ -41,8 +41,10 @@ module.exports = Self => {
}
});
Self.download = async function(ctx, id, fileCabinet) {
Self.download = async function(ctx, id, fileCabinet, filter) {
const models = Self.app.models;
// REVIEW
const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet, true);
if (!docuwareFile) throw new UserError('The DOCUWARE PDF document does not exists');

View File

@ -0,0 +1,3 @@
INSERT INTO `vn`.`docuware`
(code, fileCabinetName, `action`, dialogName, findById)
VALUES('hr', 'RRHH', 'find', 'Búsqueda', 'N__DOCUMENTO');

View File

@ -0,0 +1,46 @@
module.exports = Self => {
Self.remoteMethodCtx('docuwareDownload', {
description: 'Download a worker document',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'Number',
description: 'The document id',
http: {source: 'path'}
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: `/:id/docuwareDownload`,
verb: 'GET'
}
});
Self.docuwareDownload = async function(ctx, id) {
// CHECK ROLE?
const filter = {
condition: [
{
DBName: 'FILENAME',
Value: [id]
}
]
};
return await Self.app.models.Docuware.download(ctx, id, 'hr', false, filter);
};
};

View File

@ -5,6 +5,12 @@ module.exports = Self => {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'Number',
description: 'The worker id',
http: {source: 'path'}
},
{
arg: 'filter',
type: 'Object',
@ -17,16 +23,17 @@ module.exports = Self => {
root: true
},
http: {
path: `/filter`,
path: `/:id/filter`,
verb: 'GET'
}
});
Self.filter = async(ctx, filter) => {
Self.filter = async(ctx, id, filter) => {
const conn = Self.dataSource.connector;
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
const account = await Self.app.models.VnUser.findById(userId);
const account = await models.VnUser.findById(userId);
const stmt = new ParameterizedSQL(
`SELECT d.id dmsFk, d.reference, d.description, d.file, d.created, d.hardCopyNumber, d.hasFile
FROM workerDocument wd
@ -47,7 +54,31 @@ module.exports = Self => {
}]
}, oldWhere]};
stmt.merge(conn.makeSuffix(filter));
const workerDms = await conn.executeStmt(stmt);
return await conn.executeStmt(stmt);
// Get docuware info
const worker = await models.Worker.findById(id, {fields: ['fi', 'firstName', 'lastName']});
const docuwareParse = {
'Document ID': 'dmsFk',
'Tipo Documento': 'description',
'Stored on': 'created',
};
const workerDocuware = await models.Docuware.getById('hr', 'BONO MOLA XAVIER', docuwareParse);// worker.lastName + worker.firstName);
for (document of workerDocuware) {
const defaultData = {
file: document.dmsFk + '.png',
isDocuware: true,
hardCopyNumber: null,
hasFile: false,
reference: worker.fi
};
document = Object.assign(document, defaultData);
}
console.log(workerDocuware);
console.log(workerDms);
return workerDms.concat(workerDocuware);
};
};

View File

@ -2,6 +2,7 @@ module.exports = Self => {
require('../methods/worker-dms/downloadFile')(Self);
require('../methods/worker-dms/removeFile')(Self);
require('../methods/worker-dms/filter')(Self);
require('../methods/worker-dms/docuware')(Self);
Self.isMine = async function(ctx, dmsId) {
const myUserId = ctx.req.accessToken.userId;

View File

@ -1,6 +1,6 @@
<vn-crud-model
vn-id="model"
url="WorkerDms/filter"
url="WorkerDms/{{$ctrl.$params.id}}/filter"
link="{worker: $ctrl.$params.id}"
limit="20"
data="$ctrl.workerDms"
@ -53,7 +53,7 @@
</vn-td>
<vn-td shrink>
<span title="{{'Download file' | translate}}" class="link"
ng-click="$ctrl.downloadFile(document.dmsFk)">
ng-click="$ctrl.downloadFile(document.dmsFk, document.isDocuware)">
{{::document.file}}
</span>
</vn-td>
@ -63,7 +63,7 @@
<vn-td shrink>
<vn-icon-button title="{{'Download file' | translate}}"
icon="cloud_download"
ng-click="$ctrl.downloadFile(document.dmsFk)">
ng-click="$ctrl.downloadFile(document.dmsFk, document.isDocuware)">
</vn-icon-button>
</vn-td>
<vn-td shrink>

View File

@ -17,7 +17,8 @@ class Controller extends Component {
});
}
downloadFile(dmsId) {
downloadFile(dmsId, isDocuware) {
if (isDocuware) return this.vnFile.download(`api/workerDms/${dmsId}/docuwareDownload`);
this.vnFile.download(`api/workerDms/${dmsId}/downloadFile`);
}
}