From def41c011c79d03dae71bc70f796e9677d6fa7f8 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 13 Jan 2023 13:33:59 +0100 Subject: [PATCH] feat(docuware_upload): send to trash last file --- back/methods/docuware/checkFile.js | 10 +++++-- back/methods/docuware/{basic.js => core.js} | 10 +++---- back/methods/docuware/download.js | 2 +- back/methods/docuware/specs/checkFile.spec.js | 8 +++--- back/methods/docuware/specs/download.spec.js | 2 +- back/methods/docuware/specs/upload.spec.js | 20 +++++++------- back/methods/docuware/upload.js | 27 +++++++------------ back/models/docuware-config.json | 4 +-- back/models/docuware.js | 2 +- db/changes/230201/00-docuwareStore.sql | 2 ++ db/dump/fixtures.sql | 2 +- db/export-data.sh | 1 + loopback/locale/es.json | 5 ++-- .../ticket/front/descriptor-menu/index.html | 10 ++++++- modules/ticket/front/descriptor-menu/index.js | 7 +++-- .../front/descriptor-menu/index.spec.js | 27 ++++++++++++++++++- .../front/descriptor-menu/locale/es.yml | 2 ++ 17 files changed, 92 insertions(+), 49 deletions(-) rename back/methods/docuware/{basic.js => core.js} (91%) diff --git a/back/methods/docuware/checkFile.js b/back/methods/docuware/checkFile.js index c5c4abec0..c0a4e8ef3 100644 --- a/back/methods/docuware/checkFile.js +++ b/back/methods/docuware/checkFile.js @@ -16,6 +16,12 @@ module.exports = Self => { type: 'string', required: true, description: 'The fileCabinet name' + }, + { + arg: 'signed', + type: 'boolean', + required: true, + description: 'If pdf is necessary to be signed' } ], returns: { @@ -28,7 +34,7 @@ module.exports = Self => { } }); - Self.checkFile = async function(ctx, id, fileCabinet) { + Self.checkFile = async function(ctx, id, fileCabinet, signed) { const models = Self.app.models; const action = 'find'; @@ -70,7 +76,7 @@ module.exports = Self => { if (!documents) return false; const state = documents.Fields.find(field => field.FieldName == 'ESTADO'); - if (state.Item != 'Firmado') return false; + if (signed && state.Item != 'Firmado') return false; return {id: documents.Id}; } catch (error) { diff --git a/back/methods/docuware/basic.js b/back/methods/docuware/core.js similarity index 91% rename from back/methods/docuware/basic.js rename to back/methods/docuware/core.js index 5c1d7bfa9..2053ddf85 100644 --- a/back/methods/docuware/basic.js +++ b/back/methods/docuware/core.js @@ -20,8 +20,8 @@ module.exports = Self => { const options = await Self.getOptions(); - // if (!process.env.NODE_ENV) - // return Math.round(); + if (!process.env.NODE_ENV) + return Math.round(); const response = await axios.get(`${options.url}/FileCabinets/${fileCabinetId}/dialogs`, options.headers); const dialogs = response.data.Dialog; @@ -44,8 +44,8 @@ module.exports = Self => { } }); - // if (!process.env.NODE_ENV) - // return Math.round(); + if (!process.env.NODE_ENV) + return Math.round(); const fileCabinetResponse = await axios.get(`${options.url}/FileCabinets`, options.headers); const fileCabinets = fileCabinetResponse.data.FileCabinet; @@ -66,7 +66,7 @@ module.exports = Self => { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', - 'Cookie': docuwareConfig.token + 'Cookie': docuwareConfig.cookie } }; diff --git a/back/methods/docuware/download.js b/back/methods/docuware/download.js index 880b51b34..56d006ee7 100644 --- a/back/methods/docuware/download.js +++ b/back/methods/docuware/download.js @@ -43,7 +43,7 @@ module.exports = Self => { Self.download = async function(ctx, id, fileCabinet) { const models = Self.app.models; - const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet); + const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet, true); if (!docuwareFile) throw new UserError('The DOCUWARE PDF document does not exists'); const fileCabinetId = await Self.getFileCabinet(fileCabinet); diff --git a/back/methods/docuware/specs/checkFile.spec.js b/back/methods/docuware/specs/checkFile.spec.js index 3cb1a4074..dd11951cc 100644 --- a/back/methods/docuware/specs/checkFile.spec.js +++ b/back/methods/docuware/specs/checkFile.spec.js @@ -1,7 +1,7 @@ const models = require('vn-loopback/server/server').models; const axios = require('axios'); -fdescribe('docuware download()', () => { +describe('docuware download()', () => { const ticketId = 1; const userId = 9; const ctx = { @@ -28,7 +28,7 @@ fdescribe('docuware download()', () => { }; spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(response))); - const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName); + const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, true); expect(result).toEqual(false); }); @@ -51,7 +51,7 @@ fdescribe('docuware download()', () => { }; spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(response))); - const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName); + const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, true); expect(result).toEqual(false); }); @@ -75,7 +75,7 @@ fdescribe('docuware download()', () => { }; spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(response))); - const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName); + const result = await models.Docuware.checkFile(ctx, ticketId, fileCabinetName, true); expect(result.id).toEqual(docuwareId); }); diff --git a/back/methods/docuware/specs/download.spec.js b/back/methods/docuware/specs/download.spec.js index b1eff2c0d..fcc1671a6 100644 --- a/back/methods/docuware/specs/download.spec.js +++ b/back/methods/docuware/specs/download.spec.js @@ -2,7 +2,7 @@ const models = require('vn-loopback/server/server').models; const axios = require('axios'); const stream = require('stream'); -fdescribe('docuware download()', () => { +describe('docuware download()', () => { const userId = 9; const ticketId = 1; const ctx = { diff --git a/back/methods/docuware/specs/upload.spec.js b/back/methods/docuware/specs/upload.spec.js index c46dc01fd..7ac873e95 100644 --- a/back/methods/docuware/specs/upload.spec.js +++ b/back/methods/docuware/specs/upload.spec.js @@ -1,35 +1,37 @@ const models = require('vn-loopback/server/server').models; -fdescribe('docuware download()', () => { +describe('docuware upload()', () => { const userId = 9; - const ticketId = 1; + const ticketId = 10; const ctx = { req: { - + getLocale: () => { + return 'en'; + }, accessToken: {userId: userId}, headers: {origin: 'http://localhost:5000'}, } }; const docuwareModel = models.Docuware; + const ticketModel = models.Ticket; const fileCabinetName = 'deliveryNote'; beforeAll(() => { - spyOn(docuwareModel, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random())))); - spyOn(docuwareModel, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random())))); + spyOn(docuwareModel, 'getFileCabinet').and.returnValue(new Promise(resolve => resolve(Math.random()))); + spyOn(docuwareModel, 'getDialog').and.returnValue(new Promise(resolve => resolve(Math.random()))); }); it('should try upload file', async() => { - spyOn(docuwareModel, 'checkFile').and.returnValue(false); - spyOn(axios, 'get').and.returnValue(new stream.PassThrough({objectMode: true})); + spyOn(ticketModel, 'deliveryNotePdf').and.returnValue(new Promise(resolve => resolve({}))); let error; try { - await models.Docuware.download(ctx, ticketId, fileCabinetName); + await models.Docuware.upload(ctx, ticketId, fileCabinetName); } catch (e) { error = e.message; } - expect(error).toEqual('The DOCUWARE PDF document does not exists'); + expect(error).toEqual('Action not allowed on the test environment'); }); }); diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index 23aa2d8d0..76067e84a 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -380,27 +380,20 @@ module.exports = Self => { ] }; - // if (process.env.NODE_ENV != 'production') - // throw new UserError('Action not allowed on the test environment'); - const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet); + if (process.env.NODE_ENV != 'production') + throw new UserError('Action not allowed on the test environment'); - console.log(docuwareFile, id, fileCabinet); - // replace + // delete old + const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet, false); if (docuwareFile) { - console.log(docuwareFile); - const uri = `${options.url}/FileCabinets/${fileCabinetId}/Sections?DocId=${docuwareFile.id}`; - console.log(uri); - return await axios.post(uri, deliveryNote[0], {headers: { - 'Content-Type': 'application/pdf', - 'Content-Disposition': 'file; filename="10.pdf"', - 'X-File-ModifiedDate': '2020-08-26T00:00:00.000Z' - } - - }); + const deleteJson = { + 'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}] + }; + const deleteUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/Fields`; + await axios.put(deleteUri, deleteJson, options.headers); } - let uploadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${dialogId}`; - + const uploadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${dialogId}`; const FormData = require('form-data'); const data = new FormData(); diff --git a/back/models/docuware-config.json b/back/models/docuware-config.json index 8ca76d8ba..9d06c4874 100644 --- a/back/models/docuware-config.json +++ b/back/models/docuware-config.json @@ -16,7 +16,7 @@ "url": { "type": "string" }, - "token": { + "cookie": { "type": "string" } }, @@ -29,4 +29,4 @@ "permission": "ALLOW" } ] -} \ No newline at end of file +} diff --git a/back/models/docuware.js b/back/models/docuware.js index de057e7ec..b983f7bb4 100644 --- a/back/models/docuware.js +++ b/back/models/docuware.js @@ -3,5 +3,5 @@ module.exports = Self => { require('../methods/docuware/upload')(Self); require('../methods/docuware/checkFile')(Self); require('../methods/docuware/deliveryNoteEmail')(Self); - require('../methods/docuware/basic')(Self); + require('../methods/docuware/core')(Self); }; diff --git a/db/changes/230201/00-docuwareStore.sql b/db/changes/230201/00-docuwareStore.sql index 6dfee7a86..b20c2554f 100644 --- a/db/changes/230201/00-docuwareStore.sql +++ b/db/changes/230201/00-docuwareStore.sql @@ -19,3 +19,5 @@ INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`princip ('Docuware','download','READ','ALLOW','salesPerson'), ('Docuware','upload','WRITE','ALLOW','productionAssi'), ('Docuware','deliveryNoteEmail','WRITE','ALLOW','salesPerson'); + +ALTER TABLE `vn`.`docuwareConfig` CHANGE token cookie varchar(1000) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT NULL NULL; diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 8ac4dc5b5..b80b781da 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2582,7 +2582,7 @@ INSERT INTO `bs`.`sale` (`saleFk`, `amount`, `dated`, `typeFk`, `clientFk`) INSERT INTO `vn`.`docuwareConfig` (`url`) VALUES - ('https://verdnatura.docuware.cloud/docuware/platform'); + ('http://docuware.url/'); INSERT INTO `vn`.`calendarHolidaysName` (`id`, `name`) VALUES diff --git a/db/export-data.sh b/db/export-data.sh index 8bff538a7..bdf8049e0 100755 --- a/db/export-data.sh +++ b/db/export-data.sh @@ -59,6 +59,7 @@ TABLES=( componentType continent department + docuware itemPackingType pgc sample diff --git a/loopback/locale/es.json b/loopback/locale/es.json index ad845cb38..fe256e2ea 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -252,5 +252,6 @@ "Receipt's bank was not found": "No se encontró el banco del recibo", "This receipt was not compensated": "Este recibo no ha sido compensado", "Client's email was not found": "No se encontró el email del cliente", - "Failed to upload file": "Error al subir archivo" -} + "Failed to upload file": "Error al subir archivo", + "The DOCUWARE PDF document does not exists": "The DOCUWARE PDF document does not exists" +} \ No newline at end of file diff --git a/modules/ticket/front/descriptor-menu/index.html b/modules/ticket/front/descriptor-menu/index.html index e9e99fc3e..c2ebc3e3a 100644 --- a/modules/ticket/front/descriptor-menu/index.html +++ b/modules/ticket/front/descriptor-menu/index.html @@ -63,7 +63,7 @@ Send PDF Send PDF to tablet @@ -333,3 +333,11 @@ company-fk="$ctrl.vnConfig.companyFk" client-fk="$ctrl.ticket.client.id"> + + + + diff --git a/modules/ticket/front/descriptor-menu/index.js b/modules/ticket/front/descriptor-menu/index.js index 14d884ba7..c3e5ebf58 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -307,13 +307,16 @@ class Controller extends Section { } hasDocuware() { - this.$http.post(`Docuwares/${this.id}/checkFile`, {fileCabinet: 'deliveryNote'}) + this.$http.post(`Docuwares/${this.id}/checkFile`, {fileCabinet: 'deliveryNote', signed: true}) .then(res => { this.hasDocuwareFile = res.data; }); } - uploadDocuware() { + uploadDocuware(force) { + if (!force) + return this.$.pdfToTablet.show(); + return this.$http.post(`Docuwares/${this.id}/upload`, {fileCabinet: 'deliveryNote'}) .then(() => { this.vnApp.showSuccess(this.$t('PDF sent!')); diff --git a/modules/ticket/front/descriptor-menu/index.spec.js b/modules/ticket/front/descriptor-menu/index.spec.js index 48b64f4a0..67dc0affa 100644 --- a/modules/ticket/front/descriptor-menu/index.spec.js +++ b/modules/ticket/front/descriptor-menu/index.spec.js @@ -286,9 +286,34 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { describe('hasDocuware()', () => { it('should call hasDocuware method', () => { - $httpBackend.whenPOST(`Docuwares/${ticket.id}/checkFile`).respond(); + $httpBackend.whenPOST(`Docuwares/${ticket.id}/checkFile`).respond(true); controller.hasDocuware(); $httpBackend.flush(); + + expect(controller.hasDocuwareFile).toBe(true); + }); + }); + + describe('uploadDocuware()', () => { + it('should open dialog if not force', () => { + controller.$.pdfToTablet = {show: () => {}}; + jest.spyOn(controller.$.pdfToTablet, 'show'); + controller.uploadDocuware(false); + + expect(controller.$.pdfToTablet.show).toHaveBeenCalled(); + }); + + it('should make a query and show balance create', () => { + controller.$.balanceCreate = {show: () => {}}; + jest.spyOn(controller.$.balanceCreate, 'show'); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.whenPOST(`Docuwares/${ticket.id}/upload`).respond(true); + controller.uploadDocuware(true); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + expect(controller.$.balanceCreate.show).toHaveBeenCalled(); }); }); diff --git a/modules/ticket/front/descriptor-menu/locale/es.yml b/modules/ticket/front/descriptor-menu/locale/es.yml index 82425864c..b51637524 100644 --- a/modules/ticket/front/descriptor-menu/locale/es.yml +++ b/modules/ticket/front/descriptor-menu/locale/es.yml @@ -16,3 +16,5 @@ The following refund ticket have been created: "Se ha creado siguiente ticket de Transfer client: Transferir cliente SMS Notify changes: SMS Notificar cambios PDF sent!: ¡PDF enviado! +Already exist signed delivery note: Ya existe albarán de entrega firmado +Are you sure you want to replace this delivery note?: ¿Seguro que quieres reemplazar este albarán de entrega?