chore(inventory): convert to dml (#10569)
Fixes: FRMW-2848 Co-authored-by: Harminder Virk <1706381+thetutlage@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
ae1d875fcf
commit
729eb5da7b
45
packages/modules/inventory/src/utils/apply-decorators.ts
Normal file
45
packages/modules/inventory/src/utils/apply-decorators.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
BigNumber,
|
||||
isDefined,
|
||||
MathBN,
|
||||
toMikroORMEntity,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { Formula, OnInit } from "@mikro-orm/core"
|
||||
|
||||
import InventoryItem from "../models/inventory-item"
|
||||
import InventoryLevel from "../models/inventory-level"
|
||||
|
||||
function applyHook() {
|
||||
const MikroORMEntity = toMikroORMEntity(InventoryLevel)
|
||||
|
||||
MikroORMEntity.prototype["onInit"] = function () {
|
||||
if (isDefined(this.stocked_quantity) && isDefined(this.reserved_quantity)) {
|
||||
this.available_quantity = new BigNumber(
|
||||
MathBN.sub(this.raw_stocked_quantity, this.raw_reserved_quantity)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
OnInit()(MikroORMEntity.prototype, "onInit")
|
||||
}
|
||||
|
||||
function applyFormulas() {
|
||||
const MikroORMEntity = toMikroORMEntity(InventoryItem)
|
||||
|
||||
Formula(
|
||||
(item) =>
|
||||
`(SELECT SUM(reserved_quantity) FROM inventory_level il WHERE il.inventory_item_id = ${item}.id AND il.deleted_at IS NULL)`,
|
||||
{ lazy: true, serializer: Number, hidden: true, type: "number" }
|
||||
)(MikroORMEntity.prototype, "reserved_quantity")
|
||||
|
||||
Formula(
|
||||
(item) =>
|
||||
`(SELECT SUM(stocked_quantity) FROM inventory_level il WHERE il.inventory_item_id = ${item}.id AND il.deleted_at IS NULL)`,
|
||||
{ lazy: true, serializer: Number, hidden: true, type: "number" }
|
||||
)(MikroORMEntity.prototype, "stocked_quantity")
|
||||
}
|
||||
|
||||
export const applyEntityHooks = () => {
|
||||
applyHook()
|
||||
applyFormulas()
|
||||
}
|
||||
Reference in New Issue
Block a user