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

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

* chore: update amount when unit price is present
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-10-07 13:55:06 +02:00
1 parent f9e8403d29
commit 70564c3c66
3 files changed
+44 -26

No files matched your search

@@ -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 })
}
+15 -13
View File
@@ -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 queryObject = remoteQueryObjectFromString({
entryPoint: "order",
variables: {
const 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,
},
})
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,
@@ -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
)
@@ -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
)
existing.unit_price = unitPriceBN
existing.detail.unit_price = unitPriceBN
}
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 = 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 }) {