From 815826e891d069c71769d17f486704e3d1f39b1e Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 25 Nov 2021 11:23:18 +0100 Subject: [PATCH] refactor(ticket): modify transferSales.js for avoid unnecessary logs --- .../back/methods/ticket/transferSales.js | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/modules/ticket/back/methods/ticket/transferSales.js b/modules/ticket/back/methods/ticket/transferSales.js index 49529135c..6d55f764f 100644 --- a/modules/ticket/back/methods/ticket/transferSales.js +++ b/modules/ticket/back/methods/ticket/transferSales.js @@ -85,9 +85,10 @@ module.exports = Self => { }; if (sale.quantity == originalSale.quantity) { - await models.Sale.updateAll({ - id: sale.id - }, {ticketFk: ticketId}, myOptions); + query = `UPDATE sale + SET ticketFk = ? + WHERE id = ?`; + await Self.rawSql(query, [ticketId, sale.id], myOptions); } else if (sale.quantity != originalSale.quantity) { await transferPartialSale( ticketId, originalSale, sale, myOptions); @@ -170,29 +171,31 @@ module.exports = Self => { // Update original sale const rest = originalSale.quantity - sale.quantity; - const originalInstance = await models.Sale.findById(sale.id, null, options); - await originalInstance.updateAttribute('quantity', rest, options); + query = `UPDATE sale + SET quantity = ? + WHERE id = ?`; + await Self.rawSql(query, [rest, sale.id], options); // Clone sale with new quantity - const newSale = originalSale; - newSale.id = undefined; - newSale.ticketFk = ticketId; - newSale.quantity = sale.quantity; - - const createdSale = await models.Sale.create(newSale, options); + query = `INSERT INTO sale (itemFk, ticketFk, concept, quantity, originalQuantity, price, discount, priceFixed, + reserved, isPicked, isPriceFixed, isAdded) + SELECT itemFk, ?, concept, ?, originalQuantity, price, discount, priceFixed, + reserved, isPicked, isPriceFixed, isAdded + FROM sale + WHERE id = ?`; + await Self.rawSql(query, [ticketId, sale.quantity, sale.id], options); + const [lastInsertedSale] = await Self.rawSql('SELECT LAST_INSERT_ID() AS id', null, options); // Clone sale components const saleComponents = await models.SaleComponent.find({ where: {saleFk: sale.id} }, options); const newComponents = saleComponents.map(component => { - component.saleFk = createdSale.id; + component.saleFk = lastInsertedSale.id; return component; }); await models.SaleComponent.create(newComponents, options); - - return originalInstance; } };