salix/back/methods/docuware/download.js

89 lines
3.2 KiB
JavaScript
Raw Normal View History

const got = require('got');
const {createWriteStream} = require('fs');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('download', {
description: 'Download an invoice PDF',
accessType: 'READ',
accepts: [
{
arg: 'ticketId',
type: 'number',
description: 'The invoiceable ticket id'
},
],
returns: {
type: 'object',
root: true
},
http: {
path: `/download`,
verb: 'POST'
}
});
Self.download = async function(ctx, ticketId) {
// const fileCabinet = 'ad2c49df-8976-4941-bb19-9b30685f14a4';
// hay que crear tambien una busqueda por cada fileCabinet
// columnas necesarias. seccion, fileCabinet, DBName, dialog
const models = Self.app.models;
const [docuwareConfig] = await Self.rawSql(`SELECT * FROM vn.docuwareConfig;`);
const docuwareInfo = await models.Docuware.findOne({
where: {
name: 'deliveryClient',
dialogName: 'findTicket'
}
});
console.log(docuwareConfig, docuwareInfo);
const docuwareUrl = docuwareConfig.url;
const cookie = docuwareConfig.token;
const fileCabinetName = docuwareInfo.fileCabinetName;
const find = docuwareInfo.find;
// get fileCabinetId
const options = {
'headers': {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Cookie': cookie
}
};
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options).json();
const fileCabinetId = fileCabinetResponse.FileCabinet.find(dialogs => dialogs.Name === fileCabinetName).Id;
// get dialogs
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options).json();
console.log(dialogResponse);
const dialogId = dialogResponse.Dialog.find(dialogs => dialogs.DisplayName === 'find').Id;
console.log(fileCabinetId, dialogId);
/*
// get DocuwareID
const docuwareOptions = {
'headers': {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Cookie': cookie
},
'body': JSON.stringify({'Condition': [{DBName: find, Value: [ticketId]}]})
};
const response = await got.post(`${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`, docuwareOptions);
const docuwareId = JSON.parse(response.body).Items[0].Id;
// download file
const downloadUrl = `${docuwareUrl}/FileCabinets/${fileCabinetId}/Documents/${docuwareId}/FileDownload?targetFileType=Auto&keepAnnotations=false`;
const downloadOptions = {
'headers': {
'Cookie': cookie
}
};
const file = await got.stream(downloadUrl, downloadOptions).pipe(createWriteStream(`${ticketId}_${docuwareId}.pdf`));
// return [file, 'application/pdf', `filename="${ticketId}_${docuwareId}.pdf"`];*/
};
};