2021-09-29 06:27:18 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2023-03-23 09:05:02 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
describe('sale reserve()', () => {
|
2021-07-29 15:40:45 +00:00
|
|
|
const ctx = {
|
|
|
|
req: {
|
2022-10-18 09:50:16 +00:00
|
|
|
accessToken: {userId: 1},
|
2021-07-29 15:40:45 +00:00
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-23 09:05:02 +00:00
|
|
|
beforeAll(async() => {
|
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
it('should throw an error if the ticket can not be modified', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2019-03-01 10:50:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const ticketId = 2;
|
|
|
|
const sales = [{id: 5}];
|
|
|
|
const reserved = false;
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Sale.reserve(ctx, ticketId, sales, reserved, options);
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
error = e;
|
|
|
|
}
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(error).toEqual(new Error(`The sales of this ticket can't be modified`));
|
2018-08-16 12:00:37 +00:00
|
|
|
});
|
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
it('should update the given sales of a ticket to reserved', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2023-06-01 06:32:06 +00:00
|
|
|
originalTicketSales = await models.Ticket.getSales(ctx, 11, options);
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(originalTicketSales[0].reserved).toEqual(false);
|
|
|
|
expect(originalTicketSales[1].reserved).toEqual(false);
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const ticketId = 11;
|
|
|
|
const sales = [{id: 7}, {id: 8}];
|
|
|
|
const reserved = true;
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Sale.reserve(ctx, ticketId, sales, reserved, options);
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2023-06-01 06:32:06 +00:00
|
|
|
const reservedTicketSales = await models.Ticket.getSales(ctx, ticketId, options);
|
2018-08-16 12:00:37 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(reservedTicketSales[0].reserved).toEqual(true);
|
|
|
|
expect(reservedTicketSales[1].reserved).toEqual(true);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-08-16 12:00:37 +00:00
|
|
|
});
|
|
|
|
});
|