fix(medusa): Creating tax lines in cart completion strategy (#2761)
This commit is contained in:
committed by
GitHub
parent
424efff919
commit
e63777e3c5
5
.changeset/happy-houses-walk.md
Normal file
5
.changeset/happy-houses-walk.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix: cart completion strategy tax lines management
|
||||
@@ -7,25 +7,22 @@ const IdempotencyKeyServiceMock = {
|
||||
return this
|
||||
},
|
||||
workStage: jest.fn().mockImplementation(async (key, fn) => {
|
||||
try {
|
||||
const { recovery_point, response_code, response_body } = await fn(
|
||||
MockManager
|
||||
)
|
||||
const { recovery_point, response_code, response_body } = await fn(
|
||||
MockManager
|
||||
)
|
||||
|
||||
if (recovery_point) {
|
||||
return {
|
||||
idempotency_key: key,
|
||||
recovery_point,
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
recovery_point: "finished",
|
||||
response_body,
|
||||
response_code,
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
return { error: err }
|
||||
const data = {
|
||||
recovery_point: recovery_point ?? "finished",
|
||||
}
|
||||
|
||||
if (!recovery_point) {
|
||||
data.response_body = response_body
|
||||
data.response_code = response_code
|
||||
}
|
||||
|
||||
return {
|
||||
...data,
|
||||
idempotency_key: key,
|
||||
}
|
||||
}),
|
||||
update: jest.fn().mockImplementation((key, data) => {
|
||||
@@ -39,7 +36,7 @@ const toTest = [
|
||||
{
|
||||
cart: {
|
||||
id: "test-cart",
|
||||
items: [],
|
||||
items: [{ id: "item", tax_lines: [] }],
|
||||
payment: { data: "some-data" },
|
||||
payment_session: { status: "authorized" },
|
||||
total: 1000,
|
||||
@@ -57,7 +54,7 @@ const toTest = [
|
||||
type: "order",
|
||||
})
|
||||
|
||||
expect(cartServiceMock.createTaxLines).toHaveBeenCalledTimes(1)
|
||||
expect(cartServiceMock.createTaxLines).toHaveBeenCalledTimes(3)
|
||||
expect(cartServiceMock.createTaxLines).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: "test-cart" })
|
||||
)
|
||||
@@ -94,7 +91,7 @@ const toTest = [
|
||||
{
|
||||
cart: {
|
||||
id: "test-cart",
|
||||
items: [],
|
||||
items: [{ id: "item", tax_lines: [] }],
|
||||
completed_at: "2021-01-02",
|
||||
payment: { data: "some-data" },
|
||||
payment_session: { status: "authorized" },
|
||||
@@ -121,7 +118,7 @@ const toTest = [
|
||||
{
|
||||
cart: {
|
||||
id: "test-cart",
|
||||
items: [],
|
||||
items: [{ id: "item", tax_lines: [] }],
|
||||
payment: { data: "some-data" },
|
||||
payment_session: { status: "requires_more" },
|
||||
total: 1000,
|
||||
@@ -148,7 +145,7 @@ const toTest = [
|
||||
cart: {
|
||||
id: "test-cart",
|
||||
type: "swap",
|
||||
items: [],
|
||||
items: [{ id: "item", tax_lines: [] }],
|
||||
payment: { data: "some-data" },
|
||||
payment_session: { status: "authorized" },
|
||||
total: 1000,
|
||||
@@ -185,7 +182,12 @@ describe("CartCompletionStrategy", () => {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
createTaxLines: jest.fn(() => Promise.resolve(cart)),
|
||||
createTaxLines: jest.fn(() => {
|
||||
cart.items[0].tax_lines = [{
|
||||
id: "tax_lines"
|
||||
}]
|
||||
return Promise.resolve(cart)
|
||||
}),
|
||||
deleteTaxLines: jest.fn(() => Promise.resolve(cart)),
|
||||
authorizePayment: jest.fn(() => Promise.resolve(cart)),
|
||||
retrieve: jest.fn(() => Promise.resolve(cart)),
|
||||
|
||||
@@ -66,7 +66,8 @@ class CartCompletionStrategy extends AbstractCartCompletionStrategy {
|
||||
.withTransaction(transactionManager)
|
||||
.workStage(
|
||||
idempotencyKey.idempotency_key,
|
||||
async (manager) => await this.handleStarted(id, { manager })
|
||||
async (manager) =>
|
||||
await this.handleCreateTaxLines(id, { manager })
|
||||
)
|
||||
})
|
||||
.catch((e) => {
|
||||
@@ -156,7 +157,7 @@ class CartCompletionStrategy extends AbstractCartCompletionStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
protected async handleStarted(
|
||||
protected async handleCreateTaxLines(
|
||||
id: string,
|
||||
{ manager }: { manager: EntityManager }
|
||||
) {
|
||||
@@ -198,6 +199,11 @@ class CartCompletionStrategy extends AbstractCartCompletionStrategy {
|
||||
idempotencyKey: IdempotencyKey,
|
||||
{ context, manager }: { context: any; manager: EntityManager }
|
||||
) {
|
||||
const res = await this.handleCreateTaxLines(id, { manager })
|
||||
if (res.response_code) {
|
||||
return res
|
||||
}
|
||||
|
||||
const cart = await this.cartService_
|
||||
.withTransaction(manager)
|
||||
.authorizePayment(id, {
|
||||
@@ -233,6 +239,11 @@ class CartCompletionStrategy extends AbstractCartCompletionStrategy {
|
||||
id: string,
|
||||
{ manager }: { manager: EntityManager }
|
||||
) {
|
||||
const res = await this.handleCreateTaxLines(id, { manager })
|
||||
if (res.response_code) {
|
||||
return res
|
||||
}
|
||||
|
||||
const orderServiceTx = this.orderService_.withTransaction(manager)
|
||||
|
||||
const cart = await this.cartService_
|
||||
|
||||
Reference in New Issue
Block a user