2021-12-21 13:43:53 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2022-07-14 11:40:50 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2021-12-21 13:43:53 +00:00
|
|
|
|
2022-04-04 11:26:53 +00:00
|
|
|
describe('sale refund()', () => {
|
2022-07-14 11:40:50 +00:00
|
|
|
const userId = 5;
|
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: userId},
|
|
|
|
};
|
2022-04-26 11:25:12 +00:00
|
|
|
|
2022-07-14 11:40:50 +00:00
|
|
|
const servicesIds = [3];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create ticket with the selected lines', async() => {
|
2021-12-21 13:43:53 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-07-14 11:40:50 +00:00
|
|
|
const salesIds = [7, 8];
|
2022-01-21 10:40:48 +00:00
|
|
|
|
2021-12-21 13:43:53 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2022-07-14 11:40:50 +00:00
|
|
|
const response = await models.Sale.refund(salesIds, servicesIds, options);
|
2021-12-21 13:43:53 +00:00
|
|
|
|
2022-07-14 11:40:50 +00:00
|
|
|
expect(response.length).toBeGreaterThanOrEqual(1);
|
2021-12-21 13:43:53 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2022-01-21 10:40:48 +00:00
|
|
|
|
2022-07-14 11:40:50 +00:00
|
|
|
it('should create a ticket for each unique ticketFk in the sales', async() => {
|
2022-01-21 10:40:48 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-07-14 11:40:50 +00:00
|
|
|
const salesIds = [6, 7];
|
2022-01-21 10:40:48 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2022-07-14 11:40:50 +00:00
|
|
|
const tickets = await models.Sale.refund(salesIds, servicesIds, options);
|
|
|
|
|
|
|
|
const ticketsIds = tickets.map(ticket => ticket.id);
|
|
|
|
|
|
|
|
const refundedTickets = await models.Ticket.find({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
inq: ticketsIds
|
|
|
|
}
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'ticketSales',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'components'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'ticketServices',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const firstRefoundedTicket = refundedTickets[0];
|
|
|
|
const secondRefoundedTicket = refundedTickets[1];
|
|
|
|
const salesLength = firstRefoundedTicket.ticketSales().length;
|
|
|
|
const componentsLength = firstRefoundedTicket.ticketSales()[0].components().length;
|
|
|
|
const servicesLength = secondRefoundedTicket.ticketServices().length;
|
|
|
|
|
|
|
|
expect(refundedTickets.length).toEqual(2);
|
|
|
|
expect(salesLength).toEqual(1);
|
|
|
|
expect(componentsLength).toEqual(4);
|
|
|
|
expect(servicesLength).toBeGreaterThanOrEqual(1);
|
2022-01-21 10:40:48 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
2022-07-14 11:40:50 +00:00
|
|
|
throw e;
|
2022-01-21 10:40:48 +00:00
|
|
|
}
|
|
|
|
});
|
2021-12-21 13:43:53 +00:00
|
|
|
});
|