feat: creates support for swaps on the storefront (#355)

This commit is contained in:
Sebastian Rindom
2021-08-30 12:14:56 +02:00
committed by GitHub
parent bd1e115696
commit ae82cfc70a
20 changed files with 2122 additions and 1170 deletions
@@ -0,0 +1,365 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/store/carts /store/swaps simple swap 1`] = `
Object {
"swap": Object {
"additional_items": Array [
Object {
"allow_discounts": true,
"cart_id": StringMatching /\\^cart_\\*/,
"claim_order_id": null,
"created_at": Any<String>,
"description": "Swap product",
"fulfilled_quantity": null,
"has_shipping": null,
"id": StringMatching /\\^item_\\*/,
"is_giftcard": false,
"metadata": Object {},
"order_id": null,
"quantity": 1,
"returned_quantity": null,
"shipped_quantity": null,
"should_merge": true,
"swap_id": StringMatching /\\^swap_\\*/,
"thumbnail": null,
"title": "test product",
"unit_price": 8000,
"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-2",
"inventory_quantity": 1,
"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,
"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_\\*/,
"subtitle": null,
"thumbnail": null,
"title": "test product",
"type_id": null,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
"product_id": "test-product",
"sku": null,
"title": "Swap product",
"upc": null,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
"variant_id": "test-variant-2",
},
],
"cart": Object {
"billing_address_id": "test-billing-address",
"completed_at": null,
"context": null,
"created_at": Any<String>,
"customer_id": "test-customer",
"deleted_at": null,
"email": "test@email.com",
"id": StringMatching /\\^cart_\\*/,
"idempotency_key": null,
"metadata": Object {
"parent_order_id": "test-order",
"swap_id": StringMatching /\\^swap_\\*/,
},
"payment_id": null,
"region_id": "test-region",
"shipping_address_id": "test-shipping-address",
"type": "swap",
"updated_at": Any<String>,
},
"cart_id": StringMatching /\\^cart_\\*/,
"confirmed_at": null,
"created_at": Any<String>,
"deleted_at": null,
"difference_due": null,
"fulfillment_status": "not_fulfilled",
"fulfillments": Array [],
"id": StringMatching /\\^swap_\\*/,
"idempotency_key": Any<String>,
"metadata": null,
"order": Object {
"billing_address_id": "test-billing-address",
"canceled_at": null,
"cart_id": null,
"created_at": Any<String>,
"currency_code": "usd",
"customer_id": "test-customer",
"display_id": 1,
"draft_order_id": null,
"email": "test@email.com",
"fulfillment_status": "fulfilled",
"id": "test-order",
"idempotency_key": null,
"metadata": null,
"no_notification": null,
"payment_status": "captured",
"region_id": "test-region",
"shipping_address_id": "test-shipping-address",
"status": "pending",
"tax_rate": 0,
"updated_at": Any<String>,
},
"order_id": "test-order",
"payment": null,
"payment_status": "not_paid",
"return_order": Object {
"claim_order_id": null,
"created_at": Any<String>,
"id": StringMatching /\\^ret_\\*/,
"idempotency_key": null,
"items": Array [
Object {
"is_requested": true,
"item_id": "test-item",
"metadata": null,
"note": null,
"quantity": 1,
"reason_id": null,
"received_quantity": null,
"requested_quantity": 1,
"return_id": StringMatching /\\^ret_\\*/,
},
],
"metadata": null,
"no_notification": true,
"order_id": null,
"received_at": null,
"refund_amount": 7200,
"shipping_data": null,
"shipping_method": null,
"status": "requested",
"swap_id": StringMatching /\\^swap_\\*/,
"updated_at": Any<String>,
},
"shipping_address": null,
"shipping_address_id": null,
"shipping_methods": Array [],
"updated_at": Any<String>,
},
}
`;
exports[`/store/carts /store/swaps swap with return shipping 1`] = `
Object {
"swap": Object {
"additional_items": Array [
Object {
"allow_discounts": true,
"cart_id": StringMatching /\\^cart_\\*/,
"claim_order_id": null,
"created_at": Any<String>,
"description": "Swap product",
"fulfilled_quantity": null,
"has_shipping": null,
"id": StringMatching /\\^item_\\*/,
"is_giftcard": false,
"metadata": Object {},
"order_id": null,
"quantity": 1,
"returned_quantity": null,
"shipped_quantity": null,
"should_merge": true,
"swap_id": StringMatching /\\^swap_\\*/,
"thumbnail": null,
"title": "test product",
"unit_price": 8000,
"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-2",
"inventory_quantity": 1,
"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,
"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_\\*/,
"subtitle": null,
"thumbnail": null,
"title": "test product",
"type_id": null,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
"product_id": "test-product",
"sku": null,
"title": "Swap product",
"upc": null,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
"variant_id": "test-variant-2",
},
],
"cart": Object {
"billing_address_id": "test-billing-address",
"completed_at": null,
"context": null,
"created_at": Any<String>,
"customer_id": "test-customer",
"deleted_at": null,
"email": "test@email.com",
"id": StringMatching /\\^cart_\\*/,
"idempotency_key": null,
"metadata": Object {
"parent_order_id": "test-order",
"swap_id": StringMatching /\\^swap_\\*/,
},
"payment_id": null,
"region_id": "test-region",
"shipping_address_id": "test-shipping-address",
"type": "swap",
"updated_at": Any<String>,
},
"cart_id": StringMatching /\\^cart_\\*/,
"confirmed_at": null,
"created_at": Any<String>,
"deleted_at": null,
"difference_due": null,
"fulfillment_status": "not_fulfilled",
"fulfillments": Array [],
"id": StringMatching /\\^swap_\\*/,
"idempotency_key": Any<String>,
"metadata": null,
"order": Object {
"billing_address_id": "test-billing-address",
"canceled_at": null,
"cart_id": null,
"created_at": Any<String>,
"currency_code": "usd",
"customer_id": "test-customer",
"display_id": 2,
"draft_order_id": null,
"email": "test@email.com",
"fulfillment_status": "fulfilled",
"id": "test-order",
"idempotency_key": null,
"metadata": null,
"no_notification": null,
"payment_status": "captured",
"region_id": "test-region",
"shipping_address_id": "test-shipping-address",
"status": "pending",
"tax_rate": 0,
"updated_at": Any<String>,
},
"order_id": "test-order",
"payment": null,
"payment_status": "not_paid",
"return_order": Object {
"claim_order_id": null,
"created_at": Any<String>,
"id": StringMatching /\\^ret_\\*/,
"idempotency_key": null,
"items": Array [
Object {
"is_requested": true,
"item_id": "test-item",
"metadata": null,
"note": null,
"quantity": 1,
"reason_id": null,
"received_quantity": null,
"requested_quantity": 1,
"return_id": StringMatching /\\^ret_\\*/,
},
],
"metadata": null,
"no_notification": true,
"order_id": null,
"received_at": null,
"refund_amount": 6200,
"shipping_data": Object {},
"shipping_method": Object {
"cart_id": null,
"claim_order_id": null,
"data": Object {},
"id": StringMatching /\\^sm_\\*/,
"order_id": null,
"price": 1000,
"return_id": StringMatching /\\^ret_\\*/,
"shipping_option": Object {
"admin_only": false,
"amount": 1000,
"created_at": Any<String>,
"data": Object {},
"deleted_at": null,
"id": "return-option",
"is_return": true,
"metadata": null,
"name": "Test ret",
"price_type": "flat_rate",
"profile_id": StringMatching /\\^sp_\\*/,
"provider_id": "test-ful",
"region_id": "test-region",
"updated_at": Any<String>,
},
"shipping_option_id": "return-option",
"swap_id": null,
},
"status": "requested",
"swap_id": StringMatching /\\^swap_\\*/,
"updated_at": Any<String>,
},
"shipping_address": null,
"shipping_address_id": null,
"shipping_methods": Array [],
"updated_at": Any<String>,
},
}
`;
+55 -116
View File
@@ -1,4 +1,4 @@
const path = require("path");
const path = require("path")
const {
Region,
Order,
@@ -12,110 +12,49 @@ const {
Cart,
ShippingMethod,
Swap,
} = require("@medusajs/medusa");
} = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server");
const { useApi } = require("../../../helpers/use-api");
const { initDb, useDb } = require("../../../helpers/use-db");
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const swapSeeder = require("../../helpers/swap-seeder");
const cartSeeder = require("../../helpers/cart-seeder");
const swapSeeder = require("../../helpers/swap-seeder")
const cartSeeder = require("../../helpers/cart-seeder")
jest.setTimeout(30000);
jest.setTimeout(30000)
describe("/store/carts", () => {
let medusaProcess;
let dbConnection;
let medusaProcess
let dbConnection
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."));
dbConnection = await initDb({ cwd });
medusaProcess = await setupServer({ cwd });
});
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
const db = useDb();
await db.shutdown();
medusaProcess.kill();
});
describe("/store/swaps", () => {
beforeEach(async () => {
try {
await cartSeeder(dbConnection);
await swapSeeder(dbConnection);
const manager = dbConnection.manager;
await manager.query(
`UPDATE "swap" SET cart_id='test-cart-2' WHERE id = 'test-swap'`
);
await manager.query(
`UPDATE "payment" SET swap_id=NULL WHERE id = 'test-payment-swap'`
);
} catch (err) {
console.log(err);
throw err;
}
});
afterEach(async () => {
const db = useDb();
await db.teardown();
});
it("creates a swap from a cart id", async () => {
const api = useApi();
const getRes = await api.post("/store/swaps", {
cart_id: "test-cart-2",
});
expect(getRes.status).toEqual(200);
});
it("fails due to partial inventory", async () => {
const api = useApi();
const manager = dbConnection.manager;
const li = manager.create(LineItem, {
id: "test-item-with-no-stock",
title: "No Stock Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant-2",
cart_id: "test-cart-2",
});
await manager.save(li);
try {
await api.post("/store/swaps", {
cart_id: "test-cart-2",
});
} catch (e) {
expect(e.response.data.message).toEqual(
"Variant with id: test-variant-2 does not have the required inventory"
);
}
});
});
const db = useDb()
await db.shutdown()
medusaProcess.kill()
})
describe("GET /store/orders", () => {
beforeEach(async () => {
const manager = dbConnection.manager;
const manager = dbConnection.manager
await manager.query(
`ALTER SEQUENCE order_display_id_seq RESTART WITH 111`
);
)
await manager.insert(Region, {
id: "region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
});
})
await manager.insert(Customer, {
id: "cus_1234",
email: "test@email.com",
});
})
await manager.insert(Order, {
id: "order_test",
email: "test@email.com",
@@ -124,17 +63,17 @@ describe("/store/carts", () => {
region_id: "region",
tax_rate: 0,
currency_code: "usd",
});
})
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",
@@ -147,7 +86,7 @@ describe("/store/carts", () => {
value: "Size",
},
],
});
})
await manager.insert(LineItem, {
id: "test-item",
@@ -158,59 +97,59 @@ describe("/store/carts", () => {
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
});
});
})
})
afterEach(async () => {
const db = useDb();
await db.teardown();
});
const db = useDb()
await db.teardown()
})
it("looks up order", async () => {
const api = useApi();
const api = useApi()
const response = await api
.get("/store/orders?display_id=111&email=test@email.com")
.catch((err) => {
return err.response;
});
expect(response.status).toEqual(200);
expect(response.data.order.display_id).toEqual(111);
expect(response.data.order.email).toEqual("test@email.com");
});
return err.response
})
expect(response.status).toEqual(200)
expect(response.data.order.display_id).toEqual(111)
expect(response.data.order.email).toEqual("test@email.com")
})
it("fails if display_id + email not provided", async () => {
const api = useApi();
const api = useApi()
const response = await api
.get("/store/orders?display_id=111")
.catch((err) => {
return err.response;
});
expect(response.status).toEqual(400);
});
return err.response
})
expect(response.status).toEqual(400)
})
it("fails if display_id + email not provided", async () => {
const api = useApi();
const api = useApi()
const response = await api
.get("/store/orders?email=test@email.com")
.catch((err) => {
return err.response;
});
expect(response.status).toEqual(400);
});
return err.response
})
expect(response.status).toEqual(400)
})
it("fails if email not correct", async () => {
const api = useApi();
const api = useApi()
const response = await api
.get("/store/orders?display_id=111&email=test1@email.com")
.catch((err) => {
return err.response;
});
return err.response
})
expect(response.status).toEqual(404);
});
});
});
expect(response.status).toEqual(404)
})
})
})
@@ -0,0 +1,258 @@
const path = require("path")
const {
Region,
Order,
Customer,
ShippingProfile,
Product,
ProductVariant,
MoneyAmount,
LineItem,
Payment,
Cart,
ShippingMethod,
ShippingOption,
Swap,
} = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const orderSeeder = require("../../helpers/order-seeder")
jest.setTimeout(30000)
describe("/store/carts", () => {
let medusaProcess
let dbConnection
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
medusaProcess.kill()
})
describe("/store/swaps", () => {
beforeEach(async () => {
try {
await orderSeeder(dbConnection)
const manager = dbConnection.manager
await manager.query(
`UPDATE "swap" SET cart_id='test-cart-2' WHERE id = 'test-swap'`
)
await manager.query(
`UPDATE "payment" SET swap_id=NULL WHERE id = 'test-payment-swap'`
)
const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default",
})
await manager.insert(ShippingOption, {
id: "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,
})
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("simple swap", async () => {
const api = useApi()
const response = await api
.post("/store/swaps", {
order_id: "test-order",
return_items: [
{
item_id: "test-item",
quantity: 1,
},
],
additional_items: [
{
variant_id: "test-variant-2",
quantity: 1,
},
],
})
.catch((err) => {
console.log(err.response.data.message)
})
expect(response.data).toMatchSnapshot({
swap: {
id: expect.stringMatching(/^swap_*/),
idempotency_key: expect.any(String),
additional_items: [
{
id: expect.stringMatching(/^item_*/),
cart_id: expect.stringMatching(/^cart_*/),
swap_id: expect.stringMatching(/^swap_*/),
variant: {
id: "test-variant-2",
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),
},
},
quantity: 1,
variant_id: "test-variant-2",
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
order: {
id: "test-order",
created_at: expect.any(String),
updated_at: expect.any(String),
},
cart_id: expect.stringMatching(/^cart_*/),
cart: {
id: expect.stringMatching(/^cart_*/),
billing_address_id: "test-billing-address",
type: "swap",
created_at: expect.any(String),
updated_at: expect.any(String),
metadata: {
swap_id: expect.stringMatching(/^swap_*/),
},
},
return_order: {
id: expect.stringMatching(/^ret_*/),
swap_id: expect.stringMatching(/^swap_*/),
refund_amount: 7200,
items: [
{
item_id: "test-item",
quantity: 1,
return_id: expect.stringMatching(/^ret_*/),
},
],
created_at: expect.any(String),
updated_at: expect.any(String),
},
created_at: expect.any(String),
updated_at: expect.any(String),
},
})
})
it("swap with return shipping", async () => {
const api = useApi()
const response = await api
.post("/store/swaps", {
order_id: "test-order",
return_items: [
{
item_id: "test-item",
quantity: 1,
},
],
return_shipping_option: "return-option",
additional_items: [
{
variant_id: "test-variant-2",
quantity: 1,
},
],
})
.catch((err) => {
console.log(err.response.data.message)
})
expect(response.data).toMatchSnapshot({
swap: {
id: expect.stringMatching(/^swap_*/),
idempotency_key: expect.any(String),
additional_items: [
{
id: expect.stringMatching(/^item_*/),
cart_id: expect.stringMatching(/^cart_*/),
swap_id: expect.stringMatching(/^swap_*/),
variant: {
id: "test-variant-2",
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),
},
},
quantity: 1,
variant_id: "test-variant-2",
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
order: {
id: "test-order",
created_at: expect.any(String),
updated_at: expect.any(String),
},
cart_id: expect.stringMatching(/^cart_*/),
cart: {
id: expect.stringMatching(/^cart_*/),
billing_address_id: "test-billing-address",
type: "swap",
created_at: expect.any(String),
updated_at: expect.any(String),
metadata: {
swap_id: expect.stringMatching(/^swap_*/),
},
},
return_order: {
id: expect.stringMatching(/^ret_*/),
swap_id: expect.stringMatching(/^swap_*/),
refund_amount: 6200,
shipping_method: {
id: expect.stringMatching(/^sm_*/),
return_id: expect.stringMatching(/^ret_*/),
shipping_option: {
profile_id: expect.stringMatching(/^sp_*/),
created_at: expect.any(String),
updated_at: expect.any(String),
},
},
items: [
{
item_id: "test-item",
quantity: 1,
return_id: expect.stringMatching(/^ret_*/),
},
],
created_at: expect.any(String),
updated_at: expect.any(String),
},
created_at: expect.any(String),
updated_at: expect.any(String),
},
})
})
})
})