salix/back/methods/docuware/core.js

236 lines
7.4 KiB
JavaScript

const axios = require('axios');
module.exports = Self => {
/**
* Returns templateJSON
*
* @param {string} config - The config as template upload
* @return {object} - The template parse
*/
Self.buildTemplateJSON = config => {
const templateJson = {
'Fields': []
};
templateJson.Fields = Object.keys(config).map(fieldName => ({
'FieldName': fieldName,
'ItemElementName': config[fieldName].type,
'Item': config[fieldName].value
}));
return templateJson;
};
/**
* Returns upload options
*
* @param {string} value - The document value
* @param {Object} configTemplate - The config as template upload
* @param {object} options - The options
* @return {object} - The options with headers
*/
Self.uploadOptions = async(value, configTemplate, options) => {
const FormData = require('form-data');
const data = new FormData();
const docuwareOptions = options ?? await Self.getOptions();
const templateJson = Self.buildTemplateJSON(configTemplate);
data.append('document', JSON.stringify(templateJson), 'schema.json');
data.append('file[]', value, 'pda.pdf');
const uploadOptions = {
headers: {
'Content-Type': 'multipart/form-data',
'X-File-ModifiedDate': Date.vnNew(),
'Cookie': docuwareOptions.headers.headers.Cookie,
...data.getHeaders()
},
};
return uploadOptions;
};
/**
* 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 docuware id
*
* @param {object} filter - The filter to use in findOne method
* @return {object} - The doware record
*/
Self.getDocuware = async filter => {
return await Self.app.models.Docuware.findOne(filter);
};
/**
* Returns the dialog id
*
* @param {string} code - The fileCabinet name
* @param {string} action - The fileCabinet name
* @param {string} fileCabinetId - Optional The fileCabinet name
* @return {number} - The fileCabinet id
*/
Self.getDialog = async(code, action, fileCabinetId) => {
if (!process.env.NODE_ENV)
return Math.floor(Math.random() + 100);
const docuwareInfo = await Self.getDocuware({
where: {
code,
action
}
});
if (!fileCabinetId) fileCabinetId = await Self.getFileCabinet(code);
const options = await Self.getOptions();
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;
return dialogId;
};
/**
* Returns the fileCabinetId
*
* @param {string} code - The fileCabinet code
* @return {number} - The fileCabinet id
*/
Self.getFileCabinet = async code => {
if (!process.env.NODE_ENV)
return Math.floor(Math.random() + 100);
const options = await Self.getOptions();
const docuwareInfo = await Self.app.models.Docuware.findOne({
where: {
code
}
});
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;
return fileCabinetId;
};
/**
* Returns docuware data
*
* @param {string} code - The fileCabinet code
* @param {object} filter - The filter for docuware
* @param {object} parse - The fields parsed
* @return {object} - The data
*/
Self.get = async(code, filter, parse) => {
if (!process.env.NODE_ENV) return;
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.data, parse);
};
/**
* 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) => {
if (!process.env.NODE_ENV) return;
const docuwareInfo = await Self.app.models.Docuware.findOne({
fields: ['findById'],
where: {
code,
action: 'find'
}
});
const filter = {
condition: [
{
DBName: docuwareInfo.findById,
Value: [id]
}
]
};
return Self.get(code, filter, parse);
};
/**
* Execute detete old docuware
*
* @param {string} id - The id
* @param {string} fileCabinet - The fieldCabinet
* @param {Object} configTemplate - The config
* @param {string} uri - The uri
* @param {Object} options - The options
*/
Self.deleteOld = async(id, fileCabinet, configTemplate, uri, options) => {
const docuwareOptions = options ?? await Self.getOptions();
const config = configTemplate ?? {'ESTADO': {type: 'String', value: 'Pendiente eliminar'}};
const docuwareFile = await Self.checkFile(id, fileCabinet, false);
if (docuwareFile) {
const deleteJson = Self.buildTemplateJSON(config);
const deleteUri = `${uri}/${docuwareFile.id}/Fields`;
await axios.put(deleteUri, deleteJson, docuwareOptions.headers);
}
};
/**
* 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)));
}
};