|
@@ -177,12 +177,18 @@ export class OrderCalculator {
|
|
|
// which affected the order price.
|
|
// which affected the order price.
|
|
|
const applicablePromotions = await filterAsync(promotions, p => p.test(order, utils));
|
|
const applicablePromotions = await filterAsync(promotions, p => p.test(order, utils));
|
|
|
|
|
|
|
|
|
|
+ const lineHasExistingPromotions =
|
|
|
|
|
+ line.items[0].pendingAdjustments &&
|
|
|
|
|
+ !!line.items[0].pendingAdjustments.find(a => a.type === AdjustmentType.PROMOTION);
|
|
|
const forceUpdateItems = this.orderLineHasInapplicablePromotions(applicablePromotions, line);
|
|
const forceUpdateItems = this.orderLineHasInapplicablePromotions(applicablePromotions, line);
|
|
|
|
|
+
|
|
|
|
|
+ if (forceUpdateItems || lineHasExistingPromotions) {
|
|
|
|
|
+ line.clearAdjustments(AdjustmentType.PROMOTION);
|
|
|
|
|
+ }
|
|
|
if (forceUpdateItems) {
|
|
if (forceUpdateItems) {
|
|
|
// This OrderLine contains Promotion adjustments for Promotions that are no longer
|
|
// This OrderLine contains Promotion adjustments for Promotions that are no longer
|
|
|
// applicable. So we know for sure we will need to update these OrderItems in the
|
|
// applicable. So we know for sure we will need to update these OrderItems in the
|
|
|
// DB. Therefore add them to the `updatedOrderItems` set.
|
|
// DB. Therefore add them to the `updatedOrderItems` set.
|
|
|
- line.clearAdjustments(AdjustmentType.PROMOTION);
|
|
|
|
|
line.items.forEach(i => updatedOrderItems.add(i));
|
|
line.items.forEach(i => updatedOrderItems.add(i));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -193,12 +199,6 @@ export class OrderCalculator {
|
|
|
// as to render later promotions no longer applicable.
|
|
// as to render later promotions no longer applicable.
|
|
|
if (await promotion.test(order, utils)) {
|
|
if (await promotion.test(order, utils)) {
|
|
|
for (const item of line.items) {
|
|
for (const item of line.items) {
|
|
|
- const itemHasPromotions =
|
|
|
|
|
- item.pendingAdjustments &&
|
|
|
|
|
- !!item.pendingAdjustments.find(a => a.type === AdjustmentType.PROMOTION);
|
|
|
|
|
- if (itemHasPromotions) {
|
|
|
|
|
- item.clearAdjustments(AdjustmentType.PROMOTION);
|
|
|
|
|
- }
|
|
|
|
|
const adjustment = await promotion.apply({
|
|
const adjustment = await promotion.apply({
|
|
|
orderItem: item,
|
|
orderItem: item,
|
|
|
orderLine: line,
|
|
orderLine: line,
|
|
@@ -208,8 +208,6 @@ export class OrderCalculator {
|
|
|
item.pendingAdjustments = item.pendingAdjustments.concat(adjustment);
|
|
item.pendingAdjustments = item.pendingAdjustments.concat(adjustment);
|
|
|
priceAdjusted = true;
|
|
priceAdjusted = true;
|
|
|
updatedOrderItems.add(item);
|
|
updatedOrderItems.add(item);
|
|
|
- } else if (itemHasPromotions) {
|
|
|
|
|
- updatedOrderItems.add(item);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (priceAdjusted) {
|
|
if (priceAdjusted) {
|
|
@@ -218,6 +216,13 @@ export class OrderCalculator {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ const lineNoLongerHasPromotions =
|
|
|
|
|
+ !line.items[0].pendingAdjustments ||
|
|
|
|
|
+ !line.items[0].pendingAdjustments.find(a => a.type === AdjustmentType.PROMOTION);
|
|
|
|
|
+ if (lineHasExistingPromotions && lineNoLongerHasPromotions) {
|
|
|
|
|
+ line.items.forEach(i => updatedOrderItems.add(i));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (forceUpdateItems) {
|
|
if (forceUpdateItems) {
|
|
|
// If we are forcing an update, we need to ensure that totals get
|
|
// If we are forcing an update, we need to ensure that totals get
|
|
|
// re-calculated *even if* there are no applicable promotions (i.e.
|
|
// re-calculated *even if* there are no applicable promotions (i.e.
|