Merge pull request '3301-refactor(ticket): modify transferSales.js for avoid unnecessary logs' (#805) from 3301-ticket_transferSales into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #805
Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
Carlos Jimenez Ruiz 2021-11-25 10:47:07 +00:00
commit b7b6f62343
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;
} }
}; };