Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/order-list-totals-missing-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/order": patch
---

fix(order): select shipping method fields when listing orders with totals
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,21 @@ moduleIntegrationTestRunner<IOrderModuleService>({
)
expect(orders4.length).toEqual(0)
})

it("should list orders with totals without selecting shipping method fields", async function () {
const createdOrder = await service.createOrders(input)

const [orders, count] = await service.listAndCountOrders(
{ id: createdOrder.id },
{
select: ["id", "total"],
}
)

expect(count).toEqual(1)
expect(orders[0].id).toEqual(createdOrder.id)
expect(Number(orders[0].total)).toEqual(58.9)
})
})
},
})
54 changes: 54 additions & 0 deletions packages/modules/order/src/utils/base-repository-find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,58 @@ function ensureOrderItemFieldsSelection(config: any, isRelatedEntity: boolean) {
}
}

function ensureOrderShippingMethodFieldsSelection(
config: any,
isRelatedEntity: boolean
) {
const populate = config.options?.populate ?? []
const fields = config.options?.fields ?? []

const hasShippingMethodPopulate = populate.some(
(p: string) =>
p === "shipping_methods.shipping_method" ||
p.startsWith("shipping_methods.shipping_method.") ||
p === "order.shipping_methods.shipping_method" ||
p.startsWith("order.shipping_methods.shipping_method.")
)

if (!hasShippingMethodPopulate) {
return
}

const hasOrderShippingMethodFields = fields.some((field: string) => {
if (
field === "shipping_methods.*" ||
field === "order.shipping_methods.*"
) {
return true
}

if (
field.startsWith("shipping_methods.") &&
!field.startsWith("shipping_methods.shipping_method.")
) {
return true
}

if (
field.startsWith("order.shipping_methods.") &&
!field.startsWith("order.shipping_methods.shipping_method.")
) {
return true
}

return false
})

if (!hasOrderShippingMethodFields) {
fields.push(
isRelatedEntity ? "order.shipping_methods.*" : "shipping_methods.*"
)
config.options.fields = fields
}
}

export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
klass.prototype.find = async function find(
this: any,
Expand Down Expand Up @@ -245,6 +297,7 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {

if (strategy === LoadStrategy.SELECT_IN) {
ensureOrderItemFieldsSelection(config, isRelatedEntity)
ensureOrderShippingMethodFieldsSelection(config, isRelatedEntity)
MikroOrmBaseRepository.compensateRelationFieldsSelectionFromLoadStrategy({
findOptions: config,
})
Expand Down Expand Up @@ -393,6 +446,7 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {

if (strategy === LoadStrategy.SELECT_IN) {
ensureOrderItemFieldsSelection(config, isRelatedEntity)
ensureOrderShippingMethodFieldsSelection(config, isRelatedEntity)
MikroOrmBaseRepository.compensateRelationFieldsSelectionFromLoadStrategy({
findOptions: config,
})
Expand Down
Loading