diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 01e4eb4d16..9c89203bb9 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -1,6 +1,51 @@ const axios = require('axios'); module.exports = Self => { + /** + * Returns templateJSON + * + * @param {string} config - The config as template upload + * @return {object} - The template parse + */ + Self.buildTemplateJSON = config => { + const templateJson = { + 'Fields': [] + }; + + templateJson.Fields = Object.keys(config).map(fieldName => ({ + 'FieldName': fieldName, + 'ItemElementName': config[fieldName].type, + 'Item': config[fieldName].value + })); + + return templateJson; + }; + /** + * Returns upload options + * + * @param {string} value - The document value + * @param {Object} configTemplate - The config as template upload + * @param {object} options - The options + * @return {object} - The options with headers + */ + Self.uploadOptions = async(value, configTemplate, options) => { + const FormData = require('form-data'); + const data = new FormData(); + const docuwareOptions = options ?? await Self.getOptions(); + const templateJson = Self.buildTemplateJSON(configTemplate); + data.append('document', JSON.stringify(templateJson), 'schema.json'); + data.append('file[]', value, 'pda.pdf'); + const uploadOptions = { + headers: { + 'Content-Type': 'multipart/form-data', + 'X-File-ModifiedDate': Date.vnNew(), + 'Cookie': docuwareOptions.headers.headers.Cookie, + ...data.getHeaders() + }, + }; + return uploadOptions; + }; + /** * Returns basic headers * @@ -139,6 +184,26 @@ module.exports = Self => { return Self.get(code, filter, parse); }; + /** + * Execute detete old docuware + * + * @param {string} id - The id + * @param {string} fileCabinet - The fieldCabinet + * @param {Object} configTemplate - The config + * @param {string} uri - The uri + * @param {Object} options - The options + */ + Self.deleteOld = async(id, fileCabinet, configTemplate, uri, options) => { + const docuwareOptions = options ?? await Self.getOptions(); + const config = configTemplate ?? {'ESTADO': {type: 'String', value: 'Pendiente eliminar'}}; + const docuwareFile = await Self.checkFile(id, fileCabinet, false); + if (docuwareFile) { + const deleteJson = Self.buildTemplateJSON(config); + + const deleteUri = `${uri}/${docuwareFile.id}/Fields`; + await axios.put(deleteUri, deleteJson, docuwareOptions.headers); + } + }; /** * Returns docuware data filtered * diff --git a/back/methods/docuware/specs/core.spec.js b/back/methods/docuware/specs/core.spec.js index cdf8a3b625..0063c9a9ef 100644 --- a/back/methods/docuware/specs/core.spec.js +++ b/back/methods/docuware/specs/core.spec.js @@ -38,6 +38,92 @@ describe('Docuware core', () => { }); }); + describe('buildTemplateJSON()', () => { + it('should return buildTemplateJSON', async() => { + const config = { + 'N__DOCUMENTO': { + type: 'string', + value: '12345' + }, + 'ESTADO': { + type: 'string', + value: 'Pendiente procesar' + }, + 'FIRMA_': { + type: 'string', + value: 'Si' + }, + 'FILTRO_TABLET': { + type: 'string', + value: 'Tablet123' + } + }; + const result = await models.Docuware.buildTemplateJSON(config); + + expect(result).toEqual({ + 'Fields': [ + { + 'FieldName': 'N__DOCUMENTO', + 'ItemElementName': 'string', + 'Item': '12345', + }, + { + 'FieldName': 'ESTADO', + 'ItemElementName': 'string', + 'Item': 'Pendiente procesar', + }, + { + 'FieldName': 'FIRMA_', + 'ItemElementName': 'string', + 'Item': 'Si', + }, + { + 'FieldName': 'FILTRO_TABLET', + 'ItemElementName': 'string', + 'Item': 'Tablet123', + } + ] + }); + }); + }); + + describe('uploadOptions()', () => { + it('should return uploadOptions', async() => { + spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random())))); + + // const dialogs = { + // data: { + // Dialog: [ + // { + // DisplayName: 'find', + // Id: 'getDialogTest' + // } + // ] + // } + // }; + // spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs))); + const result = await models.Docuware.uploadOptions(1, { + 'N__DOCUMENTO': { + type: 'string', + value: '12345' + }}); + + expect(result.headers.Cookie).toEqual(null); + expect(result.headers['Content-Type']).toEqual('multipart/form-data'); + expect(result.headers['content-type']).toMatch(/^multipart\/form-data; boundary=/); + }); + }); + + describe('deleteOld()', () => { + it('should return deleteOld', async() => { + await models.Docuware.deleteOld(1, 'deliveryNote', { + 'N__DOCUMENTO': { + type: 'string', + value: '12345' + }}); + }); + }); + describe('getFileCabinet()', () => { it('should return fileCabinetId', async() => { const code = 'deliveryNote'; diff --git a/back/methods/docuware/specs/upload.spec.js b/back/methods/docuware/specs/upload.spec.js index 7b794cbb9b..3c6783e655 100644 --- a/back/methods/docuware/specs/upload.spec.js +++ b/back/methods/docuware/specs/upload.spec.js @@ -1,7 +1,7 @@ const models = require('vn-loopback/server/server').models; describe('docuware upload()', () => { - const userId = 9; + const userId = 18; const ids = [10]; const ctx = { args: {ids}, diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index 454f45f839..441052be67 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -59,11 +59,11 @@ module.exports = Self => { const uri = `${docuwareOptions.url}/FileCabinets/${fileCabinetId}/Documents`; - // if (!isProduction(false)) - // throw new UserError('Action not allowed on the test environment'); + if (!isProduction(false)) + throw new UserError('Action not allowed on the test environment'); + const upload = {ctx, tabletFk, ids, myOptions, uri, fileCabinet, fileCabinetId, dialogId}; + await Self.app.models[model].docuwareUpload(upload); - await Self.app.models[model].docuwareUpload({ctx, tabletFk, ids, myOptions, uri, docuwareOptions, dialogId}); - - return models.TicketTracking.setDelivered(ctx, ids, myOptions); + // return models.TicketTracking.setDelivered(ctx, ids, myOptions); }; };