2023-01-11 14:19:09 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-05-29 13:21:37 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
/**
|
|
|
|
* 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) => {
|
|
|
|
const docuwareInfo = await Self.app.models.Docuware.findOne({
|
|
|
|
where: {
|
|
|
|
code: code,
|
|
|
|
action: 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 => {
|
|
|
|
const options = await Self.getOptions();
|
|
|
|
const docuwareInfo = await Self.app.models.Docuware.findOne({
|
|
|
|
where: {
|
|
|
|
code: 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-05-29 13:21:37 +00:00
|
|
|
* Returns docuware data
|
2023-01-11 14:19:09 +00:00
|
|
|
*
|
2023-05-29 13:21:37 +00:00
|
|
|
* @param {string} code - The fileCabinet code
|
|
|
|
* @param {object} filter - The filter for docuware
|
|
|
|
* @param {object} parse - The fields parsed
|
|
|
|
* @return {object} - The data
|
2023-01-11 14:19:09 +00:00
|
|
|
*/
|
2023-05-29 13:21:37 +00:00
|
|
|
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);
|
|
|
|
|
2023-05-30 13:34:11 +00:00
|
|
|
const data = await axios.post(
|
2023-05-29 13:21:37 +00:00
|
|
|
`${options.url}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`,
|
|
|
|
filter,
|
|
|
|
options.headers
|
|
|
|
);
|
2023-05-30 13:34:11 +00:00
|
|
|
return parser(data.data, parse);
|
2023-05-29 13:21:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'
|
2023-01-11 14:19:09 +00:00
|
|
|
}
|
2023-05-29 13:21:37 +00:00
|
|
|
});
|
|
|
|
const filter = {
|
|
|
|
condition: [
|
|
|
|
{
|
|
|
|
DBName: docuwareInfo.findById,
|
|
|
|
Value: [id]
|
|
|
|
}
|
|
|
|
]
|
2023-01-11 14:19:09 +00:00
|
|
|
};
|
|
|
|
|
2023-05-29 13:21:37 +00:00
|
|
|
return Self.get(code, filter, parse);
|
2023-01-11 14:19:09 +00:00
|
|
|
};
|
2023-05-29 13:21:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)));
|
|
|
|
}
|
2023-01-11 14:19:09 +00:00
|
|
|
};
|