refactor(ticket): modify transferSales.js for avoid unnecessary logs
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2021-11-25 11:23:18 +01:00
parent 537c1bb664
commit 815826e891
1 changed files with 17 additions and 14 deletions

View File

@ -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;
}
};