2022-02-16 13:57:47 +00:00
|
|
|
const got = require('got');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('checkFile', {
|
2022-02-21 07:48:53 +00:00
|
|
|
description: 'Check if exist docuware file',
|
2022-02-16 13:57:47 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
2022-02-21 07:48:53 +00:00
|
|
|
type: 'number',
|
|
|
|
description: 'The id',
|
2022-02-16 13:57:47 +00:00
|
|
|
http: {source: 'path'}
|
2022-02-21 07:48:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'fileCabinet',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'The fileCabinet name'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'dialog',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'The dialog name'
|
2022-02-16 13:57:47 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/checkFile`,
|
2022-02-21 07:48:53 +00:00
|
|
|
verb: 'POST'
|
2022-02-16 13:57:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-21 07:48:53 +00:00
|
|
|
Self.checkFile = async function(ctx, id, fileCabinet, dialog) {
|
|
|
|
const myUserId = ctx.req.accessToken.userId;
|
|
|
|
if (!myUserId)
|
|
|
|
return false;
|
2022-02-16 13:57:47 +00:00
|
|
|
|
|
|
|
const models = Self.app.models;
|
|
|
|
const docuwareConfig = await models.DocuwareConfig.findOne();
|
|
|
|
const docuwareInfo = await models.Docuware.findOne({
|
|
|
|
where: {
|
2022-02-22 14:23:23 +00:00
|
|
|
code: fileCabinet,
|
2022-02-21 07:48:53 +00:00
|
|
|
dialogName: dialog
|
2022-02-16 13:57:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const docuwareUrl = docuwareConfig.url;
|
|
|
|
const cookie = docuwareConfig.token;
|
|
|
|
const fileCabinetName = docuwareInfo.fileCabinetName;
|
|
|
|
const find = docuwareInfo.find;
|
|
|
|
const options = {
|
|
|
|
'headers': {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Cookie': cookie
|
|
|
|
}
|
|
|
|
};
|
2022-02-28 12:27:03 +00:00
|
|
|
const searchFilter = {
|
2022-02-21 07:48:53 +00:00
|
|
|
condition: [
|
|
|
|
{
|
|
|
|
DBName: find,
|
|
|
|
Value: [id]
|
|
|
|
}
|
|
|
|
]
|
2022-02-16 13:57:47 +00:00
|
|
|
};
|
2022-02-21 07:48:53 +00:00
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
try {
|
2022-02-21 07:48:53 +00:00
|
|
|
// get fileCabinetId
|
|
|
|
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options);
|
|
|
|
const fileCabinetJson = JSON.parse(fileCabinetResponse.body).FileCabinet;
|
|
|
|
const fileCabinetId = fileCabinetJson.find(dialogs => dialogs.Name === fileCabinetName).Id;
|
|
|
|
|
|
|
|
// get dialog
|
|
|
|
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options);
|
|
|
|
const dialogJson = JSON.parse(dialogResponse.body).Dialog;
|
|
|
|
const dialogId = dialogJson.find(dialogs => dialogs.DisplayName === 'find').Id;
|
|
|
|
|
|
|
|
// get docuwareID
|
2022-02-28 12:27:03 +00:00
|
|
|
Object.assign(options, {'body': JSON.stringify(searchFilter)});
|
2022-02-21 07:48:53 +00:00
|
|
|
const response = await got.post(
|
|
|
|
`${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`, options);
|
2022-02-16 13:57:47 +00:00
|
|
|
JSON.parse(response.body).Items[0].Id;
|
2022-02-21 07:48:53 +00:00
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|