161 lines
4.8 KiB
JavaScript
161 lines
4.8 KiB
JavaScript
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
|
|
*
|
|
* @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.app.models.Docuware.findOne({
|
|
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);
|
|
};
|
|
|
|
/**
|
|
* 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)));
|
|
}
|
|
};
|