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:
1 parent
f9e8403d29
commit
70564c3c66
3 files changed
+48
-30
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 { 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
|
// TODO: Do we want to apply some sort of authentication here? My suggestion is that we do
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: MedusaRequest,
|
req: MedusaRequest,
|
||||||
res: MedusaResponse<HttpTypes.StoreOrderResponse>
|
res: MedusaResponse<HttpTypes.StoreOrderResponse>
|
||||||
) => {
|
) => {
|
||||||
const order = await refetchOrder(
|
const workflow = getOrderDetailWorkflow(req.scope)
|
||||||
req.params.id,
|
const { result } = await workflow.run({
|
||||||
req.scope,
|
input: {
|
||||||
req.remoteQueryConfig.fields
|
fields: req.remoteQueryConfig.fields,
|
||||||
)
|
order_id: req.params.id,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
res.json({ order })
|
res.status(200).json({ order: result as HttpTypes.StoreOrder })
|
||||||
}
|
}
|
||||||
@@ -1,35 +1,37 @@
|
|||||||
import {
|
import { getOrdersListWorkflow } from "@medusajs/core-flows"
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/framework/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "@medusajs/framework/http"
|
} from "@medusajs/framework/http"
|
||||||
import { HttpTypes } from "@medusajs/framework/types"
|
import { HttpTypes, OrderDTO } from "@medusajs/framework/types"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest<HttpTypes.StoreOrderFilters>,
|
req: AuthenticatedMedusaRequest<HttpTypes.StoreOrderFilters>,
|
||||||
res: MedusaResponse<HttpTypes.StoreOrderListResponse>
|
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({
|
const workflow = getOrdersListWorkflow(req.scope)
|
||||||
entryPoint: "order",
|
const { result } = await workflow.run({
|
||||||
variables: {
|
input: {
|
||||||
filters: {
|
fields: req.remoteQueryConfig.fields,
|
||||||
...req.filterableFields,
|
variables,
|
||||||
customer_id: req.auth_context.actor_id,
|
|
||||||
},
|
|
||||||
...req.remoteQueryConfig.pagination,
|
|
||||||
},
|
},
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const { rows: orders, metadata } = await remoteQuery(queryObject)
|
const { rows, metadata } = result as {
|
||||||
|
rows: OrderDTO[]
|
||||||
|
metadata: any
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
orders,
|
orders: rows as unknown as HttpTypes.StoreOrder[],
|
||||||
count: metadata.count,
|
count: metadata.count,
|
||||||
offset: metadata.skip,
|
offset: metadata.skip,
|
||||||
limit: metadata.take,
|
limit: metadata.take,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
|
|||||||
|
|
||||||
existing.detail.quantity ??= 0
|
existing.detail.quantity ??= 0
|
||||||
|
|
||||||
const quantityDiff = MathBN.sub(
|
let quantityDiff = MathBN.sub(
|
||||||
action.details.quantity,
|
action.details.quantity,
|
||||||
existing.detail.quantity
|
existing.detail.quantity
|
||||||
)
|
)
|
||||||
@@ -28,14 +28,28 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
|
|||||||
existing.detail.quantity = quant
|
existing.detail.quantity = quant
|
||||||
|
|
||||||
if (unitPrice) {
|
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
|
const currentQuantityBn = MathBN.convert(action.details.quantity)
|
||||||
existing.detail.unit_price = unitPriceBN
|
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)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
return MathBN.mult(existing.unit_price, quantityDiff)
|
return MathBN.mult(existing.unit_price, quantityDiff)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
|
|||||||
Reference in new issue
Block a user