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