chore(order): custom display id (#14024)

* chore(order): Accept a custom display generator option

* chore(order): Accept a custom display generator option

* wip

* wip

* finalize

* Create tricky-olives-battle.md

* fields

* changeset

* update integration tests

* update migrations

* fix changeset
This commit is contained in:
Adrien de Peretti
2025-11-18 10:17:46 +01:00
committed by GitHub
parent 0a0c2f41d8
commit 6746fecd72
29 changed files with 479 additions and 260 deletions
File diff suppressed because it is too large Load Diff
@@ -1,4 +1,4 @@
import { Migration } from "@mikro-orm/migrations"
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
export class Migration20251016182939 extends Migration {
override async up(): Promise<void> {
@@ -1,25 +1,31 @@
import { Migration } from '@mikro-orm/migrations';
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
export class Migration20251017155709 extends Migration {
override async up(): Promise<void> {
this.addSql(`drop index if exists "IDX_order_item_version";`);
this.addSql(`drop index if exists "IDX_order_item_version";`)
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_item_order_id_version" ON "order_item" (order_id, version) WHERE deleted_at IS NULL;`);
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_order_item_order_id_version" ON "order_item" (order_id, version) WHERE deleted_at IS NULL;`
)
this.addSql(`drop index if exists "IDX_order_shipping_version";`);
this.addSql(`drop index if exists "IDX_order_shipping_version";`)
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_shipping_order_id_version" ON "order_shipping" (order_id, version) WHERE deleted_at IS NULL;`);
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_order_shipping_order_id_version" ON "order_shipping" (order_id, version) WHERE deleted_at IS NULL;`
)
}
override async down(): Promise<void> {
this.addSql(`drop index if exists "IDX_order_item_order_id_version";`);
this.addSql(`drop index if exists "IDX_order_item_order_id_version";`)
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_item_version" ON "order_item" (order_id, version) WHERE deleted_at IS NULL;`);
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_order_item_version" ON "order_item" (order_id, version) WHERE deleted_at IS NULL;`
)
this.addSql(`drop index if exists "IDX_order_shipping_order_id_version";`);
this.addSql(`drop index if exists "IDX_order_shipping_order_id_version";`)
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_shipping_version" ON "order_shipping" (order_id, version) WHERE deleted_at IS NULL;`);
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_order_shipping_version" ON "order_shipping" (order_id, version) WHERE deleted_at IS NULL;`
)
}
}
@@ -0,0 +1,131 @@
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
export class Migration20251114100559 extends Migration {
override async up(): Promise<void> {
this.addSql(
`alter table if exists "order" add column if not exists "custom_display_id" text null;`
)
this.addSql(
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_order_custom_display_id" ON "order" ("custom_display_id") WHERE deleted_at IS NULL;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_fulfilled_quantity" type jsonb using ("raw_fulfilled_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_fulfilled_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_delivered_quantity" type jsonb using ("raw_delivered_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_delivered_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_shipped_quantity" type jsonb using ("raw_shipped_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_shipped_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_requested_quantity" type jsonb using ("raw_return_requested_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_requested_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_received_quantity" type jsonb using ("raw_return_received_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_received_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_dismissed_quantity" type jsonb using ("raw_return_dismissed_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_dismissed_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_written_off_quantity" type jsonb using ("raw_written_off_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_written_off_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_received_quantity" type jsonb using ("raw_received_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_received_quantity" set default '{"value":"0","precision":20}';`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_damaged_quantity" type jsonb using ("raw_damaged_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_damaged_quantity" set default '{"value":"0","precision":20}';`
)
}
override async down(): Promise<void> {
this.addSql(`drop index if exists "IDX_order_custom_display_id";`)
this.addSql(
`alter table if exists "order" drop column if exists "custom_display_id";`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_fulfilled_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_fulfilled_quantity" type jsonb using ("raw_fulfilled_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_delivered_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_delivered_quantity" type jsonb using ("raw_delivered_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_shipped_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_shipped_quantity" type jsonb using ("raw_shipped_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_requested_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_requested_quantity" type jsonb using ("raw_return_requested_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_received_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_received_quantity" type jsonb using ("raw_return_received_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_dismissed_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_return_dismissed_quantity" type jsonb using ("raw_return_dismissed_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_written_off_quantity" drop default;`
)
this.addSql(
`alter table if exists "order_item" alter column "raw_written_off_quantity" type jsonb using ("raw_written_off_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_received_quantity" drop default;`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_received_quantity" type jsonb using ("raw_received_quantity"::jsonb);`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_damaged_quantity" drop default;`
)
this.addSql(
`alter table if exists "return_item" alter column "raw_damaged_quantity" type jsonb using ("raw_damaged_quantity"::jsonb);`
)
}
}
@@ -11,6 +11,7 @@ const _Order = model
.define("Order", {
id: model.id({ prefix: "order" }).primaryKey(),
display_id: model.autoincrement().searchable(),
custom_display_id: model.text().nullable(),
region_id: model.text().nullable(),
customer_id: model.text().nullable(),
version: model.number().default(1),
@@ -71,6 +72,12 @@ const _Order = model
unique: false,
where: "deleted_at IS NULL",
},
{
name: "IDX_order_custom_display_id",
on: ["custom_display_id"],
unique: true,
where: "deleted_at IS NULL",
},
{
name: "IDX_order_region_id",
on: ["region_id"],
@@ -249,6 +249,8 @@ type Order {
status: OrderStatus!
region_id: String
customer_id: String
display_id: String
custom_display_id: String
sales_channel_id: String
email: String
currency_code: String!
@@ -7,7 +7,6 @@ import {
FilterableOrderReturnReasonProps,
FindConfig,
InferEntityType,
InternalModuleDeclaration,
IOrderModuleService,
ModuleJoinerConfig,
ModulesSdkTypes,
@@ -218,6 +217,14 @@ export default class OrderModuleService
}>(generateMethodForModels)
implements IOrderModuleService
{
protected generateCustomDisplayId_: (
this: OrderModuleService,
order: OrderTypes.CreateOrderDTO,
sharedContext: Context
) => Promise<string | undefined> = async () => {
return undefined
}
protected baseRepository_: DAL.RepositoryService
protected orderService_: OrderService
protected orderAddressService_: ModulesSdkTypes.IMedusaInternalService<
@@ -311,7 +318,12 @@ export default class OrderModuleService
orderExchangeService,
orderCreditLineService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
options?: {
generateCustomDisplayId?: (
order: OrderTypes.CreateOrderDTO,
sharedContext: Context
) => Promise<string | undefined>
}
) {
// @ts-ignore
super(...arguments)
@@ -338,6 +350,9 @@ export default class OrderModuleService
this.orderClaimService_ = orderClaimService
this.orderExchangeService_ = orderExchangeService
this.orderCreditLineService_ = orderCreditLineService
this.generateCustomDisplayId_ =
options?.generateCustomDisplayId ?? this.generateCustomDisplayId_
}
__joinerConfig(): ModuleJoinerConfig {
@@ -740,14 +755,15 @@ export default class OrderModuleService
const creditLinesToCreate: CreateOrderCreditLineDTO[] = []
const createdOrders: InferEntityType<typeof Order>[] = []
for (const {
items,
shipping_methods,
credit_lines,
shipping_address,
billing_address,
...order
} of data) {
for (const data_ of data) {
const {
items,
shipping_methods,
credit_lines,
shipping_address,
billing_address,
...order
} = data_
const ord = order as any
const shippingMethods = shipping_methods?.map((sm: any) => {
@@ -777,6 +793,11 @@ export default class OrderModuleService
totals: calculated.summary,
}
ord.custom_display_id = await this.generateCustomDisplayId_.bind(this)(
data_,
sharedContext
)
const created = await this.orderService_.create(ord, sharedContext)
creditLinesToCreate.push(