From 11381258cc7adef09e4cbc94c53c0f1fa559a217 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 30 May 2024 12:31:28 +0200 Subject: [PATCH] feat(salix): refs #5926 #6321 ruse core methods --- back/methods/docuware/upload.js | 2 +- db/dump/fixtures.before.sql | 1 - .../back/methods/ticket/docuwareUpload.js | 133 +++++++++--------- .../back/methods/worker/docuwareUpload.js | 81 ++++------- 4 files changed, 96 insertions(+), 121 deletions(-) diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index 441052be6..39d56c15b 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -1,5 +1,5 @@ const UserError = require('vn-loopback/util/user-error'); -// const isProduction = require('vn-loopback/server/boot/isProduction'); +const isProduction = require('vn-loopback/server/boot/isProduction'); module.exports = Self => { Self.remoteMethodCtx('upload', { diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index da9b7c9c6..9d0d9e8e0 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -3788,7 +3788,6 @@ INSERT INTO vn.workerTeam(id, team, workerFk) VALUES (8, 1, 19); -INSERT INTO vn.workCenter (id, name, payrollCenterFk, counter, warehouseFk, street, geoFk, deliveryManAdjustment) INSERT INTO vn.workCenter (id, name, payrollCenterFk, counter, warehouseFk, street, geoFk, deliveryManAdjustment) VALUES(100, 'workCenterOne', 1, NULL, 1, 'gotham', NULL, NULL); diff --git a/modules/ticket/back/methods/ticket/docuwareUpload.js b/modules/ticket/back/methods/ticket/docuwareUpload.js index 0b77b27c1..1433dcf66 100644 --- a/modules/ticket/back/methods/ticket/docuwareUpload.js +++ b/modules/ticket/back/methods/ticket/docuwareUpload.js @@ -3,7 +3,7 @@ const {models} = require('vn-loopback/server/server'); const UserError = require('vn-loopback/util/user-error'); const isProduction = require('vn-loopback/server/boot/isProduction'); module.exports = Self => { - Self.docuwareUpload = async({ctx, tabletFk, ids: ticketIds, myOptions, uri, docuwareOptions, dialogId}) => { + Self.docuwareUpload = async({ctx, tabletFk, ids: ticketIds, myOptions, uri, dialogId}) => { for (id of ticketIds) { // get delivery note ctx.args.id = id; @@ -22,83 +22,78 @@ module.exports = Self => { }, myOptions); // upload file - const templateJson = { - 'Fields': [ - { - 'FieldName': 'N__ALBAR_N', - 'ItemElementName': 'string', - 'Item': id, - }, - { - 'FieldName': 'CIF_PROVEEDOR', - 'ItemElementName': 'string', - 'Item': ticket.client().fi, - }, - { - 'FieldName': 'CODIGO_PROVEEDOR', - 'ItemElementName': 'string', - 'Item': ticket.client().id, - }, - { - 'FieldName': 'NOMBRE_PROVEEDOR', - 'ItemElementName': 'string', - 'Item': ticket.client().name + ' - ' + id, - }, - { - 'FieldName': 'FECHA_FACTURA', - 'ItemElementName': 'date', - 'Item': ticket.shipped, - }, - { - 'FieldName': 'TOTAL_FACTURA', - 'ItemElementName': 'Decimal', - 'Item': ticket.totalWithVat, - }, - { - 'FieldName': 'ESTADO', - 'ItemElementName': 'string', - 'Item': 'Pendiente procesar', - }, - { - 'FieldName': 'FIRMA_', - 'ItemElementName': 'string', - 'Item': 'Si', - }, - { - 'FieldName': 'FILTRO_TABLET', - 'ItemElementName': 'string', - 'Item': tabletFk, - } - ] + const configTemplate = { + + 'N__ALBAR_N': { + type: 'string', + value: id, + }, + 'CIF_PROVEEDOR': { + type: 'string', + value: ticket.client().fi, + }, + 'CODIGO_PROVEEDOR': { + type: 'string', + value: ticket.client().id, + }, + 'NOMBRE_PROVEEDOR': { + type: 'string', + value: ticket.client().name + ' - ' + id, + }, + 'FECHA_FACTURA': { + type: 'date', + value: ticket.shipped, + }, + 'TOTAL_FACTURA': { + type: 'Decimal', + value: ticket.totalWithVat, + }, + 'ESTADO': { + type: 'string', + value: 'Pendiente procesar', + }, + 'FIRMA_': { + type: 'string', + value: 'Si', + }, + 'FILTRO_TABLET': { + type: 'string', + value: tabletFk, + } + }; if (!isProduction(false)) throw new UserError('Action not allowed on the test environment'); // delete old - const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false); - if (docuwareFile) { - const deleteJson = { - 'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}] - }; - const deleteUri = `${uri}/${docuwareFile.id}/Fields`; - await axios.put(deleteUri, deleteJson, docuwareOptions.headers); - } + await models.Docuware.deleteOld(id, fileCabinet, uri); + + // const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false); + // if (docuwareFile) { + // const deleteJson = { + // 'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}] + // }; + // const deleteUri = `${uri}/${docuwareFile.id}/Fields`; + // await axios.put(deleteUri, deleteJson, docuwareOptions.headers); + // } const uploadUri = `${uri}?StoreDialogId=${dialogId}`; - const FormData = require('form-data'); - const data = new FormData(); + const uploadOptions = models.Docuware.uploadOptions(deliveryNote[0], configTemplate); - data.append('document', JSON.stringify(templateJson), 'schema.json'); - data.append('file[]', deliveryNote[0], 'file.pdf'); - const uploadOptions = { - headers: { - 'Content-Type': 'multipart/form-data', - 'X-File-ModifiedDate': Date.vnNew(), - 'Cookie': docuwareOptions.headers.headers.Cookie, - ...data.getHeaders() - }, - }; + // const FormData = require('form-data'); + // const data = new FormData(); + + // data.append('document', JSON.stringify(templateJson), 'schema.json'); + // data.append('file[]', deliveryNote[0], 'file.pdf'); + // const uploadOptions = { + // headers: { + // 'Content-Type': 'multipart/form-data', + // 'X-File-ModifiedDate': Date.vnNew(), + // 'Cookie': docuwareOptions.headers.headers.Cookie, + // ...data.getHeaders() + // }, + // }; try { await axios.post(uploadUri, data, uploadOptions); diff --git a/modules/worker/back/methods/worker/docuwareUpload.js b/modules/worker/back/methods/worker/docuwareUpload.js index 7be8ccd66..24afa8465 100644 --- a/modules/worker/back/methods/worker/docuwareUpload.js +++ b/modules/worker/back/methods/worker/docuwareUpload.js @@ -3,79 +3,60 @@ const {models} = require('vn-loopback/server/server'); const UserError = require('vn-loopback/util/user-error'); const isProduction = require('vn-loopback/server/boot/isProduction'); module.exports = Self => { - Self.docuwareUpload = async({ctx, tabletFk, ids: id, myOptions, uri, docuwareOptions, dialogId}) => { + Self.docuwareUpload = async({ctx, tabletFk, ids: id, myOptions, uri, fileCabinet, dialogId}) => { // get delivery note const pdaId = id[0]; ctx.args.id = pdaId; delete ctx.args.ids; - // get ticket data - const pda = await models.DeviceProduction.findById(pdaId, myOptions); - const user = await models.UserConfig.find({where: {tabletFk}, fields: ['userFk']}, myOptions); + // // get ticket data + // const pda = await models.DeviceProduction.findById(pdaId, myOptions); + // const user = await models.UserConfig.find({where: {tabletFk}, fields: ['userFk']}, myOptions); // upload file const signPda = await models.Worker.signPdaPdf(ctx, pdaId , myOptions); - const templateJson = { - 'Fields': [ - { - 'FieldName': 'N__DOCUMENTO', - 'ItemElementName': 'string', - 'Item': id, - }, - { - 'FieldName': 'ESTADO', - 'ItemElementName': 'string', - 'Item': 'Pendiente procesar', - }, - { - 'FieldName': 'FIRMA_', - 'ItemElementName': 'string', - 'Item': 'Si', - }, - { - 'FieldName': 'FILTRO_TABLET', - 'ItemElementName': 'string', - 'Item': tabletFk, - } - ] + const configTemplate = { + 'N__DOCUMENTO': { + type: 'String', + value: pdaId + }, + 'ESTADO': { + type: 'String', + value: 'Pendiente procesar' + }, + 'FIRMA_': { + type: 'String', + value: 'Si' + }, + 'FILTRO_TABLET': { + type: 'String', + value: tabletFk + } }; if (!isProduction(false)) throw new UserError('Action not allowed on the test environment'); // delete old - const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false); - if (docuwareFile) { - const deleteJson = { - 'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}] - }; - const deleteUri = `${uri}/${docuwareFile.id}/Fields`; - await axios.put(deleteUri, deleteJson, docuwareOptions.headers); - } + await models.Docuware.deleteOld(id, fileCabinet, uri); + // const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false); + // if (docuwareFile) { + // const deleteJson = { + // 'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}] + // }; + // const deleteUri = `${uri}/${docuwareFile.id}/Fields`; + // await axios.put(deleteUri, deleteJson, docuwareOptions.headers); + // } const uploadUri = `${uri}?StoreDialogId=${dialogId}`; - const FormData = require('form-data'); - const data = new FormData(); - - data.append('document', JSON.stringify(templateJson), 'schema.json'); - data.append('file[]', signPda[0], 'file.pdf'); - const uploadOptions = { - headers: { - 'Content-Type': 'multipart/form-data', - 'X-File-ModifiedDate': Date.vnNew(), - 'Cookie': docuwareOptions.headers.headers.Cookie, - ...data.getHeaders() - }, - }; + const uploadOptions = models.Docuware.uploadOptions(signPda[0], configTemplate); try { await axios.post(uploadUri, data, uploadOptions); } catch (err) { const $t = ctx.req.__; const message = $t('Failed to upload delivery note', {id}); - if (uploaded.length) - await models.TicketTracking.setDelivered(ctx, uploaded, myOptions); throw new UserError(message); } uploaded.push(id);