From a11ef73dfbb70a780dc503767cab76c3e7476112 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 08:01:55 +0200 Subject: [PATCH] refactor: code repetead --- .../back/methods/ticket/updateDiscount.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index 99daac5127..d452b03fef 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -141,17 +141,9 @@ module.exports = Self => { }; await models.SaleComponent.destroyAll(filter, myOptions); - newComponent = models.SaleComponent.create({ - saleFk: sale.id, - value: value, - componentFk: componentId - }, myOptions); - } else { - newComponent = models.SaleComponent.create({ - saleFk: sale.id, - value: value, - componentFk: componentId}, myOptions); - } + await createSaleComponent(sale.id, value, componentId, myOptions); + } else + await createSaleComponent(sale.id, value, componentId, myOptions); const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions); @@ -198,4 +190,14 @@ module.exports = Self => { throw e; } }; + + async function createSaleComponent(saleId, value, componentId, myOptions) { + const models = Self.app.models; + + newComponent = models.SaleComponent.create({ + saleFk: saleId, + value: value, + componentFk: componentId + }, myOptions); + } };