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) => {
query = `CALL vn.sale_setQuantity(?,?)`;
const result = await Self.rawSql(query, [sale, quantity]);
Self.setQuantitySale = async(sale, quantity, options) => {
const myOptions = {};
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;
fdescribe('setQuantitySale()', () => {
describe('setQuantitySale()', () => {
it('should change quantity sale', async() => {
const saleId = 1;
const newQuantity = 1;
const originalSale = await models.Sale.findById(saleId);
const tx = await models.Sale.beginTransaction({});
await models.Collection.setQuantitySale(saleId, newQuantity);
const updateSale = await models.Sale.findById(saleId);
try {
const options = {transaction: tx};
const saleId = 30;
const newQuantity = 5;
const originalSale = await models.Sale.findById(saleId);
expect(updateSale.quantity).toBeLessThan(originalSale.quantity);
expect(updateSale.quantity).toEqual(newQuantity);
await models.Collection.setQuantitySale(saleId, newQuantity, options);
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;
}
});
});