feat: refs #7832 implement refund ticket restrictions and add unit tests for ticket service updates
This commit is contained in:
parent
be50c91299
commit
1f23ebf6d5
|
@ -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');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue