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:
Carlos R. L. Rodrigues
2024-12-13 09:51:26 -03:00
committed by GitHub
parent ae1d875fcf
commit 729eb5da7b
33 changed files with 662 additions and 593 deletions

View 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()
}