refactor: migration stock location module (#10471)

Fixes: FRMW-2831
This commit is contained in:
Harminder Virk
2024-12-10 16:08:48 +00:00
committed by GitHub
parent e41ab50f59
commit 4ad9ac1e5f
8 changed files with 171 additions and 230 deletions
@@ -1,2 +1,2 @@
export * from "./stock-location"
export * from "./stock-location-address"
export { default as StockLocation } from "./stock-location"
export { default as StockLocationAddress } from "./stock-location-address"
@@ -1,79 +1,24 @@
import {
BeforeCreate,
Entity,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import { model } from "@medusajs/framework/utils"
import { StockLocation } from "@models"
const StockLocationAddressDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "stock_location_address",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
export class StockLocationAddress {
@PrimaryKey({ columnType: "text" })
id: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
const StockLocationAddress = model
.define("StockLocationAddress", {
id: model.id({ prefix: "laddr" }).primaryKey(),
address_1: model.text(),
address_2: model.text().nullable(),
company: model.text().nullable(),
city: model.text().nullable(),
country_code: model.text(),
phone: model.text().nullable(),
province: model.text().nullable(),
postal_code: model.text().nullable(),
metadata: model.json().nullable(),
stock_locations: model.hasMany(() => StockLocation, {
mappedBy: "address",
}),
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["stock_locations"],
})
updated_at: Date
@StockLocationAddressDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@Property({ columnType: "text" })
address_1: string
@Property({ columnType: "text", nullable: true })
address_2: string | null
@Property({ columnType: "text", nullable: true })
company: string | null
@Property({ columnType: "text", nullable: true })
city: string | null
@Property({ columnType: "text" })
country_code: string
@Property({ columnType: "text", nullable: true })
phone: string | null
@Property({ columnType: "text", nullable: true })
province: string | null
@Property({ columnType: "text", nullable: true })
postal_code: string | null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null
@BeforeCreate()
beforeCreate(): void {
this.id = generateEntityId(this.id, "laddr")
}
@OnInit()
onInit(): void {
this.id = generateEntityId(this.id, "laddr")
}
}
export default StockLocationAddress
@@ -1,80 +1,15 @@
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import StockLocationAddress from "./stock-location-address"
import { StockLocationAddress } from "./stock-location-address"
const StockLocationDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "stock_location",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
const StockLocation = model.define("StockLocation", {
id: model.id({ prefix: "sloc" }).primaryKey(),
name: model.text().searchable(),
metadata: model.json().nullable(),
address: model
.belongsTo(() => StockLocationAddress, {
mappedBy: "stock_locations",
})
.nullable(),
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export class StockLocation {
@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
@StockLocationDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@Searchable()
@Property({ columnType: "text" })
name: string
@ManyToOne(() => StockLocationAddress, {
fieldName: "address_id",
type: "text",
mapToPk: true,
nullable: true,
onDelete: "cascade",
})
address_id: string | null
@ManyToOne(() => StockLocationAddress, {
nullable: true,
})
address: StockLocationAddress | null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null
@BeforeCreate()
beforeCreate(): void {
this.id = generateEntityId(this.id, "sloc")
}
@OnInit()
onInit(): void {
this.id = generateEntityId(this.id, "sloc")
}
}
export default StockLocation