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-10 10:05:36 +00:00
|
|
|
|
|
|
|
describe('sale updateQuantity()', () => {
|
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-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};
|
|
|
|
|
2023-05-05 11:58:11 +00:00
|
|
|
const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*');
|
2022-10-05 13:13:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|