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 authored and GitHub committed 2025-07-04 12:59:50 +00:00
1 parent fa76f85bba
commit 2f70f13351
11 files changed
+163 -11

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
---
"@medusajs/api-key": patch
"@medusajs/auth": patch
"@medusajs/cart": patch
"@medusajs/currency": patch
"@medusajs/file": patch
"@medusajs/fulfillment": patch
"@medusajs/index": patch
"@medusajs/inventory": patch
"@medusajs/locking": patch
"@medusajs/notification": patch
"@medusajs/order": patch
"@medusajs/payment": patch
"@medusajs/pricing": patch
"@medusajs/product": patch
"@medusajs/promotion": patch
"@medusajs/region": patch
"@medusajs/sales-channel": patch
"@medusajs/stock-location": patch
"@medusajs/store": patch
"@medusajs/locking-postgres": patch
---
fix(): Order constraint and receive return
@@ -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, {
@@ -1311,6 +1311,7 @@
"id"
],
"referencedTableName": "public.order",
"deleteRule": "cascade",
"updateRule": "cascade"
}
},
@@ -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;`
)
}
}
+7 -1
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([
{
@@ -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)
}