From 8b35c32583e3d71cf5ee0a39f3c3ec9f22738c83 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 10 Jun 2024 09:54:20 +0200 Subject: [PATCH] feat(salix): refs #5926 check if device is available --- back/methods/docuware/core.js | 41 +++++++++++++------ back/methods/docuware/specs/core.spec.js | 24 ++++++++++- back/methods/docuware/upload.js | 4 +- .../back/methods/ticket/docuwareUpload.js | 2 +- .../back/methods/worker/docuwareUpload.js | 8 ++-- 5 files changed, 56 insertions(+), 23 deletions(-) diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 7db5836f0..01a3683f7 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -1,21 +1,36 @@ const axios = require('axios'); - +const env = process.env.NODE_ENV; +const {existsSync} = require('fs'); module.exports = Self => { /** * Returns templateJSON * - * @param {string} config - The config as template upload + * @param {object} config - The path for real config file + * @return {boolean} - The template parse + */ + Self.hasDeviceReady = config => { + let isDeviceConfigured = false; + + if (!config) + config = existsSync(`../docuware.${env}.json`) ?? {}; + isDeviceConfigured = !!config?.device; + return isDeviceConfigured; + }; + /** + * Returns templateJSON + * + * @param {string} fields - The config as template upload * @return {object} - The template parse */ - Self.buildTemplateJSON = config => { + Self.buildTemplateJSON = fields => { const templateJson = { 'Fields': [] }; - templateJson.Fields = Object.keys(config).map(fieldName => ({ + templateJson.Fields = Object.keys(fields).map(fieldName => ({ 'FieldName': fieldName, - 'ItemElementName': config[fieldName].type, - 'Item': config[fieldName].value + 'ItemElementName': fields[fieldName].type, + 'Item': fields[fieldName].value })); return templateJson; @@ -24,16 +39,16 @@ module.exports = Self => { * Returns upload options * * @param {string} value - The document value - * @param {Object} configTemplate - The config as template upload + * @param {Object} template - The config as template upload * @param {string} fileName - The document fileName with extension * @param {object} options - The options * @return {object} - The options with headers */ - Self.uploadOptions = async(value, configTemplate, fileName = '', options) => { + Self.uploadOptions = async(value, template, fileName = '', options) => { const FormData = require('form-data'); const data = new FormData(); const docuwareOptions = options ?? await Self.getOptions(); - const templateJson = Self.buildTemplateJSON(configTemplate); + const templateJson = Self.buildTemplateJSON(template); data.append('document', JSON.stringify(templateJson), 'schema.json'); data.append('file[]', value, fileName); const uploadOptions = { @@ -196,16 +211,16 @@ module.exports = Self => { * * @param {string} id - The id * @param {string} fileCabinet - The fieldCabinet - * @param {Object} configTemplate - The config + * @param {Object} template - The config * @param {string} uri - The uri * @param {Object} options - The options */ - Self.deleteOld = async(id, fileCabinet, configTemplate, uri, options) => { + Self.deleteOld = async(id, fileCabinet, template, uri, options) => { const docuwareOptions = options ?? await Self.getOptions(); - const config = configTemplate ?? {'ESTADO': {type: 'String', value: 'Pendiente eliminar'}}; + template = template ?? {'ESTADO': {type: 'String', value: 'Pendiente eliminar'}}; const docuwareFile = await Self.checkFile(id, fileCabinet, false); if (docuwareFile) { - const deleteJson = Self.buildTemplateJSON(config); + const deleteJson = Self.buildTemplateJSON(template); const deleteUri = `${uri}/${docuwareFile.id}/Fields`; await axios.put(deleteUri, deleteJson, docuwareOptions.headers); diff --git a/back/methods/docuware/specs/core.spec.js b/back/methods/docuware/specs/core.spec.js index 60d1253ad..bdc63d830 100644 --- a/back/methods/docuware/specs/core.spec.js +++ b/back/methods/docuware/specs/core.spec.js @@ -1,7 +1,7 @@ const axios = require('axios'); const models = require('vn-loopback/server/server').models; -describe('Docuware core', () => { +fdescribe('Docuware core', () => { beforeAll(() => { process.env.NODE_ENV = 'testing'; }); @@ -10,6 +10,26 @@ describe('Docuware core', () => { delete process.env.NODE_ENV; }); + describe('hasDeviceReady()', () => { + it('should return true', async() => { + const result = await models.Docuware.hasDeviceReady({device: 'Tablet 1'}); + + expect(result).toBeTrue(); + }); + + it('should return false', async() => { + const result = await models.Docuware.hasDeviceReady({device: null}); + + expect(result).toBeFalse(); + }); + + it('should not exists return false ', async() => { + const result = await models.Docuware.hasDeviceReady(); + + expect(result).toBeFalse(); + }); + }); + describe('getOptions()', () => { it('should return url and headers', async() => { const result = await models.Docuware.getOptions(); @@ -102,7 +122,7 @@ describe('Docuware core', () => { // } // }; // spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs))); - const result = await models.Docuware.uploadOptions(1, { + const {uploadOptions: result} = await models.Docuware.uploadOptions(1, { 'N__DOCUMENTO': { type: 'string', value: '12345' diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index cddd8aff1..fdcc6bfa7 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -60,8 +60,8 @@ module.exports = Self => { const uri = Self.baseURL(docuwareOptions, fileCabinetId, 'Documents'); - // if (!isProduction(false)) - // throw new UserError('Action not allowed on the test environment'); + if (!isProduction(false) && !Self.hasDeviceReady()) + 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); diff --git a/modules/ticket/back/methods/ticket/docuwareUpload.js b/modules/ticket/back/methods/ticket/docuwareUpload.js index 7c722c17d..1229401ed 100644 --- a/modules/ticket/back/methods/ticket/docuwareUpload.js +++ b/modules/ticket/back/methods/ticket/docuwareUpload.js @@ -64,7 +64,7 @@ module.exports = Self => { }; - if (!isProduction(false)) + if (!isProduction(false) && !Self.hasDeviceReady()) throw new UserError('Action not allowed on the test environment'); // delete old diff --git a/modules/worker/back/methods/worker/docuwareUpload.js b/modules/worker/back/methods/worker/docuwareUpload.js index 8ae9c89ed..0aebb3870 100644 --- a/modules/worker/back/methods/worker/docuwareUpload.js +++ b/modules/worker/back/methods/worker/docuwareUpload.js @@ -16,9 +16,7 @@ module.exports = Self => { where: {deviceProductionFk: pdaId} } , myOptions); - // const worker = await models.Worker.findById( - // workerFk - // , myOptions); + const signPda = await models.Worker.signPdaPdf(ctx, pdaId, workerFk , myOptions); @@ -42,8 +40,8 @@ module.exports = Self => { } }; - // if (!isProduction(false)) - // throw new UserError('Action not allowed on the test environment'); + if (!isProduction(false) && !Self.hasDeviceReady()) + throw new UserError('Action not allowed on the test environment'); // delete old await models.Docuware.deleteOld(id, fileCabinet, uri);