feat(medusa): Swaps on swaps (#229)
Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
committed by
GitHub
parent
2f3e3fde80
commit
f8f1f57fa1
@@ -7,6 +7,7 @@ const { useApi } = require("../../../helpers/use-api");
|
||||
const { initDb } = require("../../../helpers/use-db");
|
||||
|
||||
const orderSeeder = require("../../helpers/order-seeder");
|
||||
const swapSeeder = require("../../helpers/swap-seeder");
|
||||
const adminSeeder = require("../../helpers/admin-seeder");
|
||||
|
||||
jest.setTimeout(30000);
|
||||
@@ -706,4 +707,286 @@ describe("/admin/orders", () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /admin/orders/:id/swaps", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection);
|
||||
await orderSeeder(dbConnection);
|
||||
await swapSeeder(dbConnection);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
const manager = dbConnection.manager;
|
||||
await manager.query(`DELETE FROM "fulfillment_item"`);
|
||||
await manager.query(`DELETE FROM "fulfillment"`);
|
||||
await manager.query(`DELETE FROM "return_item"`);
|
||||
await manager.query(`DELETE FROM "return_reason"`);
|
||||
await manager.query(`DELETE FROM "return"`);
|
||||
await manager.query(`DELETE FROM "claim_image"`);
|
||||
await manager.query(`DELETE FROM "claim_tag"`);
|
||||
await manager.query(`DELETE FROM "claim_item"`);
|
||||
await manager.query(`DELETE FROM "shipping_method"`);
|
||||
await manager.query(`DELETE FROM "line_item"`);
|
||||
await manager.query(`DELETE FROM "payment"`);
|
||||
await manager.query(`DELETE FROM "swap"`);
|
||||
await manager.query(`DELETE FROM "cart"`);
|
||||
await manager.query(`DELETE FROM "claim_order"`);
|
||||
await manager.query(`DELETE FROM "money_amount"`);
|
||||
await manager.query(`DELETE FROM "product_variant"`);
|
||||
await manager.query(`DELETE FROM "product"`);
|
||||
await manager.query(`DELETE FROM "shipping_option"`);
|
||||
await manager.query(`DELETE FROM "discount"`);
|
||||
await manager.query(`DELETE FROM "refund"`);
|
||||
await manager.query(`DELETE FROM "order"`);
|
||||
await manager.query(`DELETE FROM "customer"`);
|
||||
await manager.query(
|
||||
`UPDATE "country" SET region_id=NULL WHERE iso_2 = 'us'`
|
||||
);
|
||||
await manager.query(`DELETE FROM "region"`);
|
||||
await manager.query(`DELETE FROM "user"`);
|
||||
});
|
||||
|
||||
it("creates a swap", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const response = await api.post(
|
||||
"/admin/orders/test-order/swaps",
|
||||
{
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
additional_items: [{ variant_id: "test-variant-2", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
|
||||
it("creates a swap and a return", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const returnedOrderFirst = await api.post(
|
||||
"/admin/orders/order-with-swap/return",
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item-many",
|
||||
quantity: 2,
|
||||
},
|
||||
],
|
||||
receive_now: true,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(returnedOrderFirst.status).toEqual(200);
|
||||
|
||||
const returnedOrderSecond = await api.post(
|
||||
"/admin/orders/order-with-swap/return",
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item-many",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
receive_now: true,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(returnedOrderSecond.status).toEqual(200);
|
||||
expect(returnedOrderSecond.data.order.items[1].returned_quantity).toBe(3);
|
||||
});
|
||||
|
||||
it("creates a swap and receives the items", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const createdSwapOrder = await api.post(
|
||||
"/admin/orders/test-order/swaps",
|
||||
{
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
additional_items: [{ variant_id: "test-variant-2", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(createdSwapOrder.status).toEqual(200);
|
||||
|
||||
const swap = createdSwapOrder.data.order.swaps[0];
|
||||
|
||||
const receivedSwap = await api.post(
|
||||
`/admin/returns/${swap.return_order.id}/receive`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(receivedSwap.status).toEqual(200);
|
||||
expect(receivedSwap.data.return.status).toBe("received");
|
||||
});
|
||||
|
||||
it("creates a swap on a swap", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const swapOnSwap = await api.post(
|
||||
"/admin/orders/order-with-swap/swaps",
|
||||
{
|
||||
return_items: [
|
||||
{
|
||||
item_id: "test-item-swapped",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
additional_items: [{ variant_id: "test-variant", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(swapOnSwap.status).toEqual(200);
|
||||
});
|
||||
|
||||
it("receives a swap on swap", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const received = await api.post(
|
||||
`/admin/returns/return-on-swap/receive`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item-swapped",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(received.status).toEqual(200);
|
||||
});
|
||||
|
||||
it("creates a return on a swap", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const returnOnSwap = await api.post(
|
||||
"/admin/orders/order-with-swap/return",
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item-swapped",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(returnOnSwap.status).toEqual(200);
|
||||
});
|
||||
|
||||
it("creates a return on an order", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const returnOnOrder = await api.post(
|
||||
"/admin/orders/test-order/return",
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(returnOnOrder.status).toEqual(200);
|
||||
|
||||
const captured = await api.post(
|
||||
"/admin/orders/test-order/capture",
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const returnId = returnOnOrder.data.order.returns[0].id;
|
||||
|
||||
const received = await api.post(
|
||||
`/admin/returns/${returnId}/receive`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item",
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(received.status).toEqual(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,6 +10,7 @@ const {
|
||||
ProductVariant,
|
||||
Region,
|
||||
Order,
|
||||
Swap,
|
||||
} = require("@medusajs/medusa");
|
||||
|
||||
module.exports = async (connection, data = {}) => {
|
||||
@@ -39,6 +40,26 @@ module.exports = async (connection, data = {}) => {
|
||||
],
|
||||
});
|
||||
|
||||
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",
|
||||
@@ -77,6 +98,8 @@ module.exports = async (connection, data = {}) => {
|
||||
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",
|
||||
@@ -115,27 +138,31 @@ module.exports = async (connection, data = {}) => {
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test",
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
},
|
||||
],
|
||||
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",
|
||||
},
|
||||
],
|
||||
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",
|
||||
|
||||
159
integration-tests/api/helpers/swap-seeder.js
Normal file
159
integration-tests/api/helpers/swap-seeder.js
Normal file
@@ -0,0 +1,159 @@
|
||||
const {
|
||||
ShippingProfile,
|
||||
Customer,
|
||||
MoneyAmount,
|
||||
LineItem,
|
||||
Country,
|
||||
ShippingOption,
|
||||
ShippingMethod,
|
||||
Product,
|
||||
ProductVariant,
|
||||
Region,
|
||||
Order,
|
||||
Swap,
|
||||
Return,
|
||||
} = require("@medusajs/medusa");
|
||||
|
||||
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",
|
||||
data: {},
|
||||
},
|
||||
],
|
||||
items: [],
|
||||
...data,
|
||||
});
|
||||
|
||||
orderWithSwap = await manager.save(orderWithSwap);
|
||||
|
||||
const li = manager.create(LineItem, {
|
||||
id: "test-item-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: orderWithSwap.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 swap = manager.create(Swap, {
|
||||
id: "test-swap",
|
||||
order_id: "order-with-swap",
|
||||
payment_status: "captured",
|
||||
fulfillment_status: "fulfilled",
|
||||
payment: {
|
||||
id: "test-payment-swap",
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test",
|
||||
data: {},
|
||||
},
|
||||
additional_items: [
|
||||
{
|
||||
id: "test-item-swapped",
|
||||
fulfilled_quantity: 1,
|
||||
title: "Line Item",
|
||||
description: "Line Item Desc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 8000,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant-2",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await manager.save(swap);
|
||||
|
||||
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",
|
||||
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: {},
|
||||
});
|
||||
};
|
||||
@@ -8,15 +8,15 @@
|
||||
"build": "babel src -d dist --extensions \".ts,.js\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/medusa": "1.1.13-dev-1615987548667",
|
||||
"medusa-interfaces": "1.1.3-dev-1615987548667",
|
||||
"@medusajs/medusa": "1.1.19-dev-1618904018564",
|
||||
"medusa-interfaces": "1.1.7-dev-1618904018564",
|
||||
"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.0-dev-1615987548667",
|
||||
"babel-preset-medusa-package": "1.1.0-dev-1618904018564",
|
||||
"jest": "^26.6.3"
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user