feat(salix): refs #5926 check if device is available

This commit is contained in:
Javier Segarra 2024-06-10 09:54:20 +02:00
parent e3679c8b9a
commit 8b35c32583
5 changed files with 56 additions and 23 deletions

View File

@ -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);

View File

@ -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'

View File

@ -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);

View File

@ -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

View File

@ -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);