fix(medusa): Refresh adjustments when region on cart is changed (#1827)

* fix(medusa): Refresh adjustments when region on cart is changed

* fix test

* Fix unit test

* fix: integration tests

* fix: comment
This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2022-07-11 10:51:55 +02:00
1 parent dffb86bb58
commit 02967f95b1
6 files changed
+7992 -10539

No files matched your search

+46 -35
View File
@@ -7,7 +7,7 @@ import { LineItemAdjustmentServiceMock } from "../__mocks__/line-item-adjustment
const eventBusService = {
emit: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -193,7 +193,7 @@ describe("CartService", () => {
describe("create", () => {
const regionService = {
withTransaction: function() {
withTransaction: function () {
return this
},
retrieve: () => {
@@ -217,7 +217,7 @@ describe("CartService", () => {
postal_code: "12345",
country_code: "us",
}
}
},
})
const cartRepository = MockRepository()
const customerService = {
@@ -227,7 +227,7 @@ describe("CartService", () => {
email: "email@test.com",
})
),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -335,14 +335,14 @@ describe("CartService", () => {
const lineItemService = {
update: jest.fn(),
create: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
const shippingOptionService = {
deleteShippingMethods: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -558,7 +558,7 @@ describe("CartService", () => {
const lineItemService = {
delete: jest.fn(),
update: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -600,7 +600,7 @@ describe("CartService", () => {
const shippingOptionService = {
deleteShippingMethods: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -663,12 +663,14 @@ describe("CartService", () => {
expect(shippingOptionService.deleteShippingMethods).toHaveBeenCalledTimes(
1
)
expect(shippingOptionService.deleteShippingMethods).toHaveBeenCalledWith([{
id: IdMap.getId("ship-method"),
shipping_option: {
profile_id: IdMap.getId("prevPro"),
expect(shippingOptionService.deleteShippingMethods).toHaveBeenCalledWith([
{
id: IdMap.getId("ship-method"),
shipping_option: {
profile_id: IdMap.getId("prevPro"),
},
},
}])
])
expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledTimes(1)
expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledWith({
@@ -760,7 +762,7 @@ describe("CartService", () => {
quantity: 1,
})
),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -872,7 +874,7 @@ describe("CartService", () => {
email: data.email,
})
),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -1094,7 +1096,7 @@ describe("CartService", () => {
const lineItemService = {
update: jest.fn((r) => r),
delete: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -1114,7 +1116,7 @@ describe("CartService", () => {
countries: [{ iso_2: "us" }],
})
),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -1147,13 +1149,13 @@ describe("CartService", () => {
deleteSession: jest.fn(),
updateSession: jest.fn(),
createSession: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
const priceSelectionStrat = {
withTransaction: function() {
withTransaction: function () {
return this
},
calculateVariantPrice: async (variantId, context) => {
@@ -1175,6 +1177,7 @@ describe("CartService", () => {
cartRepository,
regionService,
lineItemService,
lineItemAdjustmentService: LineItemAdjustmentServiceMock,
productVariantService,
eventBusService,
paymentSessionRepository: MockRepository(),
@@ -1369,7 +1372,7 @@ describe("CartService", () => {
deleteSession: jest.fn(),
updateSession: jest.fn(),
createSession: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -1527,7 +1530,7 @@ describe("CartService", () => {
const lineItemService = {
update: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
@@ -1540,13 +1543,13 @@ describe("CartService", () => {
})
}),
deleteShippingMethods: jest.fn(),
withTransaction: function() {
withTransaction: function () {
return this
},
}
const customShippingOptionService = {
withTransaction: function() {
withTransaction: function () {
return this
},
list: jest.fn().mockImplementation(({ cart_id }) => {
@@ -1587,9 +1590,11 @@ describe("CartService", () => {
IdMap.getId("option"),
data
)
expect(
shippingOptionService.createShippingMethod
).toHaveBeenCalledWith(IdMap.getId("option"), data, { cart: cart1 })
expect(shippingOptionService.createShippingMethod).toHaveBeenCalledWith(
IdMap.getId("option"),
data,
{ cart: cart1 }
)
})
it("successfully overrides existing profile shipping method", async () => {
@@ -1601,9 +1606,11 @@ describe("CartService", () => {
IdMap.getId("profile1"),
data
)
expect(
shippingOptionService.createShippingMethod
).toHaveBeenCalledWith(IdMap.getId("profile1"), data, { cart: cart2 })
expect(shippingOptionService.createShippingMethod).toHaveBeenCalledWith(
IdMap.getId("profile1"),
data,
{ cart: cart2 }
)
expect(shippingOptionService.deleteShippingMethods).toHaveBeenCalledWith({
id: IdMap.getId("ship1"),
shipping_option: {
@@ -1629,9 +1636,11 @@ describe("CartService", () => {
expect(shippingOptionService.createShippingMethod).toHaveBeenCalledTimes(
1
)
expect(
shippingOptionService.createShippingMethod
).toHaveBeenCalledWith(IdMap.getId("additional"), data, { cart: cart2 })
expect(shippingOptionService.createShippingMethod).toHaveBeenCalledWith(
IdMap.getId("additional"),
data,
{ cart: cart2 }
)
})
it("updates item shipping", async () => {
@@ -1651,9 +1660,11 @@ describe("CartService", () => {
expect(shippingOptionService.createShippingMethod).toHaveBeenCalledTimes(
1
)
expect(
shippingOptionService.createShippingMethod
).toHaveBeenCalledWith(IdMap.getId("profile1"), data, { cart: cart3 })
expect(shippingOptionService.createShippingMethod).toHaveBeenCalledWith(
IdMap.getId("profile1"),
data,
{ cart: cart3 }
)
expect(lineItemService.update).toHaveBeenCalledTimes(1)
expect(lineItemService.update).toHaveBeenCalledWith(IdMap.getId("line"), {
+23 -23
View File
@@ -3,6 +3,7 @@ import { MedusaError, Validator } from "medusa-core-utils"
import { DeepPartial, EntityManager, In } from "typeorm"
import { TransactionBaseService } from "../interfaces"
import { IPriceSelectionStrategy } from "../interfaces/price-selection-strategy"
import { DiscountRuleType } from "../models"
import { Address } from "../models/address"
import { Cart } from "../models/cart"
import { CustomShippingOption } from "../models/custom-shipping-option"
@@ -38,7 +39,6 @@ import RegionService from "./region"
import ShippingOptionService from "./shipping-option"
import TaxProviderService from "./tax-provider"
import TotalsService from "./totals"
import { DiscountRuleType } from "../models"
type InjectedDependencies = {
manager: EntityManager
@@ -288,11 +288,8 @@ class CartService extends TransactionBaseService<CartService> {
)
const validatedId = validateId(cartId)
const {
select,
relations,
totalsToSelect,
} = this.transformQueryForTotals_(options)
const { select, relations, totalsToSelect } =
this.transformQueryForTotals_(options)
const query = buildQuery(
{ id: validatedId },
@@ -766,12 +763,6 @@ class CartService extends TransactionBaseService<CartService> {
}
}
if (typeof data.region_id !== "undefined") {
const countryCode =
(data.country_code || data.shipping_address?.country_code) ?? null
await this.setRegion_(cart, data.region_id, countryCode)
}
if (
typeof data.customer_id !== "undefined" ||
typeof data.region_id !== "undefined"
@@ -779,6 +770,12 @@ class CartService extends TransactionBaseService<CartService> {
await this.updateUnitPrices_(cart, data.region_id, data.customer_id)
}
if (typeof data.region_id !== "undefined") {
const countryCode =
(data.country_code || data.shipping_address?.country_code) ?? null
await this.setRegion_(cart, data.region_id, countryCode)
}
const addrRepo = transactionManager.getCustomRepository(
this.addressRepository_
)
@@ -898,9 +895,7 @@ class CartService extends TransactionBaseService<CartService> {
protected async createOrFetchUserFromEmail_(
email: string
): Promise<Customer> {
const schema = Validator.string()
.email()
.required()
const schema = Validator.string().email().required()
const { value, error } = schema.validate(email.toLowerCase())
if (error) {
throw new MedusaError(
@@ -1559,9 +1554,10 @@ class CartService extends TransactionBaseService<CartService> {
],
})
const cartCustomShippingOptions = await this.customShippingOptionService_
.withTransaction(transactionManager)
.list({ cart_id: cart.id })
const cartCustomShippingOptions =
await this.customShippingOptionService_
.withTransaction(transactionManager)
.list({ cart_id: cart.id })
const customShippingOption = this.findCustomShippingOption(
cartCustomShippingOptions,
@@ -1734,18 +1730,17 @@ class CartService extends TransactionBaseService<CartService> {
)
}
// Set the new region for the cart
const region = await this.regionService_
.withTransaction(this.transactionManager_)
.withTransaction(transactionManager)
.retrieve(regionId, {
relations: ["countries"],
})
const addrRepo = transactionManager.getCustomRepository(
this.addressRepository_
)
cart.region = region
cart.region_id = region.id
const addrRepo = transactionManager.getCustomRepository(
this.addressRepository_
)
/*
* When changing the region you are changing the set of countries that your
* cart can be shipped to so we need to make sure that the current shipping
@@ -1828,6 +1823,11 @@ class CartService extends TransactionBaseService<CartService> {
})
}
if (cart?.items?.length) {
// line item adjustments should be refreshed on region change after having filtered out inapplicable discounts
await this.refreshAdjustments_(cart)
}
cart.gift_cards = []
if (cart.payment_sessions && cart.payment_sessions.length) {