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
|
|
|
|
*
|
|
|
|
* @return {object} - The headers
|
|
|
|
*/
|
|
|
|
Self.getOptions = async() => {
|
2024-10-15 06:00:19 +00:00
|
|
|
const docuwareConfig = await Self.app.models.DocuwareConfig.findOne();
|
|
|
|
const now = new Date().getTime();
|
|
|
|
let {url, username, password, token, expired} = docuwareConfig;
|
|
|
|
|
|
|
|
if (!expired || expired < now + 60) {
|
|
|
|
const {data: {IdentityServiceUrl}} = await axios.get(`${url}/Home/IdentityServiceInfo`);
|
|
|
|
const {data: {token_endpoint}} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`);
|
|
|
|
const {data} = await axios.post(token_endpoint, {
|
|
|
|
grant_type: 'password',
|
|
|
|
scope: 'docuware.platform',
|
|
|
|
client_id: 'docuware.platform.net.client',
|
|
|
|
username,
|
|
|
|
password
|
|
|
|
}, {headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}});
|
|
|
|
|
|
|
|
const newToken = data.access_token;
|
|
|
|
token = data.token_type + ' ' + newToken;
|
|
|
|
await docuwareConfig.updateAttributes({
|
|
|
|
token,
|
|
|
|
expired: now + data.expires_in
|
|
|
|
});
|
|
|
|
}
|
2024-10-07 13:14:03 +00:00
|
|
|
|
2023-05-29 13:21:37 +00:00
|
|
|
const headers = {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2024-10-15 06:00:19 +00:00
|
|
|
'Authorization': token
|
2023-05-29 13:21:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2024-10-15 06:00:19 +00:00
|
|
|
url,
|
2023-05-29 13:21:37 +00:00
|
|
|
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) => {
|
2024-10-15 07:47:39 +00:00
|
|
|
if (!process.env.NODE_ENV)
|
|
|
|
return Math.floor(Math.random() + 100);
|
2023-07-05 10:09:52 +00:00
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
const docuwareInfo = await Self.app.models.Docuware.findOne({
|
|
|
|
where: {
|
2023-07-05 10:09:52 +00:00
|
|
|
code,
|
|
|
|
action
|
2023-01-11 14:19:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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 => {
|
2024-10-15 07:47:39 +00:00
|
|
|
if (!process.env.NODE_ENV)
|
|
|
|
return Math.floor(Math.random() + 100);
|
2023-07-05 10:09:52 +00:00
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
const options = await Self.getOptions();
|
|
|
|
const docuwareInfo = await Self.app.models.Docuware.findOne({
|
|
|
|
where: {
|
2023-07-05 10:09:52 +00:00
|
|
|
code
|
2023-01-11 14:19:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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) => {
|
2024-10-15 07:47:39 +00:00
|
|
|
if (!process.env.NODE_ENV) return;
|
2023-07-05 10:09:52 +00:00
|
|
|
|
2023-05-29 13:21:37 +00:00
|
|
|
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) => {
|
2024-10-15 07:47:39 +00:00
|
|
|
if (!process.env.NODE_ENV) return;
|
2023-07-05 10:09:52 +00:00
|
|
|
|
2023-05-29 13:21:37 +00:00
|
|
|
const docuwareInfo = await Self.app.models.Docuware.findOne({
|
|
|
|
fields: ['findById'],
|
|
|
|
where: {
|
2023-07-05 10:09:52 +00:00
|
|
|
code,
|
2023-05-29 13:21:37 +00:00
|
|
|
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
|
|
|
};
|