fix: include discount rule in swap retrieval (#682)
This commit is contained in:
@@ -72,6 +72,36 @@ describe("/admin/swaps", () => {
|
||||
expect(response.data.swap.cart).toHaveProperty("discount_total")
|
||||
expect(response.data.swap.cart).toHaveProperty("gift_card_total")
|
||||
})
|
||||
|
||||
it("gets a swap with a discount", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/swaps/disc-swap", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.swap).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "disc-swap",
|
||||
})
|
||||
)
|
||||
|
||||
expect(response.data.swap.cart).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "disc-swap-cart",
|
||||
discount_total: -800,
|
||||
shipping_total: 1000,
|
||||
subtotal: -8000,
|
||||
total: -6200,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/swaps/", () => {
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
const {
|
||||
ShippingProfile,
|
||||
Customer,
|
||||
MoneyAmount,
|
||||
Discount,
|
||||
DiscountRule,
|
||||
LineItem,
|
||||
Country,
|
||||
ShippingOption,
|
||||
ShippingMethod,
|
||||
Product,
|
||||
ProductVariant,
|
||||
Region,
|
||||
Payment,
|
||||
Order,
|
||||
Swap,
|
||||
Cart,
|
||||
@@ -334,4 +327,108 @@ module.exports = async (connection, data = {}) => {
|
||||
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: {},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -953,18 +953,6 @@
|
||||
exec-sh "^0.3.2"
|
||||
minimist "^1.2.0"
|
||||
|
||||
"@hapi/hoek@^9.0.0":
|
||||
version "9.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.2.1.tgz#9551142a1980503752536b5050fd99f4a7f13b17"
|
||||
integrity sha512-gfta+H8aziZsm8pZa0vj04KO6biEiisppNgA1kbJvFrrWu9Vm7eaUEy76DIxsuTaWvti5fkJVhllWc6ZTE+Mdw==
|
||||
|
||||
"@hapi/topo@^5.0.0":
|
||||
version "5.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012"
|
||||
integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==
|
||||
dependencies:
|
||||
"@hapi/hoek" "^9.0.0"
|
||||
|
||||
"@istanbuljs/load-nyc-config@^1.0.0":
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
|
||||
@@ -1166,23 +1154,6 @@
|
||||
readdirp "^2.2.1"
|
||||
upath "^1.1.1"
|
||||
|
||||
"@sideway/address@^4.1.0":
|
||||
version "4.1.2"
|
||||
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.2.tgz#811b84333a335739d3969cfc434736268170cad1"
|
||||
integrity sha512-idTz8ibqWFrPU8kMirL0CoPH/A29XOzzAzpyN3zQ4kAWnzmNfFmRaoMNN6VI8ske5M73HZyhIaW4OuSFIdM4oA==
|
||||
dependencies:
|
||||
"@hapi/hoek" "^9.0.0"
|
||||
|
||||
"@sideway/formula@^3.0.0":
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c"
|
||||
integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==
|
||||
|
||||
"@sideway/pinpoint@^2.0.0":
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df"
|
||||
integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==
|
||||
|
||||
"@sinonjs/commons@^1.7.0":
|
||||
version "1.8.3"
|
||||
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.3.tgz#3802ddd21a50a949b6721ddd72da36e67e7f1b2d"
|
||||
@@ -3531,22 +3502,6 @@ jest@^25.5.2:
|
||||
import-local "^3.0.2"
|
||||
jest-cli "^25.5.4"
|
||||
|
||||
joi-objectid@^3.0.1:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/joi-objectid/-/joi-objectid-3.0.1.tgz#63ace7860f8e1a993a28d40c40ffd8eff01a3668"
|
||||
integrity sha512-V/3hbTlGpvJ03Me6DJbdBI08hBTasFOmipsauOsxOSnsF1blxV537WTl1zPwbfcKle4AK0Ma4OPnzMH4LlvTpQ==
|
||||
|
||||
joi@^17.3.0:
|
||||
version "17.4.2"
|
||||
resolved "https://registry.yarnpkg.com/joi/-/joi-17.4.2.tgz#02f4eb5cf88e515e614830239379dcbbe28ce7f7"
|
||||
integrity sha512-Lm56PP+n0+Z2A2rfRvsfWVDXGEWjXxatPopkQ8qQ5mxCEhwHG+Ettgg5o98FFaxilOxozoa14cFhrE/hOzh/Nw==
|
||||
dependencies:
|
||||
"@hapi/hoek" "^9.0.0"
|
||||
"@hapi/topo" "^5.0.0"
|
||||
"@sideway/address" "^4.1.0"
|
||||
"@sideway/formula" "^3.0.0"
|
||||
"@sideway/pinpoint" "^2.0.0"
|
||||
|
||||
js-tokens@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
|
||||
@@ -3777,21 +3732,6 @@ 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.20, medusa-core-utils@^1.1.23:
|
||||
version "1.1.23"
|
||||
resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.23.tgz#1e9260d5066117350dbf56dc176dd25e488e90a1"
|
||||
integrity sha512-3KnfbCGud09xYC7qrc+kw2XmBFa4UuGBOPT2IU3j70BjFhEU9ftD45oolKXNUziOZIIGRf/FIwGj+Lwgy6IAfA==
|
||||
dependencies:
|
||||
joi "^17.3.0"
|
||||
joi-objectid "^3.0.1"
|
||||
|
||||
medusa-interfaces@^1.1.23:
|
||||
version "1.1.24"
|
||||
resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-1.1.24.tgz#13d0bc4afbd56c028c9e4bc562b0ce7a2023453c"
|
||||
integrity sha512-jzdHRN6AmX2Tuh3nbZd9PQJ0z1b6TzAAfyjak2rmakfoXzv19cAcXq5iUV04kQk+659Q+A14ei1LD/IemMYDmA==
|
||||
dependencies:
|
||||
medusa-core-utils "^1.1.23"
|
||||
|
||||
meilisearch@^0.20.0:
|
||||
version "0.20.0"
|
||||
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.20.0.tgz#42899fec7a2ddefcd035e30ed5dd47aa65a6727f"
|
||||
|
||||
@@ -128,6 +128,8 @@ class SwapService extends BaseService {
|
||||
relationSet.add("cart.items")
|
||||
relationSet.add("cart.gift_cards")
|
||||
relationSet.add("cart.discounts")
|
||||
relationSet.add("cart.discounts.rule")
|
||||
relationSet.add("cart.discounts.rule.valid_for")
|
||||
relationSet.add("cart.shipping_methods")
|
||||
relationSet.add("cart.region")
|
||||
relations = [...relationSet]
|
||||
|
||||
Reference in New Issue
Block a user