Skip to content

Commit 5c695b5

Browse files
authored
fix(order): call compensateRelationFieldsSelectionFromLoadStrategy when select-in strategy is configured in order repository list methods (#14566)
## Summary **What** — What changes are introduced in this PR? Fix `shipping_address` and `billing_address` not being returned when only specifying their star modifier in `fields` (without their foreign key) and pagination is configured. **Why** — Why are these changes relevant or necessary? When querying for orders and paginating, the only way to get the `shipping_address` and `billing_address` relations loaded is to include their foreign keys, along with their star field modifier. **How** — How have these changes been implemented? Added alias configuration for both relations to the order module joiner config. **Testing** — How have these changes been tested, or how can the reviewer test the feature? Integration tests. --- ## Examples Provide examples or code snippets that demonstrate how this feature works, or how it can be used in practice. This helps with documentation and ensures maintainers can quickly understand and verify the change. ```ts // Example usage ``` --- ## Checklist Please ensure the following before requesting a review: - [x] I have added a **changeset** for this PR - Every non-breaking change should be marked as a **patch** - To add a changeset, run `yarn changeset` and follow the prompts - [x] The changes are covered by relevant **tests** - [x] I have verified the code works as intended locally - [x] I have linked the related issue(s) if applicable --- ## Additional Context Add any additional context, related issues, or references that might help the reviewer understand this PR. fixes #14563
1 parent 0fc3e35 commit 5c695b5

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

.changeset/humble-peaches-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/order": patch
3+
---
4+
5+
fix(order): call `compensateRelationFieldsSelectionFromLoadStrategy` when `select-in` strategy is configured in order repository list methods

integration-tests/http/__tests__/order/admin/order.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,65 @@ medusaIntegrationTestRunner({
164164
}),
165165
])
166166
})
167+
168+
it("should return shipping_address when pagination included", async () => {
169+
const response = await api.get(
170+
`/admin/orders?fields=*shipping_address&offset=0`,
171+
adminHeaders
172+
)
173+
174+
expect(response.data.orders).toHaveLength(1)
175+
expect(response.data.orders[0].id).toEqual(order.id)
176+
expect(response.data.orders[0].shipping_address).toBeDefined()
177+
expect(response.data.orders[0].shipping_address.address_1).toEqual(
178+
order.shipping_address.address_1
179+
)
180+
expect(response.data.orders[0].shipping_address.city).toEqual(
181+
order.shipping_address.city
182+
)
183+
})
184+
185+
it("should return billing_address when pagination included", async () => {
186+
const response = await api.get(
187+
`/admin/orders?fields=*billing_address&offset=0`,
188+
adminHeaders
189+
)
190+
191+
expect(response.data.orders).toHaveLength(1)
192+
expect(response.data.orders[0].id).toEqual(order.id)
193+
expect(response.data.orders[0].billing_address).toBeDefined()
194+
expect(response.data.orders[0].billing_address.address_1).toEqual(
195+
order.billing_address.address_1
196+
)
197+
expect(response.data.orders[0].billing_address.city).toEqual(
198+
order.billing_address.city
199+
)
200+
})
201+
202+
it("should return specific address fields when pagination included", async () => {
203+
const response = await api.get(
204+
`/admin/orders?fields=+shipping_address.address_1,+shipping_address.city,+billing_address.address_1,+billing_address.city&offset=0`,
205+
adminHeaders
206+
)
207+
208+
expect(response.data.orders).toHaveLength(1)
209+
const responseOrder = response.data.orders[0]
210+
expect(responseOrder.id).toEqual(order.id)
211+
expect(responseOrder.shipping_address).toBeDefined()
212+
expect(responseOrder.shipping_address.address_1).toEqual(
213+
order.shipping_address.address_1
214+
)
215+
expect(responseOrder.shipping_address.city).toEqual(
216+
order.shipping_address.city
217+
)
218+
expect(responseOrder.billing_address).toBeDefined()
219+
expect(responseOrder.billing_address.address_1).toEqual(
220+
order.billing_address.address_1
221+
)
222+
expect(responseOrder.billing_address.city).toEqual(
223+
order.billing_address.city
224+
)
225+
})
167226
})
168227

169228
describe("POST /orders/:id", () => {

packages/modules/order/src/utils/base-repository-find.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Constructor, Context, DAL } from "@medusajs/framework/types"
2-
import { toMikroORMEntity } from "@medusajs/framework/utils"
2+
import { MikroOrmBaseRepository, toMikroORMEntity } from "@medusajs/framework/utils"
33
import { LoadStrategy } from "@medusajs/framework/mikro-orm/core"
44
import { Order, OrderClaim, OrderLineItemAdjustment } from "@models"
55

@@ -103,6 +103,12 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
103103

104104
config.where ??= {}
105105

106+
if (strategy === LoadStrategy.SELECT_IN) {
107+
MikroOrmBaseRepository.compensateRelationFieldsSelectionFromLoadStrategy({
108+
findOptions: config,
109+
})
110+
}
111+
106112
const result = await manager.find(this.entity, config.where, config.options)
107113

108114
if (loadAdjustments) {
@@ -198,6 +204,12 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
198204
config.options.orderBy = { id: "ASC" }
199205
}
200206

207+
if (strategy === LoadStrategy.SELECT_IN) {
208+
MikroOrmBaseRepository.compensateRelationFieldsSelectionFromLoadStrategy({
209+
findOptions: config,
210+
})
211+
}
212+
201213
const [result, count] = await manager.findAndCount(
202214
this.entity,
203215
config.where,

0 commit comments

Comments
 (0)