fix(medusa): Cleanup Tax lines in case of a failed cart completion (#2212)

This commit is contained in:
Philip Korsholm
2022-09-15 17:14:10 +02:00
committed by GitHub
parent 5d75e16b1f
commit 6f4b221971
6 changed files with 170 additions and 6 deletions
+24 -3
View File
@@ -919,7 +919,7 @@ class CartService extends TransactionBaseService {
cart.discounts.length = 0
await Promise.all(
data.discounts.map(({ code }) => {
data.discounts.map(async ({ code }) => {
return this.applyDiscount(cart, code)
})
)
@@ -948,7 +948,7 @@ class CartService extends TransactionBaseService {
cart.gift_cards = []
await Promise.all(
(data.gift_cards ?? []).map(({ code }) => {
(data.gift_cards ?? []).map(async ({ code }) => {
return this.applyGiftCard_(cart, code)
})
)
@@ -1021,7 +1021,7 @@ class CartService extends TransactionBaseService {
if (itemsToRemove.length) {
const results = await Promise.all(
itemsToRemove.map((item) => {
itemsToRemove.map(async (item) => {
return this.removeLineItem(cart.id, item.id)
})
)
@@ -2092,9 +2092,11 @@ class CartService extends TransactionBaseService {
}
const updatedCart = await cartRepo.save(cart)
this.eventBus_
.withTransaction(transactionManager)
.emit(CartService.Events.UPDATED, updatedCart)
return updatedCart
}
)
@@ -2131,6 +2133,25 @@ class CartService extends TransactionBaseService {
)
}
async deleteTaxLines(id: string): Promise<void> {
return await this.atomicPhase_(
async (transactionManager: EntityManager) => {
const cart = await this.retrieve(id, {
relations: [
"items",
"items.tax_lines",
"shipping_methods",
"shipping_methods.tax_lines",
],
})
await transactionManager.remove(cart.items.flatMap((i) => i.tax_lines))
await transactionManager.remove(
cart.shipping_methods.flatMap((s) => s.tax_lines)
)
}
)
}
protected async refreshAdjustments_(cart: Cart): Promise<void> {
const transactionManager = this.transactionManager_ ?? this.manager_
@@ -20,7 +20,8 @@ class IdempotencyKeyService extends TransactionBaseService {
protected readonly idempotencyKeyRepository_: typeof IdempotencyKeyRepository
constructor({ manager, idempotencyKeyRepository }: InjectedDependencies) {
super({ manager, idempotencyKeyRepository })
// eslint-disable-next-line prefer-rest-params
super(arguments[0])
this.manager_ = manager
this.idempotencyKeyRepository_ = idempotencyKeyRepository
@@ -181,6 +181,7 @@ describe("CartCompletionStrategy", () => {
return this
},
createTaxLines: jest.fn(() => Promise.resolve(cart)),
deleteTaxLines: jest.fn(() => Promise.resolve(cart)),
authorizePayment: jest.fn(() => Promise.resolve(cart)),
retrieve: jest.fn(() => Promise.resolve(cart)),
}
@@ -205,7 +206,7 @@ describe("CartCompletionStrategy", () => {
idempotencyKeyService: idempotencyKeyServiceMock,
orderService: orderServiceMock,
swapService: swapServiceMock,
manager: MockManager
manager: MockManager,
})
const val = await completionStrat.complete(cart.id, idempotencyKey, {})
@@ -120,6 +120,10 @@ class CartCompletionStrategy extends AbstractCartCompletionStrategy {
cart.payment_session.status === "requires_more" ||
cart.payment_session.status === "pending"
) {
await cartService
.withTransaction(transactionManager)
.deleteTaxLines(id)
return {
response_code: 200,
response_body: {
@@ -322,6 +326,13 @@ class CartCompletionStrategy extends AbstractCartCompletionStrategy {
}
if (err) {
if (idempotencyKey.recovery_point !== "started") {
await this.manager_.transaction(async (transactionManager) => {
await cartService
.withTransaction(transactionManager)
.deleteTaxLines(id)
})
}
throw err
}