fix(medusa): Update line item adjustments upon cart discount removal (#2751)

**What**

When a discount is deleted from a cart, the line item adjustments must be refreshed. Now, the cart service `removeDiscount` includes updating the adjustments.

**Tests**

**Add a new integration test which:**
- create a cart with a discount 
- add a line item
- validate the adjustments
- remove the discount
- check that the adjustments are not present anymore

**Update unit tests**
The actual tests cases now check that the adjustments repository is called when needed

FIXES CORE-890
This commit is contained in:
Adrien de Peretti
2022-12-12 09:53:40 +00:00
committed by GitHub
parent 72f70bc789
commit d68e81fb3d
4 changed files with 185 additions and 18 deletions
@@ -2281,6 +2281,10 @@ describe("CartService", () => {
await cartService.update(IdMap.getId("with-d"), {
discounts: [],
})
expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledTimes(1)
expect(LineItemAdjustmentServiceMock.createAdjustments).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledWith(
"cart.updated",
@@ -2329,6 +2333,7 @@ describe("CartService", () => {
},
},
],
items: [],
region_id: IdMap.getId("good"),
})
},
@@ -2339,6 +2344,7 @@ describe("CartService", () => {
totalsService,
cartRepository,
eventBusService,
lineItemAdjustmentService: LineItemAdjustmentServiceMock,
taxProviderService: taxProviderServiceMock,
newTotalsService: newTotalsServiceMock,
featureFlagRouter: new FlagRouter({}),
@@ -2351,6 +2357,9 @@ describe("CartService", () => {
it("successfully removes discount", async () => {
await cartService.removeDiscount(IdMap.getId("fr-cart"), "1234")
expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledTimes(1)
expect(LineItemAdjustmentServiceMock.createAdjustments).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledWith(
"cart.updated",
@@ -2361,6 +2370,7 @@ describe("CartService", () => {
expect(cartRepository.save).toHaveBeenCalledWith({
id: IdMap.getId("cart"),
region_id: IdMap.getId("good"),
items: [],
discounts: [
{
code: "FS1234",
+9 -2
View File
@@ -1053,7 +1053,7 @@ class CartService extends TransactionBaseService {
}
}
if (isDefined(data.discounts)) {
if (isDefined(data.discounts) && data.discounts.length) {
const previousDiscounts = [...cart.discounts]
cart.discounts.length = 0
@@ -1081,6 +1081,9 @@ class CartService extends TransactionBaseService {
if (hasFreeShipping) {
await this.adjustFreeShipping_(cart, true)
}
} else if (isDefined(data.discounts) && !data.discounts.length) {
cart.discounts.length = 0
await this.refreshAdjustments_(cart)
}
if ("gift_cards" in data) {
@@ -1411,6 +1414,8 @@ class CartService extends TransactionBaseService {
async (transactionManager: EntityManager) => {
const cart = await this.retrieve(cartId, {
relations: [
"items",
"region",
"discounts",
"discounts.rule",
"payment_sessions",
@@ -1435,7 +1440,9 @@ class CartService extends TransactionBaseService {
)
const updatedCart = await cartRepo.save(cart)
if (updatedCart.payment_sessions?.length) {
await this.refreshAdjustments_(updatedCart)
if (cart.payment_sessions?.length) {
await this.setPaymentSessions(cartId)
}