feat(inventory-next, types): inventory module conversion (#6596)
* init * create new interface * prep integration tests * update denpencies * inventory service partial tests * finalize integration tests * add events * align events * adjust inventory level reservation levels * add test validating reserved quantity after reseration item update * fix nits * rename to inventory-next * update yarn.lock * remove changelog * remove fixtures * remove unused files * ready for review * Update packages/inventory-next/package.json Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> * pr feedback * add tests and docs for partition-array util * remote decorators from private method * fix unit tests * add migrations * add foreign keys * fix build --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
8406eb5a35
commit
c19d276458
@@ -0,0 +1,3 @@
|
||||
export * from "./reservation-item"
|
||||
export * from "./inventory-item"
|
||||
export * from "./inventory-level"
|
||||
@@ -0,0 +1,120 @@
|
||||
import {
|
||||
BeforeCreate,
|
||||
Collection,
|
||||
Entity,
|
||||
Filter,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import {
|
||||
DALUtils,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { InventoryLevel } from "./inventory-level"
|
||||
|
||||
const InventoryItemDeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "inventory_item",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const InventoryItemSkuIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "inventory_item",
|
||||
columns: "sku",
|
||||
unique: true,
|
||||
})
|
||||
|
||||
type InventoryItemOptionalProps = DAL.SoftDeletableEntityDateColumns
|
||||
|
||||
@Entity()
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export class InventoryItem {
|
||||
[OptionalProps]: InventoryItemOptionalProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@InventoryItemDeletedAtIndex.MikroORMIndex()
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@InventoryItemSkuIndex.MikroORMIndex()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
sku: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
origin_country: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
hs_code: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
mid_code: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
material: string | null = null
|
||||
|
||||
@Property({ type: "int", nullable: true })
|
||||
weight: number | null = null
|
||||
|
||||
@Property({ type: "int", nullable: true })
|
||||
length: number | null = null
|
||||
|
||||
@Property({ type: "int", nullable: true })
|
||||
height: number | null = null
|
||||
|
||||
@Property({ type: "int", nullable: true })
|
||||
width: number | null = null
|
||||
|
||||
@Property({ columnType: "boolean" })
|
||||
requires_shipping: boolean = true
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
description: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
title: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
thumbnail: string | null = null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@OneToMany(
|
||||
() => InventoryLevel,
|
||||
(inventoryLevel) => inventoryLevel.inventory_item
|
||||
)
|
||||
inventory_levels = new Collection<InventoryLevel>(this)
|
||||
|
||||
@BeforeCreate()
|
||||
private beforeCreate(): void {
|
||||
this.id = generateEntityId(this.id, "iitem")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
private onInit(): void {
|
||||
this.id = generateEntityId(this.id, "iitem")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { InventoryItem } from "./inventory-item"
|
||||
import { createPsqlIndexStatementHelper } from "@medusajs/utils"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
|
||||
const InventoryLevelDeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "inventory_level",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const InventoryLevelInventoryItemIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "inventory_level",
|
||||
columns: "inventory_item_id",
|
||||
})
|
||||
|
||||
const InventoryLevelLocationIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "inventory_level",
|
||||
columns: "location_id",
|
||||
})
|
||||
|
||||
const InventoryLevelLocationIdInventoryItemIdIndex =
|
||||
createPsqlIndexStatementHelper({
|
||||
tableName: "inventory_level",
|
||||
columns: "location_id",
|
||||
})
|
||||
|
||||
@Entity()
|
||||
@InventoryLevelLocationIdInventoryItemIdIndex.MikroORMIndex()
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export class InventoryLevel {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@InventoryLevelDeletedAtIndex.MikroORMIndex()
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@ManyToOne(() => InventoryItem, {
|
||||
fieldName: "inventory_item_id",
|
||||
type: "text",
|
||||
mapToPk: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@InventoryLevelInventoryItemIdIndex.MikroORMIndex()
|
||||
inventory_item_id: string
|
||||
|
||||
@InventoryLevelLocationIdIndex.MikroORMIndex()
|
||||
@Property({ type: "text" })
|
||||
location_id: string
|
||||
|
||||
@Property({ type: "int" })
|
||||
stocked_quantity: number = 0
|
||||
|
||||
@Property({ type: "int" })
|
||||
reserved_quantity: number = 0
|
||||
|
||||
@Property({ type: "int" })
|
||||
incoming_quantity: number = 0
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null
|
||||
|
||||
@ManyToOne(() => InventoryItem, {
|
||||
persist: false,
|
||||
})
|
||||
inventory_item: InventoryItem
|
||||
|
||||
@BeforeCreate()
|
||||
private beforeCreate(): void {
|
||||
this.id = generateEntityId(this.id, "ilev")
|
||||
this.inventory_item_id ??= this.inventory_item?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
private onInit(): void {
|
||||
this.id = generateEntityId(this.id, "ilev")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { InventoryItem } from "./inventory-item"
|
||||
import { createPsqlIndexStatementHelper } from "@medusajs/utils"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
|
||||
const ReservationItemDeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "reservation_item",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
const ReservationItemLineItemIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "reservation_item",
|
||||
columns: "line_item_id",
|
||||
})
|
||||
|
||||
const ReservationItemInventoryItemIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "reservation_item",
|
||||
columns: "inventory_item_id",
|
||||
})
|
||||
|
||||
const ReservationItemLocationIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "reservation_item",
|
||||
columns: "location_id",
|
||||
})
|
||||
|
||||
@Entity()
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export class ReservationItem {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@ReservationItemDeletedAtIndex.MikroORMIndex()
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@ReservationItemLineItemIdIndex.MikroORMIndex()
|
||||
@Property({ type: "text", nullable: true })
|
||||
line_item_id: string | null = null
|
||||
|
||||
@ReservationItemLocationIdIndex.MikroORMIndex()
|
||||
@Property({ type: "text" })
|
||||
location_id: string
|
||||
|
||||
@Property({ columnType: "integer" })
|
||||
quantity: number
|
||||
|
||||
@Property({ type: "text", nullable: true })
|
||||
external_id: string | null = null
|
||||
|
||||
@Property({ type: "text", nullable: true })
|
||||
description: string | null = null
|
||||
|
||||
@Property({ type: "text", nullable: true })
|
||||
created_by: string | null = null
|
||||
|
||||
@Property({ type: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@ReservationItemInventoryItemIdIndex.MikroORMIndex()
|
||||
@ManyToOne(() => InventoryItem, {
|
||||
fieldName: "inventory_item_id",
|
||||
type: "text",
|
||||
mapToPk: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
inventory_item_id: string
|
||||
|
||||
@ManyToOne(() => InventoryItem, {
|
||||
persist: false,
|
||||
})
|
||||
inventory_item: InventoryItem
|
||||
|
||||
@BeforeCreate()
|
||||
private beforeCreate(): void {
|
||||
this.id = generateEntityId(this.id, "ilev")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
private onInit(): void {
|
||||
this.id = generateEntityId(this.id, "ilev")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user