chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,3 @@
export * from "./reservation-item"
export * from "./inventory-item"
export * from "./inventory-level"

View File

@@ -0,0 +1,154 @@
import {
BeforeCreate,
Collection,
Entity,
Filter,
Formula,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
Searchable,
} from "@medusajs/utils"
import { DAL } from "@medusajs/types"
import { InventoryLevel } from "./inventory-level"
import { ReservationItem } from "./reservation-item"
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()
@Searchable()
@Property({ columnType: "text", nullable: true })
sku: string | null = null
@Property({ columnType: "text", nullable: true })
origin_country: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true })
hs_code: string | null = null
@Searchable()
@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
@Searchable()
@Property({ columnType: "text", nullable: true })
description: string | null = null
@Searchable()
@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,
{
cascade: ["soft-remove" as any],
}
)
location_levels = new Collection<InventoryLevel>(this)
@OneToMany(
() => ReservationItem,
(reservationItem) => reservationItem.inventory_item,
{
cascade: ["soft-remove" as any],
}
)
reservation_items = new Collection<ReservationItem>(this)
@Formula(
(item) =>
`(SELECT SUM(reserved_quantity) FROM inventory_level il WHERE il.inventory_item_id = ${item}.id)`,
{ lazy: true, serializer: Number, hidden: true }
)
reserved_quantity: number
@Formula(
(item) =>
`(SELECT SUM(stocked_quantity) FROM inventory_level il WHERE il.inventory_item_id = ${item}.id)`,
{ lazy: true, serializer: Number, hidden: true }
)
stocked_quantity: number
@BeforeCreate()
private beforeCreate(): void {
this.id = generateEntityId(this.id, "iitem")
}
@OnInit()
private onInit(): void {
this.id = generateEntityId(this.id, "iitem")
}
}

View File

@@ -0,0 +1,114 @@
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OnLoad,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { DALUtils, isDefined } 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
available_quantity: number | null = null
@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")
}
@OnLoad()
private onLoad(): void {
if (isDefined(this.stocked_quantity) && isDefined(this.reserved_quantity)) {
this.available_quantity = this.stocked_quantity - this.reserved_quantity
}
}
}

View File

@@ -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, "resitem")
}
@OnInit()
private onInit(): void {
this.id = generateEntityId(this.id, "resitem")
}
}