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-21 10:56:32 +00:00
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
describe('sale deleteSales()', () => {
|
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 of the given sales is not editable', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2018-08-21 10:56:32 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
let error;
|
2020-10-15 16:57:27 +00:00
|
|
|
try {
|
2021-09-29 06:27:18 +00:00
|
|
|
const options = {transaction: tx};
|
2019-03-01 10:50:26 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
2021-07-29 15:40:45 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const sales = [{id: 1, instance: 0}, {id: 2, instance: 1}];
|
|
|
|
const ticketId = 2;
|
2018-08-21 10:56:32 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await models.Sale.deleteSales(ctx, sales, ticketId, options);
|
2018-08-21 10:56:32 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
2018-08-21 10:56:32 +00:00
|
|
|
} catch (e) {
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
2018-08-21 10:56:32 +00:00
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toEqual(new Error(`The sales of this ticket can't be modified`));
|
|
|
|
});
|
|
|
|
|
2020-10-15 16:57:27 +00:00
|
|
|
it('should delete the sale', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const sale = await models.Sale.findOne({where: {id: 9}}, options);
|
|
|
|
sale.id = null;
|
|
|
|
const newSale = await models.Sale.create(sale, options);
|
2018-08-21 10:56:32 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const sales = [{id: newSale.id, instance: 0}];
|
|
|
|
const ticketId = 16;
|
2020-07-01 12:28:05 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const deletions = await models.Sale.deleteSales(ctx, sales, ticketId, options);
|
2018-08-21 10:56:32 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
expect(deletions).toEqual([{count: 1}]);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-08-21 10:56:32 +00:00
|
|
|
});
|
|
|
|
});
|