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) { if (sale.quantity == originalSale.quantity) {
await models.Sale.updateAll({ query = `UPDATE sale
id: sale.id SET ticketFk = ?
}, {ticketFk: ticketId}, myOptions); WHERE id = ?`;
await Self.rawSql(query, [ticketId, sale.id], myOptions);
} else if (sale.quantity != originalSale.quantity) { } else if (sale.quantity != originalSale.quantity) {
await transferPartialSale( await transferPartialSale(
ticketId, originalSale, sale, myOptions); ticketId, originalSale, sale, myOptions);
@ -170,29 +171,31 @@ module.exports = Self => {
// Update original sale // Update original sale
const rest = originalSale.quantity - sale.quantity; const rest = originalSale.quantity - sale.quantity;
const originalInstance = await models.Sale.findById(sale.id, null, options); query = `UPDATE sale
await originalInstance.updateAttribute('quantity', rest, options); SET quantity = ?
WHERE id = ?`;
await Self.rawSql(query, [rest, sale.id], options);
// Clone sale with new quantity // Clone sale with new quantity
const newSale = originalSale; query = `INSERT INTO sale (itemFk, ticketFk, concept, quantity, originalQuantity, price, discount, priceFixed,
newSale.id = undefined; reserved, isPicked, isPriceFixed, isAdded)
newSale.ticketFk = ticketId; SELECT itemFk, ?, concept, ?, originalQuantity, price, discount, priceFixed,
newSale.quantity = sale.quantity; reserved, isPicked, isPriceFixed, isAdded
FROM sale
const createdSale = await models.Sale.create(newSale, options); 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 // Clone sale components
const saleComponents = await models.SaleComponent.find({ const saleComponents = await models.SaleComponent.find({
where: {saleFk: sale.id} where: {saleFk: sale.id}
}, options); }, options);
const newComponents = saleComponents.map(component => { const newComponents = saleComponents.map(component => {
component.saleFk = createdSale.id; component.saleFk = lastInsertedSale.id;
return component; return component;
}); });
await models.SaleComponent.create(newComponents, options); await models.SaleComponent.create(newComponents, options);
return originalInstance;
} }
}; };