2019-01-24 08:08:28 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-06-11 10:23:55 +00:00
|
|
|
describe('sale moveToTicket()', () => {
|
2019-05-24 13:30:50 +00:00
|
|
|
let createdTicketId;
|
|
|
|
|
|
|
|
afterAll(async done => {
|
|
|
|
await app.models.Ticket.destroyById(createdTicketId);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-02-06 15:37:59 +00:00
|
|
|
it('should throw an error if the ticket is not editable', async() => {
|
2019-05-24 13:30:50 +00:00
|
|
|
const ctx = {req: {accessToken: {userId: 101}}};
|
2018-08-17 11:23:07 +00:00
|
|
|
let error;
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
const params = {currentTicket: {currentTicketId: 10}};
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
await app.models.Sale.moveToTicket(ctx, params)
|
2018-08-17 11:23:07 +00:00
|
|
|
.catch(response => {
|
2019-05-24 13:30:50 +00:00
|
|
|
expect(response.message).toEqual(`The sales of the current ticket can't be modified`);
|
2018-08-17 11:23:07 +00:00
|
|
|
error = response;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2019-02-06 15:37:59 +00:00
|
|
|
it('should throw an error if the receiving ticket is not editable', async() => {
|
2019-05-24 13:30:50 +00:00
|
|
|
const ctx = {req: {accessToken: {userId: 101}}};
|
2018-08-17 11:23:07 +00:00
|
|
|
let error;
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
const params = {currentTicket: {currentTicketId: 16}, receiverTicket: {id: 1}};
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
await app.models.Sale.moveToTicket(ctx, params)
|
2018-08-17 11:23:07 +00:00
|
|
|
.catch(response => {
|
2019-05-24 13:30:50 +00:00
|
|
|
expect(response.message).toEqual(`The sales of the receiver ticket can't be modified`);
|
2018-08-17 11:23:07 +00:00
|
|
|
error = response;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
it('should transfer the sales from one ticket to a new one', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: 101}}};
|
|
|
|
let currentTicket = await app.models.Ticket.findById(8);
|
|
|
|
currentTicket.currentTicketId = currentTicket.id;
|
|
|
|
currentTicket.id = undefined;
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
let currentTicketSales = await app.models.Ticket.getSales(currentTicket.currentTicketId);
|
|
|
|
|
|
|
|
expect(currentTicketSales.length).toEqual(2);
|
2018-08-17 11:23:07 +00:00
|
|
|
|
|
|
|
let params = {
|
2019-05-24 13:30:50 +00:00
|
|
|
currentTicket: currentTicket,
|
|
|
|
receiverTicket: {id: undefined},
|
2018-08-17 11:23:07 +00:00
|
|
|
sales: [
|
2019-05-24 13:30:50 +00:00
|
|
|
{id: 13},
|
|
|
|
{id: 14}
|
|
|
|
]
|
2018-08-17 11:23:07 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
let createdTicket = await app.models.Sale.moveToTicket(ctx, params);
|
|
|
|
createdTicketId = createdTicket.id;
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
currentTicketSales = await app.models.Ticket.getSales(currentTicket.currentTicketId);
|
|
|
|
receiverTicketSales = await app.models.Ticket.getSales(createdTicket.id);
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
expect(currentTicketSales.length).toEqual(0);
|
2018-09-21 13:01:51 +00:00
|
|
|
expect(receiverTicketSales.length).toEqual(2);
|
2018-08-17 11:23:07 +00:00
|
|
|
});
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
it('should transfers back the sales and set the created ticket as deleted', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: 101}}};
|
|
|
|
let receiverTicketId = 8;
|
|
|
|
let currentTicket = await app.models.Ticket.findById(createdTicketId);
|
|
|
|
currentTicket.currentTicketId = createdTicketId;
|
|
|
|
currentTicket.id = undefined;
|
|
|
|
|
|
|
|
let createdTicket = await app.models.Ticket.findById(createdTicketId);
|
|
|
|
let createdTicketSales = await app.models.Ticket.getSales(createdTicketId);
|
|
|
|
let receiverTicketSales = await app.models.Ticket.getSales(receiverTicketId);
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
expect(createdTicket.isDeleted).toBeFalsy();
|
|
|
|
expect(createdTicketSales.length).toEqual(2);
|
2018-08-17 11:23:07 +00:00
|
|
|
expect(receiverTicketSales.length).toEqual(0);
|
|
|
|
|
|
|
|
let params = {
|
2019-05-24 13:30:50 +00:00
|
|
|
removeEmptyTicket: true,
|
|
|
|
currentTicket: currentTicket,
|
|
|
|
receiverTicket: {id: receiverTicketId},
|
2018-08-17 11:23:07 +00:00
|
|
|
sales: [
|
2019-05-24 13:30:50 +00:00
|
|
|
{id: 13},
|
|
|
|
{id: 14}
|
|
|
|
]
|
2018-08-17 11:23:07 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
await app.models.Sale.moveToTicket(ctx, params);
|
|
|
|
|
|
|
|
createdTicket = await app.models.Ticket.findById(createdTicketId);
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
createdTicketSales = await app.models.Ticket.getSales(createdTicketId);
|
|
|
|
receiverTicketSales = await app.models.Ticket.getSales(receiverTicketId);
|
2018-08-17 11:23:07 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
expect(createdTicket.isDeleted).toBeTruthy();
|
|
|
|
expect(createdTicketSales.length).toEqual(0);
|
2018-09-21 13:01:51 +00:00
|
|
|
expect(receiverTicketSales.length).toEqual(2);
|
2018-08-17 11:23:07 +00:00
|
|
|
});
|
|
|
|
});
|