fix: Compute "virtual" adjustments for order previews (#13306)

* wip

* add wip

* wip

* reuse action

* finish first draft

* fix tests

* cleanup

* Only compute adjustments when necessary

* Create hot-carrots-look.md

* address comments

* minor tweaks

* fix pay col

* fix test

* wip

* Dwip

* wip

* fix: adjustment typo

* fix: import

* fix: workflow imports

* wip: update test

* feat: upsert versioned  adjustments when previewing order

* fix: revert unique codes change

* fix: order spec test with versioning

* wip: save

* feat: make adjustments work for preview and confirm flow, wip base repo filtering of older version adjustments

* fix: missing populate where

* wip: populate where loading versioned adjustments

* fix: filter out older adjustment versions

* temp: comment adjustments in repo

* test: add adjustment if no version

* wip: configure populate where in order base repository

* fix: rm manual filtering

* fix: revert base repo changes

* fix: revert

* fix: use order item version instead of order version

* fix: rm only in test

* fix: update case spec

* fix: remove sceanrio, wip test with draft promotion

* feat: test correct adjustments when disabling promotion

* feat: complex test case

* feat: test consecutive order edits

* feat: 2 promotions test case with a fixed promo

* feat: migrate existing order line item adjustments to order items latest version

* feat: update dep after merge

* wip: load adjustments separatley

* feat: adjustments collections

* fix: spread result, handle related entity case

* fix: update lock

* feat: make sure version is loaded, refactor, handle related entity case

* fix: check fields

* feat: loading adjustments for list and count

* fix: correct items version field

* fix: rm empty array

* fix: wip order modules spec

* fix: order module specs

* feat: preinit items adjustments

* fix: rm only

* fix: rm only

* chore: cleanup

* fix: migration files

* fix: dont change formatting

* fix: core package build

* chore: more cleanup

* fix: item update util

* fix: duplicate import

* fix: refresh adjustments for exchanges (#13992)

* wip: exchange adjustments

* feat: test - receive items

* feat: finish test case

* fix: casing

* fix(draft-orders, core-flows, orders) refresh adjustments for draft orders (#14025)

* wip: draft orders adjustments refresh

* feat: rewrite to use REPLACE action + test

* fix: rm only

* feat: cleanup old REPLACE actions

* feat: cleanup adjustemnts when 0 promotions

* wip: canceling draft order

* fix: make version arg optional

* fix: restore promotion links

* feat: test reverting on cancelation

* fix: address comments in tests

* wip: fix summary on preview

* fix: get pending diff on preview summary from total

* fix: revert pending diff change

---------

Co-authored-by: fPolic <mainacc.polic@gmail.com>
Co-authored-by: Frane Polić <16856471+fPolic@users.noreply.github.com>
This commit is contained in:
Oli Juhl
2025-11-25 10:41:14 +01:00
committed by GitHub
co-authored by fPolic Frane Polić
parent 0ddd9e36b5
commit 78842af1c3
60 changed files with 3271 additions and 233 deletions
@@ -2540,19 +2540,29 @@ export default class OrderModuleService
sharedContext
)
const { itemsToUpsert, shippingMethodsToUpsert, calculatedOrders } =
await applyChangesToOrder(
[order],
{ [order.id]: orderChange.actions },
{ addActionReferenceToObject: true }
)
// We need to apply the latest ordering actions last
const sortedActions = orderChange.actions.sort((a, b) => {
return a.ordering - b.ordering
})
const {
itemsToUpsert,
shippingMethodsToUpsert,
calculatedOrders,
lineItemAdjustmentsToCreate,
} = await applyChangesToOrder(
[order],
{ [order.id]: sortedActions },
{ addActionReferenceToObject: true }
)
const calculated = calculatedOrders[order.id]
await this.includeTaxLinesAndAdjustementsToPreview(
await this.includeTaxLinesAndAdjustmentsToPreview(
calculated.order,
itemsToUpsert,
shippingMethodsToUpsert,
lineItemAdjustmentsToCreate, // this will add "virtual" adjustments for the preview version but no actual adjustments will be created in the DB
sharedContext
)
@@ -2568,10 +2578,11 @@ export default class OrderModuleService
return calcOrder
}
private async includeTaxLinesAndAdjustementsToPreview(
private async includeTaxLinesAndAdjustmentsToPreview(
order,
itemsToUpsert,
shippingMethodsToUpsert,
lineItemAdjustmentsToCreate,
sharedContext: Context = {}
) {
const addedItems = {}
@@ -2619,6 +2630,11 @@ export default class OrderModuleService
//@ts-ignore
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
const adjustments = lineItemAdjustmentsToCreate.filter(
(d) => d.item_id === newItem.item_id
)
const unitPrice = newItem?.unit_price ?? item.unit_price
const compareAtUnitPrice =
newItem?.compare_at_unit_price ?? item.compare_at_unit_price
@@ -2632,6 +2648,7 @@ export default class OrderModuleService
quantity: newItem.quantity,
unit_price: unitPrice,
compare_at_unit_price: compareAtUnitPrice || null,
adjustments: adjustments,
detail: {
...newItem,
...item,
@@ -3539,11 +3556,12 @@ export default class OrderModuleService
summariesToUpsert,
orderToUpdate,
creditLinesToUpsert,
lineItemAdjustmentsToCreate,
} = await applyChangesToOrder(orders, actionsMap, {
addActionReferenceToObject: true,
includeTaxLinesAndAdjustementsToPreview: async (...args) => {
includeTaxLinesAndAdjustmentsToPreview: async (...args) => {
args.push(sharedContext)
return await this.includeTaxLinesAndAdjustementsToPreview.apply(
return await this.includeTaxLinesAndAdjustmentsToPreview.apply(
this,
args
)
@@ -3582,6 +3600,14 @@ export default class OrderModuleService
sharedContext
)
: null,
lineItemAdjustmentsToCreate.length
? this.orderLineItemAdjustmentService_.create(
// this is called when a new order version is confirmed so we only create a new set of adjustments for that version
// there is no removal or upsert
lineItemAdjustmentsToCreate,
sharedContext
)
: null,
])
return {