feat(medusa): Attach or update cart sales channel (#1873)
What Allow to create a cart with a sales channel, otherwise the default one is attached. Also allow to update the sales channel on an existing cart and in that case the line items that does not belongs to the new sales channel attached are removed How Updating existing end points and service method to integrate the new requirements Tests Add new integration tests Fixes CORE-270 Fixes CORE-272 Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oliver Windall Juhl
parent
8dd85e5f03
commit
df66378535
@@ -0,0 +1,13 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`sales channels GET /store/cart/:id returns cart with sales channel for single cart 1`] = `
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"description": "test description",
|
||||
"id": Any<String>,
|
||||
"is_disabled": false,
|
||||
"name": "test name",
|
||||
"updated_at": Any<String>,
|
||||
}
|
||||
`;
|
||||
@@ -83,7 +83,7 @@ Object {
|
||||
"cart": Object {
|
||||
"billing_address_id": "test-billing-address",
|
||||
"completed_at": null,
|
||||
"context": null,
|
||||
"context": Object {},
|
||||
"created_at": Any<String>,
|
||||
"customer_id": "test-customer",
|
||||
"deleted_at": null,
|
||||
@@ -259,7 +259,7 @@ Object {
|
||||
"cart": Object {
|
||||
"billing_address_id": "test-billing-address",
|
||||
"completed_at": null,
|
||||
"context": null,
|
||||
"context": Object {},
|
||||
"created_at": Any<String>,
|
||||
"customer_id": "test-customer",
|
||||
"deleted_at": null,
|
||||
|
||||
@@ -6,12 +6,7 @@ const {
|
||||
ShippingProfile,
|
||||
Product,
|
||||
ProductVariant,
|
||||
MoneyAmount,
|
||||
LineItem,
|
||||
Payment,
|
||||
Cart,
|
||||
ShippingMethod,
|
||||
Swap,
|
||||
} = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
|
||||
@@ -0,0 +1,298 @@
|
||||
const path = require("path")
|
||||
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const {
|
||||
simpleSalesChannelFactory,
|
||||
simpleCartFactory, simpleRegionFactory, simpleProductFactory,
|
||||
} = require("../../factories")
|
||||
|
||||
const startServerWithEnvironment =
|
||||
require("../../../helpers/start-server-with-environment").default
|
||||
|
||||
const adminReqConfig = {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
describe("sales channels", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
const [process, connection] = await startServerWithEnvironment({
|
||||
cwd,
|
||||
env: { MEDUSA_FF_SALES_CHANNELS: true },
|
||||
verbose: false,
|
||||
})
|
||||
dbConnection = connection
|
||||
medusaProcess = process
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("POST /store/cart/", () => {
|
||||
let salesChannel
|
||||
let disabledSalesChannel
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
tax_rate: 0,
|
||||
})
|
||||
await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "Default Sales Channel",
|
||||
description: "Created by Medusa",
|
||||
is_default: true
|
||||
})
|
||||
disabledSalesChannel = await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "disabled cart sales channel",
|
||||
description: "disabled cart sales channel description",
|
||||
is_disabled: true,
|
||||
})
|
||||
salesChannel = await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "cart sales channel",
|
||||
description: "cart sales channel description",
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("returns a cart with the default sales channel", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(`/store/carts`, {}, adminReqConfig)
|
||||
|
||||
expect(response.data.cart.sales_channel).toBeTruthy()
|
||||
expect(response.data.cart.sales_channel).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "Default Sales Channel",
|
||||
description: "Created by Medusa",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("returns a cart with the given sales channel", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post(`/store/carts`, { sales_channel_id: salesChannel.id }, adminReqConfig)
|
||||
|
||||
expect(response.data.cart.sales_channel).toBeTruthy()
|
||||
expect(response.data.cart.sales_channel).toEqual(
|
||||
expect.objectContaining({
|
||||
name: salesChannel.name,
|
||||
description: salesChannel.description,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("throw if the given sales channel is disabled", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const err = await api.post(
|
||||
`/store/carts`,
|
||||
{ sales_channel_id: disabledSalesChannel.id },
|
||||
adminReqConfig
|
||||
).catch(err => err)
|
||||
|
||||
expect(err.response.status).toEqual(400)
|
||||
expect(err.response.data.message).toBe(`Unable to assign the cart to a disabled Sales Channel "disabled cart sales channel"`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /store/cart/:id", () => {
|
||||
let salesChannel1, salesChannel2, disabledSalesChannel
|
||||
let product1, product2
|
||||
let cart
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
await simpleRegionFactory(dbConnection, {
|
||||
name: "Test region",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
})
|
||||
|
||||
salesChannel1 = await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "salesChannel1",
|
||||
description: "salesChannel1",
|
||||
})
|
||||
salesChannel2 = await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "salesChannel2",
|
||||
description: "salesChannel2",
|
||||
})
|
||||
disabledSalesChannel = await simpleSalesChannelFactory(dbConnection, {
|
||||
name: "disabled cart sales channel",
|
||||
description: "disabled cart sales channel description",
|
||||
is_disabled: true,
|
||||
})
|
||||
|
||||
product1 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
title: "prod 1",
|
||||
sales_channels: [salesChannel1],
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant",
|
||||
prices: [
|
||||
{
|
||||
amount: 50,
|
||||
currency: "usd",
|
||||
variant_id: "test-variant",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
product2 = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
sales_channels: [salesChannel2],
|
||||
variants: [
|
||||
{
|
||||
id: "test-variant-2",
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency: "usd",
|
||||
variant_id: "test-variant-2",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
cart = await simpleCartFactory(
|
||||
dbConnection,
|
||||
{
|
||||
sales_channel: salesChannel1,
|
||||
line_items: [
|
||||
{
|
||||
variant_id: "test-variant",
|
||||
unit_price: 50,
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it(
|
||||
"updates a cart sales channels should remove the items that does not belongs to the new sales channel",
|
||||
async () => {
|
||||
const api = useApi()
|
||||
|
||||
let response = await api.get(`/store/carts/${cart.id}`, adminReqConfig)
|
||||
|
||||
expect(response.data.cart.sales_channel).toBeTruthy()
|
||||
expect(response.data.cart.sales_channel).toEqual(
|
||||
expect.objectContaining({
|
||||
name: salesChannel1.name,
|
||||
description: salesChannel1.description,
|
||||
})
|
||||
)
|
||||
expect(response.data.cart.items.length).toBe(1)
|
||||
expect(response.data.cart.items[0].variant.product).toEqual(
|
||||
expect.objectContaining({
|
||||
id: product1.id,
|
||||
title: product1.title,
|
||||
})
|
||||
)
|
||||
|
||||
response = await api.post(`/store/carts/${cart.id}`, { sales_channel_id: salesChannel2.id }, adminReqConfig)
|
||||
|
||||
expect(response.data.cart.sales_channel).toBeTruthy()
|
||||
expect(response.data.cart.sales_channel).toEqual(
|
||||
expect.objectContaining({
|
||||
name: salesChannel2.name,
|
||||
description: salesChannel2.description,
|
||||
})
|
||||
)
|
||||
expect(response.data.cart.items.length).toBe(0)
|
||||
}
|
||||
)
|
||||
|
||||
it("throw if the given sales channel is disabled", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const err = await api.post(
|
||||
`/store/carts/${cart.id}`,
|
||||
{ sales_channel_id: disabledSalesChannel.id },
|
||||
adminReqConfig
|
||||
).catch(err => err)
|
||||
|
||||
expect(err.response.status).toEqual(400)
|
||||
expect(err.response.data.message).toBe("Unable to assign the cart to a disabled Sales Channel \"disabled cart sales channel\"")
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /store/cart/:id", () => {
|
||||
let cart
|
||||
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
cart = await simpleCartFactory(dbConnection, {
|
||||
sales_channel: {
|
||||
name: "test name",
|
||||
description: "test description",
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("returns cart with sales channel for single cart", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.id}`, adminReqConfig)
|
||||
|
||||
expect(response.data.cart.sales_channel).toBeTruthy()
|
||||
expect(response.data.cart.sales_channel).toMatchSnapshot({
|
||||
id: expect.any(String),
|
||||
name: "test name",
|
||||
description: "test description",
|
||||
is_disabled: false,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user