2024-01-03 14:36:39 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2024-06-12 06:25:50 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2024-01-03 14:36:39 +00:00
|
|
|
|
|
|
|
describe('Ticket cloning - clone function', () => {
|
2024-06-12 06:25:50 +00:00
|
|
|
let ctx;
|
2024-01-03 14:36:39 +00:00
|
|
|
let options;
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
beforeEach(async() => {
|
2024-06-12 06:25:50 +00:00
|
|
|
ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
},
|
|
|
|
args: {}
|
|
|
|
};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: ctx.req
|
|
|
|
});
|
|
|
|
|
2024-01-03 14:36:39 +00:00
|
|
|
options = {transaction: tx};
|
|
|
|
tx = await models.Sale.beginTransaction({});
|
|
|
|
options.transaction = tx;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async() => {
|
2024-01-04 07:37:34 +00:00
|
|
|
await tx.rollback();
|
2024-01-03 14:36:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create new tickets with cloned sales with warehouse', async() => {
|
|
|
|
const salesIds = [1, 2, 3];
|
|
|
|
const servicesIds = [];
|
|
|
|
const withWarehouse = true;
|
|
|
|
const negative = false;
|
2024-01-04 07:37:34 +00:00
|
|
|
const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, withWarehouse, negative, options);
|
2024-01-03 14:36:39 +00:00
|
|
|
|
|
|
|
expect(newTickets).toBeDefined();
|
|
|
|
expect(newTickets.length).toBeGreaterThan(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle negative quantities correctly', async() => {
|
|
|
|
const negative = true;
|
|
|
|
const salesIds = [7, 8];
|
2024-01-04 07:37:34 +00:00
|
|
|
const servicesIds = [];
|
|
|
|
const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, false, negative, options);
|
2024-01-03 14:36:39 +00:00
|
|
|
|
|
|
|
for (const ticket of newTickets) {
|
|
|
|
const sales = await models.Sale.find({where: {ticketFk: ticket.id}}, options);
|
|
|
|
sales.forEach(sale => {
|
|
|
|
expect(sale.quantity).toBeLessThan(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create new components and services for cloned tickets', async() => {
|
|
|
|
const servicesIds = [2];
|
|
|
|
const salesIds = [5];
|
2024-01-04 07:37:34 +00:00
|
|
|
const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, false, false, options);
|
2024-01-03 14:36:39 +00:00
|
|
|
|
|
|
|
for (const ticket of newTickets) {
|
|
|
|
const sale = await models.Sale.findOne({where: {ticketFk: ticket.id}}, options);
|
|
|
|
const components = await models.SaleComponent.find({where: {saleFk: sale.id}}, options);
|
|
|
|
const services = await models.TicketService.find({where: {ticketFk: ticket.id}}, options);
|
|
|
|
|
|
|
|
expect(components.length).toBeGreaterThan(0);
|
|
|
|
expect(services.length).toBeGreaterThan(0);
|
|
|
|
}
|
|
|
|
});
|
2024-01-31 08:59:32 +00:00
|
|
|
|
|
|
|
it('should create a ticket without sales', async() => {
|
|
|
|
const servicesIds = [4];
|
2024-02-05 08:35:20 +00:00
|
|
|
|
|
|
|
const tickets = await models.Sale.clone(ctx, null, servicesIds, false, false, options);
|
|
|
|
const refundedTicket = await getTicketRefund(tickets[0].id, options);
|
|
|
|
|
|
|
|
expect(refundedTicket).toBeDefined();
|
2024-01-31 08:59:32 +00:00
|
|
|
});
|
2024-01-03 14:36:39 +00:00
|
|
|
});
|
2024-01-31 08:59:32 +00:00
|
|
|
|
|
|
|
async function getTicketRefund(id, options) {
|
|
|
|
return models.Ticket.findOne({
|
|
|
|
where: {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'ticketSales',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'components'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'ticketServices',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, options);
|
|
|
|
}
|