const got = require('got'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('checkFile', { description: 'Download an docuware PDF', accessType: 'READ', accepts: [ { arg: 'id', type: 'String', description: 'The invoice id', http: {source: 'path'} } ], returns: { type: 'boolean', root: true }, http: { path: `/:id/checkFile`, verb: 'GET' } }); Self.checkFile = 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 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: [id]}]}) }; const response = await got.post( `${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`, docuwareOptions ); try { JSON.parse(response.body).Items[0].Id; return true; } catch (error) { return false; } }; };