fix(): Order constraint and receive return (#12889)

**What**
- Fix missing `ON DELETE CASCADE` constraint on order credit lines
- Fix `receiveReturn` miss usage
- Make all order integration tests to run and rename them all to `*.spec.ts`
- Fix package.json typo
This commit is contained in:
Adrien de Peretti
2025-07-04 14:59:50 +02:00
committed by GitHub
parent fa76f85bba
commit 2f70f13351
35 changed files with 259 additions and 107 deletions

View File

@@ -1,12 +1,12 @@
import { CreateOrderDTO, IOrderModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
import { Modules, promiseAll } from "@medusajs/framework/utils"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
jest.setTimeout(100000)
moduleIntegrationTestRunner<IOrderModuleService>({
moduleName: Modules.ORDER,
testSuite: ({ service }) => {
testSuite: ({ service, MikroOrmWrapper }) => {
describe("Order Module Service", () => {
const input = {
email: "foo@bar.com",
@@ -307,6 +307,107 @@ moduleIntegrationTestRunner<IOrderModuleService>({
)
})
it("should delete entirely an order, shipping method and items. Including taxes and adjustments associated with them and credit lines", async function () {
const inpCopy = JSON.parse(JSON.stringify(input)) as CreateOrderDTO
inpCopy.transactions!.push({
amount: 10,
currency_code: "USD",
})
const created = await service.createOrders(inpCopy)
await service.createOrderCreditLines([
{
order_id: created.id,
amount: 10,
reference_id: "credit_line_1",
},
])
await service.addOrderTransactions([
{
order_id: created.id,
amount: -20,
currency_code: "USD",
},
])
async function findAllOrderRelatedEntities(orderId: string) {
const orderIdClause = { order_id: orderId }
const manager = MikroOrmWrapper.forkManager()
const [
order,
orderItems,
orderSummary,
orderShippingMethods,
orderTransactions,
orderCreditLines,
orderShippingAddress,
orderBillingAddress,
orderChange,
orderChangeAction,
] = await promiseAll([
manager.findOne("Order", {
id: created.id,
}),
manager.findOne("OrderItem", orderIdClause),
manager.findOne("OrderSummary", orderIdClause),
manager.findOne("OrderShipping", orderIdClause),
manager.findOne("OrderTransaction", orderIdClause),
manager.findOne("OrderCreditLine", orderIdClause),
manager.findOne("OrderAddress", {
first_name: "Test",
last_name: "Test",
address_1: "Test",
city: "Test",
country_code: "US",
postal_code: "12345",
phone: "12345",
}),
manager.findOne("OrderAddress", {
first_name: "Test",
last_name: "Test",
address_1: "Test",
city: "Test",
country_code: "US",
postal_code: "12345",
}),
])
return {
order,
orderItems,
orderSummary,
orderShippingMethods,
orderTransactions,
orderCreditLines,
orderShippingAddress,
orderBillingAddress,
}
}
let orderRelatedEntities = await findAllOrderRelatedEntities(created.id)
expect(orderRelatedEntities.order).not.toBeNull()
expect(orderRelatedEntities.orderItems).not.toBeNull()
expect(orderRelatedEntities.orderSummary).not.toBeNull()
expect(orderRelatedEntities.orderShippingMethods).not.toBeNull()
expect(orderRelatedEntities.orderTransactions).not.toBeNull()
expect(orderRelatedEntities.orderCreditLines).not.toBeNull()
expect(orderRelatedEntities.orderShippingAddress).not.toBeNull()
expect(orderRelatedEntities.orderBillingAddress).not.toBeNull()
await service.deleteOrders([created.id])
orderRelatedEntities = await findAllOrderRelatedEntities(created.id)
expect(orderRelatedEntities.order).toBeNull()
expect(orderRelatedEntities.orderItems).toBeNull()
expect(orderRelatedEntities.orderSummary).toBeNull()
expect(orderRelatedEntities.orderShippingMethods).toBeNull()
expect(orderRelatedEntities.orderTransactions).toBeNull()
expect(orderRelatedEntities.orderCreditLines).toBeNull()
expect(orderRelatedEntities.orderShippingAddress).toBeNull()
expect(orderRelatedEntities.orderBillingAddress).toBeNull()
})
it("should transform requested fields and relations to match the db schema and return the order", async function () {
const createdOrder = await service.createOrders(input)
const getOrder = await service.retrieveOrder(createdOrder.id, {

View File

@@ -30,10 +30,10 @@
"build": "rimraf dist && tsc --build && npm run resolve:aliases",
"test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts",
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.spec.ts",
"migration:initial": " MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm migration:create --initial",
"migration:create": " MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm migration:create",
"migration:up": " MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm migration:up",
"orm:cache:clear": " MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm cache:clear"
"migration:initial": "MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm migration:create --initial",
"migration:create": "MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm migration:create",
"migration:up": "MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm migration:up",
"orm:cache:clear": "MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts medusa-mikro-orm cache:clear"
},
"devDependencies": {
"@medusajs/framework": "2.8.6",

View File

@@ -1311,6 +1311,7 @@
"id"
],
"referencedTableName": "public.order",
"deleteRule": "cascade",
"updateRule": "cascade"
}
},

View File

@@ -0,0 +1,23 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20250704120229 extends Migration {
override async up(): Promise<void> {
this.addSql(
`alter table if exists "order_credit_line" drop constraint if exists "order_credit_line_order_id_foreign";`
)
this.addSql(
`alter table if exists "order_credit_line" add constraint "order_credit_line_order_id_foreign" foreign key ("order_id") references "order" ("id") on update cascade on delete cascade;`
)
}
override async down(): Promise<void> {
this.addSql(
`alter table if exists "order_credit_line" drop constraint if exists "order_credit_line_order_id_foreign";`
)
this.addSql(
`alter table if exists "order_credit_line" add constraint "order_credit_line_order_id_foreign" foreign key ("order_id") references "order" ("id") on update cascade;`
)
}
}

View File

@@ -56,7 +56,13 @@ const _Order = model
}),
})
.cascades({
delete: ["summary", "items", "shipping_methods", "transactions"],
delete: [
"summary",
"items",
"shipping_methods",
"transactions",
"credit_lines",
],
})
.indexes([
{

View File

@@ -897,13 +897,10 @@ export default class OrderModuleService
(orderShipping) => orderShipping.shipping_method_id
)
await promiseAll([
this.orderAddressService_.delete(orderAddressIds, sharedContext),
// Delete order changes & actions
this.orderChangeService_.delete(orderChangeIds, sharedContext),
])
await this.orderAddressService_.delete(orderAddressIds, sharedContext)
await this.orderChangeService_.delete(orderChangeIds, sharedContext)
// Delete order, order items, summary, shipping methods and transactions
// Delete order, order items, summary, shipping methods, transactions and credit lines
await super.deleteOrders(ids, sharedContext)
await promiseAll([
@@ -3653,7 +3650,7 @@ export default class OrderModuleService
): Promise<OrderTypes.ReturnDTO> {
const ret = await this.receiveReturn_(data, sharedContext)
return await this.retrieveReturn(ret.id, {
return await this.retrieveReturn(ret[0].id, {
relations: [
"items",
"items.item",
@@ -3668,7 +3665,7 @@ export default class OrderModuleService
private async receiveReturn_(
data: OrderTypes.ReceiveOrderReturnDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
): Promise<any[]> {
return await BundledActions.receiveReturn.bind(this)(data, sharedContext)
}