Skip to content

Commit

Permalink
fix(medusa): use transform order for store order endpoints (medusajs#…
Browse files Browse the repository at this point in the history
…9489)

* fix(medusa): use transform order for store order endpoints

* chore: update amount when unit price is present
  • Loading branch information
riqwan authored Oct 7, 2024
1 parent f9e8403 commit 70564c3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
18 changes: 10 additions & 8 deletions packages/medusa/src/api/store/orders/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { HttpTypes } from "@medusajs/framework/types"
import { getOrderDetailWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { refetchOrder } from "../helpers"
import { HttpTypes } from "@medusajs/framework/types"

// TODO: Do we want to apply some sort of authentication here? My suggestion is that we do
export const GET = async (
req: MedusaRequest,
res: MedusaResponse<HttpTypes.StoreOrderResponse>
) => {
const order = await refetchOrder(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
const workflow = getOrderDetailWorkflow(req.scope)
const { result } = await workflow.run({
input: {
fields: req.remoteQueryConfig.fields,
order_id: req.params.id,
},
})

res.json({ order })
res.status(200).json({ order: result as HttpTypes.StoreOrder })
}
36 changes: 19 additions & 17 deletions packages/medusa/src/api/store/orders/route.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/framework/utils"
import { getOrdersListWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import { HttpTypes } from "@medusajs/framework/types"
import { HttpTypes, OrderDTO } from "@medusajs/framework/types"

export const GET = async (
req: AuthenticatedMedusaRequest<HttpTypes.StoreOrderFilters>,
res: MedusaResponse<HttpTypes.StoreOrderListResponse>
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const variables = {
filters: {
...req.filterableFields,
customer_id: req.auth_context.actor_id,
},
...req.remoteQueryConfig.pagination,
}

const queryObject = remoteQueryObjectFromString({
entryPoint: "order",
variables: {
filters: {
...req.filterableFields,
customer_id: req.auth_context.actor_id,
},
...req.remoteQueryConfig.pagination,
const workflow = getOrdersListWorkflow(req.scope)
const { result } = await workflow.run({
input: {
fields: req.remoteQueryConfig.fields,
variables,
},
fields: req.remoteQueryConfig.fields,
})

const { rows: orders, metadata } = await remoteQuery(queryObject)
const { rows, metadata } = result as {
rows: OrderDTO[]
metadata: any
}

res.json({
orders,
orders: rows as unknown as HttpTypes.StoreOrder[],
count: metadata.count,
offset: metadata.skip,
limit: metadata.take,
Expand Down
24 changes: 19 additions & 5 deletions packages/modules/order/src/utils/actions/item-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {

existing.detail.quantity ??= 0

const quantityDiff = MathBN.sub(
let quantityDiff = MathBN.sub(
action.details.quantity,
existing.detail.quantity
)
Expand All @@ -28,14 +28,28 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
existing.detail.quantity = quant

if (unitPrice) {
const unitPriceBN = new BigNumber(unitPrice)
const currentUnitPriceBN = MathBN.convert(unitPrice)
const originalUnitPriceBn = MathBN.convert(
existing.detail.unit_price ?? existing.unit_price
)

const currentQuantityBn = MathBN.convert(action.details.quantity)
const originalQuantityBn = MathBN.convert(
existing.detail.quantity ?? existing.quantity
)

const originalTotal = MathBN.mult(originalUnitPriceBn, originalQuantityBn)
const currentTotal = MathBN.mult(currentUnitPriceBN, currentQuantityBn)

existing.unit_price = unitPriceBN
existing.detail.unit_price = unitPriceBN
existing.unit_price = currentUnitPriceBN
existing.detail.unit_price = currentUnitPriceBN

setActionReference(existing, action, options)

return MathBN.sub(currentTotal, originalTotal)
}

setActionReference(existing, action, options)

return MathBN.mult(existing.unit_price, quantityDiff)
},
validate({ action, currentOrder }) {
Expand Down

0 comments on commit 70564c3

Please sign in to comment.