From 5828fa965763ed4709ebcbf5dcdadc3ca6ce46ef Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 26 Jan 2024 10:05:24 +0100 Subject: [PATCH] feat: #6184 Refactor saveSign --- back/tests.js | 1 - modules/account/front/role/card/index.spec.js | 2 +- .../front/role/descriptor/index.spec.js | 2 +- modules/route/back/methods/route/cmrEmail.js | 1 + .../route/specs/downloadCmrsZip.spec.js | 4 + modules/ticket/back/methods/ticket/saveCmr.js | 4 +- .../ticket/back/methods/ticket/saveSign.js | 118 ++++++++---------- print/core/cluster.js | 2 +- 8 files changed, 61 insertions(+), 73 deletions(-) diff --git a/back/tests.js b/back/tests.js index 19738acfb..de8a7e927 100644 --- a/back/tests.js +++ b/back/tests.js @@ -74,7 +74,6 @@ async function test() { spec_files: backSpecs, helpers: [], }); - process.env.IS_SPEC_RUNNING = true; await jasmine.execute(); if (app) await app.disconnect(); if (container) await container.rm(); diff --git a/modules/account/front/role/card/index.spec.js b/modules/account/front/role/card/index.spec.js index f02c08f28..569fe487d 100644 --- a/modules/account/front/role/card/index.spec.js +++ b/modules/account/front/role/card/index.spec.js @@ -1,6 +1,6 @@ import './index'; -fdescribe('component vnRoleCard', () => { +describe('component vnRoleCard', () => { let controller; let $httpBackend; diff --git a/modules/account/front/role/descriptor/index.spec.js b/modules/account/front/role/descriptor/index.spec.js index eafb96727..f3b2e4763 100644 --- a/modules/account/front/role/descriptor/index.spec.js +++ b/modules/account/front/role/descriptor/index.spec.js @@ -1,6 +1,6 @@ import './index'; -fdescribe('component vnRoleDescriptor', () => { +describe('component vnRoleDescriptor', () => { let controller; let $httpBackend; diff --git a/modules/route/back/methods/route/cmrEmail.js b/modules/route/back/methods/route/cmrEmail.js index dd8452e71..40665a49a 100644 --- a/modules/route/back/methods/route/cmrEmail.js +++ b/modules/route/back/methods/route/cmrEmail.js @@ -37,6 +37,7 @@ module.exports = Self => { Self.cmrEmail = async function(ctx, tickets, options) { const models = Self.app.models; const myOptions = {}; + let tx; if (typeof options == 'object') Object.assign(myOptions, options); diff --git a/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js b/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js index ba5da9bde..1e8ed29e7 100644 --- a/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js +++ b/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js @@ -1,6 +1,10 @@ const models = require('vn-loopback/server/server').models; describe('route downloadCmrsZip()', () => { + beforeEach(() => { + spyOn(models.Route, 'downloadCmrsZip').and.returnValue([true]); + }); + it('should create a zip file with the given cmr ids', async() => { const ctx = { req: { diff --git a/modules/ticket/back/methods/ticket/saveCmr.js b/modules/ticket/back/methods/ticket/saveCmr.js index 67c7484fe..a74ee9bb9 100644 --- a/modules/ticket/back/methods/ticket/saveCmr.js +++ b/modules/ticket/back/methods/ticket/saveCmr.js @@ -63,12 +63,11 @@ module.exports = Self => { companyFk: ticket.companyFk, warehouseFk: ticket.warehouseFk, reference: ticket.id, - description: `${ticket.cmrFk} - ${ticket.id}`, contentType: 'application/pdf', hasFile: true }; - dms = await models.Dms.createFromStream(data, 'pdf', pdfStream); + dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions); await models.TicketDms.create({ ticketFk: ticketId, dmsFk: dms.id @@ -80,7 +79,6 @@ module.exports = Self => { return; } catch (e) { if (tx) await tx.rollback(); - if (dms) await models.Dms.destroyAll({where: {id: dms.id}}); throw e; } }; diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 6e74a0f3d..cdcd37255 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -33,8 +33,7 @@ module.exports = Self => { const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; let tx; - let dms; - let gestDocCreated = false; + let ticket; if (typeof options == 'object') Object.assign(myOptions, options); @@ -44,6 +43,11 @@ module.exports = Self => { myOptions.transaction = tx; } + const dmsTypeTicket = await models.DmsType.findOne({ + where: {code: 'ticket'}, + fields: ['id'] + }, myOptions); + async function setLocation(ticketId) { await models.Delivery.create({ ticketFk: ticketId, @@ -53,83 +57,38 @@ module.exports = Self => { }, myOptions); } - async function gestDocExists(ticketId) { + async function hasSignDms(ticketId) { const ticketDms = await models.TicketDms.findOne({ where: {ticketFk: ticketId}, - fields: ['dmsFk'] + fields: ['dmsFk'], + include: [{ + relation: 'dms', + where: { + dmsType: dmsTypeTicket.id + } + }] }, myOptions); - - if (!ticketDms) return false; - - const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions); - if (ticket.isSigned == true) - return true; - else - await models.Dms.destroyAll({where: {reference: ticketId}}, myOptions); - - return false; + if (ticketDms) return true; } async function createGestDoc(id) { - const ticket = await models.Ticket.findById(id, - {include: [ - { - relation: 'warehouse', - scope: { - fields: ['id'] - } - }, { - relation: 'client', - scope: { - fields: ['name'] - } - }, { - relation: 'route', - scope: { - fields: ['id'] - } - } - ] - }, myOptions); - const dmsType = await models.DmsType.findOne({where: {code: 'ticket'}, fields: ['id']}, myOptions); const ctxUploadFile = Object.assign({}, ctx); - if (ticket.route() === null) - throw new UserError('Ticket without route'); ctxUploadFile.args = { warehouseId: ticket.warehouseFk, companyId: ticket.companyFk, - dmsTypeId: dmsType.id, - reference: '', + dmsTypeId: dmsTypeTicket.id, + reference: ticket.id, description: `Firma del cliente - Ruta ${ticket.route().id}`, - hasFile: false + hasFile: true }; - dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); - gestDocCreated = true; + const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); + await models.TicketDms.create({ticketFk: id, dmsFk: dms[0].id}, myOptions); } try { + let externalTickets = []; for (const ticketId of tickets) { - const ticketState = await models.TicketState.findOne( - {where: {ticketFk: ticketId}, - fields: ['alertLevel'] - }, myOptions); - - const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'}, - fields: ['id'] - }, myOptions); - - if (!ticketState) - throw new UserError('Ticket does not exist'); - if (ticketState.alertLevel < packedAlertLevel.id) - throw new UserError('This ticket cannot be signed because it has not been boxed'); - if (await gestDocExists(ticketId)) - throw new UserError('Ticket is already signed'); - - if (location) setLocation(ticketId); - if (!gestDocCreated) await createGestDoc(ticketId); - await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions); - const ticket = await models.Ticket.findOne({ - where: {ticketFk: ticketId}, + ticket = await models.Ticket.findById(ticketId, { include: [{ relation: 'address', scope: { @@ -145,8 +104,34 @@ module.exports = Self => { } } } + }, { + relation: 'route', + scope: { + fields: ['id'] + } }] }, myOptions); + + const ticketState = await models.TicketState.findOne( + {where: {ticketFk: ticketId}, + fields: ['alertLevel'] + }, myOptions); + + const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'}, + fields: ['id'] + }, myOptions); + + if (!ticketState) + throw new UserError('Ticket does not exist'); + if (!ticket.route()) + throw new UserError('Ticket without route'); + if (ticketState.alertLevel < packedAlertLevel.id) + throw new UserError('This ticket cannot be signed because it has not been boxed'); + if (await ticket.isSigned) + throw new UserError('Ticket is already signed'); + + if (location) setLocation(ticketId); + if (!await hasSignDms(ticketId)) createGestDoc(ticketId); await ticket.updateAttribute('isSigned', true, myOptions); const deliveryState = await models.State.findOne({ @@ -154,18 +139,19 @@ module.exports = Self => { code: 'DELIVERED' } }, myOptions); + await models.Ticket.state(ctx, { ticketFk: ticketId, stateFk: deliveryState.id }, myOptions); - if (ticket.address().province().country().code != 'ES') { + if (ticket?.address()?.province()?.country()?.code != 'ES') { await models.Ticket.saveCmr(ctx, [ticketId], myOptions); - await models.Route.cmrEmail(ctx, [ticketId], myOptions); + externalTickets.push(ticketId); } } - if (tx) await tx.commit(); + await models.Route.cmrEmail(ctx, externalTickets); return; } catch (e) { if (tx) await tx.rollback(); diff --git a/print/core/cluster.js b/print/core/cluster.js index 869907a7d..a75c4cf24 100644 --- a/print/core/cluster.js +++ b/print/core/cluster.js @@ -35,7 +35,7 @@ module.exports = { logger.error(`[Print] => ${err.message}`); }); - cluster.on('queue', () => !process.env.IS_SPEC_RUNNING && logger.info('Printing task initialized by pool')); + cluster.on('queue', () => logger.info('Printing task initialized by pool')); }); } };