fix(utils): bignumber util considers nullable options when setting value (#6499)
what: - when setting null for nullable columns, big number utils should not throw error - remove circular soft delete depenedencies for cart module - trims zeros on big number values
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { MikroOrmBigNumberProperty } from "../big-number-field"
|
||||
import { BigNumberRawValue } from "@medusajs/types"
|
||||
import { BigNumber } from "../../../totals/big-number"
|
||||
import { MikroOrmBigNumberProperty } from "../big-number-field"
|
||||
|
||||
describe("@MikroOrmBigNumberProperty", () => {
|
||||
it("should correctly assign and update BigNumber values", () => {
|
||||
@@ -9,6 +9,11 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
amount: BigNumber | number
|
||||
|
||||
raw_amount: BigNumberRawValue
|
||||
|
||||
@MikroOrmBigNumberProperty({ nullable: true })
|
||||
nullable_amount: BigNumber | number | null = null
|
||||
|
||||
raw_nullable_amount: BigNumberRawValue | null = null
|
||||
}
|
||||
|
||||
const testAmount = new TestAmount()
|
||||
@@ -21,10 +26,20 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
expect(testAmount.amount).toEqual(100)
|
||||
expect((testAmount as any).amount_).toEqual(100)
|
||||
expect(testAmount.raw_amount).toEqual({
|
||||
value: "100.00000000000000000",
|
||||
value: "100",
|
||||
precision: 20,
|
||||
})
|
||||
|
||||
try {
|
||||
;(testAmount as any).amount = null
|
||||
} catch (e) {
|
||||
expect(e.message).toEqual(
|
||||
"Invalid BigNumber value: null. Should be one of: string, number, BigNumber (bignumber.js), BigNumberRawValue"
|
||||
)
|
||||
}
|
||||
|
||||
testAmount.nullable_amount = null
|
||||
expect(testAmount.nullable_amount).toEqual(null)
|
||||
// Update the amount
|
||||
|
||||
testAmount.amount = 200
|
||||
@@ -32,7 +47,7 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
expect(testAmount.amount).toEqual(200)
|
||||
expect((testAmount as any).amount_).toEqual(200)
|
||||
expect(testAmount.raw_amount).toEqual({
|
||||
value: "200.00000000000000000",
|
||||
value: "200",
|
||||
precision: 20,
|
||||
})
|
||||
|
||||
@@ -42,6 +57,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
|
||||
expect(testAmount.amount).toEqual(300)
|
||||
expect((testAmount as any).amount_).toEqual(300)
|
||||
expect(testAmount.raw_amount).toEqual({ value: "300.00", precision: 5 })
|
||||
expect(testAmount.raw_amount).toEqual({ value: "300", precision: 5 })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { BigNumber } from "../../totals/big-number"
|
||||
import { Property } from "@mikro-orm/core"
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
import { Property } from "@mikro-orm/core"
|
||||
import { isPresent, trimZeros } from "../../common"
|
||||
import { BigNumber } from "../../totals/big-number"
|
||||
|
||||
export function MikroOrmBigNumberProperty(
|
||||
options: Parameters<typeof Property>[0] & {
|
||||
@@ -16,21 +17,37 @@ export function MikroOrmBigNumberProperty(
|
||||
return this[targetColumn]
|
||||
},
|
||||
set(value: BigNumberInput) {
|
||||
if (options?.nullable && !isPresent(value)) {
|
||||
this[targetColumn] = null
|
||||
this[rawColumnName] = null
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
let bigNumber: BigNumber
|
||||
|
||||
if (value instanceof BigNumber) {
|
||||
bigNumber = value
|
||||
} else if (this[rawColumnName]) {
|
||||
const precision = this[rawColumnName].precision
|
||||
this[rawColumnName].value = new BigNumber(value, {
|
||||
precision,
|
||||
}).raw!.value
|
||||
|
||||
this[rawColumnName].value = trimZeros(
|
||||
new BigNumber(value, {
|
||||
precision,
|
||||
}).raw!.value as string
|
||||
)
|
||||
|
||||
bigNumber = new BigNumber(this[rawColumnName])
|
||||
} else {
|
||||
bigNumber = new BigNumber(value)
|
||||
}
|
||||
|
||||
this[targetColumn] = bigNumber.numeric
|
||||
this[rawColumnName] = bigNumber.raw
|
||||
|
||||
const raw = bigNumber.raw!
|
||||
raw.value = trimZeros(raw.value as string)
|
||||
|
||||
this[rawColumnName] = raw
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user