Feat(orders,utils, cart): orders get endpoints and cart totals (#7010)

This commit is contained in:
Carlos R. L. Rodrigues
2024-04-22 09:01:18 +00:00
committed by GitHub
parent 89143e1032
commit cc557c8752
23 changed files with 2411 additions and 30 deletions
@@ -0,0 +1,88 @@
import { BigNumber } from "../big-number"
import { createRawPropertiesFromBigNumber } from "../create-raw-properties-from-bignumber"
describe("Create Raw properties from BigNumber", function () {
it("should create raw properties from BigNumber properties", function () {
const obj = {
price: new BigNumber({
value: "42",
precision: 10,
}),
field: 111,
metadata: {
numeric_field: new BigNumber({
value: "100",
}),
random_field: 134,
},
abc: null,
raw_abc: {
value: "9.00000010000103991234",
precision: 20,
},
}
createRawPropertiesFromBigNumber(obj)
expect(obj).toEqual(
expect.objectContaining({
raw_price: {
value: "42",
precision: 10,
},
field: 111,
metadata: expect.objectContaining({
raw_numeric_field: {
value: "100",
precision: 20,
},
random_field: 134,
}),
abc: null,
raw_abc: {
value: "9.00000010000103991234",
precision: 20,
},
})
)
})
it("should create all properties containing BigNumber properties excluding selected ones", function () {
const obj = {
price: new BigNumber({
value: "42",
precision: 10,
}),
field: 111,
metadata: {
numeric_field: new BigNumber({
value: "100",
}),
random_field: 134,
},
}
createRawPropertiesFromBigNumber(obj, {
exclude: ["metadata.numeric_field"],
})
expect(obj).toEqual({
price: new BigNumber({
value: "42",
precision: 10,
}),
raw_price: {
value: "42",
precision: 10,
},
field: 111,
metadata: {
numeric_field: new BigNumber({
value: "100",
}),
random_field: 134,
},
})
})
})
+9 -4
View File
@@ -63,22 +63,24 @@ export function decorateCartTotals(
let discountTaxTotal = MathBN.convert(0)
let itemsSubtotal = MathBN.convert(0)
let itemsSubtotalWithoutTaxes = MathBN.convert(0)
let itemsTotal = MathBN.convert(0)
let itemsOriginalTotal = MathBN.convert(0)
let itemsOriginalSubtotal = MathBN.convert(0)
let itemsTaxTotal = MathBN.convert(0)
let itemsOriginalTaxTotal = MathBN.convert(0)
let shippingSubtotal = MathBN.convert(0)
let shippingTotal = MathBN.convert(0)
let shippingOriginalTotal = MathBN.convert(0)
let shippingOriginalSubtotal = MathBN.convert(0)
let shippingTaxTotal = MathBN.convert(0)
let shippingTaxSubTotal = MathBN.convert(0)
let shippingOriginalTaxTotal = MathBN.convert(0)
let shippingOriginalTaxSubtotal = MathBN.convert(0)
@@ -94,6 +96,7 @@ export function decorateCartTotals(
const itemOriginalTaxTotal = MathBN.convert(itemTotals.original_tax_total)
const itemDiscountTotal = MathBN.convert(itemTotals.discount_total)
const itemDiscountTaxTotal = MathBN.convert(itemTotals.discount_tax_total)
subtotal = MathBN.add(subtotal, itemSubtotal)
@@ -108,6 +111,7 @@ export function decorateCartTotals(
itemsSubtotal = MathBN.add(itemsSubtotal, itemSubtotal)
itemsTaxTotal = MathBN.add(itemsTaxTotal, itemTaxTotal)
itemsOriginalTaxTotal = MathBN.add(
itemsOriginalTaxTotal,
itemOriginalTaxTotal
@@ -164,6 +168,7 @@ export function decorateCartTotals(
})
const taxTotal = MathBN.add(itemsTaxTotal, shippingTaxTotal)
const originalTaxTotal = MathBN.add(
itemsOriginalTaxTotal,
shippingOriginalTaxTotal
@@ -181,7 +186,7 @@ export function decorateCartTotals(
const tempTotal = MathBN.add(subtotal, shippingTotal, taxTotal)
const total = MathBN.sub(tempTotal, discountTotal)
const cart = { ...cartLike } as any
const cart = cartLike as any
cart.total = new BigNumber(total)
cart.subtotal = new BigNumber(subtotal)
@@ -0,0 +1,59 @@
import { isDefined, trimZeros } from "../common"
import { BigNumber } from "./big-number"
export function createRawPropertiesFromBigNumber(
obj,
{
prefix = "raw_",
exclude = [],
}: {
prefix?: string
exclude?: string[]
} = {}
) {
const stack = [{ current: obj, path: "" }]
while (stack.length > 0) {
const { current, path } = stack.pop()!
if (
current == null ||
typeof current !== "object" ||
current instanceof BigNumber
) {
continue
}
if (Array.isArray(current)) {
current.forEach((element, index) =>
stack.push({ current: element, path })
)
} else {
for (const key of Object.keys(current)) {
const 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_)
if (isBigNumber) {
const newKey = prefix + key
const newPath = path ? `${path}.${newKey}` : newKey
if (!exclude.includes(newPath)) {
current[newKey] = {
...value.raw_,
value: trimZeros(value.raw_.value),
}
continue
}
}
}
stack.push({ current: value, path: currentPath })
}
}
}
}
+1
View File
@@ -1,4 +1,5 @@
export * from "./cart"
export * from "./create-raw-properties-from-bignumber"
export * from "./line-item"
export * from "./math"
export * from "./promotion"