From c442be47d4a0b4cf3b46ce604b05e16424544046 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 5 May 2022 15:07:00 +0200 Subject: [PATCH 1/7] fix(medusa): CartService lost shipping address when using the id --- packages/medusa/src/services/cart.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index 6e5d0c4284..222283509b 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -412,7 +412,7 @@ class CartService extends BaseService { } if (!data.shipping_address) { - if (region.countries.length === 1) { + if (!rawCart.shipping_address && region.countries.length === 1) { // Preselect the country if the region only has 1 // and create address entity rawCart.shipping_address = addressRepo.create({ From 32a65d27fca19b17a4650fc31536f072a0a0f49c Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 5 May 2022 15:28:33 +0200 Subject: [PATCH 2/7] test(medusa): Add a test to check that the shipping_address is not erase when an id is have been given --- .../medusa/src/services/__tests__/cart.js | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 571b7c26a7..20431e5f4f 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -204,7 +204,21 @@ describe("CartService", () => { }, } - const addressRepository = MockRepository({ create: (c) => c }) + const addressRepository = MockRepository({ + create: (c) => c, + findOne: (id) => { + return { + id, + first_name: "LeBron", + last_name: "James", + address_1: "Dunk St", + city: "Dunkville", + province: "CA", + postal_code: "12345", + country_code: "us", + } + } + }) const cartRepository = MockRepository() const customerService = { retrieveByEmail: jest.fn().mockReturnValue( @@ -262,6 +276,39 @@ describe("CartService", () => { expect(cartRepository.save).toHaveBeenCalledTimes(1) }) + it("successfully creates a cart with a shipping address id", async () => { + await cartService.create({ + region_id: IdMap.getId("testRegion"), + email: "email@test.com", + shipping_address_id: "test_shipping_address", + }) + + expect(eventBusService.emit).toHaveBeenCalledTimes(1) + expect(eventBusService.emit).toHaveBeenCalledWith( + "cart.created", + expect.any(Object) + ) + + expect(cartRepository.create).toHaveBeenCalledTimes(1) + expect(cartRepository.create).toHaveBeenCalledWith(expect.objectContaining({ + region_id: IdMap.getId("testRegion"), + shipping_address: { + id: "test_shipping_address", + first_name: "LeBron", + last_name: "James", + address_1: "Dunk St", + city: "Dunkville", + province: "CA", + postal_code: "12345", + country_code: "us", + }, + customer_id: IdMap.getId("customer"), + email: "email@test.com" + })) + + expect(cartRepository.save).toHaveBeenCalledTimes(1) + }) + it("creates a cart with a prefilled shipping address", async () => { const res = cartService.create({ region_id: IdMap.getId("testRegion"), From f5edaf51ea2d49d662b00f7a5360449cf827d825 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Fri, 6 May 2022 10:31:02 +0200 Subject: [PATCH 3/7] fix(medusa): Proper fix of the cart service --- .../medusa/src/services/__tests__/cart.js | 4 +- packages/medusa/src/services/cart.ts | 49 ++++++++----------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 20431e5f4f..0dd999c58f 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -276,7 +276,7 @@ describe("CartService", () => { expect(cartRepository.save).toHaveBeenCalledTimes(1) }) - it("successfully creates a cart with a shipping address id", async () => { + it("successfully creates a cart with a shipping address id but no shippings_address", async () => { await cartService.create({ region_id: IdMap.getId("testRegion"), email: "email@test.com", @@ -309,7 +309,7 @@ describe("CartService", () => { expect(cartRepository.save).toHaveBeenCalledTimes(1) }) - it("creates a cart with a prefilled shipping address", async () => { + it("creates a cart with a prefilled shipping address but a country not part of the region", async () => { const res = cartService.create({ region_id: IdMap.getId("testRegion"), shipping_address: { diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index 222283509b..c2cf84154f 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -393,41 +393,32 @@ class CartService extends BaseService { rawCart.region_id = region.id - if (data.shipping_address_id !== undefined) { - const shippingAddress = data.shipping_address_id - ? await addressRepo.findOne(data.shipping_address_id) - : null - - if ( - shippingAddress && - !regCountries.includes(shippingAddress.country_code) - ) { - throw new MedusaError( - MedusaError.Types.NOT_ALLOWED, - "Shipping country not in region" - ) - } - - rawCart.shipping_address = shippingAddress - } - - if (!data.shipping_address) { - if (!rawCart.shipping_address && region.countries.length === 1) { - // Preselect the country if the region only has 1 - // and create address entity + if (!data.shipping_address && !data.shipping_address_id) { + if (region.countries.length === 1) { rawCart.shipping_address = addressRepo.create({ country_code: regCountries[0], }) } } else { - if (!regCountries.includes(data.shipping_address.country_code)) { - throw new MedusaError( - MedusaError.Types.NOT_ALLOWED, - "Shipping country not in region" - ) + if (data.shipping_address) { + if (!regCountries.includes(data.shipping_address.country_code)) { + throw new MedusaError( + MedusaError.Types.NOT_ALLOWED, + "Shipping country not in region" + ) + } + rawCart.shipping_address = data.shipping_address + } + if (data.shipping_address_id) { + const addr = await addressRepo.findOne(data.shipping_address_id) + if (addr && !regCountries.includes(addr.country_code)) { + throw new MedusaError( + MedusaError.Types.NOT_ALLOWED, + "Shipping country not in region" + ) + } + rawCart.shipping_address = addr } - - rawCart.shipping_address = data.shipping_address } const remainingFields: (keyof Cart)[] = [ From 9b5354502f69ca438ca16c4fbe92b67493842450 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Tue, 10 May 2022 11:09:52 +0200 Subject: [PATCH 4/7] test(medusa): Fix test pipelines --- .../store/__snapshots__/swaps.js.snap | 4 +-- .../api/__tests__/store/swaps.js | 15 ++------ .../medusa/src/services/__tests__/cart.js | 35 ++----------------- packages/medusa/src/services/cart.ts | 3 +- 4 files changed, 8 insertions(+), 49 deletions(-) diff --git a/integration-tests/api/__tests__/store/__snapshots__/swaps.js.snap b/integration-tests/api/__tests__/store/__snapshots__/swaps.js.snap index 5605ac2735..654c95ac5e 100644 --- a/integration-tests/api/__tests__/store/__snapshots__/swaps.js.snap +++ b/integration-tests/api/__tests__/store/__snapshots__/swaps.js.snap @@ -98,7 +98,7 @@ Object { "payment_authorized_at": null, "payment_id": null, "region_id": "test-region", - "shipping_address_id": StringMatching /\\^addr_\\*/, + "shipping_address_id": "test-shipping-address", "type": "swap", "updated_at": Any, }, @@ -274,7 +274,7 @@ Object { "payment_authorized_at": null, "payment_id": null, "region_id": "test-region", - "shipping_address_id": StringMatching /\\^addr_\\*/, + "shipping_address_id": "test-shipping-address", "type": "swap", "updated_at": Any, }, diff --git a/integration-tests/api/__tests__/store/swaps.js b/integration-tests/api/__tests__/store/swaps.js index 243d5e1e96..1065aef2c6 100644 --- a/integration-tests/api/__tests__/store/swaps.js +++ b/integration-tests/api/__tests__/store/swaps.js @@ -1,18 +1,7 @@ const path = require("path") const { - Region, - Order, - Customer, ShippingProfile, - Product, - ProductVariant, - MoneyAmount, - LineItem, - Payment, - Cart, - ShippingMethod, ShippingOption, - Swap, } = require("@medusajs/medusa") const setupServer = require("../../../helpers/setup-server") @@ -137,7 +126,7 @@ describe("/store/carts", () => { type: "swap", created_at: expect.any(String), updated_at: expect.any(String), - shipping_address_id: expect.stringMatching(/^addr_*/), + shipping_address_id: "test-shipping-address", metadata: { swap_id: expect.stringMatching(/^swap_*/), }, @@ -221,7 +210,7 @@ describe("/store/carts", () => { cart: { id: expect.stringMatching(/^cart_*/), billing_address_id: "test-billing-address", - shipping_address_id: expect.stringMatching(/^addr_*/), + shipping_address_id: "test-shipping-address", type: "swap", created_at: expect.any(String), updated_at: expect.any(String), diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 0dd999c58f..92c4455c64 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -1,9 +1,11 @@ import _ from "lodash" +import { getManager } from "typeorm" import { MedusaError } from "medusa-core-utils" import { IdMap, MockManager, MockRepository } from "medusa-test-utils" import CartService from "../cart" import { InventoryServiceMock } from "../__mocks__/inventory" import { LineItemAdjustmentServiceMock } from "../__mocks__/line-item-adjustment" +import { CartRepository } from "../../repositories/cart" const eventBusService = { emit: jest.fn(), @@ -276,39 +278,6 @@ describe("CartService", () => { expect(cartRepository.save).toHaveBeenCalledTimes(1) }) - it("successfully creates a cart with a shipping address id but no shippings_address", async () => { - await cartService.create({ - region_id: IdMap.getId("testRegion"), - email: "email@test.com", - shipping_address_id: "test_shipping_address", - }) - - expect(eventBusService.emit).toHaveBeenCalledTimes(1) - expect(eventBusService.emit).toHaveBeenCalledWith( - "cart.created", - expect.any(Object) - ) - - expect(cartRepository.create).toHaveBeenCalledTimes(1) - expect(cartRepository.create).toHaveBeenCalledWith(expect.objectContaining({ - region_id: IdMap.getId("testRegion"), - shipping_address: { - id: "test_shipping_address", - first_name: "LeBron", - last_name: "James", - address_1: "Dunk St", - city: "Dunkville", - province: "CA", - postal_code: "12345", - country_code: "us", - }, - customer_id: IdMap.getId("customer"), - email: "email@test.com" - })) - - expect(cartRepository.save).toHaveBeenCalledTimes(1) - }) - it("creates a cart with a prefilled shipping address but a country not part of the region", async () => { const res = cartService.create({ region_id: IdMap.getId("testRegion"), diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index c2cf84154f..ae1755f6cb 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -37,6 +37,7 @@ import InventoryService from "./inventory" import CustomShippingOptionService from "./custom-shipping-option" import LineItemAdjustmentService from "./line-item-adjustment" import { LineItemRepository } from "../repositories/line-item" +import { raw } from "express" type InjectedDependencies = { manager: EntityManager @@ -417,7 +418,7 @@ class CartService extends BaseService { "Shipping country not in region" ) } - rawCart.shipping_address = addr + rawCart.shipping_address_id = data.shipping_address_id } } From d9bc6b4c4117a994ad1f5300076daabfdb26c706 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Tue, 10 May 2022 14:13:01 +0200 Subject: [PATCH 5/7] style(medusa): Remove unused dependencies --- packages/medusa/src/services/__tests__/cart.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 92c4455c64..07bab79711 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -1,11 +1,9 @@ import _ from "lodash" -import { getManager } from "typeorm" import { MedusaError } from "medusa-core-utils" import { IdMap, MockManager, MockRepository } from "medusa-test-utils" import CartService from "../cart" import { InventoryServiceMock } from "../__mocks__/inventory" import { LineItemAdjustmentServiceMock } from "../__mocks__/line-item-adjustment" -import { CartRepository } from "../../repositories/cart" const eventBusService = { emit: jest.fn(), From 1c3c62e9668fa955e39b2571ac4c7ed919898c6e Mon Sep 17 00:00:00 2001 From: adrien2p Date: Tue, 10 May 2022 14:14:59 +0200 Subject: [PATCH 6/7] test(medusa): Fix integration --- .../medusa-plugin-sendgrid/__snapshots__/index.js.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap index b414a3f7a1..e80d777ab5 100644 --- a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap +++ b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap @@ -2389,9 +2389,9 @@ Object { "created_at": Any, "customer_id": null, "deleted_at": null, - "first_name": null, + "first_name": "Chyna", "id": Any, - "last_name": null, + "last_name": "Osinski", "metadata": null, "phone": "12353245", "postal_code": "1234", From 1c5647f69e41b622e71cac22255621fcae75b48c Mon Sep 17 00:00:00 2001 From: adrien2p Date: Tue, 10 May 2022 16:28:10 +0200 Subject: [PATCH 7/7] style(medusa): Cleanup some tests case naming --- packages/medusa/src/services/__tests__/cart.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 07bab79711..0fefc85592 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -276,7 +276,7 @@ describe("CartService", () => { expect(cartRepository.save).toHaveBeenCalledTimes(1) }) - it("creates a cart with a prefilled shipping address but a country not part of the region", async () => { + it("should throw shipping country not in region", async () => { const res = cartService.create({ region_id: IdMap.getId("testRegion"), shipping_address: { @@ -293,7 +293,7 @@ describe("CartService", () => { await expect(res).rejects.toThrow("Shipping country not in region") }) - it("creates a cart with a prefilled shipping address", async () => { + it("a cart with a prefilled shipping address", async () => { await cartService.create({ region_id: IdMap.getId("testRegion"), shipping_address: {