2019-07-25 07:55:09 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
|
|
|
|
|
|
|
describe('ticket-request confirm()', () => {
|
2020-01-23 12:31:07 +00:00
|
|
|
let ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ctx.req.__ = value => {
|
|
|
|
return value;
|
|
|
|
};
|
2019-07-25 07:55:09 +00:00
|
|
|
|
|
|
|
it(`should throw an error if the item doesn't exist`, async() => {
|
2020-01-23 12:31:07 +00:00
|
|
|
ctx.args = {itemFk: 999};
|
2019-07-25 07:55:09 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
let error;
|
2019-07-25 07:55:09 +00:00
|
|
|
try {
|
|
|
|
await app.models.TicketRequest.confirm(ctx);
|
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toEqual(`That item doesn't exists`);
|
|
|
|
});
|
|
|
|
|
2020-02-27 11:31:38 +00:00
|
|
|
it('should throw an error if the item is not available', async() => {
|
2019-07-31 05:46:24 +00:00
|
|
|
const requestId = 5;
|
|
|
|
const itemId = 4;
|
2019-07-25 07:55:09 +00:00
|
|
|
const quantity = 99999;
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
ctx.args = {
|
2019-07-25 07:55:09 +00:00
|
|
|
itemFk: itemId,
|
|
|
|
id: requestId,
|
|
|
|
quantity: quantity
|
2020-01-23 12:31:07 +00:00
|
|
|
};
|
|
|
|
|
2019-07-25 07:55:09 +00:00
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await app.models.TicketRequest.confirm(ctx);
|
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toEqual(`This item is not available`);
|
|
|
|
});
|
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
it(`should throw if there's a sale id`, async() => {
|
2019-07-25 07:55:09 +00:00
|
|
|
const requestId = 4;
|
2020-10-06 08:15:27 +00:00
|
|
|
const itemId = 1;
|
2019-07-25 07:55:09 +00:00
|
|
|
const quantity = 10;
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
ctx.args = {
|
|
|
|
itemFk: itemId,
|
|
|
|
id: requestId,
|
|
|
|
quantity: quantity
|
|
|
|
};
|
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
const request = await app.models.TicketRequest.findById(requestId);
|
|
|
|
|
|
|
|
expect(request.saleFk).toBeNull();
|
|
|
|
|
|
|
|
await request.updateAttributes({saleFk: 2});
|
|
|
|
|
|
|
|
ctx.args = {
|
|
|
|
itemFk: itemId,
|
|
|
|
id: requestId,
|
|
|
|
quantity: quantity
|
|
|
|
};
|
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
let error;
|
2019-07-25 07:55:09 +00:00
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
try {
|
|
|
|
await app.models.TicketRequest.confirm(ctx);
|
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
2019-07-25 07:55:09 +00:00
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
expect(error.message).toEqual(`This request already contains a sale`);
|
2020-10-06 08:15:27 +00:00
|
|
|
|
|
|
|
// restores
|
|
|
|
await request.updateAttributes({saleFk: null});
|
2019-07-25 07:55:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`should create a new sale for the the request if there's no sale id`, async() => {
|
|
|
|
const requestId = 4;
|
|
|
|
const itemId = 1;
|
|
|
|
const quantity = 10;
|
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
const originalRequest = await app.models.TicketRequest.findById(requestId);
|
2020-10-02 07:39:58 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
ctx.args = {
|
2019-07-25 07:55:09 +00:00
|
|
|
itemFk: itemId,
|
|
|
|
id: requestId,
|
2020-01-23 12:31:07 +00:00
|
|
|
quantity: quantity
|
|
|
|
};
|
2019-07-25 07:55:09 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
const request = await app.models.TicketRequest.findById(requestId);
|
|
|
|
await request.updateAttributes({saleFk: null});
|
2019-07-25 07:55:09 +00:00
|
|
|
await app.models.TicketRequest.confirm(ctx);
|
|
|
|
|
2020-10-06 08:15:27 +00:00
|
|
|
const updatedRequest = await app.models.TicketRequest.findById(requestId);
|
|
|
|
const createdSaleId = updatedRequest.saleFk;
|
2019-07-25 07:55:09 +00:00
|
|
|
|
|
|
|
expect(updatedRequest.saleFk).toEqual(createdSaleId);
|
|
|
|
expect(updatedRequest.isOk).toEqual(true);
|
|
|
|
expect(updatedRequest.itemFk).toEqual(itemId);
|
2020-10-06 08:15:27 +00:00
|
|
|
|
|
|
|
// restores
|
|
|
|
await originalRequest.updateAttributes(originalRequest);
|
|
|
|
await app.models.Sale.destroyById(createdSaleId);
|
|
|
|
await app.models.Sale.destroyById(createdSaleId);
|
|
|
|
|
|
|
|
await app.models.Item.rawSql(`
|
|
|
|
TRUNCATE TABLE cache.last_buy
|
|
|
|
`);
|
2019-07-25 07:55:09 +00:00
|
|
|
});
|
|
|
|
});
|