The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
add store sellers endpoint
vholik9f8e8f5bca14
diff --git a/integration-tests/http/seller/store/seller.spec.ts b/integration-tests/http/seller/store/seller.spec.tsnew file mode 100644index 000000000..08b4f0cd3--- /dev/null+++ b/integration-tests/http/seller/store/seller.spec.ts@@ -0,0 +1,210 @@+import { medusaIntegrationTestRunner } from "@medusajs/test-utils"+import { MedusaContainer } from "@medusajs/framework/types"+import { generatePublishableKey, generateStoreHeaders } from "../../../helpers/create-admin-user"++jest.setTimeout(50000)++medusaIntegrationTestRunner({+ testSuite: ({ getContainer, api }) => {+ describe("Store - Sellers", () => {+ let appContainer: MedusaContainer+ let storeHeaders: any+ let activeSeller1: any+ let activeSeller2: any+ let pendingSeller: any+ let sellerModule: any++ beforeAll(async () => {+ appContainer = getContainer()+ })++ beforeEach(async () => {+ sellerModule = appContainer.resolve("seller")++ // Create active sellers+ activeSeller1 = await sellerModule.createSellers({+ name: "Active Seller One",+ handle: "active-seller-one",+ email: "[email protected]",+ status: "active",+ })++ activeSeller2 = await sellerModule.createSellers({+ name: "Active Seller Two",+ handle: "active-seller-two",+ email: "[email protected]",+ status: "active",+ })++ // Create pending seller (should not be visible in store)+ pendingSeller = await sellerModule.createSellers({+ name: "Pending Seller",+ handle: "pending-seller",+ email: "[email protected]",+ status: "pending",+ })++ const apiKey = await generatePublishableKey(appContainer)+ storeHeaders = generateStoreHeaders({ publishableKey: apiKey })+ })++ describe("GET /store/sellers", () => {+ it("should list only active sellers", async () => {+ const response = await api.get(+ `/store/sellers`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.sellers).toHaveLength(2)+ expect(response.data.sellers).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ id: activeSeller1.id,+ name: "Active Seller One",+ handle: "active-seller-one",+ }),+ expect.objectContaining({+ id: activeSeller2.id,+ name: "Active Seller Two",+ handle: "active-seller-two",+ }),+ ])+ )+ // Should not include pending seller+ expect(response.data.sellers.map((s: any) => s.id)).not.toContain(pendingSeller.id)+ })++ it("should filter sellers by handle", async () => {+ const response = await api.get(+ `/store/sellers?handle=active-seller-one`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.sellers).toHaveLength(1)+ expect(response.data.sellers[0]).toEqual(+ expect.objectContaining({+ id: activeSeller1.id,+ handle: "active-seller-one",+ })+ )+ })++ it("should filter sellers by name", async () => {+ const response = await api.get(+ `/store/sellers?name=Active Seller Two`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.sellers).toHaveLength(1)+ expect(response.data.sellers[0]).toEqual(+ expect.objectContaining({+ id: activeSeller2.id,+ name: "Active Seller Two",+ })+ )+ })++ it("should filter sellers by id", async () => {+ const response = await api.get(+ `/store/sellers?id=${activeSeller1.id}`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.sellers).toHaveLength(1)+ expect(response.data.sellers[0].id).toEqual(activeSeller1.id)+ })++ it("should search sellers with q parameter", async () => {+ const response = await api.get(+ `/store/sellers?q=One`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.sellers).toHaveLength(1)+ expect(response.data.sellers[0]).toEqual(+ expect.objectContaining({+ id: activeSeller1.id,+ name: "Active Seller One",+ })+ )+ })++ it("should support pagination", async () => {+ const response = await api.get(+ `/store/sellers?limit=1&offset=0`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.sellers).toHaveLength(1)+ expect(response.data.count).toEqual(2)+ expect(response.data.limit).toEqual(1)+ expect(response.data.offset).toEqual(0)+ })++ it("should not expose sensitive fields like email", async () => {+ const response = await api.get(+ `/store/sellers`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ response.data.sellers.forEach((seller: any) => {+ expect(seller.email).toBeUndefined()+ expect(seller.phone).toBeUndefined()+ })+ })+ })++ describe("GET /store/sellers/:id", () => {+ it("should get a single active seller by id", async () => {+ const response = await api.get(+ `/store/sellers/${activeSeller1.id}`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.seller).toEqual(+ expect.objectContaining({+ id: activeSeller1.id,+ name: "Active Seller One",+ handle: "active-seller-one",+ })+ )+ })++ it("should return 404 for pending seller", async () => {+ const response = await api+ .get(`/store/sellers/${pendingSeller.id}`, storeHeaders)+ .catch((e) => e.response)++ expect(response.status).toEqual(404)+ })++ it("should return 404 for non-existent seller", async () => {+ const response = await api+ .get(`/store/sellers/non-existent-id`, storeHeaders)+ .catch((e) => e.response)++ expect(response.status).toEqual(404)+ })++ it("should not expose sensitive fields like email", async () => {+ const response = await api.get(+ `/store/sellers/${activeSeller1.id}`,+ storeHeaders+ )++ expect(response.status).toEqual(200)+ expect(response.data.seller.email).toBeUndefined()+ expect(response.data.seller.phone).toBeUndefined()+ })+ })+ })+ },+})diff --git a/packages/core/src/api/store/middlewares.ts b/packages/core/src/api/store/middlewares.tsindex 978488759..6678f46b5 100644--- a/packages/core/src/api/store/middlewares.ts+++ b/packages/core/src/api/store/middlewares.ts@@ -2,8 +2,10 @@ import { MiddlewareRoute } from "@medusajs/medusa"import { storeCartsMiddlewares } from "./carts/middlewares"import { storeOrderGroupsMiddlewares } from "./order-groups/middlewares"+import { storeSellersMiddlewares } from "./sellers/middlewares"export const storeMiddlewares: MiddlewareRoute[] = [...storeCartsMiddlewares,...storeOrderGroupsMiddlewares,+ ...storeSellersMiddlewares,]diff --git a/packages/core/src/api/store/sellers/[id]/route.ts b/packages/core/src/api/store/sellers/[id]/route.tsnew file mode 100644index 000000000..7d75df338--- /dev/null+++ b/packages/core/src/api/store/sellers/[id]/route.ts@@ -0,0 +1,34 @@+import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"+import {+ ContainerRegistrationKeys,+ MedusaError,+} from "@medusajs/framework/utils"+import { HttpTypes } from "@mercurjs/types"+import { SellerStatus } from "@mercurjs/types"++export const GET = async (+ req: MedusaRequest,+ res: MedusaResponse<HttpTypes.StoreSellerResponse>+) => {+ const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)++ const {+ data: [seller],+ } = await query.graph({+ entity: "seller",+ filters: {+ id: req.params.id,+ status: SellerStatus.ACTIVE,+ },+ fields: req.queryConfig.fields,+ })++ if (!seller) {+ throw new MedusaError(+ MedusaError.Types.NOT_FOUND,+ `Seller with id ${req.params.id} was not found`+ )+ }++ res.json({ seller })+}diff --git a/packages/core/src/api/store/sellers/middlewares.ts b/packages/core/src/api/store/sellers/middlewares.tsnew file mode 100644index 000000000..9e34a9a1a--- /dev/null+++ b/packages/core/src/api/store/sellers/middlewares.ts@@ -0,0 +1,28 @@+import { validateAndTransformQuery } from "@medusajs/framework/http"+import { MiddlewareRoute } from "@medusajs/medusa"++import { storeSellerQueryConfig } from "./query-config"+import { StoreGetSellerParams, StoreGetSellersParams } from "./validators"++export const storeSellersMiddlewares: MiddlewareRoute[] = [+ {+ method: ["GET"],+ matcher: "/store/sellers",+ middlewares: [+ validateAndTransformQuery(+ StoreGetSellersParams,+ storeSellerQueryConfig.list+ ),+ ],+ },+ {+ method: ["GET"],+ matcher: "/store/sellers/:id",+ middlewares: [+ validateAndTransformQuery(+ StoreGetSellerParams,+ storeSellerQueryConfig.retrieve+ ),+ ],+ },+]diff --git a/packages/core/src/api/store/sellers/query-config.ts b/packages/core/src/api/store/sellers/query-config.tsnew file mode 100644index 000000000..2a79db075--- /dev/null+++ b/packages/core/src/api/store/sellers/query-config.ts@@ -0,0 +1,28 @@+export const storeSellerFields = [+ "id",+ "name",+ "handle",+ "logo",+ "cover_image",+ "address_1",+ "address_2",+ "city",+ "country_code",+ "province",+ "postal_code",+ "created_at",+ "updated_at",+]++export const storeSellerQueryConfig = {+ list: {+ defaults: storeSellerFields,+ allowed: storeSellerFields,+ isList: true,+ },+ retrieve: {+ defaults: storeSellerFields,+ allowed: storeSellerFields,+ isList: false,+ },+}diff --git a/packages/core/src/api/store/sellers/route.ts b/packages/core/src/api/store/sellers/route.tsnew file mode 100644index 000000000..d21606f74--- /dev/null+++ b/packages/core/src/api/store/sellers/route.ts@@ -0,0 +1,28 @@+import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"+import { ContainerRegistrationKeys } from "@medusajs/framework/utils"+import { HttpTypes } from "@mercurjs/types"+import { SellerStatus } from "@mercurjs/types"++export const GET = async (+ req: MedusaRequest,+ res: MedusaResponse<HttpTypes.StoreSellerListResponse>+) => {+ const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)++ const { data: sellers, metadata } = await query.graph({+ entity: "seller",+ filters: {+ ...req.filterableFields,+ status: SellerStatus.ACTIVE,+ },+ fields: req.queryConfig.fields,+ pagination: req.queryConfig.pagination,+ })++ res.json({+ sellers,+ count: metadata?.count ?? 0,+ offset: metadata?.skip ?? 0,+ limit: metadata?.take ?? 0,+ })+}diff --git a/packages/core/src/api/store/sellers/validators.ts b/packages/core/src/api/store/sellers/validators.tsnew file mode 100644index 000000000..38dc83dee--- /dev/null+++ b/packages/core/src/api/store/sellers/validators.ts@@ -0,0 +1,24 @@+import { z } from "zod"+import {+ createFindParams,+ createOperatorMap,+ createSelectParams,+} from "@medusajs/medusa/api/utils/validators"++export type StoreGetSellerParamsType = z.infer<typeof StoreGetSellerParams>+export const StoreGetSellerParams = createSelectParams()++export type StoreGetSellersParamsType = z.infer<typeof StoreGetSellersParams>+export const StoreGetSellersParams = createFindParams({+ offset: 0,+ limit: 50,+}).merge(+ z.object({+ q: z.string().optional(),+ id: z.union([z.string(), z.array(z.string())]).optional(),+ handle: z.union([z.string(), z.array(z.string())]).optional(),+ name: z.union([z.string(), z.array(z.string())]).optional(),+ created_at: createOperatorMap().optional(),+ updated_at: createOperatorMap().optional(),+ })+)diff --git a/packages/types/src/http/seller.ts b/packages/types/src/http/seller.tsindex 0855c8a94..44bd82617 100644--- a/packages/types/src/http/seller.ts+++ b/packages/types/src/http/seller.ts@@ -16,3 +16,17 @@ export type VendorSellerListResponse = PaginatedResponse<{}>export type VendorSellerDeleteResponse = DeleteResponse<"seller">++export interface StoreSellerResponse {+ /**+ * The seller's details.+ */+ seller: SellerDTO+}++export type StoreSellerListResponse = PaginatedResponse<{+ /**+ * The list of sellers.+ */+ sellers: SellerDTO[]+}>