From 9005975f1c851a42dcf6581076adbe023c74390d Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 25 Jan 2024 13:56:47 +0100 Subject: [PATCH] feat: #6184 Modified email cmr --- modules/route/back/methods/route/cmrEmail.js | 115 ++++++++++-------- .../back/methods/route/specs/saveCmr.spec.js | 2 +- modules/ticket/back/methods/ticket/saveCmr.js | 9 +- .../ticket/back/methods/ticket/saveSign.js | 31 ++++- .../methods/ticket/specs/saveSign.spec.js | 15 +-- 5 files changed, 100 insertions(+), 72 deletions(-) diff --git a/modules/route/back/methods/route/cmrEmail.js b/modules/route/back/methods/route/cmrEmail.js index e24d8a58f..dd8452e71 100644 --- a/modules/route/back/methods/route/cmrEmail.js +++ b/modules/route/back/methods/route/cmrEmail.js @@ -1,4 +1,5 @@ const {Email} = require('vn-print'); +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('cmrEmail', { @@ -6,22 +7,10 @@ module.exports = Self => { accessType: 'WRITE', accepts: [ { - arg: 'ticketId', - type: 'number', + arg: 'tickets', + type: ['number'], required: true, description: 'The ticket id', - }, - { - arg: 'recipientId', - type: 'number', - description: 'The client id', - required: true - }, - { - arg: 'recipient', - type: 'string', - description: 'The recipient email', - required: false, } ], returns: [ @@ -45,54 +34,76 @@ module.exports = Self => { } }); - Self.cmrEmail = async function(ctx, ticketId, recipientId, recipient, options) { + Self.cmrEmail = async function(ctx, tickets, options) { const models = Self.app.models; const myOptions = {}; - const args = Object.assign({}, ctx.args); - const params = { - recipient: args.recipient, - lang: ctx.req.getLocale() - }; - - delete args.ctx; - for (const param in args) - params[param] = args[param]; if (typeof options == 'object') Object.assign(myOptions, options); - if (!recipient) - params.recipient = (await models.Client.findById(recipientId, {fields: ['email']}, myOptions)).email; + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } - const cmr = (await models.Ticket.findById(ticketId, {fields: ['cmrFk']}, myOptions)).cmrFk; + try { + for (const ticketId of tickets) { + const ticket = await models.Ticket.findOne({ + where: { + id: ticketId + }, + include: [{ + relation: 'client', + fields: ['email'] + }] + }, myOptions); - const dms = await models.TicketDms.findOne({ - where: { - ticketFk: ticketId - }, - include: [ - { - relation: 'dms', - fields: ['id'], - scope: { - relation: 'dmsType', - where: { - code: 'cmr' + const recipient = ticket.client().email; + if (!recipient) + throw new UserError('Client does not have an email'); + + const params = { + ticketId, + lang: ctx.req.getLocale(), + recipient + }; + + const dms = await models.TicketDms.findOne({ + where: { + ticketFk: ticketId + }, + include: [ + { + relation: 'dms', + fields: ['id'], + scope: { + relation: 'dmsType', + where: { + code: 'cmr' + } + } } - } - } - ] - }, myOptions); + ] + }, myOptions); - const response = await models.Dms.downloadFile(ctx, dms.id); - const email = new Email('cmr', params); + if (!dms) throw new UserError('Cmr file does not exist'); - return email.send({ - overrideAttachments: true, - attachments: [{ - filename: `${cmr}.pdf`, - content: response[0] - }] - }); + const response = await models.Dms.downloadFile(ctx, dms.id); + const email = new Email('cmr', params); + + return email.send({ + overrideAttachments: true, + attachments: [{ + filename: `${ticket.cmrFk}.pdf`, + content: response[0] + }] + }); + } + if (tx) await tx.commit(); + return; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/route/back/methods/route/specs/saveCmr.spec.js b/modules/route/back/methods/route/specs/saveCmr.spec.js index 2f99723fe..9de29540f 100644 --- a/modules/route/back/methods/route/specs/saveCmr.spec.js +++ b/modules/route/back/methods/route/specs/saveCmr.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -fdescribe('Ticket saveCmr()', () => { +describe('Ticket saveCmr()', () => { let ctx = {req: { accessToken: {userId: 9} }}; diff --git a/modules/ticket/back/methods/ticket/saveCmr.js b/modules/ticket/back/methods/ticket/saveCmr.js index 9de84a1fa..67c7484fe 100644 --- a/modules/ticket/back/methods/ticket/saveCmr.js +++ b/modules/ticket/back/methods/ticket/saveCmr.js @@ -4,8 +4,7 @@ module.exports = Self => { Self.remoteMethodCtx('saveCmr', { description: 'Save cmr', accessType: 'WRITE', - accepts: - [ + accepts: [ { arg: 'tickets', type: ['number'], @@ -23,6 +22,7 @@ module.exports = Self => { const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; let tx; + let dms; if (typeof options == 'object') Object.assign(myOptions, options); @@ -56,7 +56,7 @@ module.exports = Self => { if (!hasDmsCmr?.dms()) { ctx.args.id = ticket.cmrFk; const response = await models.Route.cmr(ctx, myOptions); - const pdfStream = Readable.from(Buffer.from(response)); + const pdfStream = Readable.from(Buffer.from(response[0])); const data = { workerFk: ctx.req.accessToken.userId, dmsTypeFk: dmsTypeCmr.id, @@ -68,7 +68,7 @@ module.exports = Self => { hasFile: true }; - const dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions); + dms = await models.Dms.createFromStream(data, 'pdf', pdfStream); await models.TicketDms.create({ ticketFk: ticketId, dmsFk: dms.id @@ -80,6 +80,7 @@ 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 26ca2e1c5..6e74a0f3d 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -128,22 +128,41 @@ module.exports = Self => { 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.findById(ticketId, null, myOptions); + const ticket = await models.Ticket.findOne({ + where: {ticketFk: ticketId}, + include: [{ + relation: 'address', + scope: { + include: { + relation: 'province', + scope: { + include: { + relation: 'country', + scope: { + fields: ['code'] + } + } + } + } + } + }] + }, myOptions); await ticket.updateAttribute('isSigned', true, myOptions); - const deliveryState = await models.State.find({ + const deliveryState = await models.State.findOne({ where: { code: 'DELIVERED' } - }, options); - + }, myOptions); await models.Ticket.state(ctx, { ticketFk: ticketId, stateFk: deliveryState.id }, myOptions); - await models.Ticket.saveCmr(ctx, [ticketId], myOptions); - await models.Route.cmrEmail(ctx, [ticketId], myOptions); + if (ticket.address().province().country().code != 'ES') { + await models.Ticket.saveCmr(ctx, [ticketId], myOptions); + await models.Route.cmrEmail(ctx, [ticketId], myOptions); + } } if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/specs/saveSign.spec.js b/modules/ticket/back/methods/ticket/specs/saveSign.spec.js index 6b532a5d1..792e9e824 100644 --- a/modules/ticket/back/methods/ticket/specs/saveSign.spec.js +++ b/modules/ticket/back/methods/ticket/specs/saveSign.spec.js @@ -1,14 +1,11 @@ const models = require('vn-loopback/server/server').models; describe('Ticket saveSign()', () => { - const FormData = require('form-data'); - const data = new FormData(); let ctx = {req: { - accessToken: {userId: 9}, - headers: { - ...data.getHeaders() - } - + getLocale: () => { + return 'en'; + }, + accessToken: {userId: 9} }}; it(`should throw error if the ticket's alert level is lower than 2`, async() => { @@ -17,9 +14,9 @@ describe('Ticket saveSign()', () => { let error; try { const options = {transaction: tx}; - ctx.args = {tickets: [ticketWithOkState]}; + const tickets = [ticketWithOkState]; - await models.Ticket.saveSign(ctx, options); + await models.Ticket.saveSign(ctx, tickets, options); await tx.rollback(); } catch (e) {