chore(order): big number calculations (#6651)
Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
7c46b0f88b
commit
d48c076b77
@@ -1,7 +1,7 @@
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
import { Property } from "@mikro-orm/core"
|
||||
import { isPresent, trimZeros } from "../../common"
|
||||
import { BigNumber } from "../../totals/big-number"
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
|
||||
export function MikroOrmBigNumberProperty(
|
||||
options: Parameters<typeof Property>[0] & {
|
||||
@@ -13,47 +13,52 @@ export function MikroOrmBigNumberProperty(
|
||||
|
||||
Object.defineProperty(target, columnName, {
|
||||
get() {
|
||||
return this.__helper.__data[columnName]
|
||||
let value = this.__helper?.__data?.[columnName]
|
||||
|
||||
if (!value && this[rawColumnName]) {
|
||||
value = new BigNumber(this[rawColumnName].value, {
|
||||
precision: this[rawColumnName].precision,
|
||||
}).numeric
|
||||
}
|
||||
|
||||
return value
|
||||
},
|
||||
set(value: BigNumberInput) {
|
||||
if (options?.nullable && !isPresent(value)) {
|
||||
this.__helper.__data[columnName] = null
|
||||
this.__helper.__data[rawColumnName]
|
||||
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 = trimZeros(
|
||||
new BigNumber(value, {
|
||||
precision,
|
||||
}).raw!.value as string
|
||||
)
|
||||
|
||||
bigNumber = new BigNumber(this[rawColumnName])
|
||||
} else {
|
||||
bigNumber = new BigNumber(value)
|
||||
let bigNumber: BigNumber
|
||||
|
||||
if (value instanceof BigNumber) {
|
||||
bigNumber = value
|
||||
} else if (this[rawColumnName]) {
|
||||
const precision = this[rawColumnName].precision
|
||||
bigNumber = new BigNumber(value, {
|
||||
precision,
|
||||
})
|
||||
} else {
|
||||
bigNumber = new BigNumber(value)
|
||||
}
|
||||
|
||||
const raw = bigNumber.raw!
|
||||
raw.value = trimZeros(raw.value as string)
|
||||
|
||||
this.__helper.__data[columnName] = bigNumber.numeric
|
||||
this.__helper.__data[rawColumnName] = raw
|
||||
|
||||
this[rawColumnName] = raw
|
||||
}
|
||||
|
||||
this.__helper.__data[columnName] = bigNumber.numeric
|
||||
|
||||
const raw = bigNumber.raw!
|
||||
raw.value = trimZeros(raw.value as string)
|
||||
|
||||
this[rawColumnName] = raw
|
||||
|
||||
this.__helper.__touched = !this.__helper.hydrator.isRunning()
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
})
|
||||
|
||||
Property({
|
||||
type: "number",
|
||||
type: "any",
|
||||
columnType: "numeric",
|
||||
trackChanges: false,
|
||||
...options,
|
||||
|
||||
@@ -278,10 +278,7 @@ export function mikroOrmBaseRepositoryFactory<T extends object = object>(
|
||||
async update(data: { entity; update }[], context?: Context): Promise<T[]> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
const entities = data.map((data_) => {
|
||||
return manager.assign(
|
||||
data_.entity,
|
||||
data_.update as RequiredEntityData<T>
|
||||
)
|
||||
return manager.assign(data_.entity, data_.update)
|
||||
})
|
||||
|
||||
manager.persist(entities)
|
||||
|
||||
Reference in New Issue
Block a user