diff --git a/packages/medusa/src/api/store/orders/[id]/route.ts b/packages/medusa/src/api/store/orders/[id]/route.ts index 1c7a611703..a1f8e12d05 100644 --- a/packages/medusa/src/api/store/orders/[id]/route.ts +++ b/packages/medusa/src/api/store/orders/[id]/route.ts @@ -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 ) => { - 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 }) } diff --git a/packages/medusa/src/api/store/orders/route.ts b/packages/medusa/src/api/store/orders/route.ts index 23465ce7de..02eace8e8f 100644 --- a/packages/medusa/src/api/store/orders/route.ts +++ b/packages/medusa/src/api/store/orders/route.ts @@ -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, res: MedusaResponse ) => { - 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, diff --git a/packages/modules/order/src/utils/actions/item-update.ts b/packages/modules/order/src/utils/actions/item-update.ts index 3e3d521156..5b28bb7a91 100644 --- a/packages/modules/order/src/utils/actions/item-update.ts +++ b/packages/modules/order/src/utils/actions/item-update.ts @@ -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 }) {