feat(salix): refs #5926 check if device is available
This commit is contained in:
parent
e3679c8b9a
commit
8b35c32583
|
@ -1,21 +1,36 @@
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
|
const env = process.env.NODE_ENV;
|
||||||
|
const {existsSync} = require('fs');
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
/**
|
/**
|
||||||
* Returns templateJSON
|
* 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
|
* @return {object} - The template parse
|
||||||
*/
|
*/
|
||||||
Self.buildTemplateJSON = config => {
|
Self.buildTemplateJSON = fields => {
|
||||||
const templateJson = {
|
const templateJson = {
|
||||||
'Fields': []
|
'Fields': []
|
||||||
};
|
};
|
||||||
|
|
||||||
templateJson.Fields = Object.keys(config).map(fieldName => ({
|
templateJson.Fields = Object.keys(fields).map(fieldName => ({
|
||||||
'FieldName': fieldName,
|
'FieldName': fieldName,
|
||||||
'ItemElementName': config[fieldName].type,
|
'ItemElementName': fields[fieldName].type,
|
||||||
'Item': config[fieldName].value
|
'Item': fields[fieldName].value
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return templateJson;
|
return templateJson;
|
||||||
|
@ -24,16 +39,16 @@ module.exports = Self => {
|
||||||
* Returns upload options
|
* Returns upload options
|
||||||
*
|
*
|
||||||
* @param {string} value - The document value
|
* @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 {string} fileName - The document fileName with extension
|
||||||
* @param {object} options - The options
|
* @param {object} options - The options
|
||||||
* @return {object} - The options with headers
|
* @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 FormData = require('form-data');
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
const docuwareOptions = options ?? await Self.getOptions();
|
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('document', JSON.stringify(templateJson), 'schema.json');
|
||||||
data.append('file[]', value, fileName);
|
data.append('file[]', value, fileName);
|
||||||
const uploadOptions = {
|
const uploadOptions = {
|
||||||
|
@ -196,16 +211,16 @@ module.exports = Self => {
|
||||||
*
|
*
|
||||||
* @param {string} id - The id
|
* @param {string} id - The id
|
||||||
* @param {string} fileCabinet - The fieldCabinet
|
* @param {string} fileCabinet - The fieldCabinet
|
||||||
* @param {Object} configTemplate - The config
|
* @param {Object} template - The config
|
||||||
* @param {string} uri - The uri
|
* @param {string} uri - The uri
|
||||||
* @param {Object} options - The options
|
* @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 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);
|
const docuwareFile = await Self.checkFile(id, fileCabinet, false);
|
||||||
if (docuwareFile) {
|
if (docuwareFile) {
|
||||||
const deleteJson = Self.buildTemplateJSON(config);
|
const deleteJson = Self.buildTemplateJSON(template);
|
||||||
|
|
||||||
const deleteUri = `${uri}/${docuwareFile.id}/Fields`;
|
const deleteUri = `${uri}/${docuwareFile.id}/Fields`;
|
||||||
await axios.put(deleteUri, deleteJson, docuwareOptions.headers);
|
await axios.put(deleteUri, deleteJson, docuwareOptions.headers);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const models = require('vn-loopback/server/server').models;
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
|
||||||
describe('Docuware core', () => {
|
fdescribe('Docuware core', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
process.env.NODE_ENV = 'testing';
|
process.env.NODE_ENV = 'testing';
|
||||||
});
|
});
|
||||||
|
@ -10,6 +10,26 @@ describe('Docuware core', () => {
|
||||||
delete process.env.NODE_ENV;
|
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()', () => {
|
describe('getOptions()', () => {
|
||||||
it('should return url and headers', async() => {
|
it('should return url and headers', async() => {
|
||||||
const result = await models.Docuware.getOptions();
|
const result = await models.Docuware.getOptions();
|
||||||
|
@ -102,7 +122,7 @@ describe('Docuware core', () => {
|
||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
// spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs)));
|
// 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': {
|
'N__DOCUMENTO': {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
value: '12345'
|
value: '12345'
|
||||||
|
|
|
@ -60,8 +60,8 @@ module.exports = Self => {
|
||||||
|
|
||||||
const uri = Self.baseURL(docuwareOptions, fileCabinetId, 'Documents');
|
const uri = Self.baseURL(docuwareOptions, fileCabinetId, 'Documents');
|
||||||
|
|
||||||
// if (!isProduction(false))
|
if (!isProduction(false) && !Self.hasDeviceReady())
|
||||||
// throw new UserError('Action not allowed on the test environment');
|
throw new UserError('Action not allowed on the test environment');
|
||||||
const upload = {ctx, tabletFk, ids, myOptions, uri, fileCabinet, fileCabinetId, dialogId};
|
const upload = {ctx, tabletFk, ids, myOptions, uri, fileCabinet, fileCabinetId, dialogId};
|
||||||
await Self.app.models[model].docuwareUpload(upload);
|
await Self.app.models[model].docuwareUpload(upload);
|
||||||
|
|
||||||
|
|
|
@ -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');
|
throw new UserError('Action not allowed on the test environment');
|
||||||
|
|
||||||
// delete old
|
// delete old
|
||||||
|
|
|
@ -16,9 +16,7 @@ module.exports = Self => {
|
||||||
where: {deviceProductionFk: pdaId}
|
where: {deviceProductionFk: pdaId}
|
||||||
}
|
}
|
||||||
, myOptions);
|
, myOptions);
|
||||||
// const worker = await models.Worker.findById(
|
|
||||||
// workerFk
|
|
||||||
// , myOptions);
|
|
||||||
const signPda = await models.Worker.signPdaPdf(ctx,
|
const signPda = await models.Worker.signPdaPdf(ctx,
|
||||||
pdaId, workerFk
|
pdaId, workerFk
|
||||||
, myOptions);
|
, myOptions);
|
||||||
|
@ -42,8 +40,8 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// if (!isProduction(false))
|
if (!isProduction(false) && !Self.hasDeviceReady())
|
||||||
// throw new UserError('Action not allowed on the test environment');
|
throw new UserError('Action not allowed on the test environment');
|
||||||
|
|
||||||
// delete old
|
// delete old
|
||||||
await models.Docuware.deleteOld(id, fileCabinet, uri);
|
await models.Docuware.deleteOld(id, fileCabinet, uri);
|
||||||
|
|
Loading…
Reference in New Issue