This commit is contained in:
parent
1a18ba66a9
commit
444b08c566
|
@ -1,5 +1,3 @@
|
|||
const axios = require('axios');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethod('checkFile', {
|
||||
description: 'Check if exist docuware file',
|
||||
|
@ -73,17 +71,8 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
try {
|
||||
const options = await Self.getOptions();
|
||||
|
||||
const fileCabinetId = await Self.getFileCabinet(fileCabinet);
|
||||
const dialogId = await Self.getDialog(fileCabinet, action, fileCabinetId);
|
||||
|
||||
const response = await axios.post(
|
||||
`${options.url}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`,
|
||||
filter,
|
||||
options.headers
|
||||
);
|
||||
const [documents] = response.data.Items;
|
||||
const response = await Self.get(fileCabinet, filter);
|
||||
const [documents] = response.Items;
|
||||
if (!documents) return false;
|
||||
|
||||
return {id: documents.Id};
|
||||
|
|
|
@ -32,10 +32,13 @@ module.exports = Self => {
|
|||
* @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: code,
|
||||
action: action
|
||||
code,
|
||||
action
|
||||
}
|
||||
});
|
||||
if (!fileCabinetId) fileCabinetId = await Self.getFileCabinet(code);
|
||||
|
@ -56,10 +59,13 @@ module.exports = Self => {
|
|||
* @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: code
|
||||
code
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -79,6 +85,8 @@ module.exports = Self => {
|
|||
* @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);
|
||||
|
@ -100,10 +108,12 @@ module.exports = Self => {
|
|||
* @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: code,
|
||||
code,
|
||||
action: 'find'
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,57 +1,15 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
const axios = require('axios');
|
||||
|
||||
describe('docuware download()', () => {
|
||||
const ticketId = 1;
|
||||
const userId = 9;
|
||||
const ctx = {
|
||||
req: {
|
||||
|
||||
accessToken: {userId: userId},
|
||||
headers: {origin: 'http://localhost:5000'},
|
||||
}
|
||||
};
|
||||
|
||||
const docuwareModel = models.Docuware;
|
||||
const fileCabinetName = 'deliveryNote';
|
||||
|
||||
beforeAll(() => {
|
||||
spyOn(docuwareModel, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
spyOn(docuwareModel, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
});
|
||||
|
||||
it('should return false if there are no documents', async() => {
|
||||
const response = {
|
||||
data: {
|
||||
Items: []
|
||||
}
|
||||
};
|
||||
spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(response)));
|
||||
spyOn(docuwareModel, 'get').and.returnValue((new Promise(resolve => resolve({Items: []}))));
|
||||
|
||||
const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, true);
|
||||
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
|
||||
it('should return false if the document is unsigned', async() => {
|
||||
const response = {
|
||||
data: {
|
||||
Items: [
|
||||
{
|
||||
Id: 1,
|
||||
Fields: [
|
||||
{
|
||||
FieldName: 'ESTADO',
|
||||
Item: 'Unsigned'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(response)));
|
||||
|
||||
const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, true);
|
||||
const result = await models.Docuware.checkFile(ticketId, fileCabinetName, null, true);
|
||||
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
|
@ -59,23 +17,21 @@ describe('docuware download()', () => {
|
|||
it('should return the document data', async() => {
|
||||
const docuwareId = 1;
|
||||
const response = {
|
||||
data: {
|
||||
Items: [
|
||||
{
|
||||
Id: docuwareId,
|
||||
Fields: [
|
||||
{
|
||||
FieldName: 'ESTADO',
|
||||
Item: 'Firmado'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Items: [
|
||||
{
|
||||
Id: docuwareId,
|
||||
Fields: [
|
||||
{
|
||||
FieldName: 'ESTADO',
|
||||
Item: 'Firmado'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(response)));
|
||||
spyOn(docuwareModel, 'get').and.returnValue((new Promise(resolve => resolve(response))));
|
||||
|
||||
const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, true);
|
||||
const result = await models.Docuware.checkFile(ticketId, fileCabinetName, null, true);
|
||||
|
||||
expect(result.id).toEqual(docuwareId);
|
||||
});
|
||||
|
|
|
@ -0,0 +1,135 @@
|
|||
const axios = require('axios');
|
||||
const models = require('vn-loopback/server/server').models;
|
||||
|
||||
describe('Docuware core', () => {
|
||||
beforeAll(() => {
|
||||
process.env.NODE_ENV = 'testing';
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
delete process.env.NODE_ENV;
|
||||
});
|
||||
|
||||
describe('getOptions()', () => {
|
||||
it('should return url and headers', async() => {
|
||||
const result = await models.Docuware.getOptions();
|
||||
|
||||
expect(result.url).toBeDefined();
|
||||
expect(result.headers).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDialog()', () => {
|
||||
it('should return dialogId', async() => {
|
||||
const dialogs = {
|
||||
data: {
|
||||
Dialog: [
|
||||
{
|
||||
DisplayName: 'find',
|
||||
Id: 'getDialogTest'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs)));
|
||||
const result = await models.Docuware.getDialog('deliveryNote', 'find', 'randomFileCabinetId');
|
||||
|
||||
expect(result).toEqual('getDialogTest');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileCabinet()', () => {
|
||||
it('should return fileCabinetId', async() => {
|
||||
const code = 'deliveryNote';
|
||||
const docuwareInfo = await models.Docuware.findOne({
|
||||
where: {
|
||||
code
|
||||
}
|
||||
});
|
||||
const dialogs = {
|
||||
data: {
|
||||
FileCabinet: [
|
||||
{
|
||||
Name: docuwareInfo.fileCabinetName,
|
||||
Id: 'getFileCabinetTest'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs)));
|
||||
const result = await models.Docuware.getFileCabinet(code);
|
||||
|
||||
expect(result).toEqual('getFileCabinetTest');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get()', () => {
|
||||
it('should return data without parse', async() => {
|
||||
spyOn(models.Docuware, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
const data = {
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
};
|
||||
spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(data)));
|
||||
const result = await models.Docuware.get('deliveryNote');
|
||||
|
||||
expect(result.id).toEqual(1);
|
||||
});
|
||||
|
||||
it('should return data with parse', async() => {
|
||||
spyOn(models.Docuware, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
const data = {
|
||||
data: {
|
||||
Items: [{
|
||||
Fields: [
|
||||
{
|
||||
ItemElementName: 'integer',
|
||||
FieldLabel: 'firstRequiredField',
|
||||
Item: 1
|
||||
},
|
||||
{
|
||||
ItemElementName: 'string',
|
||||
FieldLabel: 'secondRequiredField',
|
||||
Item: 'myName'
|
||||
},
|
||||
{
|
||||
ItemElementName: 'integer',
|
||||
FieldLabel: 'notRequiredField',
|
||||
Item: 2
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
||||
};
|
||||
const parse = {
|
||||
'firstRequiredField': 'id',
|
||||
'secondRequiredField': 'name',
|
||||
};
|
||||
spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(data)));
|
||||
const [result] = await models.Docuware.get('deliveryNote', null, parse);
|
||||
|
||||
expect(result.id).toEqual(1);
|
||||
expect(result.name).toEqual('myName');
|
||||
expect(result.notRequiredField).not.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getById()', () => {
|
||||
it('should return data', async() => {
|
||||
spyOn(models.Docuware, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random()))));
|
||||
const data = {
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
};
|
||||
spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(data)));
|
||||
const result = await models.Docuware.getById('deliveryNote', 1);
|
||||
|
||||
expect(result.id).toEqual(1);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -39,7 +39,7 @@ describe('docuware download()', () => {
|
|||
spyOn(docuwareModel, 'checkFile').and.returnValue({});
|
||||
spyOn(axios, 'get').and.returnValue(new stream.PassThrough({objectMode: true}));
|
||||
|
||||
const result = await models.Docuware.download(ctx, ticketId, fileCabinetName);
|
||||
const result = await models.Docuware.download(ticketId, fileCabinetName);
|
||||
|
||||
expect(result[1]).toEqual('application/pdf');
|
||||
expect(result[2]).toEqual(`filename="${ticketId}.pdf"`);
|
||||
|
|
|
@ -2,7 +2,7 @@ ALTER TABLE `vn`.`docuware` ADD dmsTypeFk INT(11) DEFAULT NULL NULL;
|
|||
ALTER TABLE `vn`.`docuware` ADD CONSTRAINT docuware_FK FOREIGN KEY (dmsTypeFk) REFERENCES `vn`.`dmsType`(id) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
INSERT INTO `vn`.`docuware` (code, fileCabinetName, `action`, dialogName, findById, dmsTypeFk)
|
||||
VALUES
|
||||
('hr', 'RRHH', 'find', 'Búsqueda', 'N__DOCUMENTO', NULL);
|
||||
('hr', 'RRHH', 'find', 'Búsqueda', 'N__DOCUMENTO', NULL); -- set dmsTypeFk 3 when deploy in production
|
||||
|
||||
INSERT INTO `salix`.`url` (appName, environment, url)
|
||||
VALUES
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('docuwareDownload', {
|
||||
Self.remoteMethod('docuwareDownload', {
|
||||
description: 'Download a ticket delivery note document',
|
||||
accessType: 'READ',
|
||||
accepts: [
|
||||
|
@ -31,7 +31,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.docuwareDownload = async function(ctx, id) {
|
||||
Self.docuwareDownload = async id => {
|
||||
const filter = {
|
||||
condition: [
|
||||
{
|
||||
|
@ -50,6 +50,6 @@ module.exports = Self => {
|
|||
}
|
||||
]
|
||||
};
|
||||
return await Self.app.models.Docuware.download(id, 'deliveryNote', filter);
|
||||
return Self.app.models.Docuware.download(id, 'deliveryNote', filter);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('docuwareDownload', {
|
||||
Self.remoteMethod('docuwareDownload', {
|
||||
description: 'Download a worker document',
|
||||
accessType: 'READ',
|
||||
accepts: [
|
||||
|
@ -31,7 +31,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.docuwareDownload = async function(ctx, id) {
|
||||
Self.docuwareDownload = async id => {
|
||||
const filter = {
|
||||
condition: [
|
||||
{
|
||||
|
@ -40,6 +40,6 @@ module.exports = Self => {
|
|||
}
|
||||
]
|
||||
};
|
||||
return await Self.app.models.Docuware.download(id, 'hr', filter);
|
||||
return Self.app.models.Docuware.download(id, 'hr', filter);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -62,8 +62,8 @@ module.exports = Self => {
|
|||
where: {code: 'hr', action: 'find'}
|
||||
});
|
||||
const docuwareDmsType = docuware.dmsTypeFk;
|
||||
let workerDocuware;
|
||||
if (!(docuwareDmsType && !await models.DmsType.hasReadRole(ctx, docuwareDmsType))) {
|
||||
let workerDocuware = [];
|
||||
if (!docuwareDmsType || (docuwareDmsType && await models.DmsType.hasReadRole(ctx, docuwareDmsType))) {
|
||||
const worker = await models.Worker.findById(id, {fields: ['fi', 'firstName', 'lastName']});
|
||||
const docuwareParse = {
|
||||
'Filename': 'dmsFk',
|
||||
|
@ -86,7 +86,6 @@ module.exports = Self => {
|
|||
document = Object.assign(document, defaultData);
|
||||
}
|
||||
}
|
||||
|
||||
return workerDms.concat(workerDocuware);
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue