3352-docuware #880
|
@ -0,0 +1,93 @@
|
|||
const got = require('got');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('checkFile', {
|
||||
description: 'Check if exist docuware file',
|
||||
accessType: 'READ',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'id',
|
||||
type: 'number',
|
||||
description: 'The id',
|
||||
http: {source: 'path'}
|
||||
},
|
||||
{
|
||||
arg: 'fileCabinet',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The fileCabinet name'
|
||||
},
|
||||
{
|
||||
arg: 'dialog',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The dialog name'
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
type: 'boolean',
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/:id/checkFile`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.checkFile = async function(ctx, id, fileCabinet, dialog) {
|
||||
const myUserId = ctx.req.accessToken.userId;
|
||||
if (!myUserId)
|
||||
return false;
|
||||
|
||||
const models = Self.app.models;
|
||||
const docuwareConfig = await models.DocuwareConfig.findOne();
|
||||
const docuwareInfo = await models.Docuware.findOne({
|
||||
where: {
|
||||
code: fileCabinet,
|
||||
dialogName: dialog
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
const searchFilter = {
|
||||
condition: [
|
||||
{
|
||||
DBName: find,
|
||||
Value: [id]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
try {
|
||||
// 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
|
||||
Object.assign(options, {'body': JSON.stringify(searchFilter)});
|
||||
const response = await got.post(
|
||||
`${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`, options);
|
||||
JSON.parse(response.body).Items[0].Id;
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
};
|
|
@ -0,0 +1,120 @@
|
|||
/* eslint max-len: ["error", { "code": 180 }]*/
|
||||
const got = require('got');
|
||||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('download', {
|
||||
description: 'Download an docuware PDF',
|
||||
accessType: 'READ',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'id',
|
||||
type: 'number',
|
||||
description: 'The id',
|
||||
http: {source: 'path'}
|
||||
},
|
||||
{
|
||||
arg: 'fileCabinet',
|
||||
type: 'string',
|
||||
description: 'The id',
|
||||
http: {source: 'path'}
|
||||
},
|
||||
{
|
||||
arg: 'dialog',
|
||||
type: 'string',
|
||||
description: 'The id',
|
||||
http: {source: 'path'}
|
||||
}
|
||||
],
|
||||
returns: [
|
||||
{
|
||||
arg: 'body',
|
||||
type: 'file',
|
||||
root: true
|
||||
}, {
|
||||
arg: 'Content-Type',
|
||||
type: 'string',
|
||||
http: {target: 'header'}
|
||||
}, {
|
||||
arg: 'Content-Disposition',
|
||||
type: 'string',
|
||||
http: {target: 'header'}
|
||||
}
|
||||
],
|
||||
http: {
|
||||
path: `/:id/download/:fileCabinet/:dialog`,
|
||||
verb: 'GET'
|
||||
}
|
||||
});
|
||||
|
||||
Self.download = async function(ctx, id, fileCabinet, dialog) {
|
||||
const myUserId = ctx.req.accessToken.userId;
|
||||
if (!myUserId)
|
||||
throw new UserError(`You don't have enough privileges`);
|
||||
|
||||
const models = Self.app.models;
|
||||
const docuwareConfig = await models.DocuwareConfig.findOne();
|
||||
const docuwareInfo = await models.Docuware.findOne({
|
||||
where: {
|
||||
code: fileCabinet,
|
||||
dialogName: dialog
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
const searchFilter = {
|
||||
condition: [
|
||||
{
|
||||
DBName: find,
|
||||
Value: [id]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
try {
|
||||
// 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
|
||||
Object.assign(options, {'body': JSON.stringify(searchFilter)});
|
||||
const response = await got.post(`${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`, options);
|
||||
const docuwareId = JSON.parse(response.body).Items[0].Id;
|
||||
|
||||
// download & save file
|
||||
const fileName = `filename="${id}.pdf"`;
|
||||
const contentType = 'application/pdf';
|
||||
const downloadUri = `${docuwareUrl}/FileCabinets/${fileCabinetId}/Documents/${docuwareId}/FileDownload?targetFileType=Auto&keepAnnotations=false`;
|
||||
const downloadOptions = {
|
||||
'headers': {
|
||||
'Cookie': cookie
|
||||
}
|
||||
};
|
||||
|
||||
const stream = got.stream(downloadUri, downloadOptions);
|
||||
|
||||
return [stream, contentType, fileName];
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT')
|
||||
throw new UserError('The DOCUWARE PDF document does not exists');
|
||||
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
};
|
|
@ -0,0 +1,64 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
const got = require('got');
|
||||
|
||||
describe('docuware download()', () => {
|
||||
const ticketId = 1;
|
||||
const userId = 9;
|
||||
const ctx = {
|
||||
req: {
|
||||
|
||||
accessToken: {userId: userId},
|
||||
headers: {origin: 'http://localhost:5000'},
|
||||
}
|
||||
};
|
||||
|
||||
const fileCabinetName = 'deliveryClientTest';
|
||||
const dialogDisplayName = 'find';
|
||||
const dialogName = 'findTest';
|
||||
|
||||
const gotGetResponse = {
|
||||
body: JSON.stringify(
|
||||
{
|
||||
FileCabinet: [
|
||||
{Id: 12, Name: fileCabinetName}
|
||||
],
|
||||
Dialog: [
|
||||
{Id: 34, DisplayName: dialogDisplayName}
|
||||
]
|
||||
})
|
||||
};
|
||||
|
||||
it('should return exist file in docuware', async() => {
|
||||
const gotPostResponse = {
|
||||
body: JSON.stringify(
|
||||
{
|
||||
Items: [
|
||||
{Id: 56}
|
||||
],
|
||||
})
|
||||
};
|
||||
|
||||
spyOn(got, 'get').and.returnValue(new Promise(resolve => resolve(gotGetResponse)));
|
||||
spyOn(got, 'post').and.returnValue(new Promise(resolve => resolve(gotPostResponse)));
|
||||
|
||||
const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, dialogName);
|
||||
|
||||
expect(result).toEqual(true);
|
||||
});
|
||||
|
||||
it('should return not exist file in docuware', async() => {
|
||||
const gotPostResponse = {
|
||||
body: JSON.stringify(
|
||||
{
|
||||
Items: [],
|
||||
})
|
||||
};
|
||||
|
||||
spyOn(got, 'get').and.returnValue(new Promise(resolve => resolve(gotGetResponse)));
|
||||
spyOn(got, 'post').and.returnValue(new Promise(resolve => resolve(gotPostResponse)));
|
||||
|
||||
const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, dialogName);
|
||||
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,50 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
const got = require('got');
|
||||
const stream = require('stream');
|
||||
|
||||
describe('docuware download()', () => {
|
||||
const userId = 9;
|
||||
const ticketId = 1;
|
||||
const ctx = {
|
||||
req: {
|
||||
|
||||
accessToken: {userId: userId},
|
||||
headers: {origin: 'http://localhost:5000'},
|
||||
}
|
||||
};
|
||||
|
||||
it('should return the downloaded file name', async() => {
|
||||
const fileCabinetName = 'deliveryClientTest';
|
||||
const dialogDisplayName = 'find';
|
||||
const dialogName = 'findTest';
|
||||
const gotGetResponse = {
|
||||
body: JSON.stringify(
|
||||
{
|
||||
FileCabinet: [
|
||||
{Id: 12, Name: fileCabinetName}
|
||||
],
|
||||
Dialog: [
|
||||
{Id: 34, DisplayName: dialogDisplayName}
|
||||
]
|
||||
})
|
||||
};
|
||||
|
||||
const gotPostResponse = {
|
||||
body: JSON.stringify(
|
||||
{
|
||||
Items: [
|
||||
{Id: 56}
|
||||
],
|
||||
})
|
||||
};
|
||||
|
||||
spyOn(got, 'get').and.returnValue(new Promise(resolve => resolve(gotGetResponse)));
|
||||
spyOn(got, 'post').and.returnValue(new Promise(resolve => resolve(gotPostResponse)));
|
||||
spyOn(got, 'stream').and.returnValue(new stream.PassThrough({objectMode: true}));
|
||||
|
||||
const result = await models.Docuware.download(ctx, ticketId, fileCabinetName, dialogName);
|
||||
|
||||
expect(result[1]).toEqual('application/pdf');
|
||||
expect(result[2]).toEqual(`filename="${ticketId}.pdf"`);
|
||||
});
|
||||
});
|
|
@ -44,6 +44,12 @@
|
|||
"DmsType": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"Docuware": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"DocuwareConfig": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"EmailUser": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"name": "DocuwareConfig",
|
||||
"description": "Docuware config",
|
||||
"base": "VnModel",
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "docuwareConfig"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "number",
|
||||
"id": true,
|
||||
"description": "Identifier"
|
||||
},
|
||||
"url": {
|
||||
"type": "string"
|
||||
},
|
||||
"token": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"acls": [
|
||||
{
|
||||
"property": "*",
|
||||
"accessType": "*",
|
||||
"principalType": "ROLE",
|
||||
"principalId": "$everyone",
|
||||
"permission": "ALLOW"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module.exports = Self => {
|
||||
require('../methods/docuware/download')(Self);
|
||||
require('../methods/docuware/checkFile')(Self);
|
||||
};
|
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
"name": "Docuware",
|
||||
"description": "Docuware sections",
|
||||
"base": "VnModel",
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "docuware"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "number",
|
||||
"id": true,
|
||||
"description": "Identifier"
|
||||
},
|
||||
"code": {
|
||||
"type": "string"
|
||||
},
|
||||
"fileCabinetName": {
|
||||
"type": "string"
|
||||
},
|
||||
"dialogName": {
|
||||
"type": "string"
|
||||
},
|
||||
"find": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"acls": [
|
||||
{
|
||||
"property": "*",
|
||||
"accessType": "*",
|
||||
"principalType": "ROLE",
|
||||
"principalId": "$everyone",
|
||||
"permission": "ALLOW"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
INSERT INTO salix.ACL
|
||||
(model, property, accessType, permission, principalType, principalId)
|
||||
VALUES('Docuware', '*', '*', 'ALLOW', 'ROLE', 'employee');
|
|
@ -0,0 +1,11 @@
|
|||
CREATE TABLE `vn`.`docuware` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`code` varchar(50) NULL,
|
||||
`fileCabinetName` varchar(50) NULL,
|
||||
`dialogName` varchar(255) DEFAULT NULL,
|
||||
`find` varchar(50) DEFAULT NULL
|
||||
);
|
||||
|
||||
INSERT INTO `vn`.`docuware` (`code`, `fileCabinetName`, `dialogName` , `find`)
|
||||
VALUES
|
||||
('deliveryClient', 'Albaranes cliente', 'findTicket', 'N__ALBAR_N');
|
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE `vn`.`docuwareConfig` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`url` varchar(75) NULL,
|
||||
`token` varchar(1000) DEFAULT NULL
|
||||
);
|
||||
|
||||
INSERT INTO `vn`.`docuwareConfig` (`url`)
|
||||
VALUES
|
||||
('https://verdnatura.docuware.cloud/docuware/platform');
|
|
@ -2443,3 +2443,11 @@ INSERT INTO `bs`.`defaulter` (`clientFk`, `amount`, `created`, `defaulterSinced`
|
|||
(1103, 500, CURDATE(), CURDATE()),
|
||||
(1107, 500, CURDATE(), CURDATE()),
|
||||
(1109, 500, CURDATE(), CURDATE());
|
||||
|
||||
INSERT INTO `vn`.`docuware` (`code`, `fileCabinetName`, `dialogName` , `find`)
|
||||
VALUES
|
||||
('deliveryClientTest', 'deliveryClientTest', 'findTest', 'word');
|
||||
|
||||
INSERT INTO `vn`.`docuwareConfig` (`url`)
|
||||
VALUES
|
||||
('https://verdnatura.docuware.cloud/docuware/platform');
|
|
@ -10,7 +10,6 @@
|
|||
name="showInvoicePdf"
|
||||
translate>
|
||||
Show invoice...
|
||||
|
||||
<vn-menu vn-id="showInvoiceMenu">
|
||||
<vn-list>
|
||||
<a class="vn-item"
|
||||
|
@ -33,7 +32,6 @@
|
|||
name="sendInvoice"
|
||||
translate>
|
||||
Send invoice...
|
||||
|
||||
<vn-menu vn-id="sendInvoiceMenu">
|
||||
<vn-list>
|
||||
<vn-item
|
||||
|
|
|
@ -20,14 +20,22 @@
|
|||
<vn-menu vn-id="showDeliveryNoteMenu">
|
||||
<vn-list>
|
||||
<vn-item
|
||||
ng-if="!$ctrl.hasDocuwareFile"
|
||||
ng-click="$ctrl.showPdfDeliveryNote()"
|
||||
translate>
|
||||
Show as PDF
|
||||
as PDF
|
||||
</vn-item>
|
||||
<a class="vn-item"
|
||||
ng-if="$ctrl.hasDocuwareFile"
|
||||
href='api/Docuwares/{{$ctrl.ticket.id}}/download/deliveryClient/findTicket?access_token={{$ctrl.vnToken.token}}'
|
||||
target="_blank"
|
||||
translate>
|
||||
as PDF
|
||||
</a>
|
||||
<vn-item
|
||||
ng-click="$ctrl.showCsvDeliveryNote()"
|
||||
translate>
|
||||
Show as CSV
|
||||
as CSV
|
||||
</vn-item>
|
||||
</vn-list>
|
||||
</vn-menu>
|
||||
|
|
|
@ -84,6 +84,7 @@ class Controller extends Section {
|
|||
.then(() => {
|
||||
this.canStowaway();
|
||||
this.isTicketEditable();
|
||||
this.hasDocuware();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -122,6 +123,15 @@ class Controller extends Section {
|
|||
});
|
||||
}
|
||||
|
||||
hasDocuware() {
|
||||
const params = {
|
||||
fileCabinet: 'deliveryClient',
|
||||
dialog: 'findTicket'
|
||||
};
|
||||
this.$http.post(`Docuwares/${this.id}/checkFile`, params)
|
||||
.then(res => this.hasDocuwareFile = res.data);
|
||||
}
|
||||
|
||||
showCsvDeliveryNote() {
|
||||
this.vnReport.showCsv('delivery-note', {
|
||||
recipientId: this.ticket.client.id,
|
||||
|
|
|
@ -206,7 +206,8 @@ describe('Ticket Component vnTicketDescriptorMenu', () => {
|
|||
it('should make a query and show a success snackbar', () => {
|
||||
jest.spyOn(controller.vnApp, 'showSuccess');
|
||||
|
||||
$httpBackend.whenGET(`Tickets/16`).respond();
|
||||
$httpBackend.whenPOST(`Docuwares/${ticket.id}/checkFile`).respond();
|
||||
$httpBackend.whenGET(`Tickets/${ticket.id}`).respond();
|
||||
$httpBackend.expectPOST(`InvoiceOuts/${ticket.invoiceOut.id}/createPdf`).respond();
|
||||
controller.createPdfInvoice();
|
||||
$httpBackend.flush();
|
||||
|
@ -275,4 +276,12 @@ describe('Ticket Component vnTicketDescriptorMenu', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasDocuware()', () => {
|
||||
it('should call hasDocuware method', () => {
|
||||
$httpBackend.whenPOST(`Docuwares/${ticket.id}/checkFile`).respond();
|
||||
controller.hasDocuware();
|
||||
$httpBackend.flush();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Show Delivery Note...: Ver albarán...
|
||||
Send Delivery Note...: Enviar albarán...
|
||||
Show as PDF: Ver como PDF
|
||||
Show as CSV: Ver como CSV
|
||||
as PDF: como PDF
|
||||
as CSV: como CSV
|
||||
Send PDF: Enviar PDF
|
||||
Send CSV: Enviar CSV
|
||||
Send CSV Delivery Note: Enviar albarán en CSV
|
||||
|
|
Loading…
Reference in New Issue