2022-02-15 15:48:37 +00:00
|
|
|
const got = require('got');
|
2022-02-16 13:57:47 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2022-02-15 15:48:37 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2022-02-16 13:57:47 +00:00
|
|
|
const {promisify} = require('util');
|
|
|
|
const nodeStream = require('stream');
|
2022-02-15 15:48:37 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('download', {
|
2022-02-16 13:57:47 +00:00
|
|
|
description: 'Download an docuware PDF',
|
2022-02-15 15:48:37 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
2022-02-16 13:57:47 +00:00
|
|
|
arg: 'id',
|
2022-02-15 15:48:37 +00:00
|
|
|
type: 'number',
|
2022-02-16 13:57:47 +00:00
|
|
|
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'}
|
|
|
|
}
|
2022-02-15 15:48:37 +00:00
|
|
|
],
|
|
|
|
http: {
|
2022-02-16 13:57:47 +00:00
|
|
|
path: `/:id/download`,
|
|
|
|
verb: 'GET'
|
2022-02-15 15:48:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
Self.download = async function(ctx, id) {
|
2022-02-15 15:48:37 +00:00
|
|
|
// const fileCabinet = 'ad2c49df-8976-4941-bb19-9b30685f14a4';
|
|
|
|
// hay que crear tambien una busqueda por cada fileCabinet
|
|
|
|
// columnas necesarias. seccion, fileCabinet, DBName, dialog
|
2022-02-16 13:57:47 +00:00
|
|
|
/* const myUserId = ctx.req.accessToken.userId;
|
|
|
|
if (!myUserId)
|
|
|
|
throw new UserError(`You don't have enough privileges`);*/
|
|
|
|
|
2022-02-15 15:48:37 +00:00
|
|
|
const models = Self.app.models;
|
2022-02-16 13:57:47 +00:00
|
|
|
const docuwareConfig = await models.DocuwareConfig.findOne();
|
2022-02-15 15:48:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
2022-02-16 13:57:47 +00:00
|
|
|
// get fileCabinetId
|
2022-02-15 15:48:37 +00:00
|
|
|
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options).json();
|
|
|
|
const fileCabinetId = fileCabinetResponse.FileCabinet.find(dialogs => dialogs.Name === fileCabinetName).Id;
|
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
// get dialog
|
2022-02-15 15:48:37 +00:00
|
|
|
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options).json();
|
|
|
|
const dialogId = dialogResponse.Dialog.find(dialogs => dialogs.DisplayName === 'find').Id;
|
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
// get docuwareID
|
2022-02-15 15:48:37 +00:00
|
|
|
const docuwareOptions = {
|
|
|
|
'headers': {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Cookie': cookie
|
|
|
|
},
|
2022-02-16 13:57:47 +00:00
|
|
|
'body': JSON.stringify({'Condition': [{DBName: find, Value: [0]}]})
|
2022-02-15 15:48:37 +00:00
|
|
|
};
|
2022-02-16 13:57:47 +00:00
|
|
|
const response = await got.post(
|
|
|
|
`${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`,
|
|
|
|
docuwareOptions
|
|
|
|
);
|
2022-02-15 15:48:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
try {
|
|
|
|
// save file
|
|
|
|
const ticket = await models.Ticket.findById(id);
|
2022-02-15 15:48:37 +00:00
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-02-15 15:48:37 +00:00
|
|
|
};
|
|
|
|
};
|