From a5a0256e44490fb095c282aefb6103ce0744bb28 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 14 Feb 2023 10:58:41 +0100 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20a=C3=B1adido=20parametro=20que=20in?= =?UTF-8?q?dica=20si=20genera=20uno=20o=20multiples=20ticket=20de=20abono?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../back/methods/invoiceOut/refund.js | 4 +- modules/ticket/back/methods/sale/refund.js | 105 +++++++++++++----- modules/ticket/back/methods/ticket/refund.js | 9 +- 3 files changed, 85 insertions(+), 33 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/refund.js b/modules/invoiceOut/back/methods/invoiceOut/refund.js index 7ad6b03ec..584e0939b 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/refund.js +++ b/modules/invoiceOut/back/methods/invoiceOut/refund.js @@ -35,8 +35,8 @@ module.exports = Self => { const tickets = await models.Ticket.find(filter, myOptions); const ticketsIds = tickets.map(ticket => ticket.id); - - const refundedTickets = await models.Ticket.refund(ticketsIds, myOptions); + const createSingleTicket = true; + const refundedTickets = await models.Ticket.refund(ticketsIds, createSingleTicket, myOptions); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index febef9730..3558be15a 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -12,6 +12,11 @@ module.exports = Self => { arg: 'servicesIds', type: ['number'] }, + { + arg: 'createSingleTicket', + type: 'boolean', + required: false + } ], returns: { type: ['number'], @@ -23,7 +28,7 @@ module.exports = Self => { } }); - Self.refund = async(salesIds, servicesIds, options) => { + Self.refund = async(salesIds, servicesIds, createSingleTicket = false, options) => { const models = Self.app.models; const myOptions = {}; let tx; @@ -63,38 +68,37 @@ module.exports = Self => { const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; const refundTickets = []; - - const now = Date.vnNew(); const mappedTickets = new Map(); + const now = Date.vnNew(); - for (let ticketId of ticketsIds) { - const filter = {include: {relation: 'address'}}; - const ticket = await models.Ticket.findById(ticketId, filter, myOptions); - - const refundTicket = await models.Ticket.create({ - clientFk: ticket.clientFk, - shipped: now, - addressFk: ticket.address().id, - agencyModeFk: refundAgencyMode.id, - nickname: ticket.address().nickname, - warehouseFk: ticket.warehouseFk, - companyFk: ticket.companyFk, - landed: now, - zoneFk: refoundZoneId - }, myOptions); - - refundTickets.push(refundTicket); - - mappedTickets.set(ticketId, refundTicket.id); - - await models.TicketRefund.create({ - refundTicketFk: refundTicket.id, - originalTicketFk: ticket.id, - }, myOptions); + const [firstTicketId] = ticketsIds; + if (createSingleTicket) { + await createTicketRefund( + firstTicketId, + refundTickets, + mappedTickets, + now, + refundAgencyMode, + refoundZoneId, + myOptions + ); + } else { + for (let ticketId of ticketsIds) { + await createTicketRefund( + ticketId, + refundTickets, + mappedTickets, + now, + refundAgencyMode, + refoundZoneId, + myOptions + ); + } } for (const sale of sales) { - const refundTicketId = mappedTickets.get(sale.ticketFk); + const refundTicketId = await getTicketRefundId(createSingleTicket, sale.ticketFk, refundTickets); + const createdSale = await models.Sale.create({ ticketFk: refundTicketId, itemFk: sale.itemFk, @@ -118,7 +122,7 @@ module.exports = Self => { const services = await models.TicketService.find(servicesFilter, myOptions); for (const service of services) { - const refundTicketId = mappedTickets.get(service.ticketFk); + const refundTicketId = await getTicketRefundId(createSingleTicket, service.ticketFk, refundTickets); await models.TicketService.create({ description: service.description, @@ -139,4 +143,47 @@ module.exports = Self => { throw e; } }; + + async function createTicketRefund( + ticketId, + refundTickets, + mappedTickets, + now, + refundAgencyMode, + refoundZoneId, + myOptions + ) { + const models = Self.app.models; + + const filter = {include: {relation: 'address'}}; + const ticket = await models.Ticket.findById(ticketId, filter, myOptions); + + const refundTicket = await models.Ticket.create({ + clientFk: ticket.clientFk, + shipped: now, + addressFk: ticket.address().id, + agencyModeFk: refundAgencyMode.id, + nickname: ticket.address().nickname, + warehouseFk: ticket.warehouseFk, + companyFk: ticket.companyFk, + landed: now, + zoneFk: refoundZoneId + }, myOptions); + + refundTickets.push(refundTicket); + + mappedTickets.set(ticketId, refundTicket.id); + + await models.TicketRefund.create({ + refundTicketFk: refundTicket.id, + originalTicketFk: ticket.id, + }, myOptions); + } + + async function getTicketRefundId(createSingleTicket, ticketId, refundTickets) { + if (createSingleTicket) { + const [firstRefundTicket] = refundTickets; + return firstRefundTicket.id; + } else return mappedTickets.get(ticketId); + } }; diff --git a/modules/ticket/back/methods/ticket/refund.js b/modules/ticket/back/methods/ticket/refund.js index 620c8b0c7..1f0021316 100644 --- a/modules/ticket/back/methods/ticket/refund.js +++ b/modules/ticket/back/methods/ticket/refund.js @@ -8,6 +8,11 @@ module.exports = Self => { type: ['number'], required: true }, + { + arg: 'createSingleTicket', + type: 'boolean', + required: false + } ], returns: { type: ['number'], @@ -19,7 +24,7 @@ module.exports = Self => { } }); - Self.refund = async(ticketsIds, options) => { + Self.refund = async(ticketsIds, createSingleTicket = false, options) => { const models = Self.app.models; const myOptions = {}; let tx; @@ -41,7 +46,7 @@ module.exports = Self => { const services = await models.TicketService.find(filter, myOptions); const servicesIds = services.map(service => service.id); - const refundedTickets = await models.Sale.refund(salesIds, servicesIds, myOptions); + const refundedTickets = await models.Sale.refund(salesIds, servicesIds, createSingleTicket, myOptions); if (tx) await tx.commit(); From b5d2d534350b4281c2b581415197130531ea0f23 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 16 Feb 2023 10:45:13 +0100 Subject: [PATCH 2/4] fix: test back --- .../back/methods/invoiceOut/specs/refund.spec.js | 2 +- modules/ticket/back/methods/sale/refund.js | 6 +++--- modules/ticket/back/methods/sale/specs/refund.spec.js | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/refund.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/refund.spec.js index 628318d42..c5a1ac603 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/refund.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/refund.spec.js @@ -17,7 +17,7 @@ describe('InvoiceOut refund()', () => { try { const result = await models.InvoiceOut.refund('T1111111', options); - expect(result.length).toEqual(2); + expect(result.length).toEqual(1); await tx.rollback(); } catch (e) { diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index 3558be15a..7b63fd66e 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -97,7 +97,7 @@ module.exports = Self => { } for (const sale of sales) { - const refundTicketId = await getTicketRefundId(createSingleTicket, sale.ticketFk, refundTickets); + const refundTicketId = await getTicketRefundId(createSingleTicket, sale.ticketFk, refundTickets, mappedTickets); const createdSale = await models.Sale.create({ ticketFk: refundTicketId, @@ -122,7 +122,7 @@ module.exports = Self => { const services = await models.TicketService.find(servicesFilter, myOptions); for (const service of services) { - const refundTicketId = await getTicketRefundId(createSingleTicket, service.ticketFk, refundTickets); + const refundTicketId = await getTicketRefundId(createSingleTicket, service.ticketFk, refundTickets, mappedTickets); await models.TicketService.create({ description: service.description, @@ -180,7 +180,7 @@ module.exports = Self => { }, myOptions); } - async function getTicketRefundId(createSingleTicket, ticketId, refundTickets) { + async function getTicketRefundId(createSingleTicket, ticketId, refundTickets, mappedTickets) { if (createSingleTicket) { const [firstRefundTicket] = refundTickets; return firstRefundTicket.id; diff --git a/modules/ticket/back/methods/sale/specs/refund.spec.js b/modules/ticket/back/methods/sale/specs/refund.spec.js index 74077cf29..403b4b477 100644 --- a/modules/ticket/back/methods/sale/specs/refund.spec.js +++ b/modules/ticket/back/methods/sale/specs/refund.spec.js @@ -1,7 +1,7 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); -describe('sale refund()', () => { +describe('Sale refund()', () => { const userId = 5; const activeCtx = { accessToken: {userId: userId}, @@ -22,7 +22,7 @@ describe('sale refund()', () => { try { const options = {transaction: tx}; - const response = await models.Sale.refund(salesIds, servicesIds, options); + const response = await models.Sale.refund(salesIds, servicesIds, false, options); expect(response.length).toBeGreaterThanOrEqual(1); @@ -40,7 +40,8 @@ describe('sale refund()', () => { try { const options = {transaction: tx}; - const tickets = await models.Sale.refund(salesIds, servicesIds, options); + const createSingleTicket = false; + const tickets = await models.Sale.refund(salesIds, servicesIds, createSingleTicket, options); const ticketsIds = tickets.map(ticket => ticket.id); From 874f45995621281214b74b19c8e1a0ce7110c25b Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 17 Feb 2023 09:49:47 +0100 Subject: [PATCH 3/4] refactor: borrada variable innecesaria --- modules/invoiceOut/back/methods/invoiceOut/refund.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/refund.js b/modules/invoiceOut/back/methods/invoiceOut/refund.js index 584e0939b..ba1fdfedd 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/refund.js +++ b/modules/invoiceOut/back/methods/invoiceOut/refund.js @@ -35,8 +35,7 @@ module.exports = Self => { const tickets = await models.Ticket.find(filter, myOptions); const ticketsIds = tickets.map(ticket => ticket.id); - const createSingleTicket = true; - const refundedTickets = await models.Ticket.refund(ticketsIds, createSingleTicket, myOptions); + const refundedTickets = await models.Ticket.refund(ticketsIds, true, myOptions); if (tx) await tx.commit(); From 3ef0b3a68b579cf4f004ff01803abb25dbe0ae7f Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 20 Feb 2023 12:42:42 +0100 Subject: [PATCH 4/4] feat: add acl --- db/changes/230801/00-acl_itemConfig.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 db/changes/230801/00-acl_itemConfig.sql diff --git a/db/changes/230801/00-acl_itemConfig.sql b/db/changes/230801/00-acl_itemConfig.sql new file mode 100644 index 000000000..8c35022b4 --- /dev/null +++ b/db/changes/230801/00-acl_itemConfig.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('ItemConfig', '*', '*', 'ALLOW', 'ROLE', 'employee');