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:
Sebastian RindomandOliver Windall Juhl authored and olivermrbl committed 2022-02-25 18:53:49 +01:00
1 parent d80eaa172d
commit 47588e7a8d
223 files changed
+28546 -2229

No files matched your search

+14
View File
@@ -31,6 +31,20 @@ module.exports = {
plugins: [`@typescript-eslint/eslint-plugin`],
extends: [`plugin:@typescript-eslint/recommended`],
rules: {
"valid-jsdoc": [
"error",
{
requireParamType: false,
requireReturnType: false,
prefer: {
arg: "param",
argument: "param",
class: "constructor",
return: "return",
virtual: "abstract",
},
},
],
"@typescript-eslint/explicit-function-return-type": ["error"],
"@typescript-eslint/no-non-null-assertion": ["off"],
},
+13
View File
@@ -0,0 +1,13 @@
let ignore = [`**/dist`]
// Jest needs to compile this code, but generally we don't want this copied
// to output folders
if (process.env.NODE_ENV !== `test`) {
ignore.push(`**/__tests__`)
}
module.exports = {
sourceMaps: true,
presets: ["babel-preset-medusa-package"],
ignore,
}
@@ -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>,
}
`;
@@ -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
}
@@ -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")
@@ -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>,
}
`;
@@ -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}&region_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 }
}
@@ -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)
})
})
@@ -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)
})
})
@@ -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,
}),
])
})
})
+13
View File
@@ -0,0 +1,13 @@
export * from "./simple-payment-factory"
export * from "./simple-order-factory"
export * from "./simple-cart-factory"
export * from "./simple-region-factory"
export * from "./simple-line-item-factory"
export * from "./simple-product-factory"
export * from "./simple-product-variant-factory"
export * from "./simple-product-tax-rate-factory"
export * from "./simple-shipping-tax-rate-factory"
export * from "./simple-tax-rate-factory"
export * from "./simple-shipping-option-factory"
export * from "./simple-shipping-method-factory"
export * from "./simple-product-type-tax-rate-factory"
@@ -0,0 +1,34 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Address } from "@medusajs/medusa"
export type AddressFactoryData = {
first_name?: string
last_name?: string
country_code?: string
address_1?: string
postal_code?: string
}
export const simpleAddressFactory = async (
connection: Connection,
data: AddressFactoryData = {},
seed?: number
): Promise<Address> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const address = manager.create(Address, {
id: `simple-id-${Math.random() * 1000}`,
first_name: data.first_name || faker.name.firstName(),
last_name: data.last_name || faker.name.lastName(),
country_code: data.country_code || "us",
address_1: data.address_1 || faker.address.streetAddress(),
postal_code: data.postal_code || faker.address.zipCode(),
})
return await manager.save(address)
}
@@ -0,0 +1,70 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Cart } from "@medusajs/medusa"
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
import {
LineItemFactoryData,
simpleLineItemFactory,
} from "./simple-line-item-factory"
import {
AddressFactoryData,
simpleAddressFactory,
} from "./simple-address-factory"
import {
ShippingMethodFactoryData,
simpleShippingMethodFactory,
} from "./simple-shipping-method-factory"
export type CartFactoryData = {
id?: string
region?: RegionFactoryData | string
email?: string | null
line_items?: LineItemFactoryData[]
shipping_address?: AddressFactoryData
shipping_methods?: ShippingMethodFactoryData[]
}
export const simpleCartFactory = async (
connection: Connection,
data: CartFactoryData = {},
seed: number
): Promise<Cart> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let regionId: string
if (typeof data.region === "string") {
regionId = data.region
} else {
const region = await simpleRegionFactory(connection, data.region)
regionId = region.id
}
const address = await simpleAddressFactory(connection, data.shipping_address)
const id = data.id || `simple-cart-${Math.random() * 1000}`
const toSave = manager.create(Cart, {
id,
email:
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
region_id: regionId,
shipping_address_id: address.id,
})
const cart = await manager.save(toSave)
const shippingMethods = data.shipping_methods || []
for (const sm of shippingMethods) {
await simpleShippingMethodFactory(connection, { ...sm, cart_id: id })
}
const items = data.line_items
for (const item of items) {
await simpleLineItemFactory(connection, { ...item, cart_id: id })
}
return cart
}
@@ -0,0 +1,55 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
Discount,
DiscountRule,
DiscountRuleType,
AllocationType,
} from "@medusajs/medusa"
export type DiscountRuleFactoryData = {
type?: DiscountRuleType
value?: number
allocation?: AllocationType
}
export type DiscountFactoryData = {
id?: string
code?: string
is_dynamic?: boolean
rule?: DiscountRuleFactoryData
regions?: string[]
}
export const simpleDiscountFactory = async (
connection: Connection,
data: DiscountFactoryData = {},
seed?: number
): Promise<Discount> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const ruleData = data.rule ?? {}
const ruleToSave = manager.create(DiscountRule, {
type: ruleData.type ?? DiscountRuleType.PERCENTAGE,
value: ruleData.value ?? 10,
allocation: ruleData.allocation ?? AllocationType.TOTAL,
})
const dRule = await manager.save(ruleToSave)
const toSave = manager.create(Discount, {
id: data.id,
is_dynamic: data.is_dynamic ?? false,
is_disabled: false,
rule_id: dRule.id,
code: data.code ?? "TESTCODE",
regions: data.regions?.map((r) => ({ id: r })) || [],
})
const discount = await manager.save(toSave)
return discount
}
@@ -0,0 +1,84 @@
import { Connection } from "typeorm"
import faker from "faker"
import { LineItem, LineItemTaxLine } from "@medusajs/medusa"
type TaxLineFactoryData = {
rate: number
code: string
name: string
}
export type LineItemFactoryData = {
id?: string
cart_id?: string
order_id?: string
variant_id: string | null
title?: string
description?: string
thumbnail?: string
should_merge?: boolean
allow_discounts?: boolean
unit_price?: number
quantity?: number
fulfilled_quantity?: boolean
shipped_quantity?: boolean
returned_quantity?: boolean
tax_lines?: TaxLineFactoryData[]
}
export const simpleLineItemFactory = async (
connection: Connection,
data: LineItemFactoryData,
seed?: number
): Promise<LineItem> => {
if (
typeof data.cart_id === "undefined" &&
typeof data.order_id === "undefined"
) {
throw Error()
}
if (typeof seed !== "undefined") {
faker.seed(seed)
Math
}
const manager = connection.manager
const id = data.id || `simple-line-${Math.random() * 1000}`
const toSave = manager.create(LineItem, {
id,
cart_id: data.cart_id,
order_id: data.order_id,
title: data.title || faker.commerce.productName(),
description: data.description || "",
thumbnail: data.thumbnail || "",
should_merge:
typeof data.should_merge !== "undefined" ? data.should_merge : true,
allow_discounts:
typeof data.allow_discounts !== "undefined" ? data.allow_discounts : true,
unit_price: typeof data.unit_price !== "undefined" ? data.unit_price : 100,
variant_id: data.variant_id,
quantity: data.quantity || 1,
fulfilled_quantity: data.fulfilled_quantity || null,
shipped_quantity: data.shipped_quantity || null,
returned_quantity: data.returned_quantity || null,
})
const line = await manager.save(toSave)
if (typeof data.tax_lines !== "undefined") {
const taxLinesToSave = data.tax_lines.map((tl) =>
manager.create(LineItemTaxLine, {
item_id: id,
rate: tl.rate,
code: tl.code,
name: tl.name,
})
)
await manager.save(taxLinesToSave)
}
return line
}
@@ -0,0 +1,110 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
Customer,
Order,
PaymentStatus,
FulfillmentStatus,
} from "@medusajs/medusa"
import {
DiscountFactoryData,
simpleDiscountFactory,
} from "./simple-discount-factory"
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
import {
LineItemFactoryData,
simpleLineItemFactory,
} from "./simple-line-item-factory"
import {
AddressFactoryData,
simpleAddressFactory,
} from "./simple-address-factory"
import {
ShippingMethodFactoryData,
simpleShippingMethodFactory,
} from "./simple-shipping-method-factory"
export type OrderFactoryData = {
id?: string
payment_status?: PaymentStatus
fulfillment_status?: FulfillmentStatus
region?: RegionFactoryData | string
email?: string | null
currency_code?: string
tax_rate?: number | null
line_items?: LineItemFactoryData[]
discounts?: DiscountFactoryData[]
shipping_address?: AddressFactoryData
shipping_methods?: ShippingMethodFactoryData[]
}
export const simpleOrderFactory = async (
connection: Connection,
data: OrderFactoryData = {},
seed: number
): Promise<Order> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let currencyCode: string
let regionId: string
let taxRate: number
if (typeof data.region === "string") {
currencyCode = data.currency_code
regionId = data.region
taxRate = data.tax_rate
} else {
const region = await simpleRegionFactory(connection, data.region)
taxRate =
typeof data.tax_rate !== "undefined" ? data.tax_rate : region.tax_rate
currencyCode = region.currency_code
regionId = region.id
}
const address = await simpleAddressFactory(connection, data.shipping_address)
const customerToSave = manager.create(Customer, {
email:
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
})
const customer = await manager.save(customerToSave)
let discounts = []
if (typeof data.discounts !== "undefined") {
discounts = await Promise.all(
data.discounts.map((d) => simpleDiscountFactory(connection, d, seed))
)
}
const id = data.id || `simple-order-${Math.random() * 1000}`
const toSave = manager.create(Order, {
id,
discounts,
payment_status: data.payment_status ?? PaymentStatus.AWAITING,
fulfillment_status:
data.fulfillment_status ?? FulfillmentStatus.NOT_FULFILLED,
customer_id: customer.id,
email: customer.email,
region_id: regionId,
currency_code: currencyCode,
tax_rate: taxRate,
shipping_address_id: address.id,
})
const order = await manager.save(toSave)
const shippingMethods = data.shipping_methods || []
for (const sm of shippingMethods) {
await simpleShippingMethodFactory(connection, { ...sm, order_id: order.id })
}
const items = data.line_items
for (const item of items) {
await simpleLineItemFactory(connection, { ...item, order_id: id })
}
return order
}
@@ -0,0 +1,43 @@
import { Connection } from "typeorm"
import { Payment } from "@medusajs/medusa"
export type PaymentFactoryData = {
provider_id?: string
order?: string
cart?: string
data?: any
amount?: number
currency_code?: string
captured?: Date | boolean
}
export const simplePaymentFactory = async (
connection: Connection,
data: PaymentFactoryData,
_?: number
): Promise<Payment> => {
const manager = connection.manager
let captured_at = data.captured
if (typeof captured_at === "boolean") {
if (captured_at) {
captured_at = new Date()
} else {
captured_at = null
}
} else if (typeof captured_at === "undefined") {
captured_at = null
}
const address = manager.create(Payment, {
provider_id: data.provider_id ?? "test-pay",
order_id: data.order,
cart_id: data.cart,
data: data.data ?? {},
amount: data.amount ?? 1000,
currency_code: data.currency_code ?? "usd",
captured_at,
})
return await manager.save(address)
}
@@ -0,0 +1,99 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
ShippingProfileType,
ShippingProfile,
Product,
ProductType,
ProductOption,
} from "@medusajs/medusa"
import {
simpleProductVariantFactory,
ProductVariantFactoryData,
} from "./simple-product-variant-factory"
export type ProductFactoryData = {
id?: string
is_giftcard?: boolean
title?: string
type?: string
options?: { id: string; title: string }[]
variants?: ProductVariantFactoryData[]
}
export const simpleProductFactory = async (
connection: Connection,
data: ProductFactoryData = {},
seed?: number
): Promise<Product> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.DEFAULT,
})
const gcProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.GIFT_CARD,
})
let typeId: string
if (typeof data.type !== "undefined") {
const toSave = manager.create(ProductType, {
value: data.type,
})
const res = await manager.save(toSave)
typeId = res.id
}
const prodId = data.id || `simple-product-${Math.random() * 1000}`
const toSave = manager.create(Product, {
id: prodId,
type_id: typeId,
title: data.title || faker.commerce.productName(),
is_giftcard: data.is_giftcard || false,
discountable: !data.is_giftcard,
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
})
const product = await manager.save(toSave)
const optionId = `${prodId}-option`
const options = data.options || [{ id: optionId, title: "Size" }]
for (const o of options) {
await manager.insert(ProductOption, {
id: o.id,
title: o.title,
product_id: prodId,
})
}
const variants = data.variants || [
{
id: `simple-test-variant-${Math.random() * 1000}`,
title: "Test",
product_id: prodId,
prices: [{ currency: "usd", amount: 100 }],
options: [{ option_id: optionId, value: "Large" }],
},
]
for (const pv of variants) {
const factoryData = {
...pv,
product_id: prodId,
}
if (typeof pv.options === "undefined") {
factoryData.options = [
{ option_id: optionId, value: faker.commerce.productAdjective() },
]
}
await simpleProductVariantFactory(connection, factoryData)
}
return product
}
@@ -0,0 +1,48 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ProductTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ProductTaxRateFactoryData = {
product_id: string
rate: RateFactoryData | string
}
export const simpleProductTaxRateFactory = async (
connection: Connection,
data: ProductTaxRateFactoryData,
seed?: number
): Promise<ProductTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ProductTaxRate, {
product_id: data.product_id,
rate_id: rateId,
})
return await manager.save(toSave)
}
@@ -0,0 +1,48 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ProductTypeTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ProductTypeTaxRateFactoryData = {
product_type_id: string
rate: RateFactoryData | string
}
export const simpleProductTypeTaxRateFactory = async (
connection: Connection,
data: ProductTypeTaxRateFactoryData,
seed?: number
): Promise<ProductTypeTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ProductTypeTaxRate, {
product_type_id: data.product_type_id,
rate_id: rateId,
})
return await manager.save(toSave)
}
@@ -0,0 +1,64 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
ProductOptionValue,
ProductVariant,
MoneyAmount,
} from "@medusajs/medusa"
export type ProductVariantFactoryData = {
product_id: string
id?: string
is_giftcard?: boolean
inventory_quantity?: number
title?: string
options?: { option_id: string; value: string }[]
prices?: { currency: string; amount: number }[]
}
export const simpleProductVariantFactory = async (
connection: Connection,
data: ProductVariantFactoryData,
seed?: number
): Promise<ProductVariant> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const id = data.id || `simple-variant-${Math.random() * 1000}`
const toSave = manager.create(ProductVariant, {
id,
product_id: data.product_id,
inventory_quantity:
typeof data.inventory_quantity !== "undefined"
? data.inventory_quantity
: 10,
title: data.title || faker.commerce.productName(),
})
const variant = await manager.save(toSave)
const options = data.options || [{ option_id: "test-option", value: "Large" }]
for (const o of options) {
await manager.insert(ProductOptionValue, {
id: `${o.value}-${o.option_id}`,
value: o.value,
variant_id: id,
option_id: o.option_id,
})
}
const prices = data.prices || [{ currency: "usd", amount: 100 }]
for (const p of prices) {
await manager.insert(MoneyAmount, {
id: `${p.currency}-${p.amount}-${Math.random()}`,
variant_id: id,
currency_code: p.currency,
amount: p.amount,
})
}
return variant
}
@@ -0,0 +1,47 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Region } from "@medusajs/medusa"
export type RegionFactoryData = {
id?: string
name?: string
currency_code?: string
tax_rate?: number
countries?: string[]
automatic_taxes?: boolean
}
export const simpleRegionFactory = async (
connection: Connection,
data: RegionFactoryData = {},
seed?: number
): Promise<Region> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const regionId = data.id || `simple-region-${Math.random() * 1000}`
const r = manager.create(Region, {
id: regionId,
name: data.name || "Test Region",
currency_code: data.currency_code || "usd",
tax_rate: data.tax_rate || 0,
payment_providers: [{ id: "test-pay" }],
automatic_taxes:
typeof data.automatic_taxes !== "undefined" ? data.automatic_taxes : true,
})
const region = await manager.save(r)
const countries = data.countries || ["us"]
for (const cc of countries) {
await manager.query(
`UPDATE "country" SET region_id='${regionId}' WHERE iso_2 = '${cc}'`
)
}
return region
}
@@ -0,0 +1,68 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ShippingMethodTaxLine, ShippingMethod } from "@medusajs/medusa"
import {
ShippingOptionFactoryData,
simpleShippingOptionFactory,
} from "./simple-shipping-option-factory"
export type ShippingMethodFactoryData = {
id?: string
cart_id?: string
order_id?: string
data?: object
price?: number
shipping_option: string | ShippingOptionFactoryData
tax_lines?: ShippingMethodTaxLine[]
}
export const simpleShippingMethodFactory = async (
connection: Connection,
data: ShippingMethodFactoryData,
seed?: number
): Promise<ShippingMethod> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let shippingOptionId: string
if (typeof data.shipping_option === "string") {
shippingOptionId = data.shipping_option
} else {
const option = await simpleShippingOptionFactory(
connection,
data.shipping_option
)
shippingOptionId = option.id
}
const id = data.id || `simple-sm-${Math.random() * 1000}`
const toSave = manager.create(ShippingMethod, {
id,
cart_id: data.cart_id,
order_id: data.order_id,
shipping_option_id: shippingOptionId,
data: data.data || {},
price: typeof data.price !== "undefined" ? data.price : 500,
})
const shippingMethod = await manager.save(toSave)
if (typeof data.tax_lines !== "undefined") {
const taxLinesToSave = data.tax_lines.map((tl) =>
manager.create(ShippingMethodTaxLine, {
shipping_method_id: shippingMethod.id,
rate: tl.rate,
code: tl.code || "default",
name: tl.name || "default",
})
)
await manager.save(taxLinesToSave)
}
return shippingMethod
}
@@ -0,0 +1,49 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
ShippingOptionPriceType,
ShippingProfile,
ShippingOption,
ShippingProfileType,
} from "@medusajs/medusa"
export type ShippingOptionFactoryData = {
name?: string
region_id: string
is_return?: boolean
is_giftcard?: boolean
price?: number
}
export const simpleShippingOptionFactory = async (
connection: Connection,
data: ShippingOptionFactoryData,
seed?: number
): Promise<ShippingOption> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.DEFAULT,
})
const gcProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.GIFT_CARD,
})
const created = manager.create(ShippingOption, {
id: `simple-so-${Math.random() * 1000}`,
name: data.name || "Test Method",
is_return: data.is_return ?? false,
region_id: data.region_id,
provider_id: "test-ful",
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
price_type: ShippingOptionPriceType.FLAT_RATE,
data: {},
amount: typeof data.price !== "undefined" ? data.price : 500,
})
const option = await manager.save(created)
return option
}
@@ -0,0 +1,48 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ShippingTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ShippingTaxRateFactoryData = {
shipping_option_id: string
rate: RateFactoryData | string
}
export const simpleShippingTaxRateFactory = async (
connection: Connection,
data: ShippingTaxRateFactoryData,
seed?: number
): Promise<ShippingTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ShippingTaxRate, {
shipping_option_id: data.shipping_option_id,
rate_id: rateId,
})
return await manager.save(toSave)
}
@@ -0,0 +1,32 @@
import { Connection } from "typeorm"
import faker from "faker"
import { TaxRate } from "@medusajs/medusa"
export type TaxRateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export const simpleTaxRateFactory = async (
connection: Connection,
data: TaxRateFactoryData,
seed?: number
): Promise<TaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const toSave = manager.create(TaxRate, {
region_id: data.region_id,
rate:
data.rate ?? faker.datatype.number({ min: 0, max: 100, precision: 2 }),
code: data.code || faker.random.word(),
name: data.name || faker.random.words(2),
})
return await manager.save(toSave)
}
+3 -2
View File
@@ -8,8 +8,9 @@
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "1.1.64-dev-1645441522984",
"medusa-interfaces": "1.1.34-dev-1645441522984",
"@medusajs/medusa": "1.1.64-dev-1645628651544",
"faker": "^5.5.3",
"medusa-interfaces": "1.1.34-dev-1645628651544",
"typeorm": "^0.2.31"
},
"devDependencies": {
+16 -2
View File
@@ -15,8 +15,22 @@ class TestPayService extends PaymentService {
return Promise.resolve([])
}
async createPayment() {
return {}
async createPayment(cart) {
const fields = [
"total",
"subtotal",
"tax_total",
"discount_total",
"shipping_total",
"gift_card_total",
]
const data = {}
for (const k of fields) {
data[k] = cart[k]
}
return data
}
async retrievePayment(data) {
+40 -35
View File
@@ -1256,10 +1256,10 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"
"@medusajs/medusa-cli@1.1.27-dev-1645441522984":
version "1.1.27-dev-1645441522984"
resolved "http://localhost:4873/@medusajs%2fmedusa-cli/-/medusa-cli-1.1.27-dev-1645441522984.tgz#e3a5d8430c98592cce7909e1052fb5e2aec6797f"
integrity sha512-NnyH16LliwphtoGHg4phJBHFmtbt8I6tI9gs1ora2J/1gbZZbl5bjn9LG5MGZrCye69OBb1UdTWB+UWROgTBzg==
"@medusajs/medusa-cli@1.1.27-dev-1645628651544":
version "1.1.27-dev-1645628651544"
resolved "http://localhost:4873/@medusajs%2fmedusa-cli/-/medusa-cli-1.1.27-dev-1645628651544.tgz#0b5531d3c0df9a6a9ea01990b53f751692439458"
integrity sha512-3NrmJwIiUyLB/mYhVY0vUzrsFqHBCV+8LF+mrRwYWlsixXT8muSVUpObPJtvSRG4D/6xQrKcFk9wreDoT+Az9w==
dependencies:
"@babel/polyfill" "^7.8.7"
"@babel/runtime" "^7.9.6"
@@ -1277,8 +1277,8 @@
is-valid-path "^0.1.1"
joi-objectid "^3.0.1"
meant "^1.0.1"
medusa-core-utils "1.1.31-dev-1645441522984"
medusa-telemetry "0.0.11-dev-1645441522984"
medusa-core-utils "1.1.31-dev-1645628651544"
medusa-telemetry "0.0.11-dev-1645628651544"
netrc-parser "^3.1.6"
open "^8.0.6"
ora "^5.4.1"
@@ -1292,13 +1292,13 @@
winston "^3.3.3"
yargs "^15.3.1"
"@medusajs/medusa@1.1.64-dev-1645441522984":
version "1.1.64-dev-1645441522984"
resolved "http://localhost:4873/@medusajs%2fmedusa/-/medusa-1.1.64-dev-1645441522984.tgz#46dda1904705f2adb15e8aaafe0e1ecdce09763d"
integrity sha512-oQkdKGRhpa504vBhAmdRZMRxD1HUnXA6l0amaIeHk7OpT8JewMt63f/G6nttXatkHUNcYV0tlRW+mgy1gAyAmA==
"@medusajs/medusa@1.1.64-dev-1645628651544":
version "1.1.64-dev-1645628651544"
resolved "http://localhost:4873/@medusajs%2fmedusa/-/medusa-1.1.64-dev-1645628651544.tgz#d4e314cc8511d337bf380cdf0d3714b3ab039298"
integrity sha512-h2A1V96bJzMn1pe0jhQ98J4b8tv1JUGUQV3vYNIjHLKrWQVVNoY52Rk8bQjaCyH9YoITbiLOSRTApTSUsqzpyQ==
dependencies:
"@hapi/joi" "^16.1.8"
"@medusajs/medusa-cli" "1.1.27-dev-1645441522984"
"@medusajs/medusa-cli" "1.1.27-dev-1645628651544"
"@types/lodash" "^4.14.168"
awilix "^4.2.3"
body-parser "^1.19.0"
@@ -1322,8 +1322,8 @@
joi "^17.3.0"
joi-objectid "^3.0.1"
jsonwebtoken "^8.5.1"
medusa-core-utils "1.1.31-dev-1645441522984"
medusa-test-utils "1.1.37-dev-1645441522984"
medusa-core-utils "1.1.31-dev-1645628651544"
medusa-test-utils "1.1.37-dev-1645628651544"
morgan "^1.9.1"
multer "^1.4.2"
passport "^0.4.0"
@@ -1947,10 +1947,10 @@ babel-preset-jest@^26.6.2:
babel-plugin-jest-hoist "^26.6.2"
babel-preset-current-node-syntax "^1.0.0"
babel-preset-medusa-package@1.1.19-dev-1645441522984:
version "1.1.19-dev-1645441522984"
resolved "http://localhost:4873/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.19-dev-1645441522984.tgz#d7f494e6bcdf97d22e32913809a842d90720ee71"
integrity sha512-slmfLD+uwJhvYz2MMwFepjpbBB6K4EaZbKImJJ/Bp4RcZzoc44lCV/e5+9q2HU7hz+aLgZVangFvL9vLXglyzQ==
babel-preset-medusa-package@1.1.19-dev-1645628651544:
version "1.1.19-dev-1645628651544"
resolved "http://localhost:4873/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.19-dev-1645628651544.tgz#506bd183276d30458da4e2857459c6f38435d4b0"
integrity sha512-0XBmV1OuUDVIQccYKfU3Yj2gMYIeLEV8I/ugyVfgQ4AiYU7to97wFzdD8dXUTXd1I94gw4jbyZeZwflj0NoMhw==
dependencies:
"@babel/plugin-proposal-class-properties" "^7.12.1"
"@babel/plugin-proposal-decorators" "^7.12.1"
@@ -3265,6 +3265,11 @@ extsprintf@^1.2.0:
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.1.tgz#8d172c064867f235c0c84a596806d279bf4bcc07"
integrity sha512-Wrk35e8ydCKDj/ArClo1VrPVmN8zph5V4AtHwIuHhvMXsKf73UT3BOD+azBIW+3wOJ4FhEH7zyaJCFvChjYvMA==
faker@^5.5.3:
version "5.5.3"
resolved "https://registry.yarnpkg.com/faker/-/faker-5.5.3.tgz#c57974ee484431b25205c2c8dc09fda861e51e0e"
integrity sha512-wLTv2a28wjUyWkbnX7u/ABZBkUkIF2fCd73V6P2oFqEGEktDfzWx4UxrSqtPRw0xPRAcjeAOIiJWqZm3pP4u3g==
fast-deep-equal@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
@@ -5135,25 +5140,25 @@ media-typer@0.3.0:
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
medusa-core-utils@1.1.31-dev-1645441522984:
version "1.1.31-dev-1645441522984"
resolved "http://localhost:4873/medusa-core-utils/-/medusa-core-utils-1.1.31-dev-1645441522984.tgz#e8942d486689b3fa5b2dcdd6b98cb7f2a46ffc3a"
integrity sha512-BRQf/vQoiHbRfEty1vSqcX5rbiQaoeYigiParQMmD3fnRprrKkxh9UjsPzVckiyjLM2QU4NIPcve42HmCJ+iKA==
medusa-core-utils@1.1.31-dev-1645628651544:
version "1.1.31-dev-1645628651544"
resolved "http://localhost:4873/medusa-core-utils/-/medusa-core-utils-1.1.31-dev-1645628651544.tgz#bd6c5dc2aec5f9a109afef8eee22cbef1c77d63c"
integrity sha512-xkLY5QPcM1yjvhhSv39zJX9RimCMS7p8Y3j9ey6JrZYXBwzznzKsDPTlhZAdNKZuvGXVvnnmJXh563fvS8lGlg==
dependencies:
joi "^17.3.0"
joi-objectid "^3.0.1"
medusa-interfaces@1.1.34-dev-1645441522984:
version "1.1.34-dev-1645441522984"
resolved "http://localhost:4873/medusa-interfaces/-/medusa-interfaces-1.1.34-dev-1645441522984.tgz#81522e69f2416916a4a134263db62f73dc4c00c0"
integrity sha512-1IMOtBJvCwPh4pscOn7Z9c+vOkebadHu8AB9KbwWRM3ziyYgwNMVg8tC90sn20PctDXeQyWhsPfY1IkWj83WXw==
medusa-interfaces@1.1.34-dev-1645628651544:
version "1.1.34-dev-1645628651544"
resolved "http://localhost:4873/medusa-interfaces/-/medusa-interfaces-1.1.34-dev-1645628651544.tgz#6d19acdef4019813eaa7554721cbc4f02fb16083"
integrity sha512-QsP0CtriL0avWFtSWtFoLx1oAcMtgMKAyCIfkkRQuPLYtM0o7Rc1l4cMnvYw9I8XPw+/RFQnc0CL3vJqDVzEUQ==
dependencies:
medusa-core-utils "1.1.31-dev-1645441522984"
medusa-core-utils "1.1.31-dev-1645628651544"
medusa-telemetry@0.0.11-dev-1645441522984:
version "0.0.11-dev-1645441522984"
resolved "http://localhost:4873/medusa-telemetry/-/medusa-telemetry-0.0.11-dev-1645441522984.tgz#6939f21cbf01015df6d59983cfdfebc4d853d781"
integrity sha512-l2kYVlYYs0tMIy27xCyj1USfh0yE8L9i+c8j0jW6mqcAJ+rVMPrahBs4jQs8TD1ptFtLDgYUNRg4WK1/f6RN8Q==
medusa-telemetry@0.0.11-dev-1645628651544:
version "0.0.11-dev-1645628651544"
resolved "http://localhost:4873/medusa-telemetry/-/medusa-telemetry-0.0.11-dev-1645628651544.tgz#b1534c1e52d699f10cd66cdb270f04217d8aba56"
integrity sha512-+IkNVi75gogVyXTL4dDSYY+3g26B69yljQDH6XOqwNSb8OXU/XBxmv6EiHzjopESe82caXr8kUnyFWOyybp9VA==
dependencies:
axios "^0.21.1"
axios-retry "^3.1.9"
@@ -5165,13 +5170,13 @@ medusa-telemetry@0.0.11-dev-1645441522984:
remove-trailing-slash "^0.1.1"
uuid "^8.3.2"
medusa-test-utils@1.1.37-dev-1645441522984:
version "1.1.37-dev-1645441522984"
resolved "http://localhost:4873/medusa-test-utils/-/medusa-test-utils-1.1.37-dev-1645441522984.tgz#14e197adaab890e0aa1bbe812d8dd51387e38661"
integrity sha512-5qfcgPA/usM+n9vUoZluK0rEldaz5movukqDeyPTI1YbhJpIjQAq2f8GkHPIORDg4ppawUYz93d3gchIhe87FA==
medusa-test-utils@1.1.37-dev-1645628651544:
version "1.1.37-dev-1645628651544"
resolved "http://localhost:4873/medusa-test-utils/-/medusa-test-utils-1.1.37-dev-1645628651544.tgz#45103cf82131b41d073e17188f9b7f5985b4c083"
integrity sha512-56OByKGpTGnFQ6ThEadThovK5+h/24/CTBOvBCq/voOmwxsr89LBTwd9UB7wbubb6L/voMWSfmOsCb/7kUkq6w==
dependencies:
"@babel/plugin-transform-classes" "^7.9.5"
medusa-core-utils "1.1.31-dev-1645441522984"
medusa-core-utils "1.1.31-dev-1645628651544"
randomatic "^3.1.1"
merge-descriptors@1.0.1:
+30
View File
@@ -0,0 +1,30 @@
const path = require("path")
const express = require("express")
const getPort = require("get-port")
const importFrom = require("import-from")
module.exports = {
bootstrapApp: async ({ cwd } = {}) => {
const app = express()
const loaders = importFrom(
cwd || process.cwd(),
"@medusajs/medusa/dist/loaders"
).default
const { container, dbConnection } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
})
const PORT = await getPort()
return {
container,
db: dbConnection,
app,
port: PORT,
}
},
}
+2 -27
View File
@@ -1,32 +1,7 @@
const path = require("path")
const express = require("express")
const getPort = require("get-port")
const importFrom = require("import-from")
const initialize = async () => {
const app = express()
const loaders = importFrom(
process.cwd(),
"@medusajs/medusa/dist/loaders"
).default
const { dbConnection } = await loaders({
directory: path.resolve(process.cwd()),
expressApp: app,
})
const PORT = await getPort()
return {
db: dbConnection,
app,
port: PORT,
}
}
const { bootstrapApp } = require("./bootstrap-app")
const setup = async () => {
const { app, port } = await initialize()
const { app, port } = await bootstrapApp()
app.listen(port, (err) => {
process.send(port)
+8 -2
View File
@@ -16,6 +16,7 @@ const pgGodCredentials = {
}
const keepTables = [
"store",
"staged_job",
"shipping_profile",
"fulfillment_provider",
@@ -37,7 +38,9 @@ const DbTestUtil = {
this.db_.synchronize(true)
},
teardown: async function () {
teardown: async function ({ forceDelete } = {}) {
forceDelete = forceDelete || []
const entities = this.db_.entityMetadatas
const manager = this.db_.manager
@@ -48,7 +51,10 @@ const DbTestUtil = {
}
for (const entity of entities) {
if (keepTables.includes(entity.tableName)) {
if (
keepTables.includes(entity.tableName) &&
!forceDelete.includes(entity.tableName)
) {
continue
}
+1
View File
@@ -15,6 +15,7 @@ module.exports = {
`/www/`,
`/dist/`,
`/node_modules/`,
`/plugins/`,
`__tests__/fixtures`,
`__testfixtures__`,
`.cache`,
+3
View File
@@ -10,5 +10,8 @@
"license": "ISC",
"devDependencies": {
"dotenv": "^10.0.0"
},
"dependencies": {
"@faker-js/faker": "^5.5.3"
}
}
+13
View File
@@ -0,0 +1,13 @@
let ignore = [`**/dist`]
// Jest needs to compile this code, but generally we don't want this copied
// to output folders
if (process.env.NODE_ENV !== `test`) {
ignore.push(`**/__tests__`)
}
module.exports = {
sourceMaps: true,
presets: ["babel-preset-medusa-package"],
ignore,
}
+4
View File
@@ -0,0 +1,4 @@
dist/
node_modules
*yarn-error.log
@@ -0,0 +1,911 @@
const path = require("path")
const { bootstrapApp } = require("../../../helpers/bootstrap-app")
const { initDb, useDb } = require("../../../helpers/use-db")
const { setPort, useApi } = require("../../../helpers/use-api")
const adminSeeder = require("../../helpers/admin-seeder")
const {
simpleOrderFactory,
simpleStoreFactory,
simpleProductFactory,
simpleShippingOptionFactory,
} = require("../../factories")
describe("medusa-plugin-sendgrid", () => {
let appContainer
let dbConnection
let express
const doAfterEach = async () => {
const db = useDb()
return await db.teardown()
}
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
try {
dbConnection = await initDb({ cwd })
const { container, app, port } = await bootstrapApp({ cwd })
appContainer = container
setPort(port)
express = app.listen(port, (err) => {
process.send(port)
})
} catch (error) {
console.log(error)
throw error
}
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
express.close()
// medusaProcess.kill()
})
afterEach(async () => {
return await doAfterEach()
})
test("order canceled data", async () => {
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection, {
notShipped: true,
})
const api = useApi()
const response = await api.post(
`/admin/orders/${order.id}/cancel`,
{},
{ headers: { authorization: "Bearer test_token" } }
)
expect(response.status).toEqual(200)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("order.canceled", {
id: order.id,
})
expect(data).toMatchSnapshot({
date: expect.any(String),
id: expect.any(String),
display_id: expect.any(Number),
created_at: expect.any(Date),
canceled_at: expect.any(Date),
updated_at: expect.any(Date),
customer_id: expect.any(String),
customer: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
shipping_methods: [
{
id: expect.any(String),
shipping_option_id: expect.any(String),
order_id: expect.any(String),
shipping_option: {
id: expect.any(String),
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
],
shipping_address_id: expect.any(String),
shipping_address: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
items: [
{
created_at: expect.any(Date),
updated_at: expect.any(Date),
order_id: expect.any(String),
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
variant: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
product: {
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
},
],
region: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
})
})
test("order shipment created data", async () => {
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection, {
notShipped: true,
})
const api = useApi()
const { data: fulfillmentData } = await api.post(
`/admin/orders/${order.id}/fulfillment`,
{ items: [{ item_id: "test-item", quantity: 2 }] },
{ headers: { authorization: "Bearer test_token" } }
)
const fulfillment = fulfillmentData.order.fulfillments[0]
const response = await api.post(
`/admin/orders/${order.id}/shipment`,
{ fulfillment_id: fulfillment.id },
{ headers: { authorization: "Bearer test_token" } }
)
expect(response.status).toEqual(200)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("order.shipment_created", {
id: order.id,
fulfillment_id: fulfillment.id,
})
expect(data).toMatchSnapshot({
date: expect.any(String),
fulfillment: {
id: expect.any(String),
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
shipped_at: expect.any(Date),
items: [
{
fulfillment_id: expect.any(String),
},
],
},
order: {
display_id: expect.any(Number),
id: expect.any(String),
display_id: expect.any(Number),
created_at: expect.any(Date),
updated_at: expect.any(Date),
customer_id: expect.any(String),
customer: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
fulfillments: [
{
id: expect.any(String),
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
shipped_at: expect.any(Date),
items: [
{
fulfillment_id: expect.any(String),
},
],
},
],
shipping_methods: [
{
id: expect.any(String),
shipping_option_id: expect.any(String),
order_id: expect.any(String),
shipping_option: {
id: expect.any(String),
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
],
shipping_address_id: expect.any(String),
shipping_address: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
items: [
{
created_at: expect.any(Date),
updated_at: expect.any(Date),
order_id: expect.any(String),
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
variant: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
product: {
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
},
],
region: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
})
})
test("order placed data", async () => {
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("order.placed", {
id: order.id,
})
expect(data).toMatchSnapshot({
date: expect.any(String),
id: expect.any(String),
display_id: expect.any(Number),
created_at: expect.any(Date),
updated_at: expect.any(Date),
customer_id: expect.any(String),
customer: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
shipping_address_id: expect.any(String),
shipping_address: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
items: [
{
created_at: expect.any(Date),
updated_at: expect.any(Date),
order_id: expect.any(String),
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
variant: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
product: {
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
totals: {
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
},
},
],
region: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
})
})
test("swap received data", async () => {
await simpleStoreFactory(dbConnection)
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)
expect(response.status).toEqual(200)
const swap = response.data.order.swaps[0]
const returnOrder = swap.return_order
await api.post(
`/admin/returns/${returnOrder.id}/receive`,
{
items: returnOrder.items.map((i) => ({
item_id: i.item_id,
quantity: i.quantity,
})),
},
{ headers: { authorization: "Bearer test_token" } }
)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("swap.received", {
id: swap.id,
order_id: order.id,
})
expect(data.return_total).toMatchSnapshot()
expect(data.refund_amount).toMatchSnapshot()
expect(data.additional_total).toMatchSnapshot()
})
test("items returned data", async () => {
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection)
const api = useApi()
const response = await api.post(
`/admin/orders/${order.id}/return`,
{
items: [{ item_id: "test-item", quantity: 1 }],
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
expect(response.status).toEqual(200)
const returnOrder = response.data.order.returns[0]
const returnId = returnOrder.id
await api.post(
`/admin/returns/${returnId}/receive`,
{
items: returnOrder.items.map((i) => ({
item_id: i.item_id,
quantity: i.quantity,
})),
},
{ headers: { authorization: "Bearer test_token" } }
)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("order.items_returned", {
id: order.id,
return_id: returnId,
})
const returnSnap = getReturnSnap(true)
expect(data).toMatchSnapshot(returnSnap)
})
test("claim shipment created data", async () => {
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection)
const shippingOut = await simpleShippingOptionFactory(dbConnection, {
region_id: "test-region",
price: 500,
})
const api = useApi()
const response = await api.post(
`/admin/orders/${order.id}/claims`,
{
type: "replace",
additional_items: [{ variant_id: "variant-2", quantity: 1 }],
shipping_methods: [
{
option_id: shippingOut.id,
price: 0,
},
],
claim_items: [
{ reason: "missing_item", item_id: "test-item", quantity: 1 },
],
},
{ headers: { authorization: "Bearer test_token" } }
)
expect(response.status).toEqual(200)
const claimId = response.data.order.claims[0].id
const { data: fulfillmentData } = await api.post(
`/admin/orders/${order.id}/claims/${claimId}/fulfillments`,
{},
{ headers: { authorization: "Bearer test_token" } }
)
const fulfillmentId = fulfillmentData.order.claims[0].fulfillments[0].id
await api.post(
`/admin/orders/${order.id}/claims/${claimId}/shipments`,
{ fulfillment_id: fulfillmentId },
{ headers: { authorization: "Bearer test_token" } }
)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("claim.shipment_created", {
id: claimId,
fulfillment_id: fulfillmentId,
})
const orderSnap = {
id: expect.any(String),
display_id: expect.any(Number),
customer_id: expect.any(String),
shipping_address_id: expect.any(String),
shipping_address: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
created_at: expect.any(Date),
updated_at: expect.any(Date),
items: [
{
id: expect.any(String),
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
variant: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
product: {
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
},
],
}
expect(data).toMatchSnapshot({
claim: {
id: expect.any(String),
order_id: expect.any(String),
shipping_address_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
idempotency_key: expect.any(String),
order: orderSnap,
},
fulfillment: {
id: expect.any(String),
claim_order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
shipped_at: expect.any(Date),
items: [
{
fulfillment_id: expect.any(String),
item_id: expect.any(String),
},
],
},
order: orderSnap,
})
})
test("swap shipment created data", async () => {
await simpleStoreFactory(dbConnection)
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection)
const shippingOut = await simpleShippingOptionFactory(dbConnection, {
region_id: "test-region",
price: 500,
})
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 swapId = response.data.order.swaps[0].id
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`)
await api.post(`/store/carts/${cartId}/complete`)
const { data: fulfillmentData } = await api.post(
`/admin/orders/${order.id}/swaps/${swapId}/fulfillments`,
{},
{ headers: { authorization: "Bearer test_token" } }
)
const fulfillmentId = fulfillmentData.order.swaps[0].fulfillments[0].id
await api.post(
`/admin/orders/${order.id}/swaps/${swapId}/shipments`,
{ fulfillment_id: fulfillmentId },
{ headers: { authorization: "Bearer test_token" } }
)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("swap.shipment_created", {
id: swapId,
fulfillment_id: fulfillmentId,
})
const itemSnap = {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
variant: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
product: {
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
tax_lines: [
{
id: expect.any(String),
item_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
}
const swapSnap = {
id: expect.any(String),
cart_id: expect.any(String),
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
confirmed_at: expect.any(Date),
idempotency_key: expect.any(String),
shipping_address_id: expect.any(String),
additional_items: [
{
swap_id: expect.any(String),
cart_id: expect.any(String),
...itemSnap,
},
],
}
expect(data).toMatchSnapshot({
date: expect.any(String),
swap: {
...swapSnap,
shipping_address: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
shipping_methods: [
{
id: expect.any(String),
cart_id: expect.any(String),
swap_id: expect.any(String),
shipping_option: {
id: expect.any(String),
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
shipping_option_id: expect.any(String),
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
shipping_method_id: expect.any(String),
},
],
},
],
return_order: {
id: expect.any(String),
swap_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
items: [
{
return_id: expect.any(String),
},
],
},
},
fulfillment: {
id: expect.any(String),
created_at: expect.any(Date),
shipped_at: expect.any(Date),
updated_at: expect.any(Date),
swap_id: expect.any(String),
items: [
{
fulfillment_id: expect.any(String),
item_id: expect.any(String),
},
],
},
order: {
display_id: expect.any(Number),
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
items: [{ order_id: expect.any(String), ...itemSnap }],
customer_id: expect.any(String),
shipping_address_id: expect.any(String),
swaps: [swapSnap],
region: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
items: [
{
swap_id: expect.any(String),
cart_id: expect.any(String),
...itemSnap,
},
],
})
})
test("return requested data", async () => {
await adminSeeder(dbConnection)
const order = await createReturnableOrder(dbConnection)
const api = useApi()
const response = await api.post(
`/admin/orders/${order.id}/return`,
{
items: [{ item_id: "test-item", quantity: 1 }],
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
expect(response.status).toEqual(200)
const sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("order.return_requested", {
id: response.data.order.id,
return_id: response.data.order.returns[0].id,
})
const returnSnap = getReturnSnap()
expect(data).toMatchSnapshot(returnSnap)
})
test("swap created data", 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 sendgridService = appContainer.resolve("sendgridService")
const data = await sendgridService.fetchData("swap.created", {
id: response.data.order.swaps[0].id,
})
expect(data.return_total).toMatchSnapshot()
expect(data.refund_amount).toMatchSnapshot()
expect(data.additional_total).toMatchSnapshot()
})
})
const getReturnSnap = (received = false) => {
const itemSnap = {
id: expect.any(String),
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
variant: {
created_at: expect.any(Date),
updated_at: expect.any(Date),
product: {
profile_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
}
return {
date: expect.any(String),
order: {
display_id: expect.any(Number),
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
items: [itemSnap],
customer_id: expect.any(String),
shipping_address_id: expect.any(String),
returns: [
{
id: expect.any(String),
received_at: received ? expect.any(Date) : null,
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
idempotency_key: expect.any(String),
items: [
{
return_id: expect.any(String),
},
],
},
],
shipping_address: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
region: {
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
},
return_request: {
id: expect.any(String),
received_at: received ? expect.any(Date) : null,
order_id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
idempotency_key: expect.any(String),
items: [
{
return_id: expect.any(String),
item: itemSnap,
},
],
},
items: [
{
...itemSnap,
totals: {
tax_lines: [
{
id: expect.any(String),
created_at: expect.any(Date),
updated_at: expect.any(Date),
},
],
},
},
],
}
}
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",
shipping_methods: options.notShipped
? [
{
price: 0,
shipping_option: { name: "free", region_id: "test-region" },
},
]
: [],
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: options.notShipped ? 0 : 2,
shipped_quantity: options.notShipped ? 0 : 2,
unit_price: 1000,
tax_lines: [
{
name: "default",
code: "default",
rate: 20,
},
],
},
],
})
}
@@ -0,0 +1,13 @@
export * from "./simple-order-factory"
export * from "./simple-cart-factory"
export * from "./simple-region-factory"
export * from "./simple-line-item-factory"
export * from "./simple-product-factory"
export * from "./simple-product-variant-factory"
export * from "./simple-product-tax-rate-factory"
export * from "./simple-shipping-tax-rate-factory"
export * from "./simple-tax-rate-factory"
export * from "./simple-shipping-option-factory"
export * from "./simple-shipping-method-factory"
export * from "./simple-product-type-tax-rate-factory"
export * from "./simple-store-factory"
@@ -0,0 +1,34 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Address } from "@medusajs/medusa"
export type AddressFactoryData = {
first_name?: string
last_name?: string
country_code?: string
address_1?: string
postal_code?: string
}
export const simpleAddressFactory = async (
connection: Connection,
data: AddressFactoryData = {},
seed?: number
): Promise<Address> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const address = manager.create(Address, {
id: `simple-id-${Math.random() * 1000}`,
first_name: data.first_name || faker.name.firstName(),
last_name: data.last_name || faker.name.lastName(),
country_code: data.country_code || "us",
address_1: data.address_1 || faker.address.streetAddress(),
postal_code: data.postal_code || faker.address.zipCode(),
})
return await manager.save(address)
}
@@ -0,0 +1,70 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Cart } from "@medusajs/medusa"
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
import {
LineItemFactoryData,
simpleLineItemFactory,
} from "./simple-line-item-factory"
import {
AddressFactoryData,
simpleAddressFactory,
} from "./simple-address-factory"
import {
ShippingMethodFactoryData,
simpleShippingMethodFactory,
} from "./simple-shipping-method-factory"
export type CartFactoryData = {
id?: string
region?: RegionFactoryData | string
email?: string | null
line_items?: LineItemFactoryData[]
shipping_address?: AddressFactoryData
shipping_methods?: ShippingMethodFactoryData[]
}
export const simpleCartFactory = async (
connection: Connection,
data: CartFactoryData = {},
seed: number
): Promise<Cart> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let regionId: string
if (typeof data.region === "string") {
regionId = data.region
} else {
const region = await simpleRegionFactory(connection, data.region)
regionId = region.id
}
const address = await simpleAddressFactory(connection, data.shipping_address)
const id = data.id || `simple-cart-${Math.random() * 1000}`
const toSave = manager.create(Cart, {
id,
email:
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
region_id: regionId,
shipping_address_id: address.id,
})
const cart = await manager.save(toSave)
const shippingMethods = data.shipping_methods || []
for (const sm of shippingMethods) {
await simpleShippingMethodFactory(connection, { ...sm, cart_id: id })
}
const items = data.line_items
for (const item of items) {
await simpleLineItemFactory(connection, { ...item, cart_id: id })
}
return cart
}
@@ -0,0 +1,55 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
Discount,
DiscountRule,
DiscountRuleType,
AllocationType,
} from "@medusajs/medusa"
export type DiscountRuleFactoryData = {
type?: DiscountRuleType
value?: number
allocation?: AllocationType
}
export type DiscountFactoryData = {
id?: string
code?: string
is_dynamic?: boolean
rule?: DiscountRuleFactoryData
regions?: string[]
}
export const simpleDiscountFactory = async (
connection: Connection,
data: DiscountFactoryData = {},
seed?: number
): Promise<Discount> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const ruleData = data.rule ?? {}
const ruleToSave = manager.create(DiscountRule, {
type: ruleData.type ?? DiscountRuleType.PERCENTAGE,
value: ruleData.value ?? 10,
allocation: ruleData.allocation ?? AllocationType.TOTAL,
})
const dRule = await manager.save(ruleToSave)
const toSave = manager.create(Discount, {
id: data.id,
is_dynamic: data.is_dynamic ?? false,
is_disabled: false,
rule_id: dRule.id,
code: data.code ?? "TESTCODE",
regions: data.regions?.map((r) => ({ id: r })) || [],
})
const discount = await manager.save(toSave)
return discount
}
@@ -0,0 +1,84 @@
import { Connection } from "typeorm"
import faker from "faker"
import { LineItem, LineItemTaxLine } from "@medusajs/medusa"
type TaxLineFactoryData = {
rate: number
code: string
name: string
}
export type LineItemFactoryData = {
id?: string
cart_id?: string
order_id?: string
variant_id: string | null
title?: string
description?: string
thumbnail?: string
should_merge?: boolean
allow_discounts?: boolean
unit_price?: number
quantity?: number
fulfilled_quantity?: boolean
shipped_quantity?: boolean
returned_quantity?: boolean
tax_lines?: TaxLineFactoryData[]
}
export const simpleLineItemFactory = async (
connection: Connection,
data: LineItemFactoryData,
seed?: number
): Promise<LineItem> => {
if (
typeof data.cart_id === "undefined" &&
typeof data.order_id === "undefined"
) {
throw Error()
}
if (typeof seed !== "undefined") {
faker.seed(seed)
Math
}
const manager = connection.manager
const id = data.id || `simple-line-${Math.random() * 1000}`
const toSave = manager.create(LineItem, {
id,
cart_id: data.cart_id,
order_id: data.order_id,
title: data.title || faker.commerce.productName(),
description: data.description || "",
thumbnail: data.thumbnail || "",
should_merge:
typeof data.should_merge !== "undefined" ? data.should_merge : true,
allow_discounts:
typeof data.allow_discounts !== "undefined" ? data.allow_discounts : true,
unit_price: typeof data.unit_price !== "undefined" ? data.unit_price : 100,
variant_id: data.variant_id,
quantity: data.quantity || 1,
fulfilled_quantity: data.fulfilled_quantity || null,
shipped_quantity: data.shipped_quantity || null,
returned_quantity: data.returned_quantity || null,
})
const line = await manager.save(toSave)
if (typeof data.tax_lines !== "undefined") {
const taxLinesToSave = data.tax_lines.map((tl) =>
manager.create(LineItemTaxLine, {
item_id: id,
rate: tl.rate,
code: tl.code,
name: tl.name,
})
)
await manager.save(taxLinesToSave)
}
return line
}
@@ -0,0 +1,110 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
Customer,
Order,
PaymentStatus,
FulfillmentStatus,
} from "@medusajs/medusa"
import {
DiscountFactoryData,
simpleDiscountFactory,
} from "./simple-discount-factory"
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
import {
LineItemFactoryData,
simpleLineItemFactory,
} from "./simple-line-item-factory"
import {
AddressFactoryData,
simpleAddressFactory,
} from "./simple-address-factory"
import {
ShippingMethodFactoryData,
simpleShippingMethodFactory,
} from "./simple-shipping-method-factory"
export type OrderFactoryData = {
id?: string
payment_status?: PaymentStatus
fulfillment_status?: FulfillmentStatus
region?: RegionFactoryData | string
email?: string | null
currency_code?: string
tax_rate?: number | null
line_items?: LineItemFactoryData[]
discounts?: DiscountFactoryData[]
shipping_address?: AddressFactoryData
shipping_methods?: ShippingMethodFactoryData[]
}
export const simpleOrderFactory = async (
connection: Connection,
data: OrderFactoryData = {},
seed: number
): Promise<Order> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let currencyCode: string
let regionId: string
let taxRate: number
if (typeof data.region === "string") {
currencyCode = data.currency_code
regionId = data.region
taxRate = data.tax_rate
} else {
const region = await simpleRegionFactory(connection, data.region)
taxRate =
typeof data.tax_rate !== "undefined" ? data.tax_rate : region.tax_rate
currencyCode = region.currency_code
regionId = region.id
}
const address = await simpleAddressFactory(connection, data.shipping_address)
const customerToSave = manager.create(Customer, {
email:
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
})
const customer = await manager.save(customerToSave)
let discounts = []
if (typeof data.discounts !== "undefined") {
discounts = await Promise.all(
data.discounts.map((d) => simpleDiscountFactory(connection, d, seed))
)
}
const id = data.id || `simple-order-${Math.random() * 1000}`
const toSave = manager.create(Order, {
id,
discounts,
payment_status: data.payment_status ?? PaymentStatus.AWAITING,
fulfillment_status:
data.fulfillment_status ?? FulfillmentStatus.NOT_FULFILLED,
customer_id: customer.id,
email: customer.email,
region_id: regionId,
currency_code: currencyCode,
tax_rate: taxRate,
shipping_address_id: address.id,
})
const order = await manager.save(toSave)
const shippingMethods = data.shipping_methods || []
for (const sm of shippingMethods) {
await simpleShippingMethodFactory(connection, { ...sm, order_id: order.id })
}
const items = data.line_items
for (const item of items) {
await simpleLineItemFactory(connection, { ...item, order_id: id })
}
return order
}
@@ -0,0 +1,99 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
ShippingProfileType,
ShippingProfile,
Product,
ProductType,
ProductOption,
} from "@medusajs/medusa"
import {
simpleProductVariantFactory,
ProductVariantFactoryData,
} from "./simple-product-variant-factory"
export type ProductFactoryData = {
id?: string
is_giftcard?: boolean
title?: string
type?: string
options?: { id: string; title: string }[]
variants?: ProductVariantFactoryData[]
}
export const simpleProductFactory = async (
connection: Connection,
data: ProductFactoryData = {},
seed?: number
): Promise<Product> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.DEFAULT,
})
const gcProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.GIFT_CARD,
})
let typeId: string
if (typeof data.type !== "undefined") {
const toSave = manager.create(ProductType, {
value: data.type,
})
const res = await manager.save(toSave)
typeId = res.id
}
const prodId = data.id || `simple-product-${Math.random() * 1000}`
const toSave = manager.create(Product, {
id: prodId,
type_id: typeId,
title: data.title || faker.commerce.productName(),
is_giftcard: data.is_giftcard || false,
discountable: !data.is_giftcard,
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
})
const product = await manager.save(toSave)
const optionId = `${prodId}-option`
const options = data.options || [{ id: optionId, title: "Size" }]
for (const o of options) {
await manager.insert(ProductOption, {
id: o.id,
title: o.title,
product_id: prodId,
})
}
const variants = data.variants || [
{
id: `simple-test-variant-${Math.random() * 1000}`,
title: "Test",
product_id: prodId,
prices: [{ currency: "usd", amount: 100 }],
options: [{ option_id: optionId, value: "Large" }],
},
]
for (const pv of variants) {
const factoryData = {
...pv,
product_id: prodId,
}
if (typeof pv.options === "undefined") {
factoryData.options = [
{ option_id: optionId, value: faker.commerce.productAdjective() },
]
}
await simpleProductVariantFactory(connection, factoryData)
}
return product
}
@@ -0,0 +1,48 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ProductTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ProductTaxRateFactoryData = {
product_id: string
rate: RateFactoryData | string
}
export const simpleProductTaxRateFactory = async (
connection: Connection,
data: ProductTaxRateFactoryData,
seed?: number
): Promise<ProductTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ProductTaxRate, {
product_id: data.product_id,
rate_id: rateId,
})
return await manager.save(toSave)
}
@@ -0,0 +1,48 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ProductTypeTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ProductTypeTaxRateFactoryData = {
product_type_id: string
rate: RateFactoryData | string
}
export const simpleProductTypeTaxRateFactory = async (
connection: Connection,
data: ProductTypeTaxRateFactoryData,
seed?: number
): Promise<ProductTypeTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ProductTypeTaxRate, {
product_type_id: data.product_type_id,
rate_id: rateId,
})
return await manager.save(toSave)
}
@@ -0,0 +1,64 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
ProductOptionValue,
ProductVariant,
MoneyAmount,
} from "@medusajs/medusa"
export type ProductVariantFactoryData = {
product_id: string
id?: string
is_giftcard?: boolean
inventory_quantity?: number
title?: string
options?: { option_id: string; value: string }[]
prices?: { currency: string; amount: number }[]
}
export const simpleProductVariantFactory = async (
connection: Connection,
data: ProductVariantFactoryData,
seed?: number
): Promise<ProductVariant> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const id = data.id || `simple-variant-${Math.random() * 1000}`
const toSave = manager.create(ProductVariant, {
id,
product_id: data.product_id,
inventory_quantity:
typeof data.inventory_quantity !== "undefined"
? data.inventory_quantity
: 10,
title: data.title || faker.commerce.productName(),
})
const variant = await manager.save(toSave)
const options = data.options || [{ option_id: "test-option", value: "Large" }]
for (const o of options) {
await manager.insert(ProductOptionValue, {
id: `${o.value}-${o.option_id}`,
value: o.value,
variant_id: id,
option_id: o.option_id,
})
}
const prices = data.prices || [{ currency: "usd", amount: 100 }]
for (const p of prices) {
await manager.insert(MoneyAmount, {
id: `${p.currency}-${p.amount}-${Math.random()}`,
variant_id: id,
currency_code: p.currency,
amount: p.amount,
})
}
return variant
}
@@ -0,0 +1,47 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Region } from "@medusajs/medusa"
export type RegionFactoryData = {
id?: string
name?: string
currency_code?: string
tax_rate?: number
countries?: string[]
automatic_taxes?: boolean
}
export const simpleRegionFactory = async (
connection: Connection,
data: RegionFactoryData = {},
seed?: number
): Promise<Region> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const regionId = data.id || `simple-region-${Math.random() * 1000}`
const r = manager.create(Region, {
id: regionId,
name: data.name || "Test Region",
currency_code: data.currency_code || "usd",
tax_rate: data.tax_rate || 0,
payment_providers: [{ id: "test-pay" }],
automatic_taxes:
typeof data.automatic_taxes !== "undefined" ? data.automatic_taxes : true,
})
const region = await manager.save(r)
const countries = data.countries || ["us"]
for (const cc of countries) {
await manager.query(
`UPDATE "country" SET region_id='${regionId}' WHERE iso_2 = '${cc}'`
)
}
return region
}
@@ -0,0 +1,68 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ShippingMethodTaxLine, ShippingMethod } from "@medusajs/medusa"
import {
ShippingOptionFactoryData,
simpleShippingOptionFactory,
} from "./simple-shipping-option-factory"
export type ShippingMethodFactoryData = {
id?: string
cart_id?: string
order_id?: string
data?: object
price?: number
shipping_option: string | ShippingOptionFactoryData
tax_lines?: ShippingMethodTaxLine[]
}
export const simpleShippingMethodFactory = async (
connection: Connection,
data: ShippingMethodFactoryData,
seed?: number
): Promise<ShippingMethod> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let shippingOptionId: string
if (typeof data.shipping_option === "string") {
shippingOptionId = data.shipping_option
} else {
const option = await simpleShippingOptionFactory(
connection,
data.shipping_option
)
shippingOptionId = option.id
}
const id = data.id || `simple-sm-${Math.random() * 1000}`
const toSave = manager.create(ShippingMethod, {
id,
cart_id: data.cart_id,
order_id: data.order_id,
shipping_option_id: shippingOptionId,
data: data.data || {},
price: typeof data.price !== "undefined" ? data.price : 500,
})
const shippingMethod = await manager.save(toSave)
if (typeof data.tax_lines !== "undefined") {
const taxLinesToSave = data.tax_lines.map((tl) =>
manager.create(ShippingMethodTaxLine, {
shipping_method_id: shippingMethod.id,
rate: tl.rate,
code: tl.code || "default",
name: tl.name || "default",
})
)
await manager.save(taxLinesToSave)
}
return shippingMethod
}
@@ -0,0 +1,49 @@
import { Connection } from "typeorm"
import faker from "faker"
import {
ShippingOptionPriceType,
ShippingProfile,
ShippingOption,
ShippingProfileType,
} from "@medusajs/medusa"
export type ShippingOptionFactoryData = {
name?: string
region_id: string
is_return?: boolean
is_giftcard?: boolean
price?: number
}
export const simpleShippingOptionFactory = async (
connection: Connection,
data: ShippingOptionFactoryData,
seed?: number
): Promise<ShippingOption> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.DEFAULT,
})
const gcProfile = await manager.findOne(ShippingProfile, {
type: ShippingProfileType.GIFT_CARD,
})
const created = manager.create(ShippingOption, {
id: `simple-so-${Math.random() * 1000}`,
name: data.name || "Test Method",
is_return: data.is_return ?? false,
region_id: data.region_id,
provider_id: "test-ful",
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
price_type: ShippingOptionPriceType.FLAT_RATE,
data: {},
amount: typeof data.price !== "undefined" ? data.price : 500,
})
const option = await manager.save(created)
return option
}
@@ -0,0 +1,48 @@
import { Connection } from "typeorm"
import faker from "faker"
import { ShippingTaxRate, TaxRate } from "@medusajs/medusa"
type RateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export type ShippingTaxRateFactoryData = {
shipping_option_id: string
rate: RateFactoryData | string
}
export const simpleShippingTaxRateFactory = async (
connection: Connection,
data: ShippingTaxRateFactoryData,
seed?: number
): Promise<ShippingTaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
let rateId: string
if (typeof data.rate === "string") {
rateId = data.rate
} else {
const newRate = manager.create(TaxRate, {
region_id: data.rate.region_id,
rate: data.rate.rate,
code: data.rate.code || "sales_tax",
name: data.rate.name || "Sales Tax",
})
const rate = await manager.save(newRate)
rateId = rate.id
}
const toSave = manager.create(ShippingTaxRate, {
shipping_option_id: data.shipping_option_id,
rate_id: rateId,
})
return await manager.save(toSave)
}
@@ -0,0 +1,24 @@
import { Connection } from "typeorm"
import faker from "faker"
import { Store } from "@medusajs/medusa"
export type StoreFactoryData = {
swap_link_template?: string
}
export const simpleStoreFactory = async (
connection: Connection,
data: StoreFactoryData = {},
seed?: number
): Promise<Store> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const store = await manager.findOne(Store)
store.swap_link_template = data.swap_link_template ?? "something/{cart_id}"
return await manager.save(store)
}
@@ -0,0 +1,32 @@
import { Connection } from "typeorm"
import faker from "faker"
import { TaxRate } from "@medusajs/medusa"
export type TaxRateFactoryData = {
region_id: string
rate?: number | null
code?: string
name?: string
}
export const simpleTaxRateFactory = async (
connection: Connection,
data: TaxRateFactoryData,
seed?: number
): Promise<TaxRate> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
}
const manager = connection.manager
const toSave = manager.create(TaxRate, {
region_id: data.region_id,
rate:
data.rate ?? faker.datatype.number({ min: 0, max: 100, precision: 2 }),
code: data.code || faker.random.word(),
name: data.name || faker.random.words(2),
})
return await manager.save(toSave)
}
@@ -0,0 +1,18 @@
const Scrypt = require("scrypt-kdf")
const { User } = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
const buf = await Scrypt.kdf("secret_password", { logN: 1, r: 1, p: 1 })
const password_hash = buf.toString("base64")
await manager.insert(User, {
id: "admin_user",
email: "admin@medusa.js",
api_token: "test_token",
role: "admin",
password_hash,
...data,
})
}
@@ -0,0 +1,100 @@
const { useApi } = require("../../helpers/use-api")
const header = {
headers: {
authorization: "Bearer test_token",
},
}
const resolveCall = async (path, payload, header) => {
const api = useApi()
let res
try {
const resp = await api.post(path, payload, header)
res = resp.status
} catch (expectedException) {
try {
res = expectedException.response.status
} catch (_) {
console.error(expectedException)
}
}
return res
}
const determineFail = (actual, expected, path) => {
if (expected !== actual) {
console.log(`failed at path : ${path}`)
}
expect(actual).toEqual(expected)
}
/**
* Allows you to wrap a Call function so that you may reuse some input values.
* @param {Function} fun - the function to call with partial information
* @param {Object} input - the constant input which we want to supply now
* @returns
*/
module.exports.partial = function (fun, input = {}) {
return async (remaining) => await fun({ ...remaining, ...input })
}
/**
* Allows you to assert a specific code result from a POST call.
* @param {Object} input - the information needed to make the call
* (path & payload) and the expected code (code)
*/
module.exports.expectPostCallToReturn = async function (
input = {
code,
path,
payload: {},
}
) {
const res = await resolveCall(input.path, input.payload, header)
determineFail(res, input.code, input.path)
}
/**
* Allows you to assert a specific code result from multiple POST
* calls.
* @param {Object} input - the collection of objects to execute
* calls from (col), a function which yields the path (pathf),
* and another one which provides the payload (payloadf), as
* well as the code (code) which we want to assert.
*/
module.exports.expectAllPostCallsToReturn = async function (
input = {
code,
col,
pathf,
payloadf,
}
) {
for (const i of input.col) {
const res = await resolveCall(
input.pathf(i),
input.payloadf ? input.payloadf(i) : {},
header
)
determineFail(res, input.code, input.pathf(i))
}
}
/**
* Allows you to retrieve a specific object the response
* from get call,
* and simultaneously assert that the call was successful.
* @param {Object} param0 - contains the path which to
* call (path), and the object within the response.data (get)
* we want to retrieve.
* @returns {Object} found within response.data corresponding
* to the get parameter provided.
*/
module.exports.callGet = async function ({ path, get }) {
const api = useApi()
const res = await api.get(path, header)
determineFail(res.status, 200, path)
return res?.data[get]
}
@@ -0,0 +1,536 @@
const {
Customer,
Region,
Cart,
DiscountRule,
Discount,
ShippingProfile,
ShippingOption,
ShippingMethod,
Address,
Product,
ProductVariant,
MoneyAmount,
LineItem,
Payment,
PaymentSession,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
new Date()
)
const tomorrow = ((today) => new Date(today.setDate(today.getDate() + 1)))(
new Date()
)
const tenDaysAgo = ((today) => new Date(today.setDate(today.getDate() - 10)))(
new Date()
)
const tenDaysFromToday = ((today) =>
new Date(today.setDate(today.getDate() + 10)))(new Date())
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default",
})
const gcProfile = await manager.findOne(ShippingProfile, {
type: "gift_card",
})
await manager.insert(Address, {
id: "test-general-address",
first_name: "superman",
country_code: "us",
})
const r = manager.create(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
// Region with multiple countries
const regionWithMultipleCoutries = manager.create(Region, {
id: "test-region-multiple",
name: "Test Region",
currency_code: "eur",
tax_rate: 0,
})
await manager.save(regionWithMultipleCoutries)
await manager.query(
`UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'no'`
)
await manager.query(
`UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'dk'`
)
const freeRule = manager.create(DiscountRule, {
id: "free-shipping-rule",
description: "Free shipping rule",
type: "free_shipping",
value: 100,
allocation: "total",
})
const freeDisc = manager.create(Discount, {
id: "free-shipping",
code: "FREE_SHIPPING",
is_dynamic: false,
is_disabled: false,
})
freeDisc.regions = [r]
freeDisc.rule = freeRule
await manager.save(freeDisc)
const tenPercentRule = manager.create(DiscountRule, {
id: "tenpercent-rule",
description: "Ten percent rule",
type: "percentage",
value: 10,
allocation: "total",
})
const tenPercent = manager.create(Discount, {
id: "10Percent",
code: "10PERCENT",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
tenPercent.regions = [r]
tenPercent.rule = tenPercentRule
await manager.save(tenPercent)
const dUsageLimit = await manager.create(Discount, {
id: "test-discount-usage-limit",
code: "SPENT",
is_dynamic: false,
is_disabled: false,
usage_limit: 10,
usage_count: 10,
})
const drUsage = await manager.create(DiscountRule, {
id: "test-discount-rule-usage-limit",
description: "Created",
type: "fixed",
value: 10000,
allocation: "total",
})
dUsageLimit.rule = drUsage
dUsageLimit.regions = [r]
await manager.save(dUsageLimit)
const d = await manager.create(Discount, {
id: "test-discount",
code: "CREATED",
is_dynamic: false,
is_disabled: false,
})
const dr = await manager.create(DiscountRule, {
id: "test-discount-rule",
description: "Created",
type: "fixed",
value: 10000,
allocation: "total",
})
d.rule = dr
d.regions = [r]
await manager.save(d)
const usedDiscount = manager.create(Discount, {
id: "used-discount",
code: "USED",
is_dynamic: false,
is_disabled: false,
usage_limit: 1,
usage_count: 1,
})
await manager.save(usedDiscount)
const expiredRule = manager.create(DiscountRule, {
id: "expiredRule",
description: "expired rule",
type: "fixed",
value: 100,
allocation: "total",
})
const expiredDisc = manager.create(Discount, {
id: "expiredDisc",
code: "EXP_DISC",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: yesterday,
})
expiredDisc.regions = [r]
expiredDisc.rule = expiredRule
await manager.save(expiredDisc)
const prematureRule = manager.create(DiscountRule, {
id: "prematureRule",
description: "premature rule",
type: "fixed",
value: 100,
allocation: "total",
})
const prematureDiscount = manager.create(Discount, {
id: "prematureDiscount",
code: "PREM_DISC",
is_dynamic: false,
is_disabled: false,
starts_at: tomorrow,
ends_at: tenDaysFromToday,
})
prematureDiscount.regions = [r]
prematureDiscount.rule = prematureRule
await manager.save(prematureDiscount)
const invalidDynamicRule = manager.create(DiscountRule, {
id: "invalidDynamicRule",
description: "invalidDynamic rule",
type: "fixed",
value: 100,
allocation: "total",
})
const invalidDynamicDiscount = manager.create(Discount, {
id: "invalidDynamicDiscount",
code: "INV_DYN_DISC",
is_dynamic: true,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
valid_duration: "P1D", // one day
})
invalidDynamicDiscount.regions = [r]
invalidDynamicDiscount.rule = invalidDynamicRule
await manager.save(invalidDynamicDiscount)
const DynamicRule = manager.create(DiscountRule, {
id: "DynamicRule",
description: "Dynamic rule",
type: "fixed",
value: 10000,
allocation: "total",
})
const DynamicDiscount = manager.create(Discount, {
id: "DynamicDiscount",
code: "DYN_DISC",
is_dynamic: true,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
valid_duration: "P1M", //one month
})
DynamicDiscount.regions = [r]
DynamicDiscount.rule = DynamicRule
await manager.save(DynamicDiscount)
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
)
await manager.insert(Customer, {
id: "test-customer",
email: "test@email.com",
})
await manager.insert(Customer, {
id: "test-customer-2",
email: "test-2@email.com",
})
await manager.insert(Customer, {
id: "some-customer",
email: "some-customer@email.com",
})
await manager.insert(ShippingOption, {
id: "test-option",
name: "test-option",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOption, {
id: "gc-option",
name: "Digital copy",
provider_id: "test-ful",
region_id: "test-region",
profile_id: gcProfile.id,
price_type: "flat_rate",
amount: 0,
data: {},
})
await manager.insert(ShippingOption, {
id: "test-option-2",
name: "test-option-2",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 500,
data: {},
})
await manager.insert(Product, {
id: "giftcard-product",
title: "Giftcard",
is_giftcard: true,
discountable: false,
profile_id: gcProfile.id,
options: [{ id: "denom", title: "Denomination" }],
})
await manager.insert(ProductVariant, {
id: "giftcard-denom",
title: "1000",
product_id: "giftcard-product",
inventory_quantity: 1,
options: [
{
option_id: "denom",
value: "1000",
},
],
})
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
})
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-2",
title: "test variant 2",
product_id: "test-product",
inventory_quantity: 0,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
amount: 1000,
})
await manager.save(ma)
const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma2)
const ma3 = manager.create(MoneyAmount, {
variant_id: "giftcard-denom",
currency_code: "usd",
amount: 1000,
})
await manager.save(ma3)
const cart = manager.create(Cart, {
id: "test-cart",
customer_id: "some-customer",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cart)
const cart2 = manager.create(Cart, {
id: "test-cart-2",
customer_id: "some-customer",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
})
const swapCart = manager.create(Cart, {
id: "swap-cart",
type: "swap",
customer_id: "some-customer",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
metadata: {
swap_id: "test-swap",
},
})
const pay = manager.create(Payment, {
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
})
await manager.save(pay)
cart2.payment = pay
await manager.save(cart2)
const swapPay = manager.create(Payment, {
id: "test-swap-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
})
await manager.save(pay)
await manager.save(swapPay)
cart2.payment = pay
swapCart.payment = swapPay
await manager.save(cart2)
await manager.save(swapCart)
await manager.insert(PaymentSession, {
id: "test-session",
cart_id: "test-cart-2",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
await manager.insert(PaymentSession, {
id: "test-swap-session",
cart_id: "swap-cart",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
await manager.insert(ShippingMethod, {
id: "test-method",
shipping_option_id: "test-option",
cart_id: "test-cart",
price: 1000,
data: {},
})
const li = manager.create(LineItem, {
id: "test-item",
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
cart_id: "test-cart-2",
})
await manager.save(li)
const cart3 = manager.create(Cart, {
id: "test-cart-3",
customer_id: "some-customer",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
})
await manager.save(cart3)
await manager.insert(ShippingMethod, {
id: "test-method-2",
shipping_option_id: "test-option",
cart_id: "test-cart-3",
price: 0,
data: {},
})
const li2 = manager.create(LineItem, {
id: "test-item-2",
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
cart_id: "test-cart-3",
})
await manager.save(li2)
}
@@ -0,0 +1,118 @@
const {
ClaimOrder,
Order,
LineItem,
Fulfillment,
Return,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
let orderWithClaim = manager.create(Order, {
id: "order-with-claim",
customer_id: "test-customer",
email: "test@email.com",
payment_status: "captured",
fulfillment_status: "fulfilled",
billing_address: {
id: "test-billing-address",
first_name: "lebron",
},
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
tax_rate: 0,
discounts: [],
payments: [
{
id: "test-payment-for-claim-order",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
],
items: [],
...data,
})
await manager.save(orderWithClaim)
const li = manager.create(LineItem, {
id: "test-item-co-2",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: orderWithClaim.id,
})
await manager.save(li)
const li2 = manager.create(LineItem, {
id: "test-item-co-3",
fulfilled_quantity: 4,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 4,
variant_id: "test-variant",
order_id: orderWithClaim.id,
})
await manager.save(li2)
const claimWithFulfillment = manager.create(ClaimOrder, {
id: "claim-w-f",
type: "replace",
payment_status: "na",
fulfillment_status: "not_fulfilled",
order_id: "order-with-claim",
...data,
})
const ful1 = manager.create(Fulfillment, {
id: "fulfillment-co-1",
data: {},
provider_id: "test-ful",
})
const ful2 = manager.create(Fulfillment, {
id: "fulfillment-co-2",
data: {},
provider_id: "test-ful",
})
claimWithFulfillment.fulfillments = [ful1, ful2]
await manager.save(claimWithFulfillment)
const claimWithReturn = manager.create(ClaimOrder, {
id: "claim-w-r",
type: "replace",
payment_status: "na",
fulfillment_status: "not_fulfilled",
order_id: "order-with-claim",
...data,
})
await manager.save(claimWithReturn)
await manager.insert(Return, {
id: "return-id-2",
claim_order_id: "claim-w-r",
status: "requested",
refund_amount: 0,
data: {},
})
}
@@ -0,0 +1,33 @@
const { Customer, Address } = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
await manager.insert(Customer, {
id: "test-customer-1",
email: "test1@email.com",
})
await manager.insert(Customer, {
id: "test-customer-2",
email: "test2@email.com",
})
await manager.insert(Customer, {
id: "test-customer-3",
email: "test3@email.com",
})
await manager.insert(Customer, {
id: "test-customer-has_account",
email: "test4@email.com",
has_account: true,
})
await manager.insert(Address, {
id: "test-address",
first_name: "Lebron",
last_name: "James",
customer_id: "test-customer-1",
})
}
@@ -0,0 +1,35 @@
const {
ShippingProfile,
Region,
Discount,
DiscountRule,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
await manager.insert(Region, {
id: "test-region-2",
name: "Test Region 2",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
}
@@ -0,0 +1,261 @@
const {
ShippingProfile,
Customer,
MoneyAmount,
ShippingOption,
ShippingOptionRequirement,
Product,
ProductVariant,
Region,
Address,
Cart,
PaymentSession,
DraftOrder,
Discount,
DiscountRule,
Payment,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default",
})
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
})
await manager.insert(Address, {
id: "oli-shipping",
first_name: "oli",
last_name: "test",
country_code: "us",
})
await manager.insert(Product, {
id: "test-product-2",
title: "test product 2",
profile_id: defaultProfile.id,
options: [{ id: "test-option-color", title: "Color" }],
})
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-2",
title: "test variant-2",
product_id: "test-product-2",
inventory_quantity: 4,
options: [
{
option_id: "test-option-color",
value: "Color",
},
],
})
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma)
const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2",
currency_code: "usd",
amount: 10000,
})
await manager.save(ma2)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
await manager.insert(Region, {
id: "test-region-2",
name: "Test Region 2",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
await manager.insert(DiscountRule, {
id: "discount_rule_id",
description: "test description",
value: 10,
allocation: "total",
type: "percentage",
})
const d = manager.create(Discount, {
id: "test-discount",
code: "TEST",
is_dynamic: false,
is_disabled: false,
rule_id: "discount_rule_id",
})
d.regions = [
{
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
},
]
await manager.save(d)
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
)
await manager.query(
`UPDATE "country" SET region_id='test-region-2' WHERE iso_2 = 'de'`
)
await manager.insert(Customer, {
id: "oli-test",
email: "oli@test.dk",
})
await manager.insert(Customer, {
id: "lebron-james",
email: "lebron@james.com",
})
await manager.insert(ShippingOption, {
id: "test-option",
name: "test-option",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOption, {
id: "test-option-req",
name: "test-option-req",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOptionRequirement, {
id: "option-req",
shipping_option_id: "test-option-req",
type: "min_subtotal",
amount: 10,
})
const c = manager.create(Cart, {
id: "test-cart",
customer_id: "oli-test",
email: "oli@test.dk",
shipping_address_id: "oli-shipping",
region_id: "test-region",
currency_code: "usd",
payment_sessions: [],
items: [
{
id: "test-item",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
},
],
type: "draft_order",
metadata: { draft_order_id: "test-draft-order" },
})
const pay = manager.create(Payment, {
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
})
await manager.save(pay)
c.payment = pay
await manager.save(c)
await manager.insert(PaymentSession, {
id: "test-session",
cart_id: "test-cart",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
const draftOrder = manager.create(DraftOrder, {
id: "test-draft-order",
status: "open",
display_id: 4,
cart_id: "test-cart",
customer_id: "oli-test",
items: [
{
id: "test-item",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
},
],
email: "oli@test.dk",
region_id: "test-region",
discounts: [],
...data,
})
await manager.save(draftOrder)
}
@@ -0,0 +1,396 @@
const {
Customer,
Discount,
DiscountRule,
LineItem,
MoneyAmount,
Order,
Payment,
Product,
ProductVariant,
Region,
ShippingMethod,
ShippingOption,
ShippingProfile,
Swap,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default",
})
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
})
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-2",
title: "Swap product",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Large",
},
],
})
const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma2)
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
)
await manager.insert(Customer, {
id: "test-customer",
email: "test@email.com",
})
await manager.insert(ShippingOption, {
id: "test-option",
name: "test-option",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOption, {
id: "test-return-option",
name: "Test ret",
profile_id: defaultProfile.id,
region_id: "test-region",
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 1000,
is_return: true,
})
const order = manager.create(Order, {
id: "test-order",
customer_id: "test-customer",
email: "test@email.com",
payment_status: "captured",
fulfillment_status: "fulfilled",
billing_address: {
id: "test-billing-address",
first_name: "lebron",
},
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
tax_rate: 0,
discounts: [
{
id: "test-discount",
code: "TEST134",
is_dynamic: false,
rule: {
id: "test-rule",
description: "Test Discount",
type: "percentage",
value: 10,
allocation: "total",
},
is_disabled: false,
regions: [
{
id: "test-region",
},
],
},
],
payments: [
{
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
],
items: [],
...data,
})
await manager.save(order)
const li = manager.create(LineItem, {
id: "test-item",
fulfilled_quantity: 1,
returned_quantity: 0,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: "test-order",
})
await manager.save(li)
await manager.insert(ShippingMethod, {
id: "test-method",
order_id: "test-order",
shipping_option_id: "test-option",
price: 1000,
data: {},
})
const orderTemplate = () => {
return {
customer_id: "test-customer",
email: "test@gmail.com",
payment_status: "not_paid",
fulfillment_status: "not_fulfilled",
region_id: "test-region",
currency_code: "usd",
tax_rate: 0,
...data,
}
}
const paymentTemplate = () => {
return {
amount: 10000,
currency_code: "usd",
provider_id: "test-pay",
data: {},
}
}
const orderWithClaim = manager.create(Order, {
id: "test-order-w-c",
claims: [
{
type: "replace",
id: "claim-1",
payment_status: "na",
fulfillment_status: "not_fulfilled",
payment_provider: "test-pay",
},
],
...orderTemplate(),
})
await manager.save(orderWithClaim)
await manager.insert(Payment, {
order_id: "test-order-w-c",
id: "o-pay1",
...paymentTemplate(),
})
const orderWithSwap = manager.create(Order, {
id: "test-order-w-s",
...orderTemplate(),
})
await manager.save(orderWithSwap)
await manager.insert(Payment, {
order_id: "test-order-w-s",
id: "o-pay2",
...paymentTemplate(),
})
const swap1 = manager.create(Swap, {
id: "swap-1",
order_id: "test-order-w-s",
fulfillment_status: "not_fulfilled",
payment_status: "not_paid",
})
const pay1 = manager.create(Payment, {
id: "pay1",
...paymentTemplate(),
})
swap1.payment = pay1
const swap2 = manager.create(Swap, {
id: "swap-2",
order_id: "test-order-w-s",
fulfillment_status: "not_fulfilled",
payment_status: "not_paid",
})
const pay2 = manager.create(Payment, {
id: "pay2",
...paymentTemplate(),
})
swap2.payment = pay2
await manager.save(swap1)
await manager.save(swap2)
const orderWithFulfillment = manager.create(Order, {
id: "test-order-w-f",
fulfillments: [
{
id: "fulfillment-on-order-1",
data: {},
provider_id: "test-ful",
},
{
id: "fulfillment-on-order-2",
data: {},
provider_id: "test-ful",
},
],
...orderTemplate(),
})
await manager.save(orderWithFulfillment)
await manager.insert(Payment, {
order_id: "test-order-w-f",
id: "o-pay3",
...paymentTemplate(),
})
const orderWithReturn = manager.create(Order, {
id: "test-order-w-r",
returns: [
{
id: "return-on-order-1",
refund_amount: 0,
},
{
id: "return-on-order-2",
refund_amount: 0,
},
],
...orderTemplate(),
})
await manager.save(orderWithReturn)
await manager.insert(Payment, {
order_id: "test-order-w-r",
id: "o-pay4",
...paymentTemplate(),
})
const drule = manager.create(DiscountRule, {
id: "test-rule",
description: "Test Discount",
type: "percentage",
value: 10,
allocation: "total",
})
const discount = manager.create(Discount, {
id: "test-discount-o",
code: "TEST1234",
is_dynamic: false,
rule: drule,
is_disabled: false,
regions: [
{
id: "test-region",
},
],
})
await manager.save(discount)
const payment = manager.create(Payment, {
id: "test-payment-d",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
captured_at: new Date(),
data: {},
})
const discountedOrder = manager.create(Order, {
id: "discount-order",
customer_id: "test-customer",
email: "test-discount@email.com",
payment_status: "captured",
fulfillment_status: "fulfilled",
discounts: [discount],
billing_address: {
id: "test-discount-billing-address",
first_name: "lebron",
},
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
tax_rate: 0,
payments: [payment],
items: [],
...data,
})
await manager.save(discountedOrder)
const dli = manager.create(LineItem, {
id: "test-item-1",
fulfilled_quantity: 1,
returned_quantity: 0,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: "discount-order",
})
await manager.save(dli)
}
@@ -0,0 +1,286 @@
const {
ProductCollection,
ProductTag,
ProductType,
ProductOption,
Region,
Product,
ShippingProfile,
ProductVariant,
Image,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default",
})
const coll = manager.create(ProductCollection, {
id: "test-collection",
handle: "test-collection",
title: "Test collection",
})
await manager.save(coll)
const coll1 = manager.create(ProductCollection, {
id: "test-collection1",
handle: "test-collection1",
title: "Test collection 1",
})
await manager.save(coll1)
const coll2 = manager.create(ProductCollection, {
id: "test-collection2",
handle: "test-collection2",
title: "Test collection 2",
})
await manager.save(coll2)
const tag = manager.create(ProductTag, {
id: "tag1",
value: "123",
})
await manager.save(tag)
const tag3 = manager.create(ProductTag, {
id: "tag3",
value: "123",
})
await manager.save(tag3)
const tag4 = manager.create(ProductTag, {
id: "tag4",
value: "123",
})
await manager.save(tag4)
const type = manager.create(ProductType, {
id: "test-type",
value: "test-type",
})
await manager.save(type)
const image = manager.create(Image, {
id: "test-image",
url: "test-image.png",
})
await manager.save(image)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
const p = manager.create(Product, {
id: "test-product",
handle: "test-product",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-description",
collection_id: "test-collection",
type: { id: "test-type", value: "test-type" },
tags: [
{ id: "tag1", value: "123" },
{ tag: "tag2", value: "456" },
],
})
p.images = [image]
await manager.save(p)
await manager.save(ProductOption, {
id: "test-option",
title: "test-option",
product_id: "test-product",
})
const variant1 = await manager.create(ProductVariant, {
id: "test-variant",
inventory_quantity: 10,
title: "Test variant",
variant_rank: 0,
sku: "test-sku",
ean: "test-ean",
upc: "test-upc",
barcode: "test-barcode",
product_id: "test-product",
prices: [{ id: "test-price", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant1)
const variant2 = await manager.create(ProductVariant, {
id: "test-variant_1",
inventory_quantity: 10,
title: "Test variant rank (1)",
variant_rank: 2,
sku: "test-sku1",
ean: "test-ean1",
upc: "test-upc1",
barcode: "test-barcode 1",
product_id: "test-product",
prices: [{ id: "test-price1", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-1",
value: "Default variant 1",
option_id: "test-option",
},
],
})
await manager.save(variant2)
const variant3 = await manager.create(ProductVariant, {
id: "test-variant_2",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 1,
sku: "test-sku2",
ean: "test-ean2",
upc: "test-upc2",
product_id: "test-product",
prices: [{ id: "test-price2", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-2",
value: "Default variant 2",
option_id: "test-option",
},
],
})
await manager.save(variant3)
const p1 = manager.create(Product, {
id: "test-product1",
handle: "test-product1",
title: "Test product1",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
type: { id: "test-type", value: "test-type" },
tags: [
{ id: "tag1", value: "123" },
{ tag: "tag2", value: "456" },
],
})
await manager.save(p1)
const variant4 = await manager.create(ProductVariant, {
id: "test-variant_3",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 1,
sku: "test-sku3",
ean: "test-ean3",
upc: "test-upc3",
product_id: "test-product1",
prices: [{ id: "test-price3", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-3",
value: "Default variant 3",
option_id: "test-option",
},
],
})
await manager.save(variant4)
const variant5 = await manager.create(ProductVariant, {
id: "test-variant_4",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 0,
sku: "test-sku4",
ean: "test-ean4",
upc: "test-upc4",
product_id: "test-product1",
prices: [{ id: "test-price4", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-4",
value: "Default variant 4",
option_id: "test-option",
},
],
})
await manager.save(variant5)
const product1 = manager.create(Product, {
id: "test-product_filtering_1",
handle: "test-product_filtering_1",
title: "Test product filtering 1",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection1",
status: "proposed",
tags: [{ id: "tag3", value: "123" }],
})
await manager.save(product1)
const product2 = manager.create(Product, {
id: "test-product_filtering_2",
handle: "test-product_filtering_2",
title: "Test product filtering 2",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection2",
status: "published",
tags: [{ id: "tag3", value: "123" }],
})
await manager.save(product2)
const product3 = manager.create(Product, {
id: "test-product_filtering_3",
handle: "test-product_filtering_3",
title: "Test product filtering 3",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection1",
status: "draft",
tags: [{ id: "tag4", value: "1234" }],
})
await manager.save(product3)
const product4 = manager.create(Product, {
id: "test-product_filtering_4",
handle: "test-product_filtering_4",
title: "Test product filtering 4",
profile_id: defaultProfile.id,
description: "test-product-description",
status: "proposed",
deleted_at: new Date().toISOString(),
})
await manager.save(product4)
}
@@ -0,0 +1,83 @@
const {
Region,
ShippingProfile,
ShippingOption,
ShippingOptionRequirement,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
await manager.insert(Region, {
id: "region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default",
})
await manager.insert(ShippingOption, {
id: "test-out",
name: "Test out",
profile_id: defaultProfile.id,
region_id: "region",
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
is_return: false,
})
await manager.insert(ShippingOption, {
id: "test-option-req",
name: "With req",
profile_id: defaultProfile.id,
region_id: "region",
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
is_return: false,
})
await manager.insert(ShippingOption, {
id: "test-option-req-admin-only",
name: "With req",
profile_id: defaultProfile.id,
region_id: "region",
admin_only: true,
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
is_return: false,
})
await manager.insert(ShippingOption, {
id: "test-option-req-return",
name: "With req",
profile_id: defaultProfile.id,
region_id: "region",
is_return: true,
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
})
await manager.insert(ShippingOptionRequirement, {
id: "option-req",
shipping_option_id: "test-option-req",
type: "min_subtotal",
amount: 5,
})
await manager.insert(ShippingOptionRequirement, {
id: "option-req-2",
shipping_option_id: "test-option-req",
type: "max_subtotal",
amount: 10,
})
}
@@ -0,0 +1,434 @@
const {
Discount,
DiscountRule,
LineItem,
ShippingMethod,
Order,
Swap,
Cart,
Return,
} = require("@medusajs/medusa")
const {
CustomShippingOption,
} = require("@medusajs/medusa/dist/models/custom-shipping-option")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
let orderWithSwap = manager.create(Order, {
id: "order-with-swap",
customer_id: "test-customer",
email: "test@email.com",
payment_status: "captured",
fulfillment_status: "fulfilled",
billing_address: {
id: "test-billing-address",
first_name: "lebron",
},
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
tax_rate: 0,
discounts: [],
payments: [
{
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
],
items: [],
...data,
})
orderWithSwap = await manager.save(orderWithSwap)
const cart = manager.create(Cart, {
id: "test-cart-w-swap",
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: "test-region",
type: "swap",
metadata: {
swap_id: "test-swap",
parent_order_id: orderWithSwap.id,
},
})
await manager.save(cart)
const swap = manager.create(Swap, {
id: "test-swap",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
cart_id: "test-cart-w-swap",
payment: {
id: "test-payment-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swapped",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 9000,
quantity: 1,
variant_id: "test-variant-2",
cart_id: "test-cart-w-swap",
},
],
})
await manager.save(swap)
const cartWithCustomSo = manager.create(Cart, {
id: "test-cart-rma",
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: "test-region",
type: "swap",
metadata: {
swap_id: "test-swap",
parent_order_id: orderWithSwap.id,
},
})
await manager.save(cartWithCustomSo)
const liRma = manager.create(LineItem, {
id: "test-item-rma",
title: "Line Item RMA",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
cart_id: "test-cart-rma",
})
await manager.save(liRma)
manager.insert(CustomShippingOption, {
id: "cso-test",
cart_id: cartWithCustomSo.id,
price: 0,
shipping_option_id: "test-option",
})
const swapWithRMAMethod = manager.create(Swap, {
id: "test-swap-rma",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
cart_id: cartWithCustomSo.id,
payment: {
id: "test-payment-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swapped",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 9000,
quantity: 1,
variant_id: "test-variant-2",
cart_id: "test-cart",
},
],
})
await manager.save(swapWithRMAMethod)
const cartTemplate = async (cartId) => {
const cart = manager.create(Cart, {
id: cartId,
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: "test-region",
type: "swap",
metadata: {},
...data,
})
await manager.save(cart)
}
const swapTemplate = async (cartId) => {
await cartTemplate(cartId)
return {
order_id: orderWithSwap.id,
fulfillment_status: "fulfilled",
payment_status: "not_paid",
cart_id: cartId,
payment: {
amount: 5000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
...data,
}
}
const swapWithFulfillments = manager.create(Swap, {
id: "swap-w-f",
fulfillments: [
{
id: "fulfillment-1",
data: {},
provider_id: "test-ful",
},
{
id: "fulfillment-2",
data: {},
provider_id: "test-ful",
},
],
...(await swapTemplate("sc-w-f")),
})
await manager.save(swapWithFulfillments)
const swapWithReturn = manager.create(Swap, {
id: "swap-w-r",
return_order: {
id: "return-id",
status: "requested",
refund_amount: 0,
},
...(await swapTemplate("sc-w-r")),
})
await manager.save(swapWithReturn)
const li = manager.create(LineItem, {
id: "return-item-1",
fulfilled_quantity: 1,
title: "Return Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: orderWithSwap.id,
cart_id: cart.id,
})
await manager.save(li)
const li2 = manager.create(LineItem, {
id: "test-item-many",
fulfilled_quantity: 4,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 4,
variant_id: "test-variant",
order_id: orderWithSwap.id,
})
await manager.save(li2)
const swapReturn = await manager.create(Return, {
swap_id: swap.id,
order_id: orderWithSwap.id,
item_id: li.id,
refund_amount: li.quantity * li.unit_price,
})
await manager.save(swapReturn)
const return_item1 = manager.create(LineItem, {
...li,
unit_price: -1 * li.unit_price,
})
await manager.save(return_item1)
await manager.insert(ShippingMethod, {
id: "another-test-method",
shipping_option_id: "test-option",
cart_id: "test-cart-w-swap",
price: 1000,
data: {},
})
const swapOnSwap = manager.create(Swap, {
id: "swap-on-swap",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
return_order: {
id: "return-on-swap",
refund_amount: 9000,
items: [
{
return_id: "return-on-swap",
item_id: "test-item-swapped",
quantity: 1,
},
],
},
payment: {
id: "test-payment-swap-on-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swap-on-swap",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
},
],
})
await manager.save(swapOnSwap)
await manager.insert(ShippingMethod, {
id: "test-method-swap-order",
shipping_option_id: "test-option",
order_id: "order-with-swap",
price: 1000,
data: {},
})
await createSwap({ id: "disc-swap" }, manager)
}
const createSwap = async (options, manager) => {
const swapId = options.id
const dRule = manager.create(DiscountRule, {
id: `${swapId}-cart-discount-rule`,
description: "Ten percent rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.save(dRule)
const discount = manager.create(Discount, {
code: `${swapId}`,
is_dynamic: false,
is_disabled: false,
rule: dRule,
})
await manager.save(discount)
const cart = manager.create(Cart, {
id: `${swapId}-cart`,
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: "test-region",
type: "swap",
discounts: [discount],
metadata: {
swap_id: swapId,
parent_order_id: "order-with-swap",
},
})
await manager.save(cart)
const swapTemplate = async () => {
return {
order_id: `order-with-swap`,
fulfillment_status: "fulfilled",
payment_status: "not_paid",
cart_id: cart.id,
}
}
const swapWithReturn = manager.create(Swap, {
id: swapId,
return_order: {
id: `${swapId}-return-id`,
status: "requested",
refund_amount: 0,
},
...(await swapTemplate(swapId)),
})
await manager.save(swapWithReturn)
const li = manager.create(LineItem, {
id: `${swapId}-return-item-1`,
fulfilled_quantity: 1,
title: "Return Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: "order-with-swap",
cart_id: cart.id,
})
await manager.save(li)
const li2 = manager.create(LineItem, {
id: `${swapId}-test-item-many`,
fulfilled_quantity: 4,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 4,
variant_id: "test-variant",
order_id: "order-with-swap",
})
await manager.save(li2)
const return_item1 = manager.create(LineItem, {
...li,
unit_price: -1 * li.unit_price,
})
await manager.save(return_item1)
await manager.insert(ShippingMethod, {
id: `${swapId}-test-method`,
shipping_option_id: "test-option",
cart_id: cart.id,
price: 1000,
data: {},
})
}
@@ -0,0 +1,53 @@
const { User, Invite } = require("@medusajs/medusa")
import jwt from "jsonwebtoken"
const generateToken = (data) => {
return jwt.sign(data, "test", {
expiresIn: "7d",
})
}
const expires_at = new Date()
expires_at.setDate(expires_at.getDate() + 8)
module.exports = async (connection, data = {}) => {
const manager = connection.manager
const memberUser = await manager.create(User, {
id: "member-user",
role: "member",
email: "member@test.com",
first_name: "member",
last_name: "user",
})
await manager.save(memberUser)
const memberInvite = await manager.create(Invite, {
id: "memberInvite",
user_email: "invite-member@test.com",
role: "member",
token: generateToken({
invite_id: "memberInvite",
role: "member",
user_email: "invite-member@test.com",
}),
accepted: false,
expires_at: expires_at,
})
await manager.save(memberInvite)
const adminInvite = await manager.create(Invite, {
id: "adminInvite",
user_email: "invite-admin@test.com",
role: "admin",
accepted: false,
token: generateToken({
invite_id: "adminInvite",
role: "admin",
user_email: "invite-admin@test.com",
}),
expires_at: expires_at,
})
await manager.save(adminInvite)
}
+19
View File
@@ -0,0 +1,19 @@
// API
module.exports = {
testEnvironment: `node`,
testPathIgnorePatterns: [
`/examples/`,
`/www/`,
`/dist/`,
`/node_modules/`,
`__tests__/fixtures`,
`__testfixtures__`,
`.cache`,
],
transformIgnorePatterns: [`/dist`],
transform: { "^.+\\.[jt]s$": `../../jest-transformer.js` },
setupFilesAfterEnv: ["../setup.js"],
globalSetup: "../globalSetup.js",
globalTeardown: "../globalTeardown.js",
}
@@ -0,0 +1,28 @@
const DB_USERNAME = process.env.DB_USERNAME || "postgres"
const DB_PASSWORD = process.env.DB_PASSWORD || ""
const workerId = parseInt(process.env.JEST_WORKER_ID || "1")
module.exports = {
plugins: [
{
resolve: `medusa-fulfillment-webshipper`,
options: {
account: "test-account",
api_token: "something",
order_channel_id: "1",
webhook_secret: "1234",
},
},
{
resolve: `medusa-plugin-sendgrid`,
options: {
api_key: "SG.TEST",
},
},
],
projectConfig: {
// redis_url: REDIS_URL,
database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration-${workerId}`,
database_type: "postgres",
},
}
+25
View File
@@ -0,0 +1,25 @@
{
"name": "plugins",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "jest --maxWorkers=50% --silent=false",
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "1.1.60-dev-1642337635041",
"faker": "^5.5.3",
"medusa-fulfillment-webshipper": "1.1.35-dev-1642337635041",
"medusa-interfaces": "1.1.32-dev-1642337635041",
"medusa-plugin-sendgrid": "1.1.36-dev-1642337635041",
"typeorm": "^0.2.31"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"babel-preset-medusa-package": "1.1.19-dev-1642337635041",
"jest": "^26.6.3"
}
}
@@ -0,0 +1,53 @@
import { FulfillmentService } from "medusa-interfaces"
class TestFulService extends FulfillmentService {
static identifier = "test-ful"
constructor() {
super()
}
getFulfillmentOptions() {
return [
{
id: "manual-fulfillment",
},
]
}
validateFulfillmentData(data, cart) {
return data
}
validateOption(data) {
return true
}
canCalculate() {
return false
}
calculatePrice() {
throw Error("Manual Fulfillment service cannot calculatePrice")
}
createOrder() {
// No data is being sent anywhere
return Promise.resolve({})
}
createReturn() {
return Promise.resolve({})
}
createFulfillment() {
// No data is being sent anywhere
return Promise.resolve({})
}
cancelFulfillment() {
return Promise.resolve({})
}
}
export default TestFulService
@@ -0,0 +1,19 @@
import { NotificationService } from "medusa-interfaces"
class TestNotiService extends NotificationService {
static identifier = "test-not"
constructor() {
super()
}
async sendNotification() {
return Promise.resolve()
}
async resendNotification() {
return Promise.resolve()
}
}
export default TestNotiService
@@ -0,0 +1,73 @@
import { PaymentService } from "medusa-interfaces"
class TestPayService extends PaymentService {
static identifier = "test-pay"
constructor() {
super()
}
async getStatus(paymentData) {
return "authorized"
}
async retrieveSavedMethods(customer) {
return Promise.resolve([])
}
async createPayment(cart) {
const fields = [
"total",
"subtotal",
"tax_total",
"discount_total",
"shipping_total",
"gift_card_total",
]
const data = {}
for (const k of fields) {
data[k] = cart[k]
}
return data
}
async retrievePayment(data) {
return {}
}
async getPaymentData(sessionData) {
return {}
}
async authorizePayment(sessionData, context = {}) {
return { data: {}, status: "authorized" }
}
async updatePaymentData(sessionData, update) {
return {}
}
async updatePayment(sessionData, cart) {
return {}
}
async deletePayment(payment) {
return {}
}
async capturePayment(payment) {
return {}
}
async refundPayment(payment, amountToRefund) {
return {}
}
async cancelPayment(payment) {
return {}
}
}
export default TestPayService
File diff suppressed because it is too large. Load diff
+13
View File
@@ -0,0 +1,13 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@faker-js/faker@^5.5.3":
version "5.5.3"
resolved "https://registry.yarnpkg.com/@faker-js/faker/-/faker-5.5.3.tgz#18e3af6b8eae7984072bbeb0c0858474d7c4cefe"
integrity sha512-R11tGE6yIFwqpaIqcfkcg7AICXzFg14+5h5v0TfF/9+RMDL6jhzCy/pxHVOfbALGdtVYdt6JdR21tuxEgl34dw==
dotenv@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==
+16 -5
View File
@@ -45,7 +45,7 @@ export class Reporter {
this.ora_ = activityLogger
}
panic = data => {
panic = (data) => {
const parsedPanic = panicHandler(data)
this.loggerInstance_.log({
@@ -66,7 +66,7 @@ export class Reporter {
* @param {string} level - the level to check if logger is configured for
* @return {boolean} whether we should log
*/
shouldLog = level => {
shouldLog = (level) => {
level = this.loggerInstance_.levels[level]
const logLevel = this.loggerInstance_.levels[this.loggerInstance_.level]
return level <= logLevel
@@ -76,7 +76,7 @@ export class Reporter {
* Sets the log level of the logger.
* @param {string} level - the level to set the logger to
*/
setLogLevel = level => {
setLogLevel = (level) => {
this.loggerInstance_.level = level
}
@@ -262,7 +262,18 @@ export class Reporter {
* Logs a message at the info level.
* @param {string} message - the message to log
*/
info = message => {
debug = (message) => {
this.loggerInstance_.log({
level: "debug",
message,
})
}
/**
* Logs a message at the info level.
* @param {string} message - the message to log
*/
info = (message) => {
this.loggerInstance_.log({
level: "info",
message,
@@ -273,7 +284,7 @@ export class Reporter {
* Logs a message at the warn level.
* @param {string} message - the message to log
*/
warn = message => {
warn = (message) => {
this.loggerInstance_.warn({
level: "warn",
message,
@@ -18,6 +18,7 @@
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"@babel/runtime": "^7.9.6",
"@medusajs/medusa": "^1.1.62",
"client-sessions": "^0.8.0",
"cross-env": "^5.2.1",
"eslint": "^6.8.0",
@@ -1,10 +1,15 @@
import { TotalsService } from "@medusajs/medusa"
import { humanizeAmount } from "medusa-core-utils"
import { FulfillmentService } from "medusa-interfaces"
import Webshipper from "../utils/webshipper"
class WebshipperFulfillmentService extends FulfillmentService {
static identifier = "webshipper"
constructor({ logger, claimService, swapService, orderService }, options) {
constructor(
{ logger, totalsService, claimService, swapService, orderService },
options
) {
super()
this.options_ = options
@@ -25,6 +30,9 @@ class WebshipperFulfillmentService extends FulfillmentService {
/** @private @const {OrderService} */
this.orderService_ = orderService
/** @private @const {TotalsService} */
this.totalsService_ = totalsService
/** @private @const {SwapService} */
this.swapService_ = swapService
@@ -170,22 +178,38 @@ class WebshipperFulfillmentService extends FulfillmentService {
width: 15,
length: 15,
},
customs_lines: returnOrder.items.map(({ item, quantity }) => {
return {
ext_ref: item.id,
sku: item.variant.sku,
description: item.title,
quantity: quantity,
country_of_origin:
item.variant.origin_country ||
item.variant.product.origin_country,
tarif_number:
item.variant.hs_code || item.variant.product.hs_code,
unit_price: item.unit_price / 100,
vat_percent: fromOrder.tax_rate,
currency: fromOrder.currency_code.toUpperCase(),
}
}),
customs_lines: await Promise.all(
returnOrder.items.map(async ({ item, quantity }) => {
const totals = await this.totalsService_.getLineItemTotals(
item,
fromOrder,
{
include_tax: true,
use_tax_lines: true,
}
)
return {
ext_ref: item.id,
sku: item.variant.sku,
description: item.title,
quantity: quantity,
country_of_origin:
item.variant.origin_country ||
item.variant.product.origin_country,
tarif_number:
item.variant.hs_code || item.variant.product.hs_code,
unit_price: humanizeAmount(
totals.unit_price,
fromOrder.currency_code
),
vat_percent: totals.tax_lines.reduce(
(acc, next) => acc + next.rate,
0
),
currency: fromOrder.currency_code.toUpperCase(),
}
})
),
},
],
sender_address: {
@@ -329,21 +353,38 @@ class WebshipperFulfillmentService extends FulfillmentService {
status: "pending",
ext_ref,
visible_ref,
order_lines: fulfillmentItems.map((item) => {
return {
ext_ref: item.id,
sku: item.variant.sku,
description: item.title,
quantity: item.quantity,
country_of_origin:
item.variant.origin_country ||
item.variant.product.origin_country,
tarif_number:
item.variant.hs_code || item.variant.product.hs_code,
unit_price: item.unit_price / 100,
vat_percent: fromOrder.tax_rate,
}
}),
order_lines: await Promise.all(
fulfillmentItems.map(async (item) => {
const totals = await this.totalsService_.getLineItemTotals(
item,
fromOrder,
{
include_tax: true,
use_tax_lines: true,
}
)
return {
ext_ref: item.id,
sku: item.variant.sku,
description: item.title,
quantity: item.quantity,
country_of_origin:
item.variant.origin_country ||
item.variant.product.origin_country,
tarif_number:
item.variant.hs_code || item.variant.product.hs_code,
unit_price: humanizeAmount(
totals.unit_price,
fromOrder.currency_code
),
vat_percent: totals.tax_lines.reduce(
(acc, next) => acc + next.rate,
0
),
}
})
),
delivery_address: {
att_contact: `${shipping_address.first_name} ${shipping_address.last_name}`,
address_1: shipping_address.address_1,
@@ -8,12 +8,14 @@ import AdminGiftCardsResource from "./gift-cards"
import AdminInvitesResource from "./invites"
import AdminNotesResource from "./notes"
import AdminProductsResource from "./products"
import AdminProductTypesResource from "./product-types"
import AdminUsersResource from "./users"
import AdminReturnsResource from "./returns"
import AdminOrdersResource from "./orders"
import AdminReturnReasonsResource from "./return-reasons"
import AdminVariantsResource from "./variants"
import AdminSwapsResource from "./swaps"
import AdminTaxRatesResource from "./tax-rates"
import AdminShippingProfilesResource from "./shipping-profiles"
import AdminStoresResource from "./store"
import AdminShippingOptionsResource from "./shipping-options"
@@ -33,6 +35,7 @@ class Admin extends BaseResource {
public notes = new AdminNotesResource(this.client)
public products = new AdminProductsResource(this.client)
public productTags = new AdminProductTagsResource(this.client)
public productTypes = new AdminProductTypesResource(this.client)
public users = new AdminUsersResource(this.client)
public returns = new AdminReturnsResource(this.client)
public orders = new AdminOrdersResource(this.client)
@@ -44,6 +47,7 @@ class Admin extends BaseResource {
public shippingOptions = new AdminShippingOptionsResource(this.client)
public regions = new AdminRegionsResource(this.client)
public notifications = new AdminNotificationsResource(this.client)
public taxRates = new AdminTaxRatesResource(this.client)
public uploads = new AdminUploadsResource(this.client)
}
@@ -22,7 +22,10 @@ import { ResponsePromise } from "../../typings"
import BaseResource from "../base"
class AdminOrdersResource extends BaseResource {
create(payload: AdminPostOrdersReq, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
create(
payload: AdminPostOrdersReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders`
return this.client.request("POST", path, payload, {}, customHeaders)
}
@@ -30,17 +33,24 @@ class AdminOrdersResource extends BaseResource {
update(
id: string,
payload: AdminPostOrdersOrderReq,
customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}`
return this.client.request("POST", path, payload, {}, customHeaders)
}
retrieve(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
retrieve(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}`
return this.client.request("GET", path, {}, {}, customHeaders)
}
list(query?: AdminGetOrdersParams, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersListRes> {
list(
query?: AdminGetOrdersParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersListRes> {
let path = `/admin/orders`
if (query) {
@@ -51,12 +61,18 @@ class AdminOrdersResource extends BaseResource {
return this.client.request("GET", path, {}, {}, customHeaders)
}
complete(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
complete(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/complete`
return this.client.request("POST", path, {}, {}, customHeaders)
}
capturePayment(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
capturePayment(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/capture`
return this.client.request("POST", path, {}, {}, customHeaders)
}
@@ -64,7 +80,8 @@ class AdminOrdersResource extends BaseResource {
refundPayment(
id: string,
payload: AdminPostOrdersOrderRefundsReq,
customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/refund`
return this.client.request("POST", path, payload, {}, customHeaders)
}
@@ -72,7 +89,8 @@ class AdminOrdersResource extends BaseResource {
createFulfillment(
id: string,
payload: AdminPostOrdersOrderFulfillmentsReq,
customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/fulfillment`
return this.client.request("POST", path, payload, {}, customHeaders)
}
@@ -124,7 +142,10 @@ class AdminOrdersResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders)
}
cancel(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
cancel(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/cancel`
return this.client.request("POST", path, {}, {}, customHeaders)
}
@@ -138,7 +159,10 @@ class AdminOrdersResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders)
}
archive(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
archive(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/archive`
return this.client.request("POST", path, {}, {}, customHeaders)
}
@@ -152,7 +176,11 @@ class AdminOrdersResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders)
}
cancelSwap(id: string, swapId: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
cancelSwap(
id: string,
swapId: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/swaps/${swapId}/cancel`
return this.client.request("POST", path, {}, {}, customHeaders)
}
@@ -195,7 +223,11 @@ class AdminOrdersResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders)
}
cancelClaim(id: string, claimId: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
cancelClaim(
id: string,
claimId: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/claims/${claimId}/cancel`
return this.client.request("POST", path, {}, {}, customHeaders)
}
@@ -230,7 +262,11 @@ class AdminOrdersResource extends BaseResource {
return this.client.request("POST", path, payload, {}, customHeaders)
}
deleteMetadata(id: string, key: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminOrdersRes> {
deleteMetadata(
id: string,
key: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminOrdersRes> {
const path = `/admin/orders/${id}/metadata/${key}`
return this.client.request("DELETE", path, {}, {}, customHeaders)
}
@@ -0,0 +1,24 @@
import {
AdminGetProductTypesParams,
AdminProductTypesListRes,
} from "@medusajs/medusa"
import qs from "qs"
import { ResponsePromise } from "../../typings"
import BaseResource from "../base"
class AdminProductTypesResource extends BaseResource {
list(
query?: AdminGetProductTypesParams
): ResponsePromise<AdminProductTypesListRes> {
let path = `/admin/product-types`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/product-types?${queryString}`
}
return this.client.request("GET", path)
}
}
export default AdminProductTypesResource
@@ -20,12 +20,18 @@ import { ResponsePromise } from "../../typings"
import BaseResource from "../base"
class AdminProductsResource extends BaseResource {
create(payload: AdminPostProductsReq, customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsRes> {
create(
payload: AdminPostProductsReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsRes> {
const path = `/admin/products/`
return this.client.request("POST", path, payload, {}, customHeaders)
}
retrieve(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsRes> {
retrieve(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsRes> {
const path = `/admin/products/${id}`
return this.client.request("GET", path, {}, {}, customHeaders)
}
@@ -33,17 +39,24 @@ class AdminProductsResource extends BaseResource {
update(
id: string,
payload: AdminPostProductsProductReq,
customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsRes> {
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsRes> {
const path = `/admin/products/${id}`
return this.client.request("POST", path, payload, {}, customHeaders)
}
delete(id: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsDeleteRes> {
delete(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsDeleteRes> {
const path = `/admin/products/${id}`
return this.client.request("DELETE", path, {}, {}, customHeaders)
}
list(query?: AdminGetProductsParams, customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsListRes> {
list(
query?: AdminGetProductsParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsListRes> {
let path = `/admin/products`
if (query) {
@@ -54,12 +67,16 @@ class AdminProductsResource extends BaseResource {
return this.client.request("GET", path, {}, {}, customHeaders)
}
listTypes(customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsListTypesRes> {
listTypes(
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsListTypesRes> {
const path = `/admin/products/types`
return this.client.request("GET", path, {}, {}, customHeaders)
}
listTags(customHeaders: Record<string, any> = {}): ResponsePromise<AdminProductsListTagsRes> {
listTags(
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductsListTagsRes> {
const path = `/admin/products/tag-usage`
return this.client.request("GET", path, {}, {}, customHeaders)
}
@@ -1,5 +1,6 @@
import {
AdminPaymentProvidersList,
AdminTaxProvidersList,
AdminPostStoreReq,
AdminStoresRes,
} from "@medusajs/medusa"
@@ -13,7 +14,10 @@ class AdminStoresResource extends BaseResource {
* @param customHeaders
* @returns the updated store.
*/
update(payload: AdminPostStoreReq, customHeaders: Record<string, any> = {}): ResponsePromise<AdminStoresRes> {
update(
payload: AdminPostStoreReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminStoresRes> {
const path = `/admin/store/`
return this.client.request("POST", path, payload, {}, customHeaders)
}
@@ -24,7 +28,10 @@ class AdminStoresResource extends BaseResource {
* @param customHeaders
* @returns updated store.
*/
addCurrency(currency_code: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminStoresRes> {
addCurrency(
currency_code: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminStoresRes> {
const path = `/admin/store/${currency_code}`
return this.client.request("POST", path, {}, {}, customHeaders)
}
@@ -35,7 +42,10 @@ class AdminStoresResource extends BaseResource {
* @param customHeaders
* @returns updated store
*/
deleteCurrency(currency_code: string, customHeaders: Record<string, any> = {}): ResponsePromise<AdminStoresRes> {
deleteCurrency(
currency_code: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminStoresRes> {
const path = `/admin/store/currencies/${currency_code}`
return this.client.request("DELETE", path, {}, {}, customHeaders)
}
@@ -44,7 +54,9 @@ class AdminStoresResource extends BaseResource {
* @description gets a medusa store
* @returns a medusa store
*/
retrieve(customHeaders: Record<string, any> = {}): ResponsePromise<AdminStoresRes> {
retrieve(
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminStoresRes> {
const path = `/admin/store/`
return this.client.request("GET", path, {}, {}, customHeaders)
}
@@ -53,10 +65,23 @@ class AdminStoresResource extends BaseResource {
* @description Lists the store's payment providers
* @returns a list of payment providers configured on the store
*/
listPaymentProviders(customHeaders: Record<string, any> = {}): ResponsePromise<AdminPaymentProvidersList> {
listPaymentProviders(
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminPaymentProvidersList> {
const path = `/admin/store/payment-providers`
return this.client.request("GET", path, {}, {}, customHeaders)
}
/**
* @description Lists the store's payment providers
* @returns a list of payment providers configured on the store
*/
listTaxProviders(
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxProvidersList> {
const path = `/admin/store/tax-providers`
return this.client.request("GET", path, {}, {}, customHeaders)
}
}
export default AdminStoresResource
@@ -0,0 +1,189 @@
import {
AdminTaxRatesRes,
AdminTaxRatesListRes,
AdminTaxRatesDeleteRes,
AdminGetTaxRatesParams,
AdminGetTaxRatesTaxRateParams,
AdminDeleteTaxRatesTaxRateProductsReq,
AdminDeleteTaxRatesTaxRateProductsParams,
AdminDeleteTaxRatesTaxRateProductTypesReq,
AdminDeleteTaxRatesTaxRateProductTypesParams,
AdminDeleteTaxRatesTaxRateShippingOptionsReq,
AdminDeleteTaxRatesTaxRateShippingOptionsParams,
AdminPostTaxRatesReq,
AdminPostTaxRatesTaxRateReq,
AdminPostTaxRatesTaxRateProductsReq,
AdminPostTaxRatesTaxRateProductTypesReq,
AdminPostTaxRatesTaxRateShippingOptionsReq,
} from "@medusajs/medusa"
import qs from "qs"
import { ResponsePromise } from "../../typings"
import BaseResource from "../base"
class AdminTaxRatesResource extends BaseResource {
retrieve(
id: string,
query?: AdminGetTaxRatesTaxRateParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}?${queryString}`
}
return this.client.request("GET", path, {}, {}, customHeaders)
}
list(
query?: AdminGetTaxRatesParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesListRes> {
let path = `/admin/tax-rates`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates?${queryString}`
}
return this.client.request("GET", path, {}, {}, customHeaders)
}
create(
payload: AdminPostTaxRatesReq,
query?: AdminGetTaxRatesTaxRateParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates?${queryString}`
}
return this.client.request("POST", path, payload, {}, customHeaders)
}
update(
id: string,
payload: AdminPostTaxRatesTaxRateReq,
query?: AdminGetTaxRatesTaxRateParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}?${queryString}`
}
return this.client.request("POST", path, payload, {}, customHeaders)
}
addProducts(
id: string,
payload: AdminPostTaxRatesTaxRateProductsReq,
query?: AdminGetTaxRatesTaxRateParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}/products/batch`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}/products/batch?${queryString}`
}
return this.client.request("POST", path, payload, {}, customHeaders)
}
addProductTypes(
id: string,
payload: AdminPostTaxRatesTaxRateProductTypesReq,
query?: AdminGetTaxRatesTaxRateParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}/product-types/batch`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}/product-types/batch?${queryString}`
}
return this.client.request("POST", path, payload, {}, customHeaders)
}
addShippingOptions(
id: string,
payload: AdminPostTaxRatesTaxRateShippingOptionsReq,
query?: AdminGetTaxRatesTaxRateParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}/shipping-options/batch`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}/shipping-options/batch?${queryString}`
}
return this.client.request("POST", path, payload, {}, customHeaders)
}
removeProducts(
id: string,
payload: AdminDeleteTaxRatesTaxRateProductsReq,
query?: AdminDeleteTaxRatesTaxRateProductsParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}/products/batch`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}/products/batch?${queryString}`
}
return this.client.request("DELETE", path, payload, {}, customHeaders)
}
removeProductTypes(
id: string,
payload: AdminDeleteTaxRatesTaxRateProductTypesReq,
query?: AdminDeleteTaxRatesTaxRateProductTypesParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}/product-types/batch`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}/product-types/batch?${queryString}`
}
return this.client.request("DELETE", path, payload, {}, customHeaders)
}
removeShippingOptions(
id: string,
payload: AdminDeleteTaxRatesTaxRateShippingOptionsReq,
query?: AdminDeleteTaxRatesTaxRateShippingOptionsParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesRes> {
let path = `/admin/tax-rates/${id}/shipping-options/batch`
if (query) {
const queryString = qs.stringify(query)
path = `/admin/tax-rates/${id}/shipping-options/batch?${queryString}`
}
return this.client.request("DELETE", path, payload, {}, customHeaders)
}
delete(
id: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminTaxRatesDeleteRes> {
const path = `/admin/tax-rates/${id}`
return this.client.request("DELETE", path, {}, {}, customHeaders)
}
}
export default AdminTaxRatesResource
@@ -3,6 +3,18 @@ export const TotalsServiceMock = {
getTaxTotal: jest.fn(),
getAllocationItemDiscounts: jest.fn(),
getDiscountTotal: jest.fn(),
getLineItemTotals: jest.fn(() => {
return {
total: 10,
tax_lines: [],
}
}),
getShippingMethodTotals: jest.fn(() => {
return {
total: 10,
tax_lines: [],
}
}),
}
const mock = jest.fn().mockImplementation(() => {
@@ -5,7 +5,7 @@ import { PaymentService } from "medusa-interfaces"
class KlarnaProviderService extends PaymentService {
static identifier = "klarna"
constructor({ shippingProfileService }, options) {
constructor({ shippingProfileService, totalsService }, options) {
super()
/**
@@ -41,48 +41,66 @@ class KlarnaProviderService extends PaymentService {
/** @private @const {ShippingProfileService} */
this.shippingProfileService_ = shippingProfileService
/** @private @const {TotalsService} */
this.totalsService_ = totalsService
}
async lineItemsToOrderLines_(cart, taxRate) {
async lineItemsToOrderLines_(cart) {
let order_lines = []
const tax = taxRate / 100
cart.items.forEach((item) => {
for (const item of cart.items) {
// Withdraw discount from the total item amount
const quantity = item.quantity
const unit_price = item.unit_price * (tax + 1)
const total_amount = unit_price * quantity
const total_tax_amount = total_amount * (tax / (1 + tax))
const totals = await this.totalsService_.getLineItemTotals(item, cart, {
include_tax: true,
})
const tax =
totals.tax_lines.reduce((acc, next) => acc + next.rate, 0) / 100
order_lines.push({
name: item.title,
tax_rate: tax * 10000,
quantity,
unit_price,
total_amount,
total_tax_amount,
unit_price: Math.round(totals.original_total / item.quantity),
total_amount: totals.total - totals.gift_card_total,
total_tax_amount: totals.tax_total,
total_discount_amount:
totals.original_total - totals.total + totals.gift_card_total,
})
})
}
if (cart.shipping_methods.length) {
const { name, price } = cart.shipping_methods.reduce(
(acc, next) => {
acc.name = [...acc.name, next?.shipping_option.name]
acc.price += next.price
return acc
},
{ name: [], price: 0 }
)
const name = []
let total = 0
let tax = 0
for (const next of cart.shipping_methods) {
const totals = await this.totalsService_.getShippingMethodTotals(
next,
cart,
{
include_tax: true,
}
)
name.push(next?.shipping_option.name)
total += totals.total
tax += totals.tax_total
}
const taxRate = tax / (total - tax)
order_lines.push({
name: name?.join(" + ") || "Shipping fee",
quantity: 1,
type: "shipping_fee",
unit_price: price * (1 + tax),
tax_rate: tax * 10000,
total_amount: price * (1 + tax),
total_tax_amount: price * tax,
unit_price: total,
tax_rate: taxRate * 10000,
total_amount: total,
total_tax_amount: tax,
})
}
@@ -96,45 +114,20 @@ class KlarnaProviderService extends PaymentService {
locale: "en-US",
}
const { region, gift_card_total, discount_total, tax_total, total } = cart
const { region, gift_card_total, tax_total, total } = cart
const taxRate = region.tax_rate / 100
order.order_lines = await this.lineItemsToOrderLines_(cart)
order.order_lines = await this.lineItemsToOrderLines_(cart, region.tax_rate)
if (discount_total > 0) {
order.order_lines.push({
name: `Discount`,
quantity: 1,
type: "discount",
unit_price: 0,
total_discount_amount: discount_total * (1 + taxRate),
tax_rate: taxRate * 10000,
total_amount: -discount_total * (1 + taxRate),
total_tax_amount: -discount_total * taxRate,
})
} else if (discount_total < 0) {
order.order_lines.push({
name: `Discount Payback`,
quantity: 1,
type: "surcharge",
unit_price: -discount_total * (1 + taxRate),
tax_rate: taxRate * 10000,
total_amount: -discount_total * (1 + taxRate),
total_tax_amount: -discount_total * taxRate,
})
}
if (gift_card_total) {
if (gift_card_total && !region.gift_cards_taxable) {
order.order_lines.push({
name: `Gift Card`,
quantity: 1,
type: "gift_card",
unit_price: 0,
total_discount_amount: gift_card_total * (1 + taxRate),
tax_rate: taxRate * 10000,
total_amount: -gift_card_total * (1 + taxRate),
total_tax_amount: -gift_card_total * taxRate,
total_discount_amount: gift_card_total,
tax_rate: 0,
total_amount: -gift_card_total,
total_tax_amount: 0,
})
}
@@ -185,11 +178,20 @@ class KlarnaProviderService extends PaymentService {
// shipping_options and set the selected shipping method
if (cart.shipping_methods.length) {
const shipping_method = cart.shipping_methods[0]
const totals = await this.totalsService_.getShippingMethodTotals(
shipping_method,
cart,
{
include_tax: true,
}
)
const taxRate = totals.tax_total / (totals.total - totals.tax_total)
order.selected_shipping_option = {
id: shipping_method.shipping_option.id,
name: shipping_method.shipping_option.name,
price: shipping_method.price * (1 + taxRate),
tax_amount: shipping_method.price * taxRate,
price: totals.total,
tax_amount: total.tax_total,
tax_rate: taxRate * 10000,
}
}
@@ -213,6 +215,8 @@ class KlarnaProviderService extends PaymentService {
const methods = Object.keys(partitioned).map((k) => partitioned[k])
const combinations = cartesian(...methods)
const taxRate = region.tax_rate / 100
// Use the cartesian product of shipping methods to generate correct
// format for the Klarna Widget
order.shipping_options = combinations.map((combination) => {
@@ -156,6 +156,20 @@ const TotalsService = {
getTotal: () => {
return Promise.resolve(123)
},
getShippingMethodTotals: () => {
return {
price: 0,
discount_total: 0,
tax_total: 0,
}
},
getLineItemTotals: () => {
return {
subtotal: 15800,
discount_total: 0,
tax_total: 3950,
}
},
getLineDiscounts: (o) => {
if (o.id === "rounding") {
return [
@@ -242,76 +256,6 @@ describe("BrightpearlService", () => {
})
})
describe("createSalesOrder", () => {
const bpService = new BrightpearlService(
{
orderService: OrderService,
totalsService: TotalsService,
oauthService: OAuthService,
regionService: RegionService,
},
{ account: "test" }
)
it("successfully builds sales order", async () => {
jest.clearAllMocks()
await bpService.createSalesOrder(order)
expect(mockCreateOrder).toHaveBeenCalledWith({
currency: { code: "DKK" },
ref: "1234",
externalRef: "12355",
channelId: "1",
installedIntegrationInstanceId: undefined,
statusId: "3",
customer: {
id: "12345",
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
delivery: {
shippingMethodId: 0,
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
rows: [
{
name: "Test",
net: 22,
tax: 5.082,
quantity: 2,
taxCode: "1234",
externalRef: undefined,
nominalCode: "4000",
},
{
name: "Shipping: standard",
quantity: 1,
net: 123.99,
tax: 28.6417,
taxCode: "1234",
nominalCode: "4040",
},
],
})
})
})
describe("rounding", () => {
const bpService = new BrightpearlService(
{
@@ -322,15 +322,8 @@ class BrightpearlService extends BaseService {
name: `${fromRefund.reason}: ${fromRefund.note}`,
quantity: 1,
taxCode: region.tax_code,
net: this.bpnum_(
fromRefund.amount,
fromOrder.currency_code,
10000 / (100 + fromOrder.tax_rate)
),
tax: this.bpnum_(
fromRefund.amount * (1 - 100 / (100 + fromOrder.tax_rate)),
fromOrder.currency_code
),
net: this.bpnum_(fromRefund.amount, fromOrder.currency_code),
tax: 0,
nominalCode: accountingCode,
},
],
@@ -410,6 +403,29 @@ class BrightpearlService extends BaseService {
}),
}
if (fromReturn.shipping_method) {
const totals = await this.totalsService_.getShippingMethodTotals(
fromReturn.shipping_method,
fromOrder,
{
include_tax: true,
use_tax_lines: true,
}
)
order.rows.push({
net: this.bpnum_(
-1 * (totals.total - totals.tax_total),
fromOrder.currency_code
),
tax: this.bpnum_(-1 * totals.tax_total, fromOrder.currency_code),
name: "Return shipping",
taxCode: region.tax_code,
nominalCode: this.options.shipping_account_code || "4040",
quantity: 1,
})
}
const total = order.rows.reduce((acc, next) => {
return acc + next.net + next.tax
}, 0)
@@ -421,15 +437,8 @@ class BrightpearlService extends BaseService {
name: "Difference",
quantity: 1,
taxCode: region.tax_code,
net: this.bpnum_(
difference,
fromOrder.currency_code,
10000 / (100 + fromOrder.tax_rate)
),
tax: this.bpnum_(
difference * (1 - 100 / (100 + fromOrder.tax_rate)),
fromOrder.currency_code
),
net: this.bpround_(difference),
tax: 0,
nominalCode: this.options.sales_account_code || "4000",
})
}
@@ -811,14 +820,24 @@ class BrightpearlService extends BaseService {
fromSwap.return_order.shipping_method &&
fromSwap.return_order.shipping_method.price
) {
const shippingTotals =
await this.totalsService_.getShippingMethodTotals(
fromSwap.return_order.shipping_method,
fromOrder,
{
include_tax: true,
use_tax_lines: true,
}
)
order.rows.push({
name: "Return Shipping",
quantity: 1,
taxCode: region.tax_code,
net: (-1 * fromSwap.return_order.shipping_method.price) / 100,
tax:
((-1 * fromSwap.return_order.shipping_method.price) / 100) *
(fromOrder.tax_rate / 100),
net: this.bpnum_(1 * shippingTotals.price, fromOrder.currency_code),
tax: this.bpnum_(
1 * shippingTotals.tax_total,
fromOrder.currency_code
),
nominalCode: this.options.shipping_account_code || "4040",
})
}
@@ -882,21 +901,19 @@ class BrightpearlService extends BaseService {
config = { include_price: true, is_claim: false }
) {
const { region } = fromOrder
const discount = fromOrder.discounts.find(
({ rule }) => rule.type !== "free_shipping"
)
let lineDiscounts = []
if (discount) {
lineDiscounts = this.totalsService_.getLineDiscounts(fromOrder, discount)
}
const lines = await Promise.all(
fromOrder.items.map(async (item) => {
const bpProduct = await this.retrieveProductBySKU(item.variant.sku)
const ld = lineDiscounts.find((l) => item.id === l.item.id) || {
amount: 0,
}
const lineTotals = await this.totalsService_.getLineItemTotals(
item,
fromOrder,
{
include_tax: true,
use_tax_lines: true,
}
)
const row = {}
if (bpProduct) {
@@ -907,23 +924,20 @@ class BrightpearlService extends BaseService {
if (config.include_price) {
row.net = this.bpnum_(
item.unit_price * item.quantity - ld.amount,
lineTotals.subtotal - lineTotals.discount_total,
fromOrder.currency_code
)
row.tax = this.bpnum_(
item.unit_price * item.quantity - ld.amount,
fromOrder.currency_code,
fromOrder.tax_rate
)
row.tax = this.bpnum_(lineTotals.tax_total, fromOrder.currency_code)
} else if (config.is_claim) {
row.net = await this.retrieveProductPrice(
bpProduct.productId,
this.options.cost_price_list || `1`
)
row.tax = this.bpnum_(
row.net * 100,
fromOrder.currency_code,
fromOrder.tax_rate
lineTotals.tax_lines.reduce((acc, next) => acc + next.rate, 0)
)
}
@@ -948,34 +962,48 @@ class BrightpearlService extends BaseService {
// purchased.
const gcTotal = fromOrder.gift_card_total
if (gcTotal) {
let tax = 0
if (fromOrder.region.gift_cards_taxable) {
tax = this.bpnum_(
-1 * gcTotal,
fromOrder.currency_code,
fromOrder.region.tax_rate
)
}
lines.push({
name: `Gift Card`,
net: this.bpnum_(-1 * gcTotal, fromOrder.currency_code),
tax: this.bpnum_(
-1 * gcTotal,
fromOrder.currency_code,
fromOrder.tax_rate
),
tax,
quantity: 1,
taxCode: region.tax_code,
nominalCode: this.options.gift_card_account_code || "4000",
})
}
const shippingTotal =
fromOrder.shipping_total ||
this.totalsService_.getShippingTotal(fromOrder)
const shippingMethods = fromOrder.shipping_methods
if (shippingMethods.length > 0) {
const shippingTotal = await shippingMethods.reduce(async (acc, next) => {
const accum = await acc
const totals = await this.totalsService_.getShippingMethodTotals(
next,
fromOrder,
{
include_tax: true,
use_tax_lines: true,
}
)
return {
price: accum.price + totals.price,
tax: accum.tax + totals.tax_total,
}
}, Promise.resolve({ price: 0, tax: 0 }))
lines.push({
name: `Shipping: ${shippingMethods.map((m) => m.name).join(" + ")}`,
quantity: 1,
net: this.bpnum_(shippingTotal, fromOrder.currency_code),
tax: this.bpnum_(
shippingTotal,
fromOrder.currency_code,
fromOrder.tax_rate
),
net: this.bpnum_(shippingTotal.price, fromOrder.currency_code),
tax: this.bpnum_(shippingTotal.tax, fromOrder.currency_code),
taxCode: region.tax_code,
nominalCode: this.options.shipping_account_code || "4040",
})
@@ -72,21 +72,20 @@ class OrderSubscriber {
const fromSwap = await this.swapService_.retrieve(id, {
relations: [
"order",
"order.payments",
"order.region",
"order.swaps",
"order.discounts",
"order.discounts.rule",
"return_order",
"return_order.items",
"return_order.shipping_method",
"return_order.shipping_method.tax_lines",
"additional_items",
"additional_items.tax_lines",
"shipping_address",
"shipping_methods",
"shipping_methods.tax_lines",
],
})
let fromOrder = fromSwap.order
const fromOrder = await this.orderService_.retrieve(fromSwap.order_id, {
relations: ["payments", "region", "swaps", "discounts", "discounts.rule"],
})
if (
!(
@@ -105,23 +104,27 @@ class OrderSubscriber {
const { id } = data
const fromClaim = await this.claimService_.retrieve(id, {
relations: [
"order",
"order.payments",
"order.region",
"order.claims",
"order.discounts",
"order.discounts.rule",
"claim_items",
"return_order",
"return_order.items",
"return_order.shipping_method",
"return_order.shipping_method.tax_lines",
"additional_items",
"additional_items.tax_lines",
"shipping_address",
"shipping_methods",
],
})
const fromOrder = fromClaim.order
const fromOrder = await this.orderService_.retrieve(fromClaim.order_id, {
relations: [
"payments",
"region",
"claims",
"discounts",
"discounts.rule",
],
})
if (fromClaim.type === "replace") {
await this.brightpearlService_.createClaim(fromOrder, fromClaim)
@@ -147,11 +150,11 @@ class OrderSubscriber {
const { id, return_id } = data
const order = await this.orderService_.retrieve(id, {
relations: ["region", "swaps", "payments"],
relations: ["discounts", "region", "swaps", "payments"],
})
const fromReturn = await this.returnService_.retrieve(return_id, {
relations: ["items"],
relations: ["items", "shipping_method", "shipping_method.tax_lines"],
})
return this.brightpearlService_
@@ -64,8 +64,11 @@ describe("SegmentService", () => {
}
const TotalsService = {
getLineItemRefund: (_, item) => {
return item.unit_price
getLineItemTotals: (item) => {
return {
total: item.unit_price,
tax_total: 0,
}
},
}
@@ -101,10 +104,10 @@ describe("SegmentService", () => {
{
category: "Collection",
name: "Test",
price: 4.47,
price: 5.5,
product_id: "prod_123",
quantity: 2,
reporting_revenue: 8.94,
reporting_revenue: 11,
sku: "",
subtitle: "Subtitle",
type: "Type",
@@ -160,10 +163,10 @@ describe("SegmentService", () => {
{
category: "Collection",
name: "Test",
price: 4.47,
price: 5.5,
product_id: "prod_123",
quantity: 2,
reporting_revenue: 8.94,
reporting_revenue: 11,
sku: "",
subtitle: "Subtitle",
type: "Type",
@@ -214,10 +217,10 @@ describe("SegmentService", () => {
{
category: "Collection",
name: "Test",
price: 446.79,
price: 550,
product_id: "prod_123",
quantity: 2,
reporting_revenue: 893.58,
reporting_revenue: 1100,
sku: "",
subtitle: "Subtitle",
type: "Type",
@@ -73,8 +73,6 @@ class SegmentService extends BaseService {
coupon = order.discounts[0] && order.discounts[0].code
}
const taxRate = order.tax_rate / 100
const orderData = {
checkout_id: order.cart_id,
order_id: order.id,
@@ -113,15 +111,18 @@ class SegmentService extends BaseService {
products: await Promise.all(
order.items.map(async (item) => {
let name = item.title
const lineTotalTax = this.totalsService_.getLineItemRefund(
const totals = await this.totalsService_.getLineItemTotals(
item,
order,
item
{
include_tax: true,
}
)
const lineTotal = lineTotalTax / (1 + taxRate)
const lineTotal = totals.total - totals.tax_total
const revenue = await this.getReportingValue(
order.currency_code,
curr,
humanizeAmount(lineTotal, curr)
)
@@ -349,7 +349,7 @@ class SendGridService extends NotificationService {
}
}
async orderPlacedData({ id }) {
async orderCanceledData({ id }) {
const order = await this.orderService_.retrieve(id, {
select: [
"shipping_total",
@@ -448,6 +448,131 @@ class SendGridService extends NotificationService {
}
}
async orderPlacedData({ id }) {
const order = await this.orderService_.retrieve(id, {
select: [
"shipping_total",
"discount_total",
"tax_total",
"refunded_total",
"gift_card_total",
"subtotal",
"total",
],
relations: [
"customer",
"billing_address",
"shipping_address",
"discounts",
"discounts.rule",
"discounts.rule.valid_for",
"shipping_methods",
"shipping_methods.shipping_option",
"payments",
"fulfillments",
"returns",
"gift_cards",
"gift_card_transactions",
],
})
const { tax_total, shipping_total, gift_card_total, total } = order
const currencyCode = order.currency_code.toUpperCase()
const items = await Promise.all(
order.items.map(async (i) => {
i.totals = await this.totalsService_.getLineItemTotals(i, order, {
include_tax: true,
use_tax_lines: true,
})
i.thumbnail = this.normalizeThumbUrl_(i.thumbnail)
i.discounted_price = `${this.humanPrice_(
i.totals.total / i.quantity,
currencyCode
)} ${currencyCode}`
i.price = `${this.humanPrice_(
i.totals.original_total / i.quantity,
currencyCode
)} ${currencyCode}`
return i
})
)
let discounts = []
if (order.discounts) {
discounts = order.discounts.map((discount) => {
return {
is_giftcard: false,
code: discount.code,
descriptor: `${discount.rule.value}${
discount.rule.type === "percentage" ? "%" : ` ${currencyCode}`
}`,
}
})
}
let giftCards = []
if (order.gift_cards) {
giftCards = order.gift_cards.map((gc) => {
return {
is_giftcard: true,
code: gc.code,
descriptor: `${gc.value} ${currencyCode}`,
}
})
discounts.concat(giftCards)
}
const locale = await this.extractLocale(order)
// Includes taxes in discount amount
const discountTotal = items.reduce((acc, i) => {
return acc + i.totals.original_total - i.totals.total
}, 0)
const discounted_subtotal = items.reduce((acc, i) => {
return acc + i.totals.total
}, 0)
const subtotal = items.reduce((acc, i) => {
return acc + i.totals.original_total
}, 0)
const subtotal_ex_tax = items.reduce((total, i) => {
return total + i.totals.subtotal
}, 0)
return {
...order,
locale,
has_discounts: order.discounts.length,
has_gift_cards: order.gift_cards.length,
date: order.created_at.toDateString(),
items,
discounts,
subtotal_ex_tax: `${this.humanPrice_(
subtotal_ex_tax,
currencyCode
)} ${currencyCode}`,
subtotal: `${this.humanPrice_(subtotal, currencyCode)} ${currencyCode}`,
gift_card_total: `${this.humanPrice_(
gift_card_total,
currencyCode
)} ${currencyCode}`,
tax_total: `${this.humanPrice_(tax_total, currencyCode)} ${currencyCode}`,
discount_total: `${this.humanPrice_(
discountTotal,
currencyCode
)} ${currencyCode}`,
shipping_total: `${this.humanPrice_(
shipping_total,
currencyCode
)} ${currencyCode}`,
total: `${this.humanPrice_(total, currencyCode)} ${currencyCode}`,
}
}
async gcCreatedData({ id }) {
const giftCard = await this.giftCardService_.retrieve(id, {
relations: ["region", "order"],
@@ -475,68 +600,76 @@ class SendGridService extends NotificationService {
relations: [
"items",
"items.item",
"items.item.tax_lines",
"items.item.variant",
"items.item.variant.product",
"shipping_method",
"shipping_method.tax_lines",
"shipping_method.shipping_option",
],
})
const items = await this.lineItemService_.list({
id: returnRequest.items.map(({ item_id }) => item_id),
})
returnRequest.items = returnRequest.items.map((item) => {
const found = items.find((i) => i.id === item.item_id)
return {
...item,
item: found,
}
})
const items = await this.lineItemService_.list(
{
id: returnRequest.items.map(({ item_id }) => item_id),
},
{ relations: ["tax_lines"] }
)
// Fetch the order
const order = await this.orderService_.retrieve(id, {
select: ["total"],
relations: [
"items",
"items.tax_lines",
"discounts",
"discounts.rule",
"discounts.rule.valid_for",
"shipping_address",
"returns",
"swaps",
"swaps.additional_items",
],
})
let merged = [...order.items]
// merge items from order with items from order swaps
if (order.swaps && order.swaps.length) {
for (const s of order.swaps) {
merged = [...merged, ...s.additional_items]
}
}
// Calculate which items are in the return
const returnItems = returnRequest.items.map((i) => {
const found = merged.find((oi) => oi.id === i.item_id)
return {
...found,
quantity: i.quantity,
}
})
const taxRate = order.tax_rate / 100
const currencyCode = order.currency_code.toUpperCase()
// Calculate which items are in the return
const returnItems = await Promise.all(
returnRequest.items.map(async (i) => {
const found = items.find((oi) => oi.id === i.item_id)
found.quantity = i.quantity
found.thumbnail = this.normalizeThumbUrl_(found.thumbnail)
found.totals = await this.totalsService_.getLineItemTotals(
found,
order,
{
include_tax: true,
use_tax_lines: true,
}
)
found.price = `${this.humanPrice_(
found.totals.total,
currencyCode
)} ${currencyCode}`
found.tax_lines = found.totals.tax_lines
return found
})
)
// Get total of the returned products
const item_subtotal = this.totalsService_.getRefundTotal(order, returnItems)
const item_subtotal = returnItems.reduce(
(acc, next) => acc + next.totals.total,
0
)
// If the return has a shipping method get the price and any attachments
let shippingTotal = 0
if (returnRequest.shipping_method) {
shippingTotal = returnRequest.shipping_method.price * (1 + taxRate)
const base = returnRequest.shipping_method.price
shippingTotal =
base +
returnRequest.shipping_method.tax_lines.reduce((acc, next) => {
return Math.round(acc + base * (next.rate / 100))
}, 0)
}
const locale = await this.extractLocale(order)
@@ -545,7 +678,7 @@ class SendGridService extends NotificationService {
locale,
has_shipping: !!returnRequest.shipping_method,
email: order.email,
items: this.processItems_(returnItems, taxRate, currencyCode),
items: returnItems,
subtotal: `${this.humanPrice_(
item_subtotal,
currencyCode
@@ -570,11 +703,12 @@ class SendGridService extends NotificationService {
}
}
async swapCreatedData({ id }) {
async swapReceivedData({ id }) {
const store = await this.storeService_.retrieve()
const swap = await this.swapService_.retrieve(id, {
relations: [
"additional_items",
"additional_items.tax_lines",
"return_order",
"return_order.items",
"return_order.items.item",
@@ -585,9 +719,14 @@ class SendGridService extends NotificationService {
const returnRequest = swap.return_order
const items = await this.lineItemService_.list({
id: returnRequest.items.map(({ item_id }) => item_id),
})
const items = await this.lineItemService_.list(
{
id: returnRequest.items.map(({ item_id }) => item_id),
},
{
relations: ["tax_lines"],
}
)
returnRequest.items = returnRequest.items.map((item) => {
const found = items.find((i) => i.id === item.item_id)
@@ -612,42 +751,51 @@ class SendGridService extends NotificationService {
"shipping_address",
"swaps",
"swaps.additional_items",
"swaps.additional_items.tax_lines",
],
})
const taxRate = order.tax_rate / 100
const cart = await this.cartService_.retrieve(swap.cart_id, {
select: [
"total",
"tax_total",
"discount_total",
"shipping_total",
"subtotal",
],
})
const currencyCode = order.currency_code.toUpperCase()
let merged = [...order.items]
const decoratedItems = await Promise.all(
cart.items.map(async (i) => {
const totals = await this.totalsService_.getLineItemTotals(i, cart, {
include_tax: true,
})
// merge items from order with items from order swaps
if (order.swaps && order.swaps.length) {
for (const s of order.swaps) {
merged = [...merged, ...s.additional_items]
}
}
const returnItems = this.processItems_(
swap.return_order.items.map((i) => {
const found = merged.find((oi) => oi.id === i.item_id)
return {
...found,
quantity: i.quantity,
...i,
totals,
price: this.humanPrice_(
totals.subtotal + totals.tax_total,
currencyCode
),
}
}),
taxRate,
currencyCode
})
)
const returnTotal = this.totalsService_.getRefundTotal(order, returnItems)
const returnTotal = decoratedItems.reduce((acc, next) => {
if (next.is_return) {
return acc + -1 * (next.totals.subtotal + next.totals.tax_total)
}
return acc
}, 0)
const constructedOrder = {
...order,
shipping_methods: [],
items: swap.additional_items,
}
const additionalTotal = this.totalsService_.getTotal(constructedOrder)
const additionalTotal = decoratedItems.reduce((acc, next) => {
if (!next.is_return) {
return acc + next.totals.subtotal + next.totals.tax_total
}
return acc
}, 0)
const refundAmount = swap.return_order.refund_amount
@@ -661,8 +809,143 @@ class SendGridService extends NotificationService {
date: swap.updated_at.toDateString(),
swap_link: swapLink,
email: order.email,
items: this.processItems_(swap.additional_items, taxRate, currencyCode),
return_items: returnItems,
items: decoratedItems.filter((di) => !di.is_return),
return_items: decoratedItems.filter((di) => di.is_return),
return_total: `${this.humanPrice_(
returnTotal,
currencyCode
)} ${currencyCode}`,
tax_total: `${this.humanPrice_(
cart.total,
currencyCode
)} ${currencyCode}`,
refund_amount: `${this.humanPrice_(
refundAmount,
currencyCode
)} ${currencyCode}`,
additional_total: `${this.humanPrice_(
additionalTotal,
currencyCode
)} ${currencyCode}`,
}
}
async swapCreatedData({ id }) {
const store = await this.storeService_.retrieve()
const swap = await this.swapService_.retrieve(id, {
relations: [
"additional_items",
"additional_items.tax_lines",
"return_order",
"return_order.items",
"return_order.items.item",
"return_order.shipping_method",
"return_order.shipping_method.shipping_option",
],
})
const returnRequest = swap.return_order
const items = await this.lineItemService_.list(
{
id: returnRequest.items.map(({ item_id }) => item_id),
},
{
relations: ["tax_lines"],
}
)
returnRequest.items = returnRequest.items.map((item) => {
const found = items.find((i) => i.id === item.item_id)
return {
...item,
item: found,
}
})
const swapLink = store.swap_link_template.replace(
/\{cart_id\}/,
swap.cart_id
)
const order = await this.orderService_.retrieve(swap.order_id, {
select: ["total"],
relations: [
"items",
"items.tax_lines",
"discounts",
"discounts.rule",
"discounts.rule.valid_for",
"shipping_address",
"swaps",
"swaps.additional_items",
"swaps.additional_items.tax_lines",
],
})
const cart = await this.cartService_.retrieve(swap.cart_id, {
select: [
"total",
"tax_total",
"discount_total",
"shipping_total",
"subtotal",
],
})
const currencyCode = order.currency_code.toUpperCase()
const decoratedItems = await Promise.all(
cart.items.map(async (i) => {
const totals = await this.totalsService_.getLineItemTotals(i, cart, {
include_tax: true,
})
return {
...i,
totals,
tax_lines: totals.tax_lines,
price: `${this.humanPrice_(
totals.original_total / i.quantity,
currencyCode
)} ${currencyCode}`,
discounted_price: `${this.humanPrice_(
totals.total / i.quantity,
currencyCode
)} ${currencyCode}`,
}
})
)
const returnTotal = decoratedItems.reduce((acc, next) => {
const { total } = next.totals
if (next.is_return && next.variant_id) {
return acc + -1 * total
}
return acc
}, 0)
const additionalTotal = decoratedItems.reduce((acc, next) => {
const { total } = next.totals
if (!next.is_return) {
return acc + total
}
return acc
}, 0)
const refundAmount = swap.return_order.refund_amount
const locale = await this.extractLocale(order)
return {
locale,
swap,
order,
return_request: returnRequest,
date: swap.updated_at.toDateString(),
swap_link: swapLink,
email: order.email,
items: decoratedItems.filter((di) => !di.is_return),
return_items: decoratedItems.filter((di) => di.is_return),
return_total: `${this.humanPrice_(
returnTotal,
currencyCode
@@ -687,7 +970,9 @@ class SendGridService extends NotificationService {
relations: [
"shipping_address",
"shipping_methods",
"shipping_methods.tax_lines",
"additional_items",
"additional_items.tax_lines",
"return_order",
"return_order.items",
],
@@ -695,40 +980,68 @@ class SendGridService extends NotificationService {
const order = await this.orderService_.retrieve(swap.order_id, {
relations: [
"region",
"items",
"items.tax_lines",
"discounts",
"discounts.rule",
"discounts.rule.valid_for",
"swaps",
"swaps.additional_items",
"swaps.additional_items.tax_lines",
],
})
let merged = [...order.items]
const cart = await this.cartService_.retrieve(swap.cart_id, {
select: [
"total",
"tax_total",
"discount_total",
"shipping_total",
"subtotal",
],
})
// merge items from order with items from order swaps
if (order.swaps && order.swaps.length) {
for (const s of order.swaps) {
merged = [...merged, ...s.additional_items]
const returnRequest = swap.return_order
const items = await this.lineItemService_.list(
{
id: returnRequest.items.map(({ item_id }) => item_id),
},
{
relations: ["tax_lines"],
}
}
)
const taxRate = order.tax_rate / 100
const currencyCode = order.currency_code.toUpperCase()
const returnItems = this.processItems_(
swap.return_order.items.map((i) => {
const found = merged.find((oi) => oi.id === i.item_id)
const returnItems = await Promise.all(
swap.return_order.items.map(async (i) => {
const found = items.find((oi) => oi.id === i.item_id)
const totals = await this.totalsService_.getLineItemTotals(i, cart, {
include_tax: true,
})
return {
...found,
thumbnail: this.normalizeThumbUrl_(found.thumbnail),
price: `${this.humanPrice_(
totals.original_total / i.quantity,
currencyCode
)} ${currencyCode}`,
discounted_price: `${this.humanPrice_(
totals.total / i.quantity,
currencyCode
)} ${currencyCode}`,
quantity: i.quantity,
}
}),
taxRate,
currencyCode
})
)
const returnTotal = this.totalsService_.getRefundTotal(order, returnItems)
const returnTotal = await this.totalsService_.getRefundTotal(
order,
returnItems
)
const constructedOrder = {
...order,
@@ -736,7 +1049,7 @@ class SendGridService extends NotificationService {
items: swap.additional_items,
}
const additionalTotal = this.totalsService_.getTotal(constructedOrder)
const additionalTotal = await this.totalsService_.getTotal(constructedOrder)
const refundAmount = swap.return_order.refund_amount
@@ -750,11 +1063,31 @@ class SendGridService extends NotificationService {
locale,
swap,
order,
items: this.processItems_(swap.additional_items, taxRate, currencyCode),
items: await Promise.all(
swap.additional_items.map(async (i) => {
const totals = await this.totalsService_.getLineItemTotals(i, cart, {
include_tax: true,
})
return {
...i,
thumbnail: this.normalizeThumbUrl_(i.thumbnail),
price: `${this.humanPrice_(
totals.original_total / i.quantity,
currencyCode
)} ${currencyCode}`,
discounted_price: `${this.humanPrice_(
totals.total / i.quantity,
currencyCode
)} ${currencyCode}`,
quantity: i.quantity,
}
})
),
date: swap.updated_at.toDateString(),
email: order.email,
tax_amount: `${this.humanPrice_(
swap.difference_due * taxRate,
cart.tax_total,
currencyCode
)} ${currencyCode}`,
paid_total: `${this.humanPrice_(
@@ -871,7 +1204,7 @@ class SendGridService extends NotificationService {
if (fromOrder.cart_id) {
try {
const cart = await this.cartService_.retrieve(fromOrder.cart_id, {
select: ["context"],
select: ["id", "context"],
})
if (cart.context && cart.context.locale) {
@@ -61,8 +61,6 @@ class SlackService extends BaseService {
const { subtotal, tax_total, discount_total, shipping_total, total } = order
const currencyCode = order.currency_code.toUpperCase()
const taxRate = order.tax_rate / 100
const getDisplayAmount = (amount) => {
const humanAmount = humanizeAmount(amount, currencyCode)
if (zeroDecimalCurrencies.includes(currencyCode.toLowerCase())) {
@@ -141,13 +139,20 @@ class SlackService extends BaseService {
type: "divider",
})
order.items.forEach((lineItem) => {
for (const lineItem of order.items) {
const totals = await this.totalsService_.getLineItemTotals(
lineItem,
order,
{
include_tax: true,
}
)
let line = {
type: "section",
text: {
type: "mrkdwn",
text: `*${lineItem.title}*\n${lineItem.quantity} x ${getDisplayAmount(
lineItem.unit_price * (1 + taxRate)
totals.original_total
)} ${currencyCode}`,
},
}
@@ -170,7 +175,7 @@ class SlackService extends BaseService {
blocks.push({
type: "divider",
})
})
}
return axios.post(this.options_.slack_url, {
text: `Order ${order.display_id} was processed`,
@@ -8,6 +8,7 @@ export * from "./gift-cards"
export * from "./orders"
export * from "./products"
export * from "./product-tags"
export * from "./product-types"
export * from "./return-reasons"
export * from "./regions"
export * from "./shipping-options"
@@ -20,3 +21,4 @@ export * from "./store"
export * from "./swaps"
export * from "./users"
export * from "./variants"
export * from "./tax-rates"
@@ -0,0 +1 @@
export * from "./queries"
@@ -0,0 +1,34 @@
import {
AdminProductTypesListRes,
AdminGetProductTypesParams,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useQuery } from "react-query"
import { useMedusa } from "../../../contexts"
import { UseQueryOptionsWrapper } from "../../../types"
import { queryKeysFactory } from "../../utils/index"
const ADMIN_PRODUCT_TYPES_QUERY_KEY = `admin_product_types` as const
export const adminProductTypeKeys = queryKeysFactory(
ADMIN_PRODUCT_TYPES_QUERY_KEY
)
type ProductTypesQueryKeys = typeof adminProductTypeKeys
export const useAdminProductTypes = (
query?: AdminGetProductTypesParams,
options?: UseQueryOptionsWrapper<
Response<AdminProductTypesListRes>,
Error,
ReturnType<ProductTypesQueryKeys["list"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
adminProductTypeKeys.list(query),
() => client.admin.productTypes.list(query),
options
)
return { ...data, ...rest } as const
}
@@ -2,7 +2,6 @@ import {
AdminProductsListRes,
AdminProductsRes,
AdminGetProductsParams,
AdminProductsListTypesRes,
AdminProductsListTagsRes,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
@@ -51,22 +50,6 @@ export const useAdminProduct = (
return { ...data, ...rest } as const
}
export const useAdminProductTypes = (
options?: UseQueryOptionsWrapper<
Response<AdminProductsListTypesRes>,
Error,
ReturnType<ProductQueryKeys["detail"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
adminProductKeys.detail("types"),
() => client.admin.products.listTypes(),
options
)
return { ...data, ...rest } as const
}
export const useAdminProductTagUsage = (
options?: UseQueryOptionsWrapper<
Response<AdminProductsListTagsRes>,
Loaded 100 of 223 files, more files were not shown because too many files have changed in this diff. Show more