feat(salix): refs #5926 #6321 refactor docuwareUpload methods

This commit is contained in:
Javier Segarra 2024-05-30 12:29:58 +02:00
parent 2d7d6230e9
commit 294fbdcbeb
4 changed files with 157 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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