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
|
|
|
|
2023-08-01 10:45:57 +00:00
|
|
|
fdescribe('Sale refund()', () => {
|
2022-07-14 11:40:50 +00:00
|
|
|
const userId = 5;
|
2023-06-01 06:32:06 +00:00
|
|
|
const ctx = {req: {accessToken: userId}};
|
2022-07-14 11:40:50 +00:00
|
|
|
const activeCtx = {
|
2023-06-01 06:32:06 +00:00
|
|
|
accessToken: {userId},
|
2022-07-14 11:40:50 +00:00
|
|
|
};
|
|
|
|
const servicesIds = [3];
|
2023-05-26 10:03:36 +00:00
|
|
|
const withWarehouse = true;
|
2022-07-14 11:40:50 +00:00
|
|
|
|
|
|
|
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};
|
|
|
|
|
2023-06-09 07:03:23 +00:00
|
|
|
const refundedTicket = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options);
|
2021-12-21 13:43:53 +00:00
|
|
|
|
2023-04-24 06:57:10 +00:00
|
|
|
expect(refundedTicket).toBeDefined();
|
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
|
|
|
|
2023-04-24 06:57:10 +00:00
|
|
|
it('should create one 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};
|
|
|
|
|
2023-06-09 07:03:23 +00:00
|
|
|
const ticket = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options);
|
2022-07-14 11:40:50 +00:00
|
|
|
|
2023-04-24 06:57:10 +00:00
|
|
|
const refundedTicket = await models.Ticket.findOne({
|
2022-07-14 11:40:50 +00:00
|
|
|
where: {
|
2023-04-24 06:57:10 +00:00
|
|
|
id: ticket.id
|
2022-07-14 11:40:50 +00:00
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'ticketSales',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'components'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'ticketServices',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, options);
|
|
|
|
|
2023-04-24 06:57:10 +00:00
|
|
|
const salesLength = refundedTicket.ticketSales().length;
|
|
|
|
const componentsLength = refundedTicket.ticketSales()[0].components().length;
|
2022-07-14 11:40:50 +00:00
|
|
|
|
2023-04-24 06:57:10 +00:00
|
|
|
expect(refundedTicket).toBeDefined();
|
|
|
|
expect(salesLength).toEqual(2);
|
2022-07-14 11:40:50 +00:00
|
|
|
expect(componentsLength).toEqual(4);
|
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
|
|
|
});
|