3570-feat(setQuantitySale): back route and test #869

Merged
joan merged 9 commits from 3558-setQuantitySale into dev 2022-02-08 12:11:24 +00:00
2 changed files with 40 additions and 12 deletions
Showing only changes of commit 386155f242 - Show all commits

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;
}
alexm marked this conversation as resolved Outdated
Outdated
Review

No need to create a new transaction for a single modification

No need to create a new transaction for a single modification
try {
query = `CALL vn.sale_setQuantity(?,?)`;
const result = await Self.rawSql(query, [sale, quantity]);
alexm marked this conversation as resolved Outdated
Outdated
Review

Try-catch block not needed

Try-catch block not needed
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);
alexm marked this conversation as resolved Outdated
Outdated
Review

Should use the saleId constant instead of "30"

Should use the saleId constant instead of "30"
await tx.rollback();
} catch (e) {
await tx.rollback();
alexm marked this conversation as resolved Outdated
Outdated
Review

Should use the saleId constant instead of "30"

Should use the saleId constant instead of "30"
throw e;
}
});
});