refs #6199 feat(sale): increase quantity if newPrice is higher
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
da20697052
commit
d9ab607126
|
@ -18,7 +18,14 @@ describe('setSaleQuantity()', () => {
|
|||
|
||||
it('should change quantity sale', async() => {
|
||||
const tx = await models.Ticket.beginTransaction({});
|
||||
spyOn(models.Item, 'getVisibleAvailable').and.returnValue((new Promise(resolve => resolve({available: 100}))));
|
||||
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 100 as available;`;
|
||||
params = null;
|
||||
}
|
||||
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||
});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
|
|
@ -134,14 +134,6 @@ describe('Ticket Edit sale path', () => {
|
|||
await page.accessToSection('ticket.card.sale');
|
||||
});
|
||||
|
||||
it('should try to add a higher quantity value and then receive an error', async() => {
|
||||
await page.waitToClick(selectors.ticketSales.firstSaleQuantityCell);
|
||||
await page.type(selectors.ticketSales.firstSaleQuantity, '11\u000d');
|
||||
const message = await page.waitForSnackbar();
|
||||
|
||||
expect(message.text).toContain('The price of the item changed');
|
||||
});
|
||||
|
||||
it('should remove 1 from the first sale quantity', async() => {
|
||||
await page.waitToClick(selectors.ticketSales.firstSaleQuantityCell);
|
||||
await page.waitForSelector(selectors.ticketSales.firstSaleQuantity);
|
||||
|
|
|
@ -24,34 +24,6 @@ describe('sale updateQuantity()', () => {
|
|||
};
|
||||
}
|
||||
|
||||
it('should throw an error if the quantity is greater than it should be', async() => {
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: 1},
|
||||
headers: {origin: 'localhost:5000'},
|
||||
__: () => {}
|
||||
}
|
||||
};
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
||||
spyOn(models.Item, 'getVisibleAvailable').and.returnValue((new Promise(resolve => resolve({available: 100}))));
|
||||
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
|
||||
let error;
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
await models.Sale.updateQuantity(ctx, 17, 31, options);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toEqual(new Error('The price of the item changed'));
|
||||
});
|
||||
|
||||
it('should add quantity if the quantity is greater than it should be and is role advanced', async() => {
|
||||
const saleId = 17;
|
||||
const buyerId = 35;
|
||||
|
@ -64,7 +36,14 @@ describe('sale updateQuantity()', () => {
|
|||
};
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(buyerId));
|
||||
spyOn(models.Item, 'getVisibleAvailable').and.returnValue((new Promise(resolve => resolve({available: 100}))));
|
||||
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 100 as available;`;
|
||||
params = null;
|
||||
}
|
||||
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||
});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
@ -193,6 +172,14 @@ describe('sale updateQuantity()', () => {
|
|||
|
||||
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 100 as available;`;
|
||||
params = null;
|
||||
}
|
||||
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||
});
|
||||
|
||||
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
||||
|
||||
|
@ -226,7 +213,15 @@ describe('sale updateQuantity()', () => {
|
|||
|
||||
const item = await models.Item.findById(itemId, null, options);
|
||||
await item.updateAttribute('minQuantity', minQuantity, options);
|
||||
spyOn(models.Item, 'getVisibleAvailable').and.returnValue((new Promise(resolve => resolve({available: newQuantity}))));
|
||||
|
||||
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;`;
|
||||
params = null;
|
||||
}
|
||||
return models.Ticket.rawSql(sqlStatement, params, options);
|
||||
});
|
||||
|
||||
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
||||
|
||||
|
@ -236,4 +231,87 @@ describe('sale updateQuantity()', () => {
|
|||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it('should increase quantity if you have enough available and the new price is the same as 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;
|
||||
|
||||
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, 8 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 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({});
|
||||
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, 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();
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toEqual(new Error('The price of the item changed'));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -31,6 +31,8 @@ module.exports = Self => {
|
|||
|
||||
const ticketId = changes?.ticketFk || instance?.ticketFk;
|
||||
const itemId = changes?.itemFk || instance?.itemFk;
|
||||
const oldQuantity = instance?.quantity ?? null;
|
||||
const quantityAdded = newQuantity - oldQuantity;
|
||||
|
||||
const ticket = await models.Ticket.findById(
|
||||
ticketId,
|
||||
|
@ -65,20 +67,9 @@ module.exports = Self => {
|
|||
fields: ['family', 'minQuantity'],
|
||||
where: {id: itemId},
|
||||
}, ctx.options);
|
||||
|
||||
if (item.family == 'EMB') return;
|
||||
|
||||
if (newQuantity < item.minQuantity)
|
||||
throw new UserError('The amount cannot be less than the minimum');
|
||||
|
||||
const oldQuantity = instance?.quantity ?? null;
|
||||
const quantityAdded = newQuantity - oldQuantity;
|
||||
|
||||
if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return;
|
||||
|
||||
if (ctx.isNewInstance || newQuantity <= oldQuantity) return;
|
||||
|
||||
await Self.rawSql(`CALL catalog_calcFromItem(?,?,?,?)`, [
|
||||
await models.Sale.rawSql(`CALL catalog_calcFromItem(?,?,?,?)`, [
|
||||
ticket.landed,
|
||||
ticket.addressFk,
|
||||
ticket.agencyModeFk,
|
||||
|
@ -86,19 +77,27 @@ module.exports = Self => {
|
|||
],
|
||||
ctx.options);
|
||||
|
||||
const [itemInfo] = await Self.rawSql(`SELECT available FROM tmp.ticketCalculateItem`, null, ctx.options);
|
||||
if (itemInfo?.available < quantityAdded)
|
||||
const [itemInfo] = await models.Sale.rawSql(`SELECT available FROM tmp.ticketCalculateItem`, null, ctx.options);
|
||||
|
||||
if (!itemInfo?.available || itemInfo.available < quantityAdded)
|
||||
throw new UserError(`This item is not available`);
|
||||
|
||||
const [saleGrouping] = await Self.rawSql(`
|
||||
if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return;
|
||||
|
||||
if (newQuantity < item.minQuantity && newQuantity != itemInfo?.available)
|
||||
throw new UserError('The amount cannot be less than the minimum');
|
||||
|
||||
if (ctx.isNewInstance || newQuantity <= oldQuantity) return;
|
||||
|
||||
const [saleGrouping] = await models.Sale.rawSql(`
|
||||
SELECT MAX(t.grouping), t.price newPrice
|
||||
FROM tmp.ticketComponentPrice t
|
||||
WHERE t.grouping <= ?`,
|
||||
[quantityAdded],
|
||||
ctx.options);
|
||||
|
||||
await Self.rawSql(`
|
||||
DROP TEMPORARY TABLE
|
||||
await models.Sale.rawSql(`
|
||||
DROP TEMPORARY TABLE IF EXISTS
|
||||
tmp.ticketCalculateItem,
|
||||
tmp.ticketComponentPrice,
|
||||
tmp.ticketComponent,
|
||||
|
@ -106,7 +105,7 @@ module.exports = Self => {
|
|||
tmp.zoneGetShipped;
|
||||
`, null, ctx.options);
|
||||
|
||||
if (!saleGrouping?.newPrice || instance.price < saleGrouping.newPrice)
|
||||
if (!saleGrouping?.newPrice || saleGrouping.newPrice < instance.price)
|
||||
throw new UserError('The price of the item changed');
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue