chore(cart, core-flows): Improve tax lines algo management (#11715)
Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
1 parent
4b3869ef2c
commit
b7678983a9
12 files changed
+1051
-144
No files matched your search
@@ -2272,6 +2272,343 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
})
|
||||
})
|
||||
|
||||
describe("setShippingMethodTaxLines", () => {
|
||||
it("should set shipping item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [itemOne] = await service.addShippingMethods(createdCart.id, [
|
||||
{
|
||||
name: "test",
|
||||
amount: 100,
|
||||
},
|
||||
])
|
||||
|
||||
const [itemTwo] = await service.addShippingMethods(createdCart.id, [
|
||||
{
|
||||
name: "test-2",
|
||||
amount: 200,
|
||||
},
|
||||
])
|
||||
|
||||
const taxLines = await service.setShippingMethodTaxLines(
|
||||
createdCart.id,
|
||||
[
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
},
|
||||
{
|
||||
shipping_method_id: itemTwo.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
expect(taxLines).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemTwo.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("should replace shipping item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [itemOne] = await service.addShippingMethods(createdCart.id, [
|
||||
{
|
||||
name: "test",
|
||||
amount: 100,
|
||||
},
|
||||
])
|
||||
|
||||
const taxLines = await service.setShippingMethodTaxLines(
|
||||
createdCart.id,
|
||||
[
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20.753,
|
||||
code: "TX",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
expect(taxLines).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20.753,
|
||||
code: "TX",
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
await service.setShippingMethodTaxLines(createdCart.id, [
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25.14789,
|
||||
code: "TX-2",
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.tax_lines"],
|
||||
})
|
||||
|
||||
expect(cart.shipping_methods).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: itemOne.id,
|
||||
tax_lines: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25.14789,
|
||||
code: "TX-2",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
expect(cart.shipping_methods?.length).toBe(1)
|
||||
expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(1)
|
||||
})
|
||||
|
||||
it("should remove all shipping item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [itemOne] = await service.addShippingMethods(createdCart.id, [
|
||||
{
|
||||
name: "test",
|
||||
amount: 100,
|
||||
},
|
||||
])
|
||||
|
||||
const taxLines = await service.setShippingMethodTaxLines(
|
||||
createdCart.id,
|
||||
[
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
expect(taxLines).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
await service.setShippingMethodTaxLines(createdCart.id, [])
|
||||
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.tax_lines"],
|
||||
})
|
||||
|
||||
expect(cart.shipping_methods).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: itemOne.id,
|
||||
tax_lines: [],
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
expect(cart.shipping_methods?.length).toBe(1)
|
||||
expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(0)
|
||||
})
|
||||
|
||||
it("should update shipping item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [itemOne] = await service.addShippingMethods(createdCart.id, [
|
||||
{
|
||||
name: "test",
|
||||
amount: 100,
|
||||
},
|
||||
])
|
||||
|
||||
const taxLines = await service.setShippingMethodTaxLines(
|
||||
createdCart.id,
|
||||
[
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
expect(taxLines).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
await service.setShippingMethodTaxLines(createdCart.id, [
|
||||
{
|
||||
id: taxLines[0].id,
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25,
|
||||
code: "TX",
|
||||
},
|
||||
])
|
||||
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.tax_lines"],
|
||||
})
|
||||
|
||||
expect(cart.shipping_methods).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: itemOne.id,
|
||||
tax_lines: [
|
||||
expect.objectContaining({
|
||||
id: taxLines[0].id,
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25,
|
||||
code: "TX",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
expect(cart.shipping_methods?.length).toBe(1)
|
||||
expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(1)
|
||||
})
|
||||
|
||||
it("should remove, update, and create shipping item tax lines for a cart", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [itemOne] = await service.addShippingMethods(createdCart.id, [
|
||||
{
|
||||
name: "test",
|
||||
amount: 100,
|
||||
},
|
||||
])
|
||||
|
||||
const taxLines = await service.setShippingMethodTaxLines(
|
||||
createdCart.id,
|
||||
[
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
},
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25,
|
||||
code: "TX",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
expect(taxLines).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 20,
|
||||
code: "TX",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25,
|
||||
code: "TX",
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
const taxLine = taxLines.find(
|
||||
(tx) => tx.shipping_method_id === itemOne.id
|
||||
)
|
||||
|
||||
await service.setShippingMethodTaxLines(createdCart.id, [
|
||||
// update
|
||||
{
|
||||
id: taxLine.id,
|
||||
rate: 40,
|
||||
code: "TX",
|
||||
},
|
||||
// create
|
||||
{
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25,
|
||||
code: "TX-2",
|
||||
},
|
||||
// remove: should remove the initial tax line for itemOne
|
||||
])
|
||||
|
||||
const cart = await service.retrieveCart(createdCart.id, {
|
||||
relations: ["shipping_methods.tax_lines"],
|
||||
})
|
||||
|
||||
expect(cart.shipping_methods).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: itemOne.id,
|
||||
tax_lines: [
|
||||
expect.objectContaining({
|
||||
id: taxLine!.id,
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 40,
|
||||
code: "TX",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
shipping_method_id: itemOne.id,
|
||||
rate: 25,
|
||||
code: "TX-2",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
expect(cart.shipping_methods?.length).toBe(1)
|
||||
expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addLineItemAdjustments", () => {
|
||||
it("should add line item tax lines for items in a cart", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
|
||||
Reference in new issue
Block a user