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
Feat/add vendor notificaiton registry (#739)
Jakub Sieradzki2da90f644884
diff --git a/packages/dashboard-sdk/src/types.ts b/packages/dashboard-sdk/src/types.tsindex 210f9d918..947fad722 100644--- a/packages/dashboard-sdk/src/types.ts+++ b/packages/dashboard-sdk/src/types.ts@@ -6,6 +6,7 @@ export interface MercurConfig {components?: {MainSidebar?: stringSettingsSidebar?: string+ TopbarActions?: string},i18n?: {defaultLanguage: stringdiff --git a/packages/registry/.mercur/_generated/index.ts b/packages/registry/.mercur/_generated/index.tsindex 0b0e2474b..32f46dbe3 100644--- a/packages/registry/.mercur/_generated/index.ts+++ b/packages/registry/.mercur/_generated/index.ts@@ -708,5 +708,6 @@ export type Routes = {reviews: typeof import("../../src/reviews/api/vendor/reviews/route") & {$id: typeof import("../../src/reviews/api/vendor/reviews/[id]/route");};+ notifications: typeof import("../../src/vendor-notifications/api/vendor/notifications/route");};};diff --git a/packages/registry/r/registry.json b/packages/registry/r/registry.jsonindex 3ec364fbc..847208100 100644--- a/packages/registry/r/registry.json+++ b/packages/registry/r/registry.json@@ -382,6 +382,47 @@"workflow","api"]+ },+ {+ "name": "vendor-notifications",+ "description": "Vendor notification feed with bell icon in navbar and vendor API endpoint.",+ "dependencies": [],+ "registryDependencies": [],+ "docs": "## Vendor Notifications\n\nThis block adds vendor notifications: a bell icon in the navbar and a `GET /vendor/notifications` API endpoint.\n\n### 1. Notification Provider\n\nAdd the `seller_feed` channel to the local notification provider in `medusa-config.ts`:\n\n```ts\n{\n resolve: '@medusajs/medusa/notification',\n options: {\n providers: [\n {\n resolve: '@medusajs/medusa/notification-local',\n id: 'local',\n options: {\n channels: ['feed', 'seller_feed']\n }\n }\n ]\n }\n}\n```\n\n### 2. Middleware Setup\n\nImport `vendorNotificationMiddlewares` and add it to your `src/api/middlewares.ts`:\n\n```ts\nimport { defineMiddlewares } from \"@medusajs/medusa\";\nimport { vendorNotificationMiddlewares } from \"./vendor/notifications/middlewares\";\n\nexport default defineMiddlewares({\n routes: [\n // ...your existing middlewares,\n ...vendorNotificationMiddlewares,\n ],\n});\n```\n\n### 3. TopbarActions Component\n\nRegister the `TopbarActions` component in your vendor app's `mercur.config.ts`:\n\n```ts\nimport { defineConfig } from '@mercurjs/dashboard-sdk'\n\nexport default defineConfig({\n components: {\n TopbarActions: './components/topbar-actions',\n },\n})\n```\n\n### 4. Sending Notifications\n\nCreate a subscriber to send notifications to sellers. Example (`src/subscribers/seller-product-notification.ts`):\n\n```ts\nimport type { SubscriberArgs, SubscriberConfig } from \"@medusajs/framework\"\nimport { ContainerRegistrationKeys, Modules } from \"@medusajs/framework/utils\"\n\nexport default async function handler({\n event: { data },\n container,\n}: SubscriberArgs<{ id: string }>) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const notificationService = container.resolve(Modules.NOTIFICATION)\n\n // Example: look up the seller linked to a product\n const { data: [product] } = await query.graph({\n entity: \"product\",\n fields: [\"id\", \"title\", \"seller.*\"],\n filters: { id: data.id },\n })\n\n if (!product?.seller?.id) return\n\n await notificationService.createNotifications({\n to: product.seller.id,\n channel: \"seller_feed\",\n template: \"product-created\",\n data: {\n title: \"Product created\",\n description: `Your product \\\"${product.title}\\\" has been created.`,\n },\n })\n}\n\nexport const config: SubscriberConfig = {\n event: \"product.created\",\n}\n```\n\n### 5. Run codegen\n\n```bash\nnpx @mercurjs/cli@canary codegen\n```",+ "files": [+ {+ "path": "vendor-notifications/api/middlewares.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/route.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/middlewares.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/validators.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/query-config.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/helpers/helpers.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/vendor/components/topbar-actions.tsx",+ "type": "registry:vendor"+ }+ ],+ "categories": [+ "api",+ "vendor"+ ]}]}\ No newline at end of filediff --git a/packages/registry/r/vendor-notifications.json b/packages/registry/r/vendor-notifications.jsonnew file mode 100644index 000000000..8d29f2f8b--- /dev/null+++ b/packages/registry/r/vendor-notifications.json@@ -0,0 +1,49 @@+{+ "$schema": "https://registry.mercurjs.com/registry-item.json",+ "name": "vendor-notifications",+ "description": "Vendor notification feed with bell icon in navbar and vendor API endpoint.",+ "dependencies": [],+ "registryDependencies": [],+ "docs": "## Vendor Notifications\n\nThis block adds vendor notifications: a bell icon in the navbar and a `GET /vendor/notifications` API endpoint.\n\n### 1. Notification Provider\n\nAdd the `seller_feed` channel to the local notification provider in `medusa-config.ts`:\n\n```ts\n{\n resolve: '@medusajs/medusa/notification',\n options: {\n providers: [\n {\n resolve: '@medusajs/medusa/notification-local',\n id: 'local',\n options: {\n channels: ['feed', 'seller_feed']\n }\n }\n ]\n }\n}\n```\n\n### 2. Middleware Setup\n\nImport `vendorNotificationMiddlewares` and add it to your `src/api/middlewares.ts`:\n\n```ts\nimport { defineMiddlewares } from \"@medusajs/medusa\";\nimport { vendorNotificationMiddlewares } from \"./vendor/notifications/middlewares\";\n\nexport default defineMiddlewares({\n routes: [\n // ...your existing middlewares,\n ...vendorNotificationMiddlewares,\n ],\n});\n```\n\n### 3. TopbarActions Component\n\nRegister the `TopbarActions` component in your vendor app's `mercur.config.ts`:\n\n```ts\nimport { defineConfig } from '@mercurjs/dashboard-sdk'\n\nexport default defineConfig({\n components: {\n TopbarActions: './components/topbar-actions',\n },\n})\n```\n\n### 4. Sending Notifications\n\nCreate a subscriber to send notifications to sellers. Example (`src/subscribers/seller-product-notification.ts`):\n\n```ts\nimport type { SubscriberArgs, SubscriberConfig } from \"@medusajs/framework\"\nimport { ContainerRegistrationKeys, Modules } from \"@medusajs/framework/utils\"\n\nexport default async function handler({\n event: { data },\n container,\n}: SubscriberArgs<{ id: string }>) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const notificationService = container.resolve(Modules.NOTIFICATION)\n\n // Example: look up the seller linked to a product\n const { data: [product] } = await query.graph({\n entity: \"product\",\n fields: [\"id\", \"title\", \"seller.*\"],\n filters: { id: data.id },\n })\n\n if (!product?.seller?.id) return\n\n await notificationService.createNotifications({\n to: product.seller.id,\n channel: \"seller_feed\",\n template: \"product-created\",\n data: {\n title: \"Product created\",\n description: `Your product \\\"${product.title}\\\" has been created.`,\n },\n })\n}\n\nexport const config: SubscriberConfig = {\n event: \"product.created\",\n}\n```\n\n### 5. Run codegen\n\n```bash\nnpx @mercurjs/cli@canary codegen\n```",+ "categories": [+ "api",+ "vendor"+ ],+ "files": [+ {+ "path": "vendor-notifications/api/middlewares.ts",+ "content": "import { vendorNotificationMiddlewares } from \"./vendor/notifications/middlewares\"\n\nexport const vendorMiddlewares = [...vendorNotificationMiddlewares]\n",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/route.ts",+ "content": "import { AuthenticatedMedusaRequest, MedusaResponse } from \"@medusajs/framework\"\nimport { ContainerRegistrationKeys } from \"@medusajs/framework/utils\"\nimport { fetchSellerByAuthActorId } from \"./helpers/helpers\"\n\nexport async function GET(\n req: AuthenticatedMedusaRequest,\n res: MedusaResponse\n): Promise<void> {\n const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)\n const seller = await fetchSellerByAuthActorId(\n req.auth_context.actor_id,\n req.scope\n )\n\n const { data: notifications, metadata } = await query.graph({\n entity: \"notification\",\n fields: req.queryConfig.fields,\n filters: { channel: \"seller_feed\", to: seller.id },\n pagination: req.queryConfig.pagination,\n })\n\n res.json({\n notifications,\n count: metadata?.count,\n offset: metadata?.skip,\n limit: metadata?.take,\n })\n}\n",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/middlewares.ts",+ "content": "import { validateAndTransformQuery } from \"@medusajs/framework\"\nimport { MiddlewareRoute } from \"@medusajs/medusa\"\nimport { vendorNotificationQueryConfig } from \"./query-config\"\nimport { VendorGetNotificationParams } from \"./validators\"\n\nexport const vendorNotificationMiddlewares: MiddlewareRoute[] = [\n {\n method: [\"GET\"],\n matcher: \"/vendor/notifications\",\n middlewares: [\n validateAndTransformQuery(\n VendorGetNotificationParams,\n vendorNotificationQueryConfig.list\n ),\n ],\n },\n]\n",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/validators.ts",+ "content": "import { z } from \"zod\"\nimport { createFindParams } from \"@medusajs/medusa/api/utils/validators\"\n\nexport type VendorGetNotificationParamsType = z.infer<\n typeof VendorGetNotificationParams\n>\nexport const VendorGetNotificationParams = createFindParams({\n offset: 0,\n limit: 50,\n order: \"-created_at\",\n})\n",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/query-config.ts",+ "content": "export const defaultVendorNotificationFields = [\n \"id\",\n \"to\",\n \"channel\",\n \"template\",\n \"data\",\n \"created_at\",\n \"updated_at\",\n]\n\nexport const vendorNotificationQueryConfig = {\n list: {\n defaults: defaultVendorNotificationFields,\n isList: true,\n },\n retrieve: {\n defaults: defaultVendorNotificationFields,\n isList: false,\n },\n}\n",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/helpers/helpers.ts",+ "content": "import { ContainerRegistrationKeys } from \"@medusajs/framework/utils\"\nimport { MedusaContainer } from \"@medusajs/framework/types\"\n\nexport const fetchSellerByAuthActorId = async (\n authActorId: string,\n scope: MedusaContainer,\n fields: string[] = [\"id\"]\n) => {\n const query = scope.resolve(ContainerRegistrationKeys.QUERY)\n\n const {\n data: [seller],\n } = await query.graph({\n entity: \"seller\",\n filters: { id: authActorId },\n fields,\n })\n\n return seller\n}\n",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/vendor/components/topbar-actions.tsx",+ "content": "import { Notifications } from \"@mercurjs/vendor\"\n\nexport default function TopbarActions() {\n return <Notifications />\n}\n",+ "type": "registry:vendor"+ }+ ]+}\ No newline at end of filediff --git a/packages/registry/registry.json b/packages/registry/registry.jsonindex 3ec364fbc..847208100 100644--- a/packages/registry/registry.json+++ b/packages/registry/registry.json@@ -382,6 +382,47 @@"workflow","api"]+ },+ {+ "name": "vendor-notifications",+ "description": "Vendor notification feed with bell icon in navbar and vendor API endpoint.",+ "dependencies": [],+ "registryDependencies": [],+ "docs": "## Vendor Notifications\n\nThis block adds vendor notifications: a bell icon in the navbar and a `GET /vendor/notifications` API endpoint.\n\n### 1. Notification Provider\n\nAdd the `seller_feed` channel to the local notification provider in `medusa-config.ts`:\n\n```ts\n{\n resolve: '@medusajs/medusa/notification',\n options: {\n providers: [\n {\n resolve: '@medusajs/medusa/notification-local',\n id: 'local',\n options: {\n channels: ['feed', 'seller_feed']\n }\n }\n ]\n }\n}\n```\n\n### 2. Middleware Setup\n\nImport `vendorNotificationMiddlewares` and add it to your `src/api/middlewares.ts`:\n\n```ts\nimport { defineMiddlewares } from \"@medusajs/medusa\";\nimport { vendorNotificationMiddlewares } from \"./vendor/notifications/middlewares\";\n\nexport default defineMiddlewares({\n routes: [\n // ...your existing middlewares,\n ...vendorNotificationMiddlewares,\n ],\n});\n```\n\n### 3. TopbarActions Component\n\nRegister the `TopbarActions` component in your vendor app's `mercur.config.ts`:\n\n```ts\nimport { defineConfig } from '@mercurjs/dashboard-sdk'\n\nexport default defineConfig({\n components: {\n TopbarActions: './components/topbar-actions',\n },\n})\n```\n\n### 4. Sending Notifications\n\nCreate a subscriber to send notifications to sellers. Example (`src/subscribers/seller-product-notification.ts`):\n\n```ts\nimport type { SubscriberArgs, SubscriberConfig } from \"@medusajs/framework\"\nimport { ContainerRegistrationKeys, Modules } from \"@medusajs/framework/utils\"\n\nexport default async function handler({\n event: { data },\n container,\n}: SubscriberArgs<{ id: string }>) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const notificationService = container.resolve(Modules.NOTIFICATION)\n\n // Example: look up the seller linked to a product\n const { data: [product] } = await query.graph({\n entity: \"product\",\n fields: [\"id\", \"title\", \"seller.*\"],\n filters: { id: data.id },\n })\n\n if (!product?.seller?.id) return\n\n await notificationService.createNotifications({\n to: product.seller.id,\n channel: \"seller_feed\",\n template: \"product-created\",\n data: {\n title: \"Product created\",\n description: `Your product \\\"${product.title}\\\" has been created.`,\n },\n })\n}\n\nexport const config: SubscriberConfig = {\n event: \"product.created\",\n}\n```\n\n### 5. Run codegen\n\n```bash\nnpx @mercurjs/cli@canary codegen\n```",+ "files": [+ {+ "path": "vendor-notifications/api/middlewares.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/route.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/middlewares.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/validators.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/query-config.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/api/vendor/notifications/helpers/helpers.ts",+ "type": "registry:api"+ },+ {+ "path": "vendor-notifications/vendor/components/topbar-actions.tsx",+ "type": "registry:vendor"+ }+ ],+ "categories": [+ "api",+ "vendor"+ ]}]}\ No newline at end of filediff --git a/packages/registry/src/vendor-notifications/api/middlewares.ts b/packages/registry/src/vendor-notifications/api/middlewares.tsnew file mode 100644index 000000000..dc942f5dc--- /dev/null+++ b/packages/registry/src/vendor-notifications/api/middlewares.ts@@ -0,0 +1,3 @@+import { vendorNotificationMiddlewares } from "./vendor/notifications/middlewares"++export const vendorMiddlewares = [...vendorNotificationMiddlewares]diff --git a/packages/registry/src/vendor-notifications/api/vendor/notifications/helpers/helpers.ts b/packages/registry/src/vendor-notifications/api/vendor/notifications/helpers/helpers.tsnew file mode 100644index 000000000..68901dc1d--- /dev/null+++ b/packages/registry/src/vendor-notifications/api/vendor/notifications/helpers/helpers.ts@@ -0,0 +1,20 @@+import { ContainerRegistrationKeys } from "@medusajs/framework/utils"+import { MedusaContainer } from "@medusajs/framework/types"++export const fetchSellerByAuthActorId = async (+ authActorId: string,+ scope: MedusaContainer,+ fields: string[] = ["id"]+) => {+ const query = scope.resolve(ContainerRegistrationKeys.QUERY)++ const {+ data: [seller],+ } = await query.graph({+ entity: "seller",+ filters: { id: authActorId },+ fields,+ })++ return seller+}diff --git a/packages/registry/src/vendor-notifications/api/vendor/notifications/middlewares.ts b/packages/registry/src/vendor-notifications/api/vendor/notifications/middlewares.tsnew file mode 100644index 000000000..6159cc2e0--- /dev/null+++ b/packages/registry/src/vendor-notifications/api/vendor/notifications/middlewares.ts@@ -0,0 +1,17 @@+import { validateAndTransformQuery } from "@medusajs/framework"+import { MiddlewareRoute } from "@medusajs/medusa"+import { vendorNotificationQueryConfig } from "./query-config"+import { VendorGetNotificationParams } from "./validators"++export const vendorNotificationMiddlewares: MiddlewareRoute[] = [+ {+ method: ["GET"],+ matcher: "/vendor/notifications",+ middlewares: [+ validateAndTransformQuery(+ VendorGetNotificationParams,+ vendorNotificationQueryConfig.list+ ),+ ],+ },+]diff --git a/packages/registry/src/vendor-notifications/api/vendor/notifications/query-config.ts b/packages/registry/src/vendor-notifications/api/vendor/notifications/query-config.tsnew file mode 100644index 000000000..298fc5a71--- /dev/null+++ b/packages/registry/src/vendor-notifications/api/vendor/notifications/query-config.ts@@ -0,0 +1,20 @@+export const defaultVendorNotificationFields = [+ "id",+ "to",+ "channel",+ "template",+ "data",+ "created_at",+ "updated_at",+]++export const vendorNotificationQueryConfig = {+ list: {+ defaults: defaultVendorNotificationFields,+ isList: true,+ },+ retrieve: {+ defaults: defaultVendorNotificationFields,+ isList: false,+ },+}diff --git a/packages/registry/src/vendor-notifications/api/vendor/notifications/route.ts b/packages/registry/src/vendor-notifications/api/vendor/notifications/route.tsnew file mode 100644index 000000000..739c2f3af--- /dev/null+++ b/packages/registry/src/vendor-notifications/api/vendor/notifications/route.ts@@ -0,0 +1,28 @@+import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework"+import { ContainerRegistrationKeys } from "@medusajs/framework/utils"+import { fetchSellerByAuthActorId } from "./helpers/helpers"++export async function GET(+ req: AuthenticatedMedusaRequest,+ res: MedusaResponse+): Promise<void> {+ const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)+ const seller = await fetchSellerByAuthActorId(+ req.auth_context.actor_id,+ req.scope+ )++ const { data: notifications, metadata } = await query.graph({+ entity: "notification",+ fields: req.queryConfig.fields,+ filters: { channel: "seller_feed", to: seller.id },+ pagination: req.queryConfig.pagination,+ })++ res.json({+ notifications,+ count: metadata?.count,+ offset: metadata?.skip,+ limit: metadata?.take,+ })+}diff --git a/packages/registry/src/vendor-notifications/api/vendor/notifications/validators.ts b/packages/registry/src/vendor-notifications/api/vendor/notifications/validators.tsnew file mode 100644index 000000000..4ec2baed0--- /dev/null+++ b/packages/registry/src/vendor-notifications/api/vendor/notifications/validators.ts@@ -0,0 +1,11 @@+import { z } from "zod"+import { createFindParams } from "@medusajs/medusa/api/utils/validators"++export type VendorGetNotificationParamsType = z.infer<+ typeof VendorGetNotificationParams+>+export const VendorGetNotificationParams = createFindParams({+ offset: 0,+ limit: 50,+ order: "-created_at",+})diff --git a/packages/registry/src/vendor-notifications/vendor/components/topbar-actions.tsx b/packages/registry/src/vendor-notifications/vendor/components/topbar-actions.tsxnew file mode 100644index 000000000..1ecf2a684--- /dev/null+++ b/packages/registry/src/vendor-notifications/vendor/components/topbar-actions.tsx@@ -0,0 +1,5 @@+import { Notifications } from "@mercurjs/vendor"++export default function TopbarActions() {+ return <Notifications />+}diff --git a/packages/vendor/src/components/layout/notifications/notifications.tsx b/packages/vendor/src/components/layout/notifications/notifications.tsxindex 58d0427d0..e95a71c49 100644--- a/packages/vendor/src/components/layout/notifications/notifications.tsx+++ b/packages/vendor/src/components/layout/notifications/notifications.tsx@@ -92,7 +92,7 @@ export const Notifications = () => {queryFn={(params: any) =>fetchQuery(`/vendor/notifications`, {method: "GET",- query: { ...params, channel: "feed" },+ query: { ...params },})}queryOptions={{ enabled: open }}diff --git a/packages/vendor/src/components/layout/shell/shell.tsx b/packages/vendor/src/components/layout/shell/shell.tsxindex 7268a3896..20f6f2f31 100644--- a/packages/vendor/src/components/layout/shell/shell.tsx+++ b/packages/vendor/src/components/layout/shell/shell.tsx@@ -12,6 +12,7 @@ import {useNavigation,} from "react-router-dom"+import components from "virtual:mercur/components"import { KeybindProvider } from "../../../providers/keybind-provider"import { useGlobalShortcuts } from "../../../providers/keybind-provider/hooks"import { useSidebar } from "../../../providers/sidebar-provider"@@ -191,6 +192,8 @@ const ToggleSidebar = () => {}const Topbar = () => {+ const TopbarActions = components.TopbarActions+return (<div className="grid w-full grid-cols-2 border-b p-3"><div className="flex items-center gap-x-1.5">@@ -198,6 +201,7 @@ const Topbar = () => {<Breadcrumbs /></div><div className="flex items-center justify-end gap-x-3">+ {TopbarActions && <TopbarActions />}</div></div>)diff --git a/packages/vendor/src/index.ts b/packages/vendor/src/index.tsindex d08926896..ff66d964a 100644--- a/packages/vendor/src/index.ts+++ b/packages/vendor/src/index.ts@@ -2,4 +2,7 @@ export { default } from './app'// Reusable form componentsexport { TabbedForm, useTabbedForm } from './components/tabbed-form'-export type { TabDefinition } from './components/tabbed-form'\ No newline at end of file+export type { TabDefinition } from './components/tabbed-form'++// Layout components+export { Notifications } from './components/layout/notifications/notifications'\ No newline at end of file