diff --git a/modules/ticket/back/models/specs/ticket-service.spec.js b/modules/ticket/back/models/specs/ticket-service.spec.js new file mode 100644 index 000000000..0edd296c6 --- /dev/null +++ b/modules/ticket/back/models/specs/ticket-service.spec.js @@ -0,0 +1,65 @@ +/* eslint max-len: ["error", { "code": 150 }]*/ +const {models} = require('vn-loopback/server/server'); + +fdescribe('ticketService model ', () => { + const originalTicketFk = 1; + const refundTicketFk = 11; + + let tx; + let opts; + let ticketService; + + beforeEach(async() => { + tx = await models.Sale.beginTransaction({}); + opts = {transaction: tx}; + + ticketService = await models.TicketService.create({ + ticketFk: refundTicketFk, + description: 'test', + quantity: 1, + price: 100, + taxClassFk: 1, + ticketServiceTypeFk: 1 + }, opts); + }); + + afterEach(async() => { + await tx.rollback(); + }); + + describe('TicketService', () => { + it('should allow updating description and quantity for non-refund tickets', async() => { + await ticketService.updateAttributes({ + ticketServiceTypeFk: 2, + quantity: 5 + }, opts); + + const updated = await models.TicketService.findById(ticketService.id, null, opts); + + expect(updated.description).not.toBe('test'); + expect(updated.quantity).toBe(5); + }); + + it('should only allow updating description for refund tickets', async() => { + await models.TicketRefund.create({ + refundTicketFk, + originalTicketFk + }, opts); + + await ticketService.updateAttributes({ + ticketServiceTypeFk: 2 + }, opts); + + try { + await ticketService.updateAttributes({ + ticketServiceTypeFk: 3, + quantity: 5 + }, opts); + + fail('Should have thrown error'); + } catch (e) { + expect(e.message).toBe('Only description can be modified in refund tickets'); + } + }); + }); +}); diff --git a/modules/ticket/back/models/ticket-service.js b/modules/ticket/back/models/ticket-service.js index 209727ee4..77479498a 100644 --- a/modules/ticket/back/models/ticket-service.js +++ b/modules/ticket/back/models/ticket-service.js @@ -10,9 +10,18 @@ module.exports = Self => { const isLocked = await models.Ticket.isLocked(ticketId); if (isLocked) throw new UserError(`The current ticket can't be modified`); + + const isRefund = await models.TicketRefund.findOne({ + where: {refundTicketFk: ticketId} + }, { + transaction: ctx.options.transaction + }); + + if (isRefund && ctx.data && Object.keys(ctx.data).some(field => field !== 'ticketServiceTypeFk')) + throw new UserError('Only description can be modified in refund tickets'); } - if (changes && changes.ticketServiceTypeFk) { + if (changes?.ticketServiceTypeFk) { const ticketServiceType = await models.TicketServiceType.findById(changes.ticketServiceTypeFk); changes.description = ticketServiceType.name; }