diff --git a/db/dump/.dump/data.sql b/db/dump/.dump/data.sql index 870b24f49..6bcfe2c9f 100644 --- a/db/dump/.dump/data.sql +++ b/db/dump/.dump/data.sql @@ -2084,7 +2084,7 @@ INSERT INTO `ACL` VALUES (756,'Route','findOne','READ','ALLOW','ROLE','employee' INSERT INTO `ACL` VALUES (757,'Route','getRoutesByWorker','READ','ALLOW','ROLE','employee',NULL); INSERT INTO `ACL` VALUES (758,'Route','canViewAllRoute','READ','ALLOW','ROLE','deliveryAssistant',NULL); INSERT INTO `ACL` VALUES (759,'Route','cmr','READ','ALLOW','ROLE','employee',NULL); -INSERT INTO `ACL` VALUES (760,'Route','downloadCmrsZip','READ','ALLOW','ROLE','employee',NULL); +INSERT INTO `ACL` VALUES (760,'Cmr','downloadZip','READ','ALLOW','ROLE','employee',NULL); INSERT INTO `ACL` VALUES (761,'Route','downloadZip','READ','ALLOW','ROLE','employee',NULL); INSERT INTO `ACL` VALUES (762,'Route','filter','READ','ALLOW','ROLE','employee',NULL); INSERT INTO `ACL` VALUES (763,'Route','getByWorker','READ','ALLOW','ROLE','employee',NULL); diff --git a/db/versions/11336-chocolateRaphis/00-firstScript.sql b/db/versions/11336-chocolateRaphis/00-firstScript.sql new file mode 100644 index 000000000..c86dc39e9 --- /dev/null +++ b/db/versions/11336-chocolateRaphis/00-firstScript.sql @@ -0,0 +1,8 @@ +DELETE FROM salix.ACL + WHERE property IN ('cmrs', 'cmr', 'downloadCmrsZip') + AND model = 'Route'; + +INSERT INTO salix.ACL (model,property,principalId) + VALUES ('Cmr','filter','production'), + ('Cmr','downloadZip','production'), + ('Cmr','print','production') \ No newline at end of file diff --git a/loopback/common/methods/vn-model/printService.js b/loopback/common/methods/vn-model/printService.js index 12a6a67cc..ed784229b 100644 --- a/loopback/common/methods/vn-model/printService.js +++ b/loopback/common/methods/vn-model/printService.js @@ -2,7 +2,7 @@ const {Report, Email} = require('vn-print'); module.exports = Self => { Self.printReport = async function(ctx, id, reportName) { - const args = Object.assign({}, ctx.args); + const args = Object.assign({id}, ctx.args); const params = {lang: ctx.req.getLocale()}; delete args.ctx; diff --git a/modules/route/back/methods/route/downloadCmrsZip.js b/modules/route/back/methods/cmr/downloadZip.js similarity index 87% rename from modules/route/back/methods/route/downloadCmrsZip.js rename to modules/route/back/methods/cmr/downloadZip.js index 895ff7a16..fcf4ce61c 100644 --- a/modules/route/back/methods/route/downloadCmrsZip.js +++ b/modules/route/back/methods/cmr/downloadZip.js @@ -1,7 +1,7 @@ const JSZip = require('jszip'); module.exports = Self => { - Self.remoteMethodCtx('downloadCmrsZip', { + Self.remoteMethodCtx('downloadZip', { description: 'Download a zip file with multiple cmrs pdfs', accessType: 'READ', accepts: [ @@ -27,13 +27,13 @@ module.exports = Self => { } ], http: { - path: '/downloadCmrsZip', + path: '/downloadZip', verb: 'GET' }, accessScopes: ['DEFAULT', 'read:multimedia'] }); - Self.downloadCmrsZip = async function(ctx, ids, options) { + Self.downloadZip = async function(ctx, ids, options) { const models = Self.app.models; const myOptions = {}; const zip = new JSZip(); @@ -44,7 +44,7 @@ module.exports = Self => { const downloadAddZip = async id => { ctx.args = ctx.args || {}; ctx.args.id = Number(id); - const [data] = await models.Route.cmr(ctx, myOptions); + const [data] = await models.Cmr.print(ctx, myOptions); zip.file(`${id}.pdf`, data, {binary: true}); }; diff --git a/modules/route/back/methods/route/cmrs.js b/modules/route/back/methods/cmr/filter.js similarity index 98% rename from modules/route/back/methods/route/cmrs.js rename to modules/route/back/methods/cmr/filter.js index 6f271a4cf..d5a217b1c 100644 --- a/modules/route/back/methods/route/cmrs.js +++ b/modules/route/back/methods/cmr/filter.js @@ -3,7 +3,7 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { - Self.remoteMethod('cmrs', { + Self.remoteMethod('filter', { description: 'Returns an array of cmrs', accessType: 'READ', accepts: [ @@ -57,12 +57,12 @@ module.exports = Self => { root: true }, http: { - path: `/cmrs`, + path: `/filter`, verb: 'GET' } }); - Self.cmrs = async( + Self.filter = async( filter, cmrFk, ticketFk, routeFk, country, clientFk, hasCmrDms, shipped, warehouseFk, options ) => { const params = {cmrFk, ticketFk, routeFk, country, clientFk, hasCmrDms, warehouseFk, shipped}; diff --git a/modules/route/back/methods/route/cmr.js b/modules/route/back/methods/cmr/print.js similarity index 82% rename from modules/route/back/methods/route/cmr.js rename to modules/route/back/methods/cmr/print.js index 5033dee2f..2fe807829 100644 --- a/modules/route/back/methods/route/cmr.js +++ b/modules/route/back/methods/cmr/print.js @@ -1,6 +1,6 @@ module.exports = Self => { - Self.remoteMethodCtx('cmr', { - description: 'Returns the cmr', + Self.remoteMethodCtx('print', { + description: 'Returns the cmr pdf', accessType: 'READ', accepts: [ { @@ -27,11 +27,11 @@ module.exports = Self => { } ], http: { - path: '/:id/cmr', + path: '/:id/print', verb: 'GET' }, accessScopes: ['DEFAULT', 'read:multimedia'] }); - Self.cmr = (ctx, id) => Self.printReport(ctx, id, 'cmr'); + Self.print = (ctx, id) => Self.printReport(ctx, id, 'cmr'); }; diff --git a/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js b/modules/route/back/methods/cmr/specs/downloadZip.spec.js similarity index 83% rename from modules/route/back/methods/route/specs/downloadCmrsZip.spec.js rename to modules/route/back/methods/cmr/specs/downloadZip.spec.js index 7312a5d44..74870038f 100644 --- a/modules/route/back/methods/route/specs/downloadCmrsZip.spec.js +++ b/modules/route/back/methods/cmr/specs/downloadZip.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('route downloadCmrsZip()', () => { +describe('route downloadZip()', () => { it('should create a zip file with the given cmr ids', async() => { const tx = await models.Route.beginTransaction({}); const ctx = { @@ -13,7 +13,7 @@ describe('route downloadCmrsZip()', () => { }; let cmrs = '1,2'; try { - const stream = await models.Route.downloadCmrsZip(ctx, cmrs); + const stream = await models.Cmr.downloadZip(ctx, cmrs); expect(stream[0]).toBeDefined(); await tx.rollback(); diff --git a/modules/route/back/model-config.json b/modules/route/back/model-config.json index ccae87bd9..be5aa07ca 100644 --- a/modules/route/back/model-config.json +++ b/modules/route/back/model-config.json @@ -5,6 +5,9 @@ "AgencyTermConfig": { "dataSource": "vn" }, + "Cmr": { + "dataSource": "vn" + }, "DeliveryPoint": { "dataSource": "vn" }, diff --git a/modules/route/back/models/cmr.js b/modules/route/back/models/cmr.js new file mode 100644 index 000000000..f9a153d52 --- /dev/null +++ b/modules/route/back/models/cmr.js @@ -0,0 +1,5 @@ +module.exports = Self => { + require('../methods/cmr/print')(Self); + require('../methods/cmr/filter')(Self); + require('../methods/cmr/downloadZip')(Self); +}; diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index 242623d6e..cd8685cec 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -14,10 +14,6 @@ module.exports = Self => { require('../methods/route/driverRouteEmail')(Self); require('../methods/route/sendSms')(Self); require('../methods/route/downloadZip')(Self); - require('../methods/route/cmr')(Self); - require('../methods/route/cmrs')(Self); - require('../methods/route/downloadCmrsZip')(Self); - require('../methods/route/cmrEmail')(Self); require('../methods/route/getExpeditionSummary')(Self); require('../methods/route/getByWorker')(Self); }; diff --git a/modules/ticket/back/methods/ticket/saveCmr.js b/modules/ticket/back/methods/ticket/saveCmr.js index 339ac1e38..5ac748751 100644 --- a/modules/ticket/back/methods/ticket/saveCmr.js +++ b/modules/ticket/back/methods/ticket/saveCmr.js @@ -41,7 +41,7 @@ module.exports = Self => { for (const ticketId of tickets) { const ticket = await models.Ticket.findById(ticketId, null, myOptions); - if (ticket.cmrFk) { + if (ticket.$cmrFk) { const hasDmsCmr = await Self.rawSql(` SELECT d.id FROM ticketDms td @@ -53,8 +53,7 @@ module.exports = Self => { if (hasDmsCmr.length) throw new UserError('This ticket already has a cmr saved'); - ctx.args.id = ticket.cmrFk; - const response = await models.Route.cmr(ctx, myOptions); + const response = await models.Cmr.print(ctx, ticket.$cmrFk, myOptions); const pdfStream = Readable.from(Buffer.from(response[0])); const data = { workerFk: ctx.req.accessToken.userId, diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index bc0da686c..ac2a7bc66 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -151,7 +151,7 @@ module.exports = Self => { await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticketId, stateCode], myOptions); - if (ticket?.address()?.province()?.country()?.code != 'ES' && ticket.cmrFk) { + if (ticket?.address()?.province()?.country()?.code != 'ES' && ticket.$cmrFk) { await models.Ticket.saveCmr(ctx, [ticketId], myOptions); externalTickets.push(ticketId); } @@ -161,6 +161,6 @@ module.exports = Self => { if (tx) await tx.rollback(); throw e; } - await models.Route.cmrEmail(ctx, externalTickets); + await models.Ticket.sendCmrEmail(ctx, externalTickets); }; }; diff --git a/modules/route/back/methods/route/cmrEmail.js b/modules/ticket/back/methods/ticket/sendCmrEmail.js similarity index 92% rename from modules/route/back/methods/route/cmrEmail.js rename to modules/ticket/back/methods/ticket/sendCmrEmail.js index 0c4cc5061..27f22ca14 100644 --- a/modules/route/back/methods/route/cmrEmail.js +++ b/modules/ticket/back/methods/ticket/sendCmrEmail.js @@ -2,7 +2,7 @@ const {Email} = require('vn-print'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('cmrEmail', { + Self.remoteMethodCtx('sendCmrEmail', { description: 'Sends the email with an cmr attached PDF', accessType: 'WRITE', accepts: [ @@ -14,12 +14,12 @@ module.exports = Self => { } ], http: { - path: '/cmrEmail', + path: '/sendCmrEmail', verb: 'POST' } }); - Self.cmrEmail = async function(ctx, tickets, options) { + Self.sendCmrEmail = async function(ctx, tickets, options) { const models = Self.app.models; const myOptions = {}; let tx; @@ -70,7 +70,7 @@ module.exports = Self => { await email.send({ overrideAttachments: true, attachments: [{ - filename: `${ticket.cmrFk}.pdf`, + filename: `${ticket.$cmrFk}.pdf`, content: response[0] }] }); diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 7fe968b26..620b3e184 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -40,6 +40,7 @@ module.exports = function(Self) { require('../methods/ticket/expeditionPalletLabel')(Self); require('../methods/ticket/saveSign')(Self); require('../methods/ticket/saveCmr')(Self); + require('../methods/ticket/sendCmrEmail')(Self); require('../methods/ticket/invoiceTickets')(Self); require('../methods/ticket/invoiceTicketsAndPdf')(Self); require('../methods/ticket/docuwareDownload')(Self);