refs #6257 fix(saleQuantity): newPrice correct restricction
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
d5f98700e7
commit
84aed7ca21
|
@ -232,86 +232,128 @@ describe('sale updateQuantity()', () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should increase quantity if you have enough available and the new price is the same as the previous one', async() => {
|
describe('newPrice', () => {
|
||||||
const ctx = {
|
it('should increase quantity if you have enough available and the new price is the same as the previous one', async() => {
|
||||||
req: {
|
const ctx = {
|
||||||
accessToken: {userId: 1},
|
req: {
|
||||||
headers: {origin: 'localhost:5000'},
|
accessToken: {userId: 1},
|
||||||
__: () => {}
|
headers: {origin: 'localhost:5000'},
|
||||||
}
|
__: () => {}
|
||||||
};
|
}
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
};
|
||||||
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
||||||
|
|
||||||
const tx = await models.Sale.beginTransaction({});
|
const tx = await models.Sale.beginTransaction({});
|
||||||
const itemId = 2;
|
const itemId = 2;
|
||||||
const saleId = 17;
|
const saleId = 17;
|
||||||
const minQuantity = 30;
|
const minQuantity = 30;
|
||||||
const newQuantity = 31;
|
const newQuantity = 31;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
const item = await models.Item.findById(itemId, null, options);
|
const item = await models.Item.findById(itemId, null, options);
|
||||||
await item.updateAttribute('minQuantity', minQuantity, options);
|
await item.updateAttribute('minQuantity', minQuantity, options);
|
||||||
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
|
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
|
||||||
if (sqlStatement.includes('catalog_calcFromItem')) {
|
if (sqlStatement.includes('catalog_calcFromItem')) {
|
||||||
sqlStatement = `
|
sqlStatement = `
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY SELECT ${newQuantity} as available;
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY SELECT ${newQuantity} as available;
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketComponentPrice ENGINE = MEMORY SELECT 1 as grouping, 8 as price;`;
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketComponentPrice ENGINE = MEMORY SELECT 1 as grouping, 7.07 as price;`;
|
||||||
params = null;
|
params = null;
|
||||||
}
|
}
|
||||||
return models.Ticket.rawSql(sqlStatement, params, options);
|
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||||
});
|
});
|
||||||
|
|
||||||
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
||||||
|
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw error when increase quantity and the new price is lower than the previous one', async() => {
|
|
||||||
const ctx = {
|
|
||||||
req: {
|
|
||||||
accessToken: {userId: 1},
|
|
||||||
headers: {origin: 'localhost:5000'},
|
|
||||||
__: () => {}
|
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
|
||||||
|
|
||||||
const tx = await models.Sale.beginTransaction({});
|
it('should increase quantity when the new price is lower than the previous one', async() => {
|
||||||
const itemId = 2;
|
const ctx = {
|
||||||
const saleId = 17;
|
req: {
|
||||||
const minQuantity = 30;
|
accessToken: {userId: 1},
|
||||||
const newQuantity = 31;
|
headers: {origin: 'localhost:5000'},
|
||||||
|
__: () => {}
|
||||||
let error;
|
|
||||||
try {
|
|
||||||
const options = {transaction: tx};
|
|
||||||
|
|
||||||
const item = await models.Item.findById(itemId, null, options);
|
|
||||||
await item.updateAttribute('minQuantity', minQuantity, options);
|
|
||||||
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
|
|
||||||
if (sqlStatement.includes('catalog_calcFromItem')) {
|
|
||||||
sqlStatement = `
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY SELECT ${newQuantity} as available;
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketComponentPrice ENGINE = MEMORY SELECT 1 as grouping, 1 as price;`;
|
|
||||||
params = null;
|
|
||||||
}
|
}
|
||||||
return models.Ticket.rawSql(sqlStatement, params, options);
|
};
|
||||||
});
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
||||||
|
|
||||||
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
const tx = await models.Sale.beginTransaction({});
|
||||||
|
const itemId = 2;
|
||||||
|
const saleId = 17;
|
||||||
|
const minQuantity = 30;
|
||||||
|
const newQuantity = 31;
|
||||||
|
|
||||||
await tx.rollback();
|
try {
|
||||||
} catch (e) {
|
const options = {transaction: tx};
|
||||||
await tx.rollback();
|
|
||||||
error = e;
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error).toEqual(new Error('The price of the item changed'));
|
const item = await models.Item.findById(itemId, null, options);
|
||||||
|
await item.updateAttribute('minQuantity', minQuantity, options);
|
||||||
|
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
|
||||||
|
if (sqlStatement.includes('catalog_calcFromItem')) {
|
||||||
|
sqlStatement = `
|
||||||
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY SELECT ${newQuantity} as available;
|
||||||
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketComponentPrice ENGINE = MEMORY SELECT 1 as grouping, 1 as price;`;
|
||||||
|
params = null;
|
||||||
|
}
|
||||||
|
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||||
|
});
|
||||||
|
|
||||||
|
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error when increase quantity and the new price is higher than the previous one', async() => {
|
||||||
|
const ctx = {
|
||||||
|
req: {
|
||||||
|
accessToken: {userId: 1},
|
||||||
|
headers: {origin: 'localhost:5000'},
|
||||||
|
__: () => {}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
||||||
|
|
||||||
|
const tx = await models.Sale.beginTransaction({});
|
||||||
|
const itemId = 2;
|
||||||
|
const saleId = 17;
|
||||||
|
const minQuantity = 30;
|
||||||
|
const newQuantity = 31;
|
||||||
|
|
||||||
|
let error;
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
|
const item = await models.Item.findById(itemId, null, options);
|
||||||
|
await item.updateAttribute('minQuantity', minQuantity, options);
|
||||||
|
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
|
||||||
|
if (sqlStatement.includes('catalog_calcFromItem')) {
|
||||||
|
sqlStatement = `
|
||||||
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY SELECT ${newQuantity} as available;
|
||||||
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketComponentPrice ENGINE = MEMORY SELECT 1 as grouping, 100000 as price;`;
|
||||||
|
params = null;
|
||||||
|
}
|
||||||
|
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||||
|
});
|
||||||
|
|
||||||
|
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).toEqual(new Error('The price of the item changed'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -106,7 +106,7 @@ module.exports = Self => {
|
||||||
tmp.zoneGetShipped;
|
tmp.zoneGetShipped;
|
||||||
`, null, ctx.options);
|
`, null, ctx.options);
|
||||||
|
|
||||||
if (!saleGrouping?.newPrice || saleGrouping.newPrice < instance.price)
|
if (!saleGrouping?.newPrice || saleGrouping.newPrice > instance.price)
|
||||||
throw new UserError('The price of the item changed');
|
throw new UserError('The price of the item changed');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue