feat(medusa): Middleware to add default SC on query if no SC already exist on it (#3694)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): Middleware to add default SC on store products query if no SC provided
|
||||||
@@ -682,7 +682,7 @@ describe("Publishable API keys", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns all products if PK is not passed", async () => {
|
it("returns default product from default sales channel if PK is not passed", async () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
await api.post(
|
await api.post(
|
||||||
@@ -703,15 +703,9 @@ describe("Publishable API keys", () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(response.data.products.length).toBe(3)
|
expect(response.data.products.length).toBe(1)
|
||||||
expect(response.data.products).toEqual(
|
expect(response.data.products).toEqual(
|
||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
|
||||||
id: product1.id,
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
id: product2.id,
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: product3.id,
|
id: product3.id,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const { initDb, useDb } = require("../../../helpers/use-db")
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
simpleProductFactory,
|
simpleProductFactory,
|
||||||
simpleProductCategoryFactory,
|
simpleSalesChannelFactory,
|
||||||
} = require("../../factories")
|
} = require("../../factories")
|
||||||
|
|
||||||
const productSeeder = require("../../helpers/store-product-seeder")
|
const productSeeder = require("../../helpers/store-product-seeder")
|
||||||
@@ -37,7 +37,14 @@ describe("/store/products", () => {
|
|||||||
|
|
||||||
describe("GET /store/products", () => {
|
describe("GET /store/products", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await productSeeder(dbConnection)
|
const defaultSalesChannel = await simpleSalesChannelFactory(
|
||||||
|
dbConnection,
|
||||||
|
{
|
||||||
|
id: "sales-channel",
|
||||||
|
is_default: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
await productSeeder(dbConnection, defaultSalesChannel)
|
||||||
await adminSeeder(dbConnection)
|
await adminSeeder(dbConnection)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
|
import { simpleSalesChannelFactory } from "../../../factories"
|
||||||
|
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
const setupServer = require("../../../../helpers/setup-server")
|
const setupServer = require("../../../../helpers/setup-server")
|
||||||
const { useApi } = require("../../../../helpers/use-api")
|
const { useApi } = require("../../../../helpers/use-api")
|
||||||
const { initDb, useDb } = require("../../../../helpers/use-db")
|
const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||||
|
|
||||||
const {
|
const { simpleProductCategoryFactory } = require("../../../factories")
|
||||||
simpleProductCategoryFactory,
|
|
||||||
} = require("../../../factories")
|
|
||||||
|
|
||||||
const productSeeder = require("../../../helpers/store-product-seeder")
|
const productSeeder = require("../../../helpers/store-product-seeder")
|
||||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||||
@@ -26,7 +26,7 @@ describe("/store/products", () => {
|
|||||||
dbConnection = await initDb({ cwd })
|
dbConnection = await initDb({ cwd })
|
||||||
medusaProcess = await setupServer({
|
medusaProcess = await setupServer({
|
||||||
cwd,
|
cwd,
|
||||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true }
|
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -51,9 +51,15 @@ describe("/store/products", () => {
|
|||||||
const internalCategoryWithProductId = "inactive-category-with-product-id"
|
const internalCategoryWithProductId = "inactive-category-with-product-id"
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const manager = dbConnection.manager
|
const defaultSalesChannel = await simpleSalesChannelFactory(
|
||||||
|
dbConnection,
|
||||||
|
{
|
||||||
|
id: "sales-channel",
|
||||||
|
is_default: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
await productSeeder(dbConnection)
|
await productSeeder(dbConnection, defaultSalesChannel)
|
||||||
await adminSeeder(dbConnection)
|
await adminSeeder(dbConnection)
|
||||||
|
|
||||||
categoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
categoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
||||||
@@ -76,25 +82,31 @@ describe("/store/products", () => {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
inactiveCategoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
inactiveCategoryWithProduct = await simpleProductCategoryFactory(
|
||||||
id: inactiveCategoryWithProductId,
|
dbConnection,
|
||||||
name: "inactive category with Product",
|
{
|
||||||
products: [{ id: testProductFilteringId2 }],
|
id: inactiveCategoryWithProductId,
|
||||||
parent_category: nestedCategoryWithProduct,
|
name: "inactive category with Product",
|
||||||
is_active: false,
|
products: [{ id: testProductFilteringId2 }],
|
||||||
is_internal: false,
|
parent_category: nestedCategoryWithProduct,
|
||||||
rank: 0,
|
is_active: false,
|
||||||
})
|
is_internal: false,
|
||||||
|
rank: 0,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
internalCategoryWithProduct = await simpleProductCategoryFactory(dbConnection, {
|
internalCategoryWithProduct = await simpleProductCategoryFactory(
|
||||||
id: inactiveCategoryWithProductId,
|
dbConnection,
|
||||||
name: "inactive category with Product",
|
{
|
||||||
products: [{ id: testProductFilteringId2 }],
|
id: inactiveCategoryWithProductId,
|
||||||
parent_category: nestedCategoryWithProduct,
|
name: "inactive category with Product",
|
||||||
is_active: true,
|
products: [{ id: testProductFilteringId2 }],
|
||||||
is_internal: true,
|
parent_category: nestedCategoryWithProduct,
|
||||||
rank: 1,
|
is_active: true,
|
||||||
})
|
is_internal: true,
|
||||||
|
rank: 1,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
nested2CategoryWithProduct = await simpleProductCategoryFactory(
|
nested2CategoryWithProduct = await simpleProductCategoryFactory(
|
||||||
dbConnection,
|
dbConnection,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const {
|
|||||||
ShippingProfileType,
|
ShippingProfileType,
|
||||||
} = require("@medusajs/medusa")
|
} = require("@medusajs/medusa")
|
||||||
|
|
||||||
module.exports = async (dataSource, data = {}) => {
|
module.exports = async (dataSource, defaultSalesChannel) => {
|
||||||
const manager = dataSource.manager
|
const manager = dataSource.manager
|
||||||
|
|
||||||
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
|
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
|
||||||
@@ -138,6 +138,7 @@ module.exports = async (dataSource, data = {}) => {
|
|||||||
{ id: "tag1", value: "123" },
|
{ id: "tag1", value: "123" },
|
||||||
{ tag: "tag2", value: "456" },
|
{ tag: "tag2", value: "456" },
|
||||||
],
|
],
|
||||||
|
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
p.images = [image]
|
p.images = [image]
|
||||||
@@ -270,6 +271,7 @@ module.exports = async (dataSource, data = {}) => {
|
|||||||
{ id: "tag1", value: "123" },
|
{ id: "tag1", value: "123" },
|
||||||
{ tag: "tag2", value: "456" },
|
{ tag: "tag2", value: "456" },
|
||||||
],
|
],
|
||||||
|
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
await manager.save(p1)
|
await manager.save(p1)
|
||||||
@@ -326,6 +328,7 @@ module.exports = async (dataSource, data = {}) => {
|
|||||||
collection_id: "test-collection1",
|
collection_id: "test-collection1",
|
||||||
status: "published",
|
status: "published",
|
||||||
tags: [{ id: "tag3", value: "123" }],
|
tags: [{ id: "tag3", value: "123" }],
|
||||||
|
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
await manager.save(product1)
|
await manager.save(product1)
|
||||||
@@ -340,6 +343,7 @@ module.exports = async (dataSource, data = {}) => {
|
|||||||
collection_id: "test-collection2",
|
collection_id: "test-collection2",
|
||||||
status: "published",
|
status: "published",
|
||||||
tags: [{ id: "tag4", value: "1234" }],
|
tags: [{ id: "tag4", value: "1234" }],
|
||||||
|
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
await manager.save(product2)
|
await manager.save(product2)
|
||||||
@@ -354,6 +358,7 @@ module.exports = async (dataSource, data = {}) => {
|
|||||||
collection_id: "test-collection1",
|
collection_id: "test-collection1",
|
||||||
status: "draft",
|
status: "draft",
|
||||||
tags: [{ id: "tag4", value: "1234" }],
|
tags: [{ id: "tag4", value: "1234" }],
|
||||||
|
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
await manager.save(product3)
|
await manager.save(product3)
|
||||||
@@ -367,6 +372,7 @@ module.exports = async (dataSource, data = {}) => {
|
|||||||
description: "test-product-description",
|
description: "test-product-description",
|
||||||
type: { id: "test-type", value: "test-type" },
|
type: { id: "test-type", value: "test-type" },
|
||||||
status: "published",
|
status: "published",
|
||||||
|
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
|
||||||
})
|
})
|
||||||
|
|
||||||
await manager.save(gift_card)
|
await manager.save(gift_card)
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { NextFunction, Request, Response } from "express"
|
||||||
|
import SalesChannelFeatureFlag from "../../loaders/feature-flags/sales-channels"
|
||||||
|
import { SalesChannelService } from "../../services"
|
||||||
|
import { FlagRouter } from "../../utils/flag-router"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Middleware that includes the default sales channel on the request, if no sales channels present
|
||||||
|
* @param context Object of options
|
||||||
|
* @param context.attachChannelAsArray Whether to attach the default sales channel as an array or just a string
|
||||||
|
*/
|
||||||
|
export function withDefaultSalesChannel({
|
||||||
|
attachChannelAsArray,
|
||||||
|
}: {
|
||||||
|
attachChannelAsArray?: boolean
|
||||||
|
}): (req: Request, res: Response, next: NextFunction) => Promise<void> {
|
||||||
|
return async (req: Request, _, next: NextFunction) => {
|
||||||
|
const featureFlagRouter = req.scope.resolve(
|
||||||
|
"featureFlagRouter"
|
||||||
|
) as FlagRouter
|
||||||
|
|
||||||
|
if (
|
||||||
|
!featureFlagRouter.isFeatureEnabled(SalesChannelFeatureFlag.key) ||
|
||||||
|
req.query.sales_channel_id?.length ||
|
||||||
|
req.get("x-publishable-api-key")
|
||||||
|
) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
const salesChannelService: SalesChannelService = req.scope.resolve(
|
||||||
|
"salesChannelService"
|
||||||
|
)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const defaultSalesChannel = await salesChannelService.retrieveDefault()
|
||||||
|
if (defaultSalesChannel?.id) {
|
||||||
|
req.query.sales_channel_id = attachChannelAsArray
|
||||||
|
? [defaultSalesChannel.id]
|
||||||
|
: defaultSalesChannel.id
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
} finally {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import { StoreGetProductsProductParams } from "./get-product"
|
|||||||
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
||||||
import { validateProductSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-product-sales-channel-association"
|
import { validateProductSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-product-sales-channel-association"
|
||||||
import { validateSalesChannelParam } from "../../../middlewares/publishable-api-key/validate-sales-channel-param"
|
import { validateSalesChannelParam } from "../../../middlewares/publishable-api-key/validate-sales-channel-param"
|
||||||
|
import { withDefaultSalesChannel } from "../../../middlewares/with-default-sales-channel"
|
||||||
|
|
||||||
const route = Router()
|
const route = Router()
|
||||||
|
|
||||||
@@ -26,6 +27,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
|||||||
|
|
||||||
route.get(
|
route.get(
|
||||||
"/",
|
"/",
|
||||||
|
withDefaultSalesChannel({ attachChannelAsArray: true }),
|
||||||
transformStoreQuery(StoreGetProductsParams, {
|
transformStoreQuery(StoreGetProductsParams, {
|
||||||
defaultRelations: defaultStoreProductsRelations,
|
defaultRelations: defaultStoreProductsRelations,
|
||||||
defaultFields: defaultStoreProductsFields,
|
defaultFields: defaultStoreProductsFields,
|
||||||
@@ -38,6 +40,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
|||||||
|
|
||||||
route.get(
|
route.get(
|
||||||
"/:id",
|
"/:id",
|
||||||
|
withDefaultSalesChannel({}),
|
||||||
transformStoreQuery(StoreGetProductsProductParams, {
|
transformStoreQuery(StoreGetProductsProductParams, {
|
||||||
defaultRelations: defaultStoreProductsRelations,
|
defaultRelations: defaultStoreProductsRelations,
|
||||||
defaultFields: defaultStoreProductsFields,
|
defaultFields: defaultStoreProductsFields,
|
||||||
|
|||||||
Reference in New Issue
Block a user