feat: new tax api (#979)
* feat: add tax calculation strategy (#885) * feat: add tax calculation strategy * fix: adds strategy loader * fix: eslint ignore * chore: cleanup * fix: allow plugin overwrites * fix: allow plugin overwrites * fix: fake region * Update packages/medusa/src/loaders/strategies.ts Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> * feat: adds tax related db entities + tax provider (#896) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: pr comments * fix: unit test * feat: totals service to ts (#911) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * feat: totals service to ts * fix: remove totals.js * fix: add shipping methods * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: tests * fix: tests * fix: unit test * fix: adds TotalsServiceProps * feat: adds integration tests for automatic tax calculation + shipping tax rates (#945) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * feat: totals service to ts * fix: remove totals.js * fix: add shipping methods * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: tests * fix: tests * fix: unit test * fix: integration test helpers * fix: adds factories + tests automatic tax rates * fix: remove verbose * fix: adds TotalsServiceProps * fix: add shipping tax lines * fix: add migration for shipping taxes * fix: integration tests for shipping taxes * fix: integration tests for shipping taxes * fix: jsdoc types * Feat/manual taxes (#950) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * feat: totals service to ts * fix: remove totals.js * fix: add shipping methods * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: tests * fix: tests * fix: unit test * fix: integration test helpers * fix: adds factories + tests automatic tax rates * fix: remove verbose * fix: adds TotalsServiceProps * fix: add shipping tax lines * fix: add migration for shipping taxes * fix: integration tests for shipping taxes * fix: integration tests for shipping taxes * fix: add integration tests for manual taxes * fix: cart service - cleanup jsdoc * feat: add /carts/id/taxes to manually calculate taxes * feat: add integration tests for order tax calculations * fix: unit tests * fix: merge * fix: rm verbose * fix: unit tests * fix: object -> cartOrOrder * fix: rounding * Feat/complete order w tax lines (#951) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * feat: totals service to ts * fix: remove totals.js * fix: add shipping methods * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: tests * fix: tests * fix: unit test * fix: integration test helpers * fix: adds factories + tests automatic tax rates * fix: remove verbose * fix: adds TotalsServiceProps * fix: add shipping tax lines * fix: add migration for shipping taxes * fix: integration tests for shipping taxes * fix: integration tests for shipping taxes * fix: add integration tests for manual taxes * fix: cart service - cleanup jsdoc * feat: add /carts/id/taxes to manually calculate taxes * feat: add integration tests for order tax calculations * feat: adds cart completion strategy + create order w. tax lines * fix: unit tests * fix: merge * fix: rm verbose * fix: unit tests * fix: unit tests * fix: unit tests * fix: ensure calculation for list orders * fix: unit tests * fix: integration tests * fix: adds cart order type gaurds * Docs/tax api (#954) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * feat: totals service to ts * fix: remove totals.js * fix: add shipping methods * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: tests * fix: tests * fix: unit test * fix: integration test helpers * fix: adds factories + tests automatic tax rates * fix: remove verbose * fix: adds TotalsServiceProps * fix: add shipping tax lines * fix: add migration for shipping taxes * fix: integration tests for shipping taxes * fix: integration tests for shipping taxes * fix: add integration tests for manual taxes * fix: cart service - cleanup jsdoc * feat: add /carts/id/taxes to manually calculate taxes * feat: add integration tests for order tax calculations * feat: adds cart completion strategy + create order w. tax lines * fix: unit tests * fix: merge * fix: rm verbose * fix: unit tests * fix: unit tests * fix: unit tests * fix: ensure calculation for list orders * fix: unit tests * fix: integration tests * docs: documents tax related methods and types * fix: require either item_id or shipping_method_id * feat: product type tax rate (#969) * feat: adds tax related db entities + tax provider * fix: add tax provider tests * fix: add tax service unit tests * fix: tests + migrations * feat: totals service to ts * fix: remove totals.js * fix: add shipping methods * fix: add inherited tax lines * chore: rm tax-line repo * fix: test * fix: tests * fix: tests * fix: unit test * fix: integration test helpers * fix: adds factories + tests automatic tax rates * fix: remove verbose * fix: adds TotalsServiceProps * fix: add shipping tax lines * fix: add migration for shipping taxes * fix: integration tests for shipping taxes * fix: integration tests for shipping taxes * fix: add integration tests for manual taxes * fix: cart service - cleanup jsdoc * feat: add /carts/id/taxes to manually calculate taxes * feat: add integration tests for order tax calculations * feat: adds cart completion strategy + create order w. tax lines * fix: unit tests * fix: merge * fix: rm verbose * fix: unit tests * fix: unit tests * fix: unit tests * fix: ensure calculation for list orders * fix: unit tests * fix: integration tests * docs: documents tax related methods and types * fix: require either item_id or shipping_method_id * feat: adds returns tests for new tax system * feat: adds return lines + integration tests for swaps * feat: return integration tests * feat: adds product type tax rates * feat: add tax management endpoints * fix: create single migration * fix: adds tax rates to js client * fix: strats * Fix/plugin tests (#998) * plugin testing setup * fix: test sendgrid plugin * fix: test sendgrid plugin * chore: clean * chore: clean * fix: clean up tests * fix: remove dirty import * fix: sendgrid + brightpearl * fix: plugin integration tests * fix: klarna * fix: shipping method tax * fix: remove taxrates * fix: unit tests * fix: integration * fix: integration * fix: plugins tests * fix: ignore plugins * fix: tests * fix: taxes (#1017) * fix: taxes * fix: taxes * fix: faulty ref * fix: create tax-lines with claim items * fix: snapshot tax-liens * fix: allows integration test teardown to force deleting tables * fix: tests * fix: merge * fix: adds tax-rates to client * fix: adds tax-rates to medusa-react * fix: tests * fix: tests * fix: add product types * fix: adds tax provider endpoint + cascaded deletes on tax rate relations * fix: move errors to service layer * fix: cleanup api * fix: unit tests * fix: error handler in base-service * fix: Add order region to swap on createFulfillment (#1110) Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
committed by
olivermrbl
parent
d80eaa172d
commit
47588e7a8d
@@ -75,8 +75,8 @@ describe("/admin/store", () => {
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
medusaProcess.kill()
|
||||
await db.teardown({ forceDelete: ["store"] })
|
||||
await medusaProcess.kill()
|
||||
})
|
||||
|
||||
it("fails to update default currency if not in store currencies", async () => {
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`Claims creates a refund claim 1`] = `
|
||||
Object {
|
||||
"additional_items": Array [],
|
||||
"canceled_at": null,
|
||||
"claim_items": ArrayContaining [
|
||||
ObjectContaining {
|
||||
"item": Any<Object>,
|
||||
"item_id": "test-item",
|
||||
"quantity": 1,
|
||||
},
|
||||
],
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"fulfillment_status": "not_fulfilled",
|
||||
"fulfillments": Array [],
|
||||
"id": StringMatching /\\^claim_\\*/,
|
||||
"idempotency_key": Any<String>,
|
||||
"metadata": null,
|
||||
"no_notification": null,
|
||||
"order_id": Any<String>,
|
||||
"payment_status": "refunded",
|
||||
"refund_amount": 1200,
|
||||
"return_order": null,
|
||||
"shipping_address": Any<Object>,
|
||||
"shipping_address_id": Any<String>,
|
||||
"shipping_methods": Array [],
|
||||
"type": "refund",
|
||||
"updated_at": Any<String>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`Claims creates a replace claim 1`] = `
|
||||
Object {
|
||||
"additional_items": Array [
|
||||
Object {
|
||||
"allow_discounts": true,
|
||||
"cart_id": null,
|
||||
"claim_order_id": StringMatching /\\^claim_\\*/,
|
||||
"created_at": Any<String>,
|
||||
"description": "Practical Granite Pizza",
|
||||
"fulfilled_quantity": null,
|
||||
"has_shipping": null,
|
||||
"id": StringMatching /\\^item_\\*/,
|
||||
"is_giftcard": false,
|
||||
"is_return": false,
|
||||
"metadata": Object {},
|
||||
"order_id": null,
|
||||
"quantity": 1,
|
||||
"refundable": 113,
|
||||
"returned_quantity": null,
|
||||
"shipped_quantity": null,
|
||||
"should_merge": true,
|
||||
"swap_id": null,
|
||||
"tax_lines": Array [
|
||||
Object {
|
||||
"code": "default",
|
||||
"created_at": Any<String>,
|
||||
"id": StringMatching /\\^litl_\\*/,
|
||||
"item_id": StringMatching /\\^item_\\*/,
|
||||
"metadata": null,
|
||||
"name": "default",
|
||||
"rate": 12.5,
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
],
|
||||
"thumbnail": null,
|
||||
"title": "Awesome Metal Ball",
|
||||
"unit_price": 100,
|
||||
"updated_at": Any<String>,
|
||||
"variant": Object {
|
||||
"allow_backorder": false,
|
||||
"barcode": null,
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"ean": null,
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-variant",
|
||||
"inventory_quantity": 9,
|
||||
"length": null,
|
||||
"manage_inventory": true,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"origin_country": null,
|
||||
"product": Object {
|
||||
"collection_id": null,
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"description": null,
|
||||
"discountable": true,
|
||||
"external_id": null,
|
||||
"handle": null,
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-product",
|
||||
"is_giftcard": false,
|
||||
"length": null,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"origin_country": null,
|
||||
"profile_id": StringMatching /\\^sp_\\*/,
|
||||
"status": "draft",
|
||||
"subtitle": null,
|
||||
"thumbnail": null,
|
||||
"title": "Awesome Metal Ball",
|
||||
"type_id": null,
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
"product_id": "test-product",
|
||||
"sku": null,
|
||||
"title": "Practical Granite Pizza",
|
||||
"upc": null,
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
"variant_id": "test-variant",
|
||||
},
|
||||
],
|
||||
"canceled_at": null,
|
||||
"claim_items": ArrayContaining [
|
||||
ObjectContaining {
|
||||
"item": Any<Object>,
|
||||
"item_id": "test-item",
|
||||
"quantity": 1,
|
||||
},
|
||||
],
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"fulfillment_status": "not_fulfilled",
|
||||
"fulfillments": Array [],
|
||||
"id": StringMatching /\\^claim_\\*/,
|
||||
"idempotency_key": Any<String>,
|
||||
"metadata": null,
|
||||
"no_notification": null,
|
||||
"order_id": Any<String>,
|
||||
"payment_status": "na",
|
||||
"refund_amount": null,
|
||||
"return_order": null,
|
||||
"shipping_address": Any<Object>,
|
||||
"shipping_address_id": Any<String>,
|
||||
"shipping_methods": Array [],
|
||||
"type": "replace",
|
||||
"updated_at": Any<String>,
|
||||
}
|
||||
`;
|
||||
272
integration-tests/api/__tests__/claims/index.js
Normal file
272
integration-tests/api/__tests__/claims/index.js
Normal file
@@ -0,0 +1,272 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
const {
|
||||
simpleOrderFactory,
|
||||
simpleShippingOptionFactory,
|
||||
simplePaymentFactory,
|
||||
simpleProductFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
describe("Claims", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("creates a refund claim", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/claims`,
|
||||
{
|
||||
type: "refund",
|
||||
claim_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
reason: "missing_item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.order.claims[0]).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^claim_*/),
|
||||
order_id: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
idempotency_key: expect.any(String),
|
||||
shipping_address_id: expect.any(String),
|
||||
refund_amount: 1200,
|
||||
shipping_address: expect.any(Object),
|
||||
claim_items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
item: expect.any(Object),
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
})
|
||||
|
||||
test("creates a replace claim", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/claims`,
|
||||
{
|
||||
type: "replace",
|
||||
additional_items: [{ variant_id: "test-variant", quantity: 1 }],
|
||||
claim_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
reason: "missing_item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.order.claims[0]).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^claim_*/),
|
||||
order_id: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
idempotency_key: expect.any(String),
|
||||
shipping_address_id: expect.any(String),
|
||||
refund_amount: null,
|
||||
shipping_address: expect.any(Object),
|
||||
additional_items: [
|
||||
{
|
||||
id: expect.stringMatching(/^item_*/),
|
||||
claim_order_id: expect.stringMatching(/^claim_*/),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
variant: {
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
product: {
|
||||
profile_id: expect.stringMatching(/^sp_*/),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
},
|
||||
},
|
||||
tax_lines: [
|
||||
{
|
||||
id: expect.stringMatching(/^litl_*/),
|
||||
item_id: expect.stringMatching(/^item_*/),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
rate: 12.5,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
claim_items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
item: expect.any(Object),
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
})
|
||||
|
||||
test("creates a replace claim fulfillment", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const option = await simpleShippingOptionFactory(dbConnection, {
|
||||
region_id: "test-region",
|
||||
})
|
||||
const api = useApi()
|
||||
|
||||
const createRes = await api.post(
|
||||
`/admin/orders/${order.id}/claims`,
|
||||
{
|
||||
type: "replace",
|
||||
shipping_methods: [
|
||||
{
|
||||
option_id: option.id,
|
||||
price: 0,
|
||||
},
|
||||
],
|
||||
additional_items: [{ variant_id: "test-variant", quantity: 1 }],
|
||||
claim_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
reason: "missing_item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/claims/${createRes.data.order.claims[0].id}/fulfillments`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
})
|
||||
|
||||
const createReturnableOrder = async (dbConnection, options = {}) => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{ id: "test-variant" },
|
||||
{ id: "variant-2", prices: [{ currency: "usd", amount: 1000 }] },
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
let discounts = []
|
||||
|
||||
if (options.discount) {
|
||||
discounts = [
|
||||
{
|
||||
code: "TESTCODE",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const order = await simpleOrderFactory(dbConnection, {
|
||||
email: "test@testson.com",
|
||||
tax_rate: null,
|
||||
fulfillment_status: "fulfilled",
|
||||
payment_status: "captured",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12.5, // Should be ignored due to item tax line
|
||||
},
|
||||
discounts,
|
||||
line_items: [
|
||||
{
|
||||
id: "test-item",
|
||||
variant_id: "test-variant",
|
||||
quantity: 2,
|
||||
fulfilled_quantity: 2,
|
||||
shipped_quantity: 2,
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
name: "default",
|
||||
code: "default",
|
||||
rate: 20,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await simplePaymentFactory(dbConnection, {
|
||||
provider_id: "test-pay",
|
||||
order: order.id,
|
||||
amount: 2400,
|
||||
captured: true,
|
||||
})
|
||||
|
||||
return order
|
||||
}
|
||||
272
integration-tests/api/__tests__/returns/index.js
Normal file
272
integration-tests/api/__tests__/returns/index.js
Normal file
@@ -0,0 +1,272 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
const {
|
||||
simpleOrderFactory,
|
||||
simpleProductFactory,
|
||||
simpleShippingOptionFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
describe("/admin/orders", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("creates a return w. old tax system", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection, { oldTaxes: true })
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/return`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
/*
|
||||
* Region has default tax rate 12.5 therefore refund amount should be
|
||||
* 1000 * 1.125 = 1125
|
||||
*/
|
||||
expect(response.data.order.returns[0].refund_amount).toEqual(1125)
|
||||
expect(response.data.order.returns[0].items).toEqual([
|
||||
expect.objectContaining({
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
test("creates a return w. new tax system", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const order = await createReturnableOrder(dbConnection, { oldTaxes: false })
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/return`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
/*
|
||||
* Region has default tax rate 12.5 but line item has tax rate 20
|
||||
* therefore refund amount should be 1000 * 1.2 = 1200
|
||||
*/
|
||||
expect(response.data.order.returns[0].refund_amount).toEqual(1200)
|
||||
|
||||
expect(response.data.order.returns[0].items).toEqual([
|
||||
expect.objectContaining({
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
test("creates a return w. new tax system + shipping", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const order = await createReturnableOrder(dbConnection, { oldTaxes: false })
|
||||
const returnOption = await simpleShippingOptionFactory(dbConnection, {
|
||||
name: "Return method",
|
||||
region_id: "test-region",
|
||||
is_return: true,
|
||||
price: 1000,
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/return`,
|
||||
{
|
||||
return_shipping: {
|
||||
option_id: returnOption.id,
|
||||
},
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
/*
|
||||
* Region has default tax rate 12.5 but line item has tax rate 20
|
||||
* therefore refund amount should be 1000 * 1.2 = 1200
|
||||
* shipping method will have 12.5 rate 1000 * 1.125 = 1125
|
||||
*/
|
||||
expect(response.data.order.returns[0].refund_amount).toEqual(75)
|
||||
expect(response.data.order.returns[0].shipping_method.tax_lines).toEqual([
|
||||
expect.objectContaining({
|
||||
rate: 12.5,
|
||||
name: "default",
|
||||
code: "default",
|
||||
}),
|
||||
])
|
||||
expect(response.data.order.returns[0].items).toEqual([
|
||||
expect.objectContaining({
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
test("creates a return w. discount", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const order = await createReturnableOrder(dbConnection, {
|
||||
discount: true,
|
||||
oldTaxes: false,
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/return`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
/*
|
||||
* Region has default tax rate 12.5 but line item has tax rate 20
|
||||
* therefore refund amount should be 1000 - 100 * 1.2 = 1080
|
||||
*/
|
||||
expect(response.data.order.returns[0].refund_amount).toEqual(1080)
|
||||
|
||||
expect(response.data.order.returns[0].items).toEqual([
|
||||
expect.objectContaining({
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
const createReturnableOrder = async (dbConnection, options) => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [{ id: "test-variant" }],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
let discounts = []
|
||||
|
||||
if (options.discount) {
|
||||
discounts = [
|
||||
{
|
||||
code: "TESTCODE",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return await simpleOrderFactory(dbConnection, {
|
||||
email: "test@testson.com",
|
||||
tax_rate: options.oldTaxes ? undefined : null,
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12.5, // Should be ignored due to item tax line
|
||||
},
|
||||
discounts,
|
||||
line_items: [
|
||||
{
|
||||
id: "test-item",
|
||||
variant_id: "test-variant",
|
||||
quantity: 2,
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
name: "default",
|
||||
code: "default",
|
||||
rate: 20,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
@@ -14,6 +14,7 @@ Object {
|
||||
"has_shipping": null,
|
||||
"id": StringMatching /\\^item_\\*/,
|
||||
"is_giftcard": false,
|
||||
"is_return": false,
|
||||
"metadata": Object {},
|
||||
"order_id": null,
|
||||
"quantity": 1,
|
||||
@@ -93,6 +94,7 @@ Object {
|
||||
"parent_order_id": "test-order",
|
||||
"swap_id": StringMatching /\\^swap_\\*/,
|
||||
},
|
||||
"object": "cart",
|
||||
"payment_authorized_at": null,
|
||||
"payment_id": null,
|
||||
"region_id": "test-region",
|
||||
@@ -126,6 +128,7 @@ Object {
|
||||
"idempotency_key": null,
|
||||
"metadata": null,
|
||||
"no_notification": null,
|
||||
"object": "order",
|
||||
"payment_status": "captured",
|
||||
"region_id": "test-region",
|
||||
"shipping_address_id": "test-shipping-address",
|
||||
@@ -187,6 +190,7 @@ Object {
|
||||
"has_shipping": null,
|
||||
"id": StringMatching /\\^item_\\*/,
|
||||
"is_giftcard": false,
|
||||
"is_return": false,
|
||||
"metadata": Object {},
|
||||
"order_id": null,
|
||||
"quantity": 1,
|
||||
@@ -266,6 +270,7 @@ Object {
|
||||
"parent_order_id": "test-order",
|
||||
"swap_id": StringMatching /\\^swap_\\*/,
|
||||
},
|
||||
"object": "cart",
|
||||
"payment_authorized_at": null,
|
||||
"payment_id": null,
|
||||
"region_id": "test-region",
|
||||
@@ -299,6 +304,7 @@ Object {
|
||||
"idempotency_key": null,
|
||||
"metadata": null,
|
||||
"no_notification": null,
|
||||
"object": "order",
|
||||
"payment_status": "captured",
|
||||
"region_id": "test-region",
|
||||
"shipping_address_id": "test-shipping-address",
|
||||
|
||||
@@ -5,7 +5,6 @@ const {
|
||||
GiftCard,
|
||||
Cart,
|
||||
CustomShippingOption,
|
||||
ShippingOption,
|
||||
} = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
|
||||
367
integration-tests/api/__tests__/swaps/index.js
Normal file
367
integration-tests/api/__tests__/swaps/index.js
Normal file
@@ -0,0 +1,367 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
const {
|
||||
simpleOrderFactory,
|
||||
simpleShippingOptionFactory,
|
||||
simpleProductFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
describe("Swaps", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("creates a swap", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/swaps`,
|
||||
{
|
||||
additional_items: [
|
||||
{
|
||||
variant_id: "variant-2",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const cartId = response.data.order.swaps[0].cart_id
|
||||
|
||||
/*
|
||||
* The return line item should use its tax_lines; the new line doesn't have
|
||||
* a tax line and uses the default region tax of 12.5
|
||||
*
|
||||
* Return line: 1000 * 1.2 = -1200
|
||||
* New line: 1000 * 1.125 = 1125
|
||||
* -
|
||||
* Difference should be -75
|
||||
*/
|
||||
const cartRes = await api.get(`/store/carts/${cartId}`)
|
||||
expect(cartRes.status).toEqual(200)
|
||||
expect(cartRes.data.cart.subtotal).toEqual(0)
|
||||
expect(cartRes.data.cart.total).toEqual(-75)
|
||||
expect(cartRes.data.cart.tax_total).toEqual(-75)
|
||||
})
|
||||
|
||||
test("creates a swap w. shipping", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const returnOption = await simpleShippingOptionFactory(dbConnection, {
|
||||
name: "Return method",
|
||||
region_id: "test-region",
|
||||
is_return: true,
|
||||
price: 100,
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/swaps`,
|
||||
{
|
||||
additional_items: [
|
||||
{
|
||||
variant_id: "variant-2",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
return_shipping: {
|
||||
option_id: returnOption.id,
|
||||
},
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const cartId = response.data.order.swaps[0].cart_id
|
||||
|
||||
/*
|
||||
* The return line item should use its tax_lines; the new line doesn't have
|
||||
* a tax line and uses the default region tax of 12.5
|
||||
*
|
||||
* Return line: 1000 * 1.2 = -1200
|
||||
* New line: 1000 * 1.125 = 1125
|
||||
* Shipping line: 100 * 1.125 = 112.5 ~ 113
|
||||
* -
|
||||
* Difference should be 38
|
||||
*/
|
||||
const cartRes = await api.get(`/store/carts/${cartId}`)
|
||||
expect(cartRes.status).toEqual(200)
|
||||
expect(cartRes.data.cart.subtotal).toEqual(100)
|
||||
expect(cartRes.data.cart.tax_total).toEqual(-62)
|
||||
expect(cartRes.data.cart.total).toEqual(38)
|
||||
})
|
||||
|
||||
test("retrieves a swap w. shipping", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const returnOption = await simpleShippingOptionFactory(dbConnection, {
|
||||
name: "Return method",
|
||||
region_id: "test-region",
|
||||
is_return: true,
|
||||
price: 100,
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/swaps`,
|
||||
{
|
||||
additional_items: [
|
||||
{
|
||||
variant_id: "variant-2",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
return_shipping: {
|
||||
option_id: returnOption.id,
|
||||
},
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const swapRes = await api.get(
|
||||
`/admin/swaps/${response.data.order.swaps[0].id}`,
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
expect(swapRes.status).toEqual(200)
|
||||
expect(swapRes.data.swap.cart.subtotal).toEqual(100)
|
||||
expect(swapRes.data.swap.cart.tax_total).toEqual(-62)
|
||||
expect(swapRes.data.swap.cart.total).toEqual(38)
|
||||
})
|
||||
|
||||
test("creates a swap from storefront", async () => {
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(`/store/swaps`, {
|
||||
order_id: order.id,
|
||||
additional_items: [
|
||||
{
|
||||
variant_id: "variant-2",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const cartId = response.data.swap.cart_id
|
||||
|
||||
/*
|
||||
* The return line item should use its tax_lines; the new line doesn't have
|
||||
* a tax line and uses the default region tax of 12.5
|
||||
*
|
||||
* Return line: 1000 * 1.2 = -1200
|
||||
* New line: 1000 * 1.125 = 1125
|
||||
* -
|
||||
* Difference should be -75
|
||||
*/
|
||||
const cartRes = await api.get(`/store/carts/${cartId}`)
|
||||
expect(cartRes.status).toEqual(200)
|
||||
expect(cartRes.data.cart.subtotal).toEqual(0)
|
||||
expect(cartRes.data.cart.total).toEqual(-75)
|
||||
expect(cartRes.data.cart.tax_total).toEqual(-75)
|
||||
})
|
||||
|
||||
test("completes a swap", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const order = await createReturnableOrder(dbConnection)
|
||||
const shippingOut = await simpleShippingOptionFactory(dbConnection, {
|
||||
region_id: "test-region",
|
||||
price: 500,
|
||||
})
|
||||
const returnOption = await simpleShippingOptionFactory(dbConnection, {
|
||||
name: "Return method",
|
||||
region_id: "test-region",
|
||||
is_return: true,
|
||||
price: 100,
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/swaps`,
|
||||
{
|
||||
additional_items: [
|
||||
{
|
||||
variant_id: "variant-2",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
return_shipping: {
|
||||
option_id: returnOption.id,
|
||||
},
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const cartId = response.data.order.swaps[0].cart_id
|
||||
|
||||
await api.post(`/store/carts/${cartId}`, {
|
||||
shipping_address: {
|
||||
address_1: "121 W Something St",
|
||||
postal_code: "1234",
|
||||
province: "something",
|
||||
city: "ville la something",
|
||||
phone: "12353245",
|
||||
},
|
||||
})
|
||||
await api.post(`/store/carts/${cartId}/shipping-methods`, {
|
||||
option_id: shippingOut.id,
|
||||
})
|
||||
await api.post(`/store/carts/${cartId}/payment-sessions`)
|
||||
const completion = await api.post(`/store/carts/${cartId}/complete`)
|
||||
|
||||
expect(completion.status).toEqual(200)
|
||||
expect(completion.data.type).toEqual("swap")
|
||||
})
|
||||
})
|
||||
|
||||
const createReturnableOrder = async (dbConnection, options = {}) => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{ id: "test-variant" },
|
||||
{ id: "variant-2", prices: [{ currency: "usd", amount: 1000 }] },
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
let discounts = []
|
||||
|
||||
if (options.discount) {
|
||||
discounts = [
|
||||
{
|
||||
code: "TESTCODE",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return await simpleOrderFactory(dbConnection, {
|
||||
email: "test@testson.com",
|
||||
tax_rate: null,
|
||||
fulfillment_status: "fulfilled",
|
||||
payment_status: "captured",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12.5, // Should be ignored due to item tax line
|
||||
},
|
||||
discounts,
|
||||
line_items: [
|
||||
{
|
||||
id: "test-item",
|
||||
variant_id: "test-variant",
|
||||
quantity: 2,
|
||||
fulfilled_quantity: 2,
|
||||
shipped_quantity: 2,
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
name: "default",
|
||||
code: "default",
|
||||
rate: 20,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`/admin/tax-rates creates a tax rate 1`] = `
|
||||
Object {
|
||||
"code": "tricks",
|
||||
"created_at": Any<String>,
|
||||
"id": StringMatching /\\^txr_\\*/,
|
||||
"name": "special",
|
||||
"rate": null,
|
||||
"region_id": "test-region",
|
||||
"updated_at": Any<String>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/admin/tax-rates deletes a tax rate 1`] = `
|
||||
Object {
|
||||
"deleted": true,
|
||||
"id": StringMatching /\\^txr_\\*/,
|
||||
"object": "tax-rate",
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/admin/tax-rates get tax rates 1`] = `
|
||||
Object {
|
||||
"code": "Bedfordshire",
|
||||
"created_at": Any<String>,
|
||||
"id": StringMatching /\\^txr_\\*/,
|
||||
"name": "Market Island",
|
||||
"rate": 96,
|
||||
"region_id": Any<String>,
|
||||
"updated_at": Any<String>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/admin/tax-rates get tax rates w. fields 1`] = `
|
||||
Object {
|
||||
"id": StringMatching /\\^txr_\\*/,
|
||||
"region_id": Any<String>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/admin/tax-rates updates a tax rate 1`] = `
|
||||
Object {
|
||||
"code": "something new",
|
||||
"created_at": Any<String>,
|
||||
"id": StringMatching /\\^txr_\\*/,
|
||||
"name": "special",
|
||||
"rate": 10,
|
||||
"region_id": "test-region",
|
||||
"updated_at": Any<String>,
|
||||
}
|
||||
`;
|
||||
526
integration-tests/api/__tests__/taxes/admin-tax-rates.js
Normal file
526
integration-tests/api/__tests__/taxes/admin-tax-rates.js
Normal file
@@ -0,0 +1,526 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
const {
|
||||
simpleProductFactory,
|
||||
simpleShippingOptionFactory,
|
||||
simpleRegionFactory,
|
||||
simpleTaxRateFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/admin/tax-rates", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd, verbose: true })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("list tax rates", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
await createTaxRates(dbConnection, 20, 2, 200)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/admin/tax-rates", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(20)
|
||||
})
|
||||
|
||||
test("list tax rates w. query", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
await createTaxRates(dbConnection, 20, 2, 200)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/tax-rates?fields[]=rate&fields[]=product_count&fields[]=id&expand[]=products&rate[gt]=80`,
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rates.some((tr) => tr.rate <= 80)).toEqual(false)
|
||||
})
|
||||
|
||||
test("list tax rates w. region query", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { regions } = await createTaxRates(dbConnection, 20, 2, 200)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/tax-rates?region_id[]=${regions[0].id}®ion_id[]=${regions[1].id}`,
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(
|
||||
response.data.tax_rates.some(
|
||||
(tr) => tr.region_id !== regions[0].id && tr.region_id !== regions[1].id
|
||||
)
|
||||
).toEqual(false)
|
||||
})
|
||||
|
||||
test("get tax rates", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates } = await createTaxRates(dbConnection, 20, 2, 200)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/admin/tax-rates/${tax_rates[0].id}`, {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^txr_*/),
|
||||
region_id: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
})
|
||||
})
|
||||
|
||||
test("get tax rates w. fields", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates } = await createTaxRates(dbConnection, 20, 2, 200)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/tax-rates/${tax_rates[0].id}?fields[]=id&fields[]=region_id`,
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^txr_*/),
|
||||
region_id: expect.any(String),
|
||||
})
|
||||
})
|
||||
|
||||
test("assigns tax rate to product type", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates } = await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const [rate] = tax_rates
|
||||
|
||||
const product = await simpleProductFactory(dbConnection, {
|
||||
type: "pants",
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates/${rate.id}/product-types/batch?fields[]=id&fields[]=region_id&fields[]=product_type_count&expand[]=product_types`,
|
||||
{
|
||||
product_types: [product.type_id],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate.product_type_count).toEqual(1)
|
||||
expect(response.data.tax_rate.product_types[0].id).toEqual(product.type_id)
|
||||
})
|
||||
|
||||
test("assigns tax rate to multiple product type", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates } = await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const [rate] = tax_rates
|
||||
|
||||
const products = await Promise.all(
|
||||
[0, 1, 2, 3].map((i) =>
|
||||
simpleProductFactory(dbConnection, {
|
||||
type: `pants-${i}`,
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates/${rate.id}/product-types/batch?fields[]=id&fields[]=region_id&fields[]=product_type_count&expand[]=product_types`,
|
||||
{
|
||||
product_types: products.map((product) => product.type_id),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate.product_type_count).toEqual(4)
|
||||
expect(response.data.tax_rate.product_types[0].id).toEqual(
|
||||
products[0].type_id
|
||||
)
|
||||
expect(response.data.tax_rate.product_types[1].id).toEqual(
|
||||
products[1].type_id
|
||||
)
|
||||
expect(response.data.tax_rate.product_types[2].id).toEqual(
|
||||
products[2].type_id
|
||||
)
|
||||
expect(response.data.tax_rate.product_types[3].id).toEqual(
|
||||
products[3].type_id
|
||||
)
|
||||
})
|
||||
|
||||
test.only("fails with 404 on unknown rate", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates } = await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const [rate] = tax_rates
|
||||
|
||||
await Promise.all(
|
||||
[0, 1, 2, 3].map(() => simpleProductFactory(dbConnection))
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
`/admin/tax-rates/${rate.id}/products/batch?fields[]=id&fields[]=product_count&expand[]=products`,
|
||||
{
|
||||
products: ["unknown_product_id"],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((err) => err.response)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
"Product with id: unknown_product_id was not found"
|
||||
)
|
||||
})
|
||||
|
||||
test("fails with 404 on unknown prod", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const products = await Promise.all(
|
||||
[0, 1, 2, 3].map(() => simpleProductFactory(dbConnection))
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
`/admin/tax-rates/unknown_rate/products/batch?fields[]=id&fields[]=product_count&expand[]=products`,
|
||||
{
|
||||
products: products.map((product) => product.id),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((err) => err.response)
|
||||
|
||||
expect(response.status).toEqual(404)
|
||||
expect(response.data.message).toEqual(
|
||||
"TaxRate with unknown_rate was not found"
|
||||
)
|
||||
})
|
||||
|
||||
test("fails to assign rate to shipping option with different reg", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates, regions } = await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const [reg] = regions
|
||||
const [rate] = tax_rates
|
||||
|
||||
const difReg = await simpleRegionFactory(dbConnection)
|
||||
const option = await simpleShippingOptionFactory(dbConnection, {
|
||||
name: "Test option",
|
||||
region_id: difReg,
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
`/admin/tax-rates/${rate.id}/shipping-options/batch?fields[]=id&fields[]=shipping_option_count&expand[]=shipping_options`,
|
||||
{
|
||||
shipping_options: [option.id],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((err) => err.response)
|
||||
|
||||
expect(response.status).toEqual(400)
|
||||
expect(response.data.message).toEqual(
|
||||
`Shipping Option and Tax Rate must belong to the same Region to be associated. Shipping Option with id: ${option.id} belongs to Region with id: ${option.region_id} and Tax Rate with id: ${rate.id} belongs to Region with id: ${rate.region_id}`
|
||||
)
|
||||
})
|
||||
|
||||
test("assigns tax rate to shipping option", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates, regions } = await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const [reg] = regions
|
||||
const [rate] = tax_rates
|
||||
|
||||
const options = await Promise.all(
|
||||
[0, 1, 2, 3].map((i) =>
|
||||
simpleShippingOptionFactory(dbConnection, {
|
||||
name: i,
|
||||
region_id: reg.id,
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates/${rate.id}/shipping-options/batch?fields[]=id&fields[]=shipping_option_count&expand[]=shipping_options`,
|
||||
{
|
||||
shipping_options: options.map((o) => o.id),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate.shipping_option_count).toEqual(4)
|
||||
expect(response.data.tax_rate.shipping_options[0].id).toEqual(options[0].id)
|
||||
expect(response.data.tax_rate.shipping_options[1].id).toEqual(options[1].id)
|
||||
expect(response.data.tax_rate.shipping_options[2].id).toEqual(options[2].id)
|
||||
expect(response.data.tax_rate.shipping_options[3].id).toEqual(options[3].id)
|
||||
})
|
||||
|
||||
test("assigns tax rate to products", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const { tax_rates } = await createTaxRates(dbConnection, 1, 1, 200)
|
||||
const [rate] = tax_rates
|
||||
|
||||
const products = await Promise.all(
|
||||
[0, 1, 2, 3].map(() => simpleProductFactory(dbConnection))
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates/${rate.id}/products/batch?fields[]=id&fields[]=product_count&expand[]=products`,
|
||||
{
|
||||
products: products.map((product) => product.id),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate.product_count).toEqual(4)
|
||||
expect(response.data.tax_rate.products[0].id).toEqual(products[0].id)
|
||||
expect(response.data.tax_rate.products[1].id).toEqual(products[1].id)
|
||||
expect(response.data.tax_rate.products[2].id).toEqual(products[2].id)
|
||||
expect(response.data.tax_rate.products[3].id).toEqual(products[3].id)
|
||||
})
|
||||
|
||||
test("creates a tax rate", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const api = useApi()
|
||||
await simpleRegionFactory(dbConnection, {
|
||||
id: "test-region",
|
||||
})
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates`,
|
||||
{
|
||||
name: "special",
|
||||
code: "tricks",
|
||||
region_id: "test-region",
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^txr_*/),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
})
|
||||
})
|
||||
|
||||
test("creates a tax rate and assigns products", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const products = await Promise.all(
|
||||
[0, 1, 2, 3].map(() => simpleProductFactory(dbConnection))
|
||||
)
|
||||
|
||||
await simpleRegionFactory(dbConnection, {
|
||||
id: "test-region",
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates?fields[]=product_count&expand[]=products`,
|
||||
{
|
||||
name: "special",
|
||||
code: "tricks",
|
||||
region_id: "test-region",
|
||||
products: products.map((p) => p.id),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate.product_count).toEqual(4)
|
||||
})
|
||||
|
||||
test("updates a tax rate", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleRegionFactory(dbConnection, { id: "test-region" })
|
||||
|
||||
const rate = await simpleTaxRateFactory(dbConnection, {
|
||||
name: "test",
|
||||
code: "something",
|
||||
rate: 10,
|
||||
region_id: "test-region",
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
const response = await api.post(
|
||||
`/admin/tax-rates/${rate.id}`,
|
||||
{
|
||||
name: "special",
|
||||
code: "something new",
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.tax_rate).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^txr_*/),
|
||||
code: "special",
|
||||
code: "something new",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
})
|
||||
})
|
||||
|
||||
test("deletes a tax rate", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleRegionFactory(dbConnection, { id: "test-region" })
|
||||
|
||||
const rate = await simpleTaxRateFactory(dbConnection, {
|
||||
name: "test",
|
||||
code: "something",
|
||||
rate: 10,
|
||||
region_id: "test-region",
|
||||
})
|
||||
|
||||
const api = useApi()
|
||||
const response = await api.delete(`/admin/tax-rates/${rate.id}`, {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data).toMatchSnapshot({
|
||||
id: expect.stringMatching(/^txr_*/),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
const createTaxRates = async (dbConnection, num, numRegions, seed) => {
|
||||
const regions = []
|
||||
for (let i = 0; i < numRegions; i++) {
|
||||
const reg = await simpleRegionFactory(dbConnection, {}, seed + i)
|
||||
regions.push(reg)
|
||||
}
|
||||
|
||||
const tax_rates = []
|
||||
for (let x = 0; x < num; x++) {
|
||||
const { id } = regions[Math.floor(Math.random() * regions.length)]
|
||||
const rate = await simpleTaxRateFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region_id: id,
|
||||
},
|
||||
seed + x
|
||||
)
|
||||
|
||||
tax_rates.push(rate)
|
||||
}
|
||||
|
||||
return { regions, tax_rates }
|
||||
}
|
||||
478
integration-tests/api/__tests__/taxes/automatic-taxes.js
Normal file
478
integration-tests/api/__tests__/taxes/automatic-taxes.js
Normal file
@@ -0,0 +1,478 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const {
|
||||
simpleProductTaxRateFactory,
|
||||
simpleShippingTaxRateFactory,
|
||||
simpleProductTypeTaxRateFactory,
|
||||
simpleShippingOptionFactory,
|
||||
simpleCartFactory,
|
||||
simpleRegionFactory,
|
||||
simpleProductFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Automatic Cart Taxes", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("correct calculation w. default tax rate", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-cart",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/store/carts/test-cart")
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(12)
|
||||
expect(response.data.cart.total).toEqual(112)
|
||||
})
|
||||
|
||||
test("correct calculation w. default tax rate w. shipping", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-cart",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
},
|
||||
shipping_methods: [
|
||||
{
|
||||
shipping_option: {
|
||||
name: "random",
|
||||
region_id: "test-region",
|
||||
},
|
||||
price: 100,
|
||||
},
|
||||
],
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/store/carts/test-cart")
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(24)
|
||||
expect(response.data.cart.total).toEqual(224)
|
||||
})
|
||||
|
||||
test("correct calculation w. same type + prod tax rate", async () => {
|
||||
const product = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
type: "Pants",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
const prodRate = await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 10,
|
||||
},
|
||||
})
|
||||
|
||||
await simpleProductTypeTaxRateFactory(dbConnection, {
|
||||
product_type_id: product.type_id,
|
||||
rate: prodRate.rate_id,
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(10)
|
||||
expect(response.data.cart.total).toEqual(110)
|
||||
})
|
||||
|
||||
test("correct calculation w. type + prod tax rate", async () => {
|
||||
const product = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
type: "Pants",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 10,
|
||||
},
|
||||
})
|
||||
|
||||
await simpleProductTypeTaxRateFactory(dbConnection, {
|
||||
product_type_id: product.type_id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(35)
|
||||
expect(response.data.cart.total).toEqual(135)
|
||||
})
|
||||
|
||||
test("correct calculation w. tax rate override type", async () => {
|
||||
const product = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
type: "Pants",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
await simpleProductTypeTaxRateFactory(dbConnection, {
|
||||
product_type_id: product.type_id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(25)
|
||||
expect(response.data.cart.total).toEqual(125)
|
||||
})
|
||||
|
||||
test("correct calculation w. tax rate override", async () => {
|
||||
const product = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(25)
|
||||
expect(response.data.cart.total).toEqual(125)
|
||||
})
|
||||
|
||||
test("correct calculation w. tax rate override w. shipping", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
const option = await simpleShippingOptionFactory(dbConnection, {
|
||||
name: "random",
|
||||
region_id: region.id,
|
||||
})
|
||||
|
||||
await simpleShippingTaxRateFactory(dbConnection, {
|
||||
shipping_option_id: option.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
shipping_methods: [
|
||||
{
|
||||
shipping_option: option.id,
|
||||
price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(37)
|
||||
expect(response.data.cart.total).toEqual(237)
|
||||
})
|
||||
|
||||
test("correct calculation w. multiple tax rate overrides", async () => {
|
||||
const product1 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const product2 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant-2",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product1.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product2.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 20,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
{
|
||||
variant_id: "test-variant-2",
|
||||
unit_price: 50,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(35)
|
||||
expect(response.data.cart.total).toEqual(185)
|
||||
})
|
||||
})
|
||||
204
integration-tests/api/__tests__/taxes/manual-taxes.js
Normal file
204
integration-tests/api/__tests__/taxes/manual-taxes.js
Normal file
@@ -0,0 +1,204 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const {
|
||||
simpleProductTaxRateFactory,
|
||||
simpleShippingTaxRateFactory,
|
||||
simpleShippingOptionFactory,
|
||||
simpleCartFactory,
|
||||
simpleRegionFactory,
|
||||
simpleProductFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Manual Cart Taxes", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("manual taxes; default tax rate", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-cart",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
automatic_taxes: false,
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/store/carts/test-cart")
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(null)
|
||||
expect(response.data.cart.total).toEqual(100)
|
||||
})
|
||||
|
||||
test("manual taxes; always forces taxes for payment sessions", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-cart",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
automatic_taxes: false,
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post("/store/carts/test-cart/payment-sessions")
|
||||
expect(response.status).toEqual(200)
|
||||
const [paySession] = response.data.cart.payment_sessions
|
||||
expect(paySession.data.tax_total).toEqual(12)
|
||||
expect(paySession.data.total).toEqual(112)
|
||||
})
|
||||
|
||||
test("manual tax calculation w. multiple tax rate overrides", async () => {
|
||||
const product1 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const product2 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant-2",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product1.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product2.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 20,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
{
|
||||
variant_id: "test-variant-2",
|
||||
unit_price: 50,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(`/store/carts/${cart.id}/taxes`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.tax_total).toEqual(35)
|
||||
expect(response.data.cart.total).toEqual(185)
|
||||
})
|
||||
})
|
||||
290
integration-tests/api/__tests__/taxes/orders.js
Normal file
290
integration-tests/api/__tests__/taxes/orders.js
Normal file
@@ -0,0 +1,290 @@
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const {
|
||||
simpleOrderFactory,
|
||||
simpleRegionFactory,
|
||||
simpleCartFactory,
|
||||
simpleProductFactory,
|
||||
simpleProductTaxRateFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Order Taxes", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
return await doAfterEach()
|
||||
})
|
||||
|
||||
test("can calculate taxes for legacy tax system", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const order = await simpleOrderFactory(
|
||||
dbConnection,
|
||||
{
|
||||
email: "test@testson.com",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12.5,
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 1000,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/orders/${order.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.order.tax_total).toEqual(125)
|
||||
expect(response.data.order.total).toEqual(1125)
|
||||
})
|
||||
|
||||
test("calculates taxes correctly", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const order = await simpleOrderFactory(
|
||||
dbConnection,
|
||||
{
|
||||
email: "test@testson.com",
|
||||
tax_rate: null,
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: null,
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 20,
|
||||
name: "default",
|
||||
code: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/orders/${order.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.order.tax_total).toEqual(200)
|
||||
expect(response.data.order.total).toEqual(1200)
|
||||
})
|
||||
|
||||
test("calculates taxes correctly w. shipping method", async () => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const order = await simpleOrderFactory(
|
||||
dbConnection,
|
||||
{
|
||||
email: "test@testson.com",
|
||||
tax_rate: null,
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: null,
|
||||
},
|
||||
shipping_methods: [
|
||||
{
|
||||
price: 1000,
|
||||
shipping_option: {
|
||||
region_id: "test-region",
|
||||
},
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
name: "default",
|
||||
code: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 20,
|
||||
name: "default",
|
||||
code: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/orders/${order.id}`)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.order.tax_total).toEqual(300)
|
||||
expect(response.data.order.total).toEqual(2300)
|
||||
})
|
||||
|
||||
test("completing cart creates tax lines", async () => {
|
||||
const product1 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const product2 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant-2",
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 12,
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product1.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 25,
|
||||
},
|
||||
})
|
||||
|
||||
await simpleProductTaxRateFactory(dbConnection, {
|
||||
product_id: product2.id,
|
||||
rate: {
|
||||
region_id: region.id,
|
||||
rate: 20,
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
region: region.id,
|
||||
email: "test@testson.com",
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 100,
|
||||
},
|
||||
{
|
||||
variant_id: "test-variant-2",
|
||||
unit_price: 50,
|
||||
},
|
||||
],
|
||||
},
|
||||
100
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
await api.post(`/store/carts/${cart.id}`, {
|
||||
email: "test@testson.com",
|
||||
})
|
||||
await api.post(`/store/carts/${cart.id}/payment-sessions`)
|
||||
const response = await api.post(`/store/carts/${cart.id}/complete`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
expect(response.data.type).toEqual("order")
|
||||
expect(response.data.data.tax_total).toEqual(35)
|
||||
expect(response.data.data.total).toEqual(185)
|
||||
|
||||
expect(response.data.data.items[0].tax_lines).toEqual([
|
||||
expect.objectContaining({
|
||||
rate: 25,
|
||||
}),
|
||||
])
|
||||
expect(response.data.data.items[1].tax_lines).toEqual([
|
||||
expect.objectContaining({
|
||||
rate: 20,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user