From eb0a4afd7918e7fef22bfc7c5e1d81eec1596174 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Tue, 8 Jun 2021 16:55:53 +0200 Subject: [PATCH] hotfix: Add shipping method to draft order --- .../src/services/__tests__/draft-order.js | 21 ++++--------- packages/medusa/src/services/draft-order.js | 30 ++++--------------- 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/packages/medusa/src/services/__tests__/draft-order.js b/packages/medusa/src/services/__tests__/draft-order.js index 81736e7347..ab5460f553 100644 --- a/packages/medusa/src/services/__tests__/draft-order.js +++ b/packages/medusa/src/services/__tests__/draft-order.js @@ -88,6 +88,7 @@ describe("DraftOrderService", () => { ...data, }) ), + addShippingMethod: jest.fn(), withTransaction: function() { return this }, @@ -156,22 +157,11 @@ describe("DraftOrderService", () => { type: "draft_order", }) - expect(shippingOptionService.createShippingMethod).toHaveBeenCalledTimes( - 1 - ) - expect(shippingOptionService.createShippingMethod).toHaveBeenCalledWith( + expect(cartService.addShippingMethod).toHaveBeenCalledTimes(1) + expect(cartService.addShippingMethod).toHaveBeenCalledWith( + "test-cart", "test-option", - {}, - { - cart: { - id: "test-cart", - region_id: "test-region", - shipping_address_id: "test-shipping", - billing_address_id: "test-billing", - customer_id: "test-customer", - type: "draft_order", - }, - } + {} ) expect(lineItemService.generate).toHaveBeenCalledTimes(1) @@ -185,7 +175,6 @@ describe("DraftOrderService", () => { expect(lineItemService.create).toHaveBeenCalledTimes(1) expect(lineItemService.create).toHaveBeenCalledWith({ cart_id: "test-cart", - has_shipping: true, title: "test-item", variant_id: "test-variant", }) diff --git a/packages/medusa/src/services/draft-order.js b/packages/medusa/src/services/draft-order.js index 5da84e98cb..e04f3ad794 100644 --- a/packages/medusa/src/services/draft-order.js +++ b/packages/medusa/src/services/draft-order.js @@ -272,22 +272,6 @@ class DraftOrderService extends BaseService { id: result.id, }) - let shippingMethods = [] - - for (const method of shipping_methods) { - const m = await this.shippingOptionService_ - .withTransaction(manager) - .createShippingMethod(method.option_id, method.data, { - cart: createdCart, - }) - - shippingMethods.push(m) - } - - const profiles = shippingMethods.map( - ({ shipping_option }) => shipping_option.profile_id - ) - for (const item of items) { if (item.variant_id) { const line = await this.lineItemService_ @@ -297,16 +281,8 @@ class DraftOrderService extends BaseService { unit_price: item.unit_price, }) - const variant = await this.productVariantService_ - .withTransaction(manager) - .retrieve(item.variant_id) - const itemProfile = variant.product.profile_id - - const hasShipping = profiles.includes(itemProfile) - await this.lineItemService_.withTransaction(manager).create({ cart_id: createdCart.id, - has_shipping: hasShipping, ...line, }) } else { @@ -329,6 +305,12 @@ class DraftOrderService extends BaseService { } } + for (const method of shipping_methods) { + await this.cartService_ + .withTransaction(manager) + .addShippingMethod(createdCart.id, method.option_id, method.data) + } + return result }) }