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) => {
|
||||
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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue