fix(medusa): Add totals when retrieving order by cart id (#3777)
* fix(medusa): Add totals when retrieving order by cart id * fix: Unit test * Create .changeset/stupid-rockets-smile.md
This commit is contained in:
@@ -19,8 +19,10 @@ describe("GET /store/orders", () => {
|
||||
})
|
||||
|
||||
it("calls orderService retrieve", () => {
|
||||
expect(OrderServiceMock.retrieveByCartId).toHaveBeenCalledTimes(1)
|
||||
expect(OrderServiceMock.retrieveByCartId).toHaveBeenCalledWith(
|
||||
expect(OrderServiceMock.retrieveByCartIdWithTotals).toHaveBeenCalledTimes(
|
||||
1
|
||||
)
|
||||
expect(OrderServiceMock.retrieveByCartIdWithTotals).toHaveBeenCalledWith(
|
||||
IdMap.getId("test-cart"),
|
||||
{
|
||||
select: defaultStoreOrdersFields,
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { defaultStoreOrdersFields, defaultStoreOrdersRelations } from "."
|
||||
|
||||
import { OrderService } from "../../../../services"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
|
||||
@@ -50,10 +48,11 @@ export default async (req, res) => {
|
||||
const { cart_id } = req.params
|
||||
|
||||
const orderService: OrderService = req.scope.resolve("orderService")
|
||||
const order = await orderService.retrieveByCartId(cart_id, {
|
||||
select: defaultStoreOrdersFields,
|
||||
relations: defaultStoreOrdersRelations,
|
||||
})
|
||||
|
||||
const order = await orderService.retrieveByCartIdWithTotals(
|
||||
cart_id,
|
||||
req.retrieveConfig
|
||||
)
|
||||
|
||||
res.json({ order: cleanResponseData(order, []) })
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Router } from "express"
|
||||
import "reflect-metadata"
|
||||
import { Order } from "../../../.."
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import middlewares, {
|
||||
transformBody,
|
||||
transformStoreQuery,
|
||||
} from "../../../middlewares"
|
||||
import requireCustomerAuthentication from "../../../middlewares/require-customer-authentication"
|
||||
import { StorePostCustomersCustomerOrderClaimReq } from "./request-order"
|
||||
import { StorePostCustomersCustomerAcceptClaimReq } from "./confirm-order-request"
|
||||
import { StoreGetOrderParams } from "./get-order"
|
||||
import { StoreGetOrdersParams } from "./lookup-order"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
import { StorePostCustomersCustomerOrderClaimReq } from "./request-order"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -219,6 +219,6 @@ export type StoreOrdersRes = {
|
||||
order: Order
|
||||
}
|
||||
|
||||
export * from "./lookup-order"
|
||||
export * from "./confirm-order-request"
|
||||
export * from "./lookup-order"
|
||||
export * from "./request-order"
|
||||
|
||||
@@ -187,6 +187,9 @@ export const OrderServiceMock = {
|
||||
retrieveByCartId: jest.fn().mockImplementation((cartId) => {
|
||||
return Promise.resolve({ id: IdMap.getId("test-order") })
|
||||
}),
|
||||
retrieveByCartIdWithTotals: jest.fn().mockImplementation((cartId) => {
|
||||
return Promise.resolve({ id: IdMap.getId("test-order") })
|
||||
}),
|
||||
decorate: jest.fn().mockImplementation((order) => {
|
||||
order.decorated = true
|
||||
return order
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||
import OrderService from "../order"
|
||||
import { LineItemServiceMock } from "../__mocks__/line-item"
|
||||
import { newTotalsServiceMock } from "../__mocks__/new-totals"
|
||||
import { ProductVariantInventoryServiceMock } from "../__mocks__/product-variant-inventory"
|
||||
import { taxProviderServiceMock } from "../__mocks__/tax-provider"
|
||||
import OrderService from "../order"
|
||||
|
||||
describe("OrderService", () => {
|
||||
const totalsService = {
|
||||
@@ -519,12 +519,13 @@ describe("OrderService", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveByCartId", () => {
|
||||
describe("retrieveByCartIdWithTotals", () => {
|
||||
const orderRepo = MockRepository({
|
||||
findOne: (q) => {
|
||||
findOneWithRelations: (q) => {
|
||||
return Promise.resolve({})
|
||||
},
|
||||
})
|
||||
|
||||
const orderService = new OrderService({
|
||||
totalsService,
|
||||
newTotalsService: newTotalsServiceMock,
|
||||
@@ -537,11 +538,15 @@ describe("OrderService", () => {
|
||||
})
|
||||
|
||||
it("calls order model functions", async () => {
|
||||
await orderService.retrieveByCartId(IdMap.getId("test-cart"))
|
||||
expect(orderRepo.findOne).toHaveBeenCalledTimes(1)
|
||||
expect(orderRepo.findOne).toHaveBeenCalledWith({
|
||||
where: { cart_id: IdMap.getId("test-cart") },
|
||||
})
|
||||
await orderService.retrieveByCartIdWithTotals(IdMap.getId("test-cart"))
|
||||
|
||||
expect(orderRepo.findOneWithRelations).toHaveBeenCalledTimes(1)
|
||||
expect(orderRepo.findOneWithRelations).toHaveBeenCalledWith(
|
||||
expect.any(Object),
|
||||
{
|
||||
where: { cart_id: IdMap.getId("test-cart") },
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -487,35 +487,43 @@ class OrderService extends TransactionBaseService {
|
||||
cartId: string,
|
||||
config: FindConfig<Order> = {}
|
||||
): Promise<Order> {
|
||||
const orderRepo = this.activeManager_.withRepository(this.orderRepository_)
|
||||
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals(config)
|
||||
|
||||
const query = {
|
||||
where: { cart_id: cartId },
|
||||
} as FindConfig<Order>
|
||||
|
||||
if (relations && relations.length > 0) {
|
||||
query.relations = relations
|
||||
if (!isDefined(cartId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"cartId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
query.select = select?.length ? select : undefined
|
||||
const orderRepo = this.activeManager_.withRepository(this.orderRepository_)
|
||||
|
||||
const raw = await orderRepo.findOne(query)
|
||||
const query = buildQuery({ cart_id: cartId }, config)
|
||||
|
||||
if (!(config.select || []).length) {
|
||||
query.select = undefined
|
||||
}
|
||||
|
||||
const queryRelations = { ...query.relations }
|
||||
delete query.relations
|
||||
|
||||
const raw = await orderRepo.findOneWithRelations(queryRelations, query)
|
||||
|
||||
if (!raw) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Order with cart id: ${cartId} was not found`
|
||||
`Order with cart id ${cartId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
if (!totalsToSelect?.length) {
|
||||
return raw
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
return await this.decorateTotals(raw, totalsToSelect)
|
||||
async retrieveByCartIdWithTotals(
|
||||
cartId: string,
|
||||
options: FindConfig<Order> = {}
|
||||
): Promise<Order> {
|
||||
const relations = this.getTotalsRelations(options)
|
||||
const order = await this.retrieveByCartId(cartId, { ...options, relations })
|
||||
return await this.decorateTotals(order, {})
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -659,7 +667,7 @@ class OrderService extends TransactionBaseService {
|
||||
// Is the cascade insert really used? Also, is it really necessary to pass the entire entities when creating or updating?
|
||||
// We normally should only pass what is needed?
|
||||
const shippingMethods = cart.shipping_methods.map((method) => {
|
||||
;(method.tax_lines as any) = undefined
|
||||
(method.tax_lines as any) = undefined
|
||||
return method
|
||||
})
|
||||
|
||||
@@ -767,7 +775,7 @@ class OrderService extends TransactionBaseService {
|
||||
// TODO: Due to cascade insert we have to remove the tax_lines that have been added by the cart decorate totals.
|
||||
// Is the cascade insert really used? Also, is it really necessary to pass the entire entities when creating or updating?
|
||||
// We normally should only pass what is needed?
|
||||
;(method.tax_lines as any) = undefined
|
||||
(method.tax_lines as any) = undefined
|
||||
return shippingOptionServiceTx.updateShippingMethod(method.id, {
|
||||
order_id: order.id,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user