chore(order): preview order change (#8025)

What:
 - new method `previewOrderChange`
   - Calculate all the actions related to an order change.
   - Return the preview of the final Order, with all the calculated values.
   - Associate actions with items and shipping_methods they modified.

FIXES: CORE-2509
This commit is contained in:
Carlos R. L. Rodrigues
2024-07-09 11:45:55 -03:00
committed by GitHub
parent 4736d9e2dd
commit 2b2e2fbb3d
25 changed files with 373 additions and 57 deletions

View File

@@ -1,3 +1,4 @@
import { BigNumber as BigNumberJS } from "bignumber.js"
import { isDefined, trimZeros } from "../common"
import { BigNumber } from "./big-number"
@@ -11,6 +12,14 @@ export function createRawPropertiesFromBigNumber(
exclude?: string[]
} = {}
) {
const isBigNumber = (value) => {
return (
typeof value === "object" &&
isDefined(value.raw_) &&
isDefined(value.numeric_)
)
}
const stack = [{ current: obj, path: "" }]
while (stack.length > 0) {
@@ -19,7 +28,8 @@ export function createRawPropertiesFromBigNumber(
if (
current == null ||
typeof current !== "object" ||
current instanceof BigNumber
isBigNumber(current) ||
path.includes("." + prefix)
) {
continue
}
@@ -30,16 +40,17 @@ export function createRawPropertiesFromBigNumber(
)
} else {
for (const key of Object.keys(current)) {
const value = current[key]
let value = current[key]
const currentPath = path ? `${path}.${key}` : key
if (value != null && !exclude.includes(currentPath)) {
const isBigNumber =
typeof value === "object" &&
isDefined(value.raw_) &&
isDefined(value.numeric_)
const isBigNumberJS = BigNumberJS.isBigNumber(value)
if (isBigNumberJS) {
current[key] = new BigNumber(current[key])
value = current[key]
}
if (isBigNumber) {
if (isBigNumber(value)) {
const newKey = prefix + key
const newPath = path ? `${path}.${newKey}` : newKey
if (!exclude.includes(newPath)) {