feat(setQuantitySale): transaction
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
9296ff83aa
commit
386155f242
|
@ -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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue