fix: only can exists 'mana' or 'manaClaim' discount. Never both
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-10-14 14:30:37 +02:00
parent 2455a11c26
commit 86b7e9cabe
1 changed files with 37 additions and 4 deletions

View File

@ -115,10 +115,43 @@ module.exports = Self => {
for (let sale of sales) {
const oldDiscount = sale.discount;
const value = ((-sale.price * newDiscount) / 100);
const newComponent = models.SaleComponent.upsert({
saleFk: sale.id,
value: value,
componentFk: componentId}, myOptions);
const manaComponent = await models.Component.findOne({
where: {code: 'mana'}
}, myOptions);
const manaClaimComponent = await models.Component.findOne({
where: {code: 'manaClaim'}
}, myOptions);
const oldComponent = await models.SaleComponent.find({
where: {
and: [
{saleFk: sale.id},
{componentFk: {inq: [manaComponent.id, manaClaimComponent.id]}}
]
}
}, myOptions);
let newComponent;
if (oldComponent) {
const filter = {
saleFk: sale.id,
componentFk: oldComponent.componentFk
};
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);
}
const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions);