Feat(medusa, medusa-js, medusa-react): Include sales channels in related queries as an optional expand parameter (#1816)
**What** - Add `transformQuery` to get endpoints for product, order and cart - ensure that the default relations (when getting a singular entity) includes sales channels when enabled - Add `EmptyQueryParams` class in common types to prevent query parameters while using `transformQuery` - update product-, order- and cartFactory to include sales channels if provided - remove `packages/medusa/src/controllers/products/admin-list-products.ts` **Testing** - expands sales channel for single order - expands sales channels for orders with expand parameter - returns single product with sales channel - expands sales channels for products with expand parameter - returns cart with sales channel for single cart Fixes CORE-293 Co-authored-by: Sebastian Rindom <7554214+srindom@users.noreply.github.com> Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,30 @@ Object {
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`sales channels GET /admin/orders/:id expands sales channel for single 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>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`sales channels GET /admin/orders?expand=sales_channels expands sales channel with parameter 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>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`sales channels GET /admin/sales-channels/:id should retrieve the requested sales channel 1`] = `
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
@@ -20,6 +44,18 @@ Object {
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`sales channels GET /store/cart/:id with saleschannel 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>,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`sales channels POST /admin/sales-channels successfully creates a sales channel 1`] = `
|
||||
Object {
|
||||
"sales_channel": ObjectContaining {
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
const path = require("path")
|
||||
|
||||
const { SalesChannel } = require("@medusajs/medusa")
|
||||
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const { simpleSalesChannelFactory } = require("../../factories")
|
||||
|
||||
const {
|
||||
simpleSalesChannelFactory,
|
||||
simpleProductFactory,
|
||||
simpleCartFactory,
|
||||
} = require("../../factories")
|
||||
const { simpleOrderFactory } = require("../../factories")
|
||||
|
||||
const startServerWithEnvironment =
|
||||
require("../../../helpers/start-server-with-environment").default
|
||||
@@ -130,7 +137,6 @@ describe("sales channels", () => {
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe("POST /admin/sales-channels", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
@@ -172,13 +178,10 @@ describe("sales channels", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/sales-channels/:id", () => {})
|
||||
describe("POST /admin/sales-channels/:id", () => {})
|
||||
|
||||
describe("DELETE /admin/sales-channels/:id", () => {
|
||||
let salesChannel
|
||||
|
||||
beforeEach(async() => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
salesChannel = await simpleSalesChannelFactory(dbConnection, {
|
||||
@@ -194,14 +197,16 @@ describe("sales channels", () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should delete the requested sales channel", async() => {
|
||||
it("should delete the requested sales channel", async () => {
|
||||
const api = useApi()
|
||||
|
||||
let deletedSalesChannel = await dbConnection.manager.findOne(SalesChannel, {
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true
|
||||
})
|
||||
let deletedSalesChannel = await dbConnection.manager.findOne(
|
||||
SalesChannel,
|
||||
{
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(deletedSalesChannel.id).toEqual(salesChannel.id)
|
||||
expect(deletedSalesChannel.deleted_at).toEqual(null)
|
||||
@@ -220,20 +225,23 @@ describe("sales channels", () => {
|
||||
|
||||
deletedSalesChannel = await dbConnection.manager.findOne(SalesChannel, {
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true
|
||||
withDeleted: true,
|
||||
})
|
||||
|
||||
expect(deletedSalesChannel.id).toEqual(salesChannel.id)
|
||||
expect(deletedSalesChannel.deleted_at).not.toEqual(null)
|
||||
})
|
||||
|
||||
it("should delete the requested sales channel idempotently", async() => {
|
||||
it("should delete the requested sales channel idempotently", async () => {
|
||||
const api = useApi()
|
||||
|
||||
let deletedSalesChannel = await dbConnection.manager.findOne(SalesChannel, {
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true
|
||||
})
|
||||
let deletedSalesChannel = await dbConnection.manager.findOne(
|
||||
SalesChannel,
|
||||
{
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(deletedSalesChannel.id).toEqual(salesChannel.id)
|
||||
expect(deletedSalesChannel.deleted_at).toEqual(null)
|
||||
@@ -247,12 +255,12 @@ describe("sales channels", () => {
|
||||
expect(response.data).toEqual({
|
||||
id: expect.any(String),
|
||||
object: "sales-channel",
|
||||
deleted: true
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
deletedSalesChannel = await dbConnection.manager.findOne(SalesChannel, {
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true
|
||||
withDeleted: true,
|
||||
})
|
||||
|
||||
expect(deletedSalesChannel.id).toEqual(salesChannel.id)
|
||||
@@ -267,16 +275,244 @@ describe("sales channels", () => {
|
||||
expect(response.data).toEqual({
|
||||
id: expect.any(String),
|
||||
object: "sales-channel",
|
||||
deleted: true
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
deletedSalesChannel = await dbConnection.manager.findOne(SalesChannel, {
|
||||
where: { id: salesChannel.id },
|
||||
withDeleted: true
|
||||
withDeleted: true,
|
||||
})
|
||||
|
||||
expect(deletedSalesChannel.id).toEqual(salesChannel.id)
|
||||
expect(deletedSalesChannel.deleted_at).not.toEqual(null)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/orders/:id", () => {
|
||||
let order
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
order = await simpleOrderFactory(dbConnection, {
|
||||
sales_channel: {
|
||||
name: "test name",
|
||||
description: "test description",
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("expands sales channel for single", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/orders/${order.id}`,
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.data.order.sales_channel).toBeTruthy()
|
||||
expect(response.data.order.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),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/orders?expand=sales_channels", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleOrderFactory(dbConnection, {
|
||||
sales_channel: {
|
||||
name: "test name",
|
||||
description: "test description",
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("expands sales channel with parameter", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
"/admin/orders?expand=sales_channel",
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.data.orders[0].sales_channel).toBeTruthy()
|
||||
expect(response.data.orders[0].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),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/product/:id", () => {
|
||||
let product
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
product = await simpleProductFactory(dbConnection, {
|
||||
sales_channels: [
|
||||
{
|
||||
name: "webshop",
|
||||
description: "Webshop sales channel",
|
||||
},
|
||||
{
|
||||
name: "amazon",
|
||||
description: "Amazon sales channel",
|
||||
},
|
||||
],
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("returns product with sales channel", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get(`/admin/products/${product.id}`, adminReqConfig)
|
||||
.catch((err) => console.log(err))
|
||||
|
||||
expect(response.data.product.sales_channels).toBeTruthy()
|
||||
expect(response.data.product.sales_channels).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "webshop",
|
||||
description: "Webshop sales channel",
|
||||
is_disabled: false,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "amazon",
|
||||
description: "Amazon sales channel",
|
||||
is_disabled: false,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/products?expand[]=sales_channels", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleProductFactory(dbConnection, {
|
||||
sales_channels: [
|
||||
{
|
||||
name: "webshop",
|
||||
description: "Webshop sales channel",
|
||||
},
|
||||
{
|
||||
name: "amazon",
|
||||
description: "Amazon sales channel",
|
||||
},
|
||||
],
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("expands sales channel with parameter", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
"/admin/products?expand=sales_channels",
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.data.products[0].sales_channels).toBeTruthy()
|
||||
expect(response.data.products[0].sales_channels).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "webshop",
|
||||
description: "Webshop sales channel",
|
||||
is_disabled: false,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "amazon",
|
||||
description: "Amazon sales channel",
|
||||
is_disabled: false,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /store/cart/:id with saleschannel", () => {
|
||||
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),
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -11,6 +11,10 @@ import {
|
||||
simpleLineItemFactory,
|
||||
} from "./simple-line-item-factory"
|
||||
import { RegionFactoryData, simpleRegionFactory } from "./simple-region-factory"
|
||||
import {
|
||||
SalesChannelFactoryData,
|
||||
simpleSalesChannelFactory,
|
||||
} from "./simple-sales-channel-factory"
|
||||
import {
|
||||
ShippingMethodFactoryData,
|
||||
simpleShippingMethodFactory,
|
||||
@@ -24,6 +28,7 @@ export type CartFactoryData = {
|
||||
line_items?: LineItemFactoryData[]
|
||||
shipping_address?: AddressFactoryData
|
||||
shipping_methods?: ShippingMethodFactoryData[]
|
||||
sales_channel?: SalesChannelFactoryData
|
||||
}
|
||||
|
||||
export const simpleCartFactory = async (
|
||||
@@ -62,6 +67,14 @@ export const simpleCartFactory = async (
|
||||
|
||||
const address = await simpleAddressFactory(connection, data.shipping_address)
|
||||
|
||||
let sales_channel
|
||||
if (typeof data.sales_channel !== "undefined") {
|
||||
sales_channel = await simpleSalesChannelFactory(
|
||||
connection,
|
||||
data.sales_channel
|
||||
)
|
||||
}
|
||||
|
||||
const id = data.id || `simple-cart-${Math.random() * 1000}`
|
||||
const toSave = manager.create(Cart, {
|
||||
id,
|
||||
@@ -70,6 +83,7 @@ export const simpleCartFactory = async (
|
||||
region_id: regionId,
|
||||
customer_id: customerId,
|
||||
shipping_address_id: address.id,
|
||||
sales_channel_id: sales_channel?.id ?? null,
|
||||
})
|
||||
|
||||
const cart = await manager.save(toSave)
|
||||
@@ -79,7 +93,7 @@ export const simpleCartFactory = async (
|
||||
await simpleShippingMethodFactory(connection, { ...sm, cart_id: id })
|
||||
}
|
||||
|
||||
const items = data.line_items
|
||||
const items = data.line_items || []
|
||||
for (const item of items) {
|
||||
await simpleLineItemFactory(connection, { ...item, cart_id: id })
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
PaymentStatus,
|
||||
FulfillmentStatus,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import {
|
||||
DiscountFactoryData,
|
||||
simpleDiscountFactory,
|
||||
@@ -24,6 +23,10 @@ import {
|
||||
ShippingMethodFactoryData,
|
||||
simpleShippingMethodFactory,
|
||||
} from "./simple-shipping-method-factory"
|
||||
import {
|
||||
SalesChannelFactoryData,
|
||||
simpleSalesChannelFactory,
|
||||
} from "./simple-sales-channel-factory"
|
||||
|
||||
export type OrderFactoryData = {
|
||||
id?: string
|
||||
@@ -37,6 +40,7 @@ export type OrderFactoryData = {
|
||||
discounts?: DiscountFactoryData[]
|
||||
shipping_address?: AddressFactoryData
|
||||
shipping_methods?: ShippingMethodFactoryData[]
|
||||
sales_channel?: SalesChannelFactoryData
|
||||
}
|
||||
|
||||
export const simpleOrderFactory = async (
|
||||
@@ -79,6 +83,14 @@ export const simpleOrderFactory = async (
|
||||
)
|
||||
}
|
||||
|
||||
let sales_channel
|
||||
if (typeof data.sales_channel !== "undefined") {
|
||||
sales_channel = await simpleSalesChannelFactory(
|
||||
connection,
|
||||
data.sales_channel
|
||||
)
|
||||
}
|
||||
|
||||
const id = data.id || `simple-order-${Math.random() * 1000}`
|
||||
const toSave = manager.create(Order, {
|
||||
id,
|
||||
@@ -92,6 +104,7 @@ export const simpleOrderFactory = async (
|
||||
currency_code: currencyCode,
|
||||
tax_rate: taxRate,
|
||||
shipping_address_id: address.id,
|
||||
sales_channel_id: sales_channel?.id ?? null,
|
||||
})
|
||||
|
||||
const order = await manager.save(toSave)
|
||||
@@ -101,16 +114,17 @@ export const simpleOrderFactory = async (
|
||||
await simpleShippingMethodFactory(connection, { ...sm, order_id: order.id })
|
||||
}
|
||||
|
||||
const items = data.line_items.map((item) => {
|
||||
let adjustments = item?.adjustments || []
|
||||
return {
|
||||
...item,
|
||||
adjustments: adjustments.map((adj) => ({
|
||||
...adj,
|
||||
discount_id: discounts.find((d) => d.code === adj?.discount_code),
|
||||
})),
|
||||
}
|
||||
})
|
||||
const items =
|
||||
data.line_items?.map((item) => {
|
||||
const adjustments = item?.adjustments || []
|
||||
return {
|
||||
...item,
|
||||
adjustments: adjustments.map((adj) => ({
|
||||
...adj,
|
||||
discount_id: discounts.find((d) => d.code === adj?.discount_code),
|
||||
})),
|
||||
}
|
||||
}) || []
|
||||
|
||||
for (const item of items) {
|
||||
await simpleLineItemFactory(connection, { ...item, order_id: id })
|
||||
|
||||
@@ -12,6 +12,10 @@ import {
|
||||
ProductVariantFactoryData,
|
||||
simpleProductVariantFactory,
|
||||
} from "./simple-product-variant-factory"
|
||||
import {
|
||||
SalesChannelFactoryData,
|
||||
simpleSalesChannelFactory,
|
||||
} from "./simple-sales-channel-factory"
|
||||
|
||||
export type ProductFactoryData = {
|
||||
id?: string
|
||||
@@ -22,6 +26,7 @@ export type ProductFactoryData = {
|
||||
tags?: string[]
|
||||
options?: { id: string; title: string }[]
|
||||
variants?: ProductVariantFactoryData[]
|
||||
sales_channels?: SalesChannelFactoryData[]
|
||||
}
|
||||
|
||||
export const simpleProductFactory = async (
|
||||
@@ -43,6 +48,16 @@ export const simpleProductFactory = async (
|
||||
type: ShippingProfileType.GIFT_CARD,
|
||||
})
|
||||
|
||||
let sales_channels
|
||||
if (data.sales_channels) {
|
||||
sales_channels = await Promise.all(
|
||||
data.sales_channels.map(
|
||||
async (salesChannel) =>
|
||||
await simpleSalesChannelFactory(connection, salesChannel)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
const prodId = data.id || `simple-product-${Math.random() * 1000}`
|
||||
const productToCreate = {
|
||||
id: prodId,
|
||||
@@ -77,6 +92,8 @@ export const simpleProductFactory = async (
|
||||
|
||||
const toSave = manager.create(Product, productToCreate)
|
||||
|
||||
toSave.sales_channels = sales_channels
|
||||
|
||||
await manager.save(toSave)
|
||||
|
||||
const optionId = `${prodId}-option`
|
||||
|
||||
Reference in New Issue
Block a user