Skip to content

Commit

Permalink
fix(utils): bignumber util considers nullable options when setting value
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Feb 25, 2024
1 parent 168f02f commit 1041667
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-bags-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix(utils): bignumber util considers nullable options when setting value
2 changes: 1 addition & 1 deletion packages/cart/src/models/line-item-adjustment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import LineItem from "./line-item"
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne({
entity: () => LineItem,
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
cascade: [Cascade.PERSIST],
})
item: LineItem

Expand Down
2 changes: 1 addition & 1 deletion packages/cart/src/models/shipping-method-adjustment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ShippingMethod from "./shipping-method"
export default class ShippingMethodAdjustment extends AdjustmentLine {
@ManyToOne({
entity: () => ShippingMethod,
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
cascade: [Cascade.PERSIST],
})
shipping_method: ShippingMethod

Expand Down
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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()
Expand All @@ -25,6 +30,16 @@ describe("@MikroOrmBigNumberProperty", () => {
precision: 20,
})

try {
;(testAmount as any).amount = null
} catch (e) {
expect(e.message).toEqual(
"Invalid BigNumber value. 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
Expand Down
15 changes: 10 additions & 5 deletions packages/utils/src/dal/mikro-orm/big-number-field.ts
Original file line number Diff line number Diff line change
@@ -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 } from "../../common"
import { BigNumber } from "../../totals/big-number"

export function MikroOrmBigNumberProperty(
options: Parameters<typeof Property>[0] & {
Expand All @@ -16,21 +17,25 @@ export function MikroOrmBigNumberProperty(
return this[targetColumn]
},
set(value: BigNumberInput) {
let bigNumber: BigNumber
let bigNumber: BigNumber | null = null

if (value instanceof BigNumber) {
bigNumber = value
} else if (this[rawColumnName]) {
const precision = this[rawColumnName].precision
this[rawColumnName].value = new BigNumber(value, {
precision,
}).raw!.value

bigNumber = new BigNumber(this[rawColumnName])
} else if (options.nullable && !isPresent(value)) {
bigNumber = null
} else {
bigNumber = new BigNumber(value)
}

this[targetColumn] = bigNumber.numeric
this[rawColumnName] = bigNumber.raw
this[targetColumn] = bigNumber?.numeric ?? null
this[rawColumnName] = bigNumber?.raw ?? null
},
})

Expand Down

0 comments on commit 1041667

Please sign in to comment.