143 lines
4.8 KiB
JavaScript
143 lines
4.8 KiB
JavaScript
const got = require('got');
|
|
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
const {promisify} = require('util');
|
|
const nodeStream = require('stream');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('download', {
|
|
description: 'Download an docuware PDF',
|
|
accessType: 'READ',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'The ticket 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`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.download = async function(ctx, id) {
|
|
// const fileCabinet = 'ad2c49df-8976-4941-bb19-9b30685f14a4';
|
|
// hay que crear tambien una busqueda por cada fileCabinet
|
|
// columnas necesarias. seccion, fileCabinet, DBName, 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: {
|
|
name: 'deliveryClient',
|
|
dialogName: 'findTicket'
|
|
}
|
|
});
|
|
|
|
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
|
|
}
|
|
};
|
|
// get fileCabinetId
|
|
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options).json();
|
|
const fileCabinetId = fileCabinetResponse.FileCabinet.find(dialogs => dialogs.Name === fileCabinetName).Id;
|
|
|
|
// get dialog
|
|
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options).json();
|
|
const dialogId = dialogResponse.Dialog.find(dialogs => dialogs.DisplayName === 'find').Id;
|
|
|
|
// get docuwareID
|
|
const docuwareOptions = {
|
|
'headers': {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'Cookie': cookie
|
|
},
|
|
'body': JSON.stringify({'Condition': [{DBName: find, Value: [0]}]})
|
|
};
|
|
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
|
|
}
|
|
};
|
|
|
|
try {
|
|
// save file
|
|
const ticket = await models.Ticket.findById(id);
|
|
|
|
const shipped = ticket.shipped;
|
|
const year = shipped.getFullYear().toString();
|
|
const month = (shipped.getMonth() + 1).toString();
|
|
const day = shipped.getDate().toString();
|
|
const fileName = `${year}${id}.pdf`;
|
|
|
|
const container = await models.DocuwareContainer.container(year);
|
|
const rootPath = container.client.root;
|
|
const src = path.join(rootPath, year, month, day);
|
|
const fileSrc = path.join(src, fileName);
|
|
|
|
await fs.mkdir(src, {recursive: true});
|
|
|
|
const pipeline = promisify(nodeStream.pipeline);
|
|
await pipeline(
|
|
got.stream(downloadUrl, downloadOptions),
|
|
fs.createWriteStream(fileSrc)
|
|
);
|
|
|
|
// open file
|
|
const file = {
|
|
path: fileSrc,
|
|
contentType: 'application/pdf',
|
|
name: fileName
|
|
};
|
|
|
|
await fs.access(file.path);
|
|
let stream = fs.createReadStream(file.path);
|
|
|
|
return [stream, file.contentType, `filename="${file.name}"`];
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT')
|
|
throw new UserError('The DOCUWARE PDF document does not exists');
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
};
|