feat(setQuantitySale): transaction
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-02-02 14:55:21 +01:00
parent 9296ff83aa
commit 386155f242
2 changed files with 40 additions and 12 deletions

View File

@ -24,10 +24,28 @@ module.exports = Self => {
} }
}); });
Self.setQuantitySale = async(sale, quantity) => { Self.setQuantitySale = async(sale, quantity, options) => {
query = `CALL vn.sale_setQuantity(?,?)`; const myOptions = {};
const result = await Self.rawSql(query, [sale, quantity]); let tx;
return result; if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
query = `CALL vn.sale_setQuantity(?,?)`;
const result = await Self.rawSql(query, [sale, quantity]);
if (tx) await tx.commit();
return result;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -1,15 +1,25 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
fdescribe('setQuantitySale()', () => { describe('setQuantitySale()', () => {
it('should change quantity sale', async() => { it('should change quantity sale', async() => {
const saleId = 1; const tx = await models.Sale.beginTransaction({});
const newQuantity = 1;
const originalSale = await models.Sale.findById(saleId);
await models.Collection.setQuantitySale(saleId, newQuantity); try {
const updateSale = await models.Sale.findById(saleId); const options = {transaction: tx};
const saleId = 30;
const newQuantity = 5;
const originalSale = await models.Sale.findById(saleId);
expect(updateSale.quantity).toBeLessThan(originalSale.quantity); await models.Collection.setQuantitySale(saleId, newQuantity, options);
expect(updateSale.quantity).toEqual(newQuantity); const updateSale = await models.Sale.findById(saleId);
expect(updateSale.quantity).toBeLessThan(originalSale.quantity);
expect(updateSale.quantity).toEqual(newQuantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });