From 38d4a7db3d762fc7f746825c64026d2711db5bd8 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Thu, 3 Nov 2022 08:44:47 +0100 Subject: [PATCH] Feat(Medusa): Allow custom shipping price on draft orders (#2531) * create custom shipping option for cart to have custom shipping price on draft orders * Create moody-chefs-stare.md --- .changeset/moody-chefs-stare.md | 5 ++ .../api/__tests__/admin/draft-order.js | 46 +++++++++++++++++++ packages/medusa/src/services/draft-order.ts | 15 ++++++ 3 files changed, 66 insertions(+) create mode 100644 .changeset/moody-chefs-stare.md diff --git a/.changeset/moody-chefs-stare.md b/.changeset/moody-chefs-stare.md new file mode 100644 index 0000000000..4c3d7da267 --- /dev/null +++ b/.changeset/moody-chefs-stare.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Feat(Medusa): Allow custom shipping price on draft orders diff --git a/integration-tests/api/__tests__/admin/draft-order.js b/integration-tests/api/__tests__/admin/draft-order.js index 4fc03c8605..e495d6d78a 100644 --- a/integration-tests/api/__tests__/admin/draft-order.js +++ b/integration-tests/api/__tests__/admin/draft-order.js @@ -71,6 +71,52 @@ describe("/admin/draft-orders", () => { expect(response.status).toEqual(200) }) + it("creates a draft order with a custom shipping option price", async () => { + const api = useApi() + + const payload = { + email: "oli@test.dk", + shipping_address: "oli-shipping", + items: [ + { + variant_id: "test-variant", + quantity: 2, + metadata: {}, + }, + ], + region_id: "test-region", + customer_id: "oli-test", + shipping_methods: [ + { + option_id: "test-option", + price: 500, + }, + ], + } + + const response = await api.post("/admin/draft-orders", payload, { + headers: { + Authorization: "Bearer test_token", + }, + }) + expect(response.status).toEqual(200) + + const draftOrderId = response.data.draft_order.id + + const draftOrderResponse = await api.get( + `/admin/draft-orders/${draftOrderId}`, + { + headers: { + Authorization: "Bearer test_token", + }, + } + ) + expect(draftOrderResponse.status).toEqual(200) + expect(draftOrderResponse.data.draft_order.cart.shipping_total).toEqual( + 500 + ) + }) + it("creates a draft order with a billing address that is an AddressPayload and a shipping address that is an ID", async () => { const api = useApi() diff --git a/packages/medusa/src/services/draft-order.ts b/packages/medusa/src/services/draft-order.ts index 5d7c26def8..e969b3d50e 100644 --- a/packages/medusa/src/services/draft-order.ts +++ b/packages/medusa/src/services/draft-order.ts @@ -9,6 +9,7 @@ import { ExtendedFindConfig, FindConfig } from "../types/common" import { DraftOrderCreateProps } from "../types/draft-orders" import { buildQuery } from "../utils" import CartService from "./cart" +import CustomShippingOptionService from "./custom-shipping-option" import EventBusService from "./event-bus" import LineItemService from "./line-item" import ProductVariantService from "./product-variant" @@ -24,6 +25,7 @@ type InjectedDependencies = { lineItemService: LineItemService productVariantService: ProductVariantService shippingOptionService: ShippingOptionService + customShippingOptionService: CustomShippingOptionService } /** @@ -47,6 +49,7 @@ class DraftOrderService extends TransactionBaseService { protected readonly lineItemService_: LineItemService protected readonly productVariantService_: ProductVariantService protected readonly shippingOptionService_: ShippingOptionService + protected readonly customShippingOptionService_: CustomShippingOptionService constructor({ manager, @@ -58,6 +61,7 @@ class DraftOrderService extends TransactionBaseService { lineItemService, productVariantService, shippingOptionService, + customShippingOptionService, }: InjectedDependencies) { // eslint-disable-next-line prefer-rest-params super(arguments[0]) @@ -70,6 +74,7 @@ class DraftOrderService extends TransactionBaseService { this.cartService_ = cartService this.productVariantService_ = productVariantService this.shippingOptionService_ = shippingOptionService + this.customShippingOptionService_ = customShippingOptionService this.eventBus_ = eventBusService } @@ -327,6 +332,16 @@ class DraftOrderService extends TransactionBaseService { } for (const method of shipping_methods) { + if (typeof method.price !== "undefined") { + await this.customShippingOptionService_ + .withTransaction(transactionManager) + .create({ + shipping_option_id: method.option_id, + cart_id: createdCart.id, + price: method.price, + }) + } + await cartServiceTx.addShippingMethod( createdCart.id, method.option_id,