Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa): use transform order for store order endpoints #9489

Merged
merged 4 commits into from
Oct 7, 2024
Merged
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
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
Loading