2021-09-29 06:27:18 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2018-08-10 10:05:36 +00:00
|
|
|
|
|
|
|
describe('sale updateQuantity()', () => {
|
2021-07-29 15:40:45 +00:00
|
|
|
const ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2018-08-10 10:05:36 +00:00
|
|
|
it('should throw an error if the quantity is greater than it should be', async() => {
|
2022-10-05 13:13:36 +00:00
|
|
|
const ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 1},
|
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
2018-08-10 10:05:36 +00:00
|
|
|
let error;
|
2021-09-29 06:27:18 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
await models.Sale.updateQuantity(ctx, 17, 99, options);
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toEqual(new Error('The new quantity should be smaller than the old one'));
|
2018-08-10 10:05:36 +00:00
|
|
|
});
|
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
it('should add quantity if the quantity is greater than it should be and is role advanced', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
const saleId = 17;
|
|
|
|
const buyerId = 35;
|
|
|
|
const ctx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: buyerId},
|
|
|
|
headers: {origin: 'localhost:5000'},
|
|
|
|
__: () => {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
const isRoleAdvanced = await models.Ticket.isRoleAdvanced(ctx, options);
|
|
|
|
|
|
|
|
expect(isRoleAdvanced).toEqual(true);
|
|
|
|
|
|
|
|
const originalLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
|
|
|
|
|
|
|
|
expect(originalLine.quantity).toEqual(30);
|
|
|
|
|
|
|
|
const newQuantity = originalLine.quantity + 1;
|
|
|
|
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
|
|
|
|
|
|
|
const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
|
|
|
|
|
|
|
|
expect(modifiedLine.quantity).toEqual(newQuantity);
|
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-10 10:05:36 +00:00
|
|
|
it('should update the quantity of a given sale current line', async() => {
|
2021-09-29 06:27:18 +00:00
|
|
|
const tx = await models.Sale.beginTransaction({});
|
2022-10-05 13:13:36 +00:00
|
|
|
const saleId = 25;
|
|
|
|
const newQuantity = 4;
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
const originalLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
expect(originalLine.quantity).toEqual(20);
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2022-10-05 13:13:36 +00:00
|
|
|
expect(modifiedLine.quantity).toEqual(newQuantity);
|
2018-08-10 10:05:36 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-08-10 10:05:36 +00:00
|
|
|
});
|
|
|
|
});
|