4804-ticket.descriptor_sms #1217

Merged
vicent merged 12 commits from 4804-ticket.descriptor_sms into dev 2022-12-21 11:30:02 +00:00
2 changed files with 42 additions and 12 deletions
Showing only changes of commit bd91d4a649 - Show all commits

View File

@ -26,11 +26,30 @@ module.exports = Self => {
Self.setSaleQuantity = async(saleId, quantity) => { Self.setSaleQuantity = async(saleId, quantity) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {};
let tx;
const sale = await models.Sale.findById(saleId); if (typeof options == 'object')
return await sale.updateAttributes({ Object.assign(myOptions, options);
originalQuantity: sale.quantity,
quantity: quantity if (!myOptions.transaction) {
}); tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const sale = await models.Sale.findById(saleId, null, myOptions);
const saleUpdated = await sale.updateAttributes({
originalQuantity: sale.quantity,
quantity: quantity
}, myOptions);
if (tx) await tx.commit();
return saleUpdated;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -2,15 +2,26 @@ const models = require('vn-loopback/server/server').models;
describe('setSaleQuantity()', () => { describe('setSaleQuantity()', () => {
it('should change quantity sale', async() => { it('should change quantity sale', async() => {
const saleId = 30; const tx = await models.Ticket.beginTransaction({});
const newQuantity = 10;
const originalSale = await models.Sale.findById(saleId); try {
const options = {transaction: tx};
await models.Collection.setSaleQuantity(saleId, newQuantity); const saleId = 30;
const updateSale = await models.Sale.findById(saleId); const newQuantity = 10;
expect(updateSale.originalQuantity).toEqual(originalSale.quantity); const originalSale = await models.Sale.findById(saleId, null, options);
expect(updateSale.quantity).toEqual(newQuantity);
await models.Collection.setSaleQuantity(saleId, newQuantity, options);
const updateSale = await models.Sale.findById(saleId, null, options);
expect(updateSale.originalQuantity).toEqual(originalSale.quantity);
expect(updateSale.quantity).toEqual(newQuantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });