Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

feat(algolia): integrate Algolia search functionality with product an… (#745)

Jakub Sieradzki03d13242115b
diff --git a/packages/registry/.mercur/_generated/index.ts b/packages/registry/.mercur/_generated/index.ts
index 1c9534804..14efaaf40 100644
--- a/packages/registry/.mercur/_generated/index.ts
+++ b/packages/registry/.mercur/_generated/index.ts
@@ -431,6 +431,7 @@ export type Routes = {
reviews: typeof import("../../src/reviews/api/admin/reviews/route") & {
$id: typeof import("../../src/reviews/api/admin/reviews/[id]/route");
};
+ algolia: typeof import("../../src/algolia/api/admin/algolia/route");
};
auth: {
$actorType: {
@@ -503,6 +504,7 @@ export type Routes = {
};
products: typeof import("@medusajs/medusa/api/store/products/route") & {
$id: typeof import("@medusajs/medusa/api/store/products/[id]/route");
+ search: typeof import("../../src/algolia/api/store/products/search/route");
};
regions: typeof import("@medusajs/medusa/api/store/regions/route") & {
$id: typeof import("@medusajs/medusa/api/store/regions/[id]/route");
@@ -710,6 +712,7 @@ export type Routes = {
};
members: typeof import("../../src/team-management/api/vendor/members/route") & {
$id: typeof import("../../src/team-management/api/vendor/members/[id]/route");
+ me: typeof import("../../src/team-management/api/vendor/members/me/route");
};
invites: typeof import("../../src/team-management/api/vendor/invites/route") & {
accept: typeof import("../../src/team-management/api/vendor/invites/accept/route");
diff --git a/packages/registry/package.json b/packages/registry/package.json
index 0cc3ecbcc..9b94a1667 100644
--- a/packages/registry/package.json
+++ b/packages/registry/package.json
@@ -26,7 +26,8 @@
"react-hook-form": "7.49.1",
"@hookform/resolvers": "3.4.2",
"jsonwebtoken": "9.0.2",
- "react-jwt": "2.0.0"
+ "react-jwt": "2.0.0",
+ "algoliasearch": "^5.40.0"
},
"devDependencies": {
"@mercurjs/types": "workspace:*",
diff --git a/packages/registry/r/algolia.json b/packages/registry/r/algolia.json
new file mode 100644
index 000000000..74c2734ad
--- /dev/null
+++ b/packages/registry/r/algolia.json
@@ -0,0 +1,123 @@
+{
+ "$schema": "https://registry.mercurjs.com/registry-item.json",
+ "name": "algolia",
+ "description": "Algolia search integration with product indexing, event-driven sync, and store search API.",
+ "dependencies": [
+ "algoliasearch"
+ ],
+ "registryDependencies": [],
+ "docs": "## Configuration\n\nAdd the algolia module to your `medusa-config.ts`:\n\n```ts\nmodules: [\n {\n resolve: './modules/algolia',\n options: {\n appId: process.env.ALGOLIA_APP_ID,\n apiKey: process.env.ALGOLIA_API_KEY,\n },\n },\n]\n```\n\n## Environment Variables\n\n```\nALGOLIA_APP_ID=your_algolia_app_id\nALGOLIA_API_KEY=your_algolia_admin_api_key\n```\n\n## Middlewares\n\nAdd algolia middlewares to your `api/middlewares.ts`:\n\n```ts\nimport { defineMiddlewares } from \"@medusajs/medusa\";\nimport { algoliaMiddlewares } from \"./store/products/search/middlewares\";\n\nexport default defineMiddlewares({\n routes: [...algoliaMiddlewares],\n});\n```\n\nIf you already have a `middlewares.ts` file, merge the algolia middleware imports and spread them into your existing `routes` array.\n\n## Database Migrations\n\nAfter installing the block, run migrations for the new links:\n\n```bash\nnpx medusa db:migrate\n```\n\n## Run codegen\n\nAfter installing, regenerate SDK types:\n\n```bash\nnpx @mercurjs/cli codegen\n```",
+ "categories": [
+ "module",
+ "workflow",
+ "api",
+ "link"
+ ],
+ "files": [
+ {
+ "path": "algolia/modules/algolia/index.ts",
+ "content": "import { Module } from '@medusajs/framework/utils'\n\nimport AlgoliaModuleService from './service'\n\nexport const ALGOLIA_MODULE = 'algolia'\nexport { AlgoliaModuleService }\nexport { defaultProductSettings } from './service'\n\nexport default Module(ALGOLIA_MODULE, {\n service: AlgoliaModuleService,\n})\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/modules/algolia/service.ts",
+ "content": "import {\n Action,\n Algoliasearch,\n BatchRequest,\n IndexSettings,\n SearchParams,\n SearchResponse,\n algoliasearch\n} from 'algoliasearch'\n\nimport { AlgoliaEntity, IndexType } from './types'\n\ntype ModuleOptions = {\n appId: string\n apiKey: string\n}\n\nexport const defaultProductSettings: IndexSettings = {\n searchableAttributes: [\n 'title',\n 'subtitle',\n 'tags.value',\n 'type.value',\n 'categories.name',\n 'collection.title',\n 'variants.title'\n ]\n}\n\nclass AlgoliaModuleService {\n private options_: ModuleOptions\n private algolia_: Algoliasearch\n\n constructor(_: unknown, options: ModuleOptions) {\n this.options_ = options\n this.algolia_ = algoliasearch(this.options_.appId, this.options_.apiKey)\n }\n\n getAppId() {\n return this.options_.appId\n }\n\n checkIndex(index: IndexType) {\n return this.algolia_.indexExists({\n indexName: index\n })\n }\n\n updateSettings(index: IndexType, settings: IndexSettings) {\n return this.algolia_.setSettings({\n indexName: index,\n indexSettings: settings\n })\n }\n\n batch(type: IndexType, toAdd: AlgoliaEntity[], toDelete: string[]) {\n const addRequests: BatchRequest[] = toAdd.map((entity) => {\n return {\n action: 'addObject' as Action,\n objectID: entity.id,\n body: entity\n }\n })\n\n const deleteRequests: BatchRequest[] = toDelete.map((id) => {\n return {\n action: 'deleteObject' as Action,\n objectID: id,\n body: {}\n }\n })\n\n const requests = [...addRequests, ...deleteRequests]\n\n return this.algolia_.batch({\n indexName: type,\n batchWriteParams: {\n requests\n }\n })\n }\n\n batchUpsert(type: IndexType, entities: AlgoliaEntity[]) {\n return this.algolia_.batch({\n indexName: type,\n batchWriteParams: {\n requests: entities.map((entity) => {\n return {\n action: 'addObject',\n objectID: entity.id,\n body: entity\n }\n })\n }\n })\n }\n\n batchDelete(type: IndexType, ids: string[]) {\n return this.algolia_.batch({\n indexName: type,\n batchWriteParams: {\n requests: ids.map((id) => {\n return {\n action: 'deleteObject',\n objectID: id,\n body: {}\n }\n })\n }\n })\n }\n\n upsert(type: IndexType, entity: AlgoliaEntity) {\n return this.algolia_.addOrUpdateObject({\n indexName: type,\n objectID: entity.id,\n body: entity\n })\n }\n\n delete(type: IndexType, id: string) {\n return this.algolia_.deleteObject({\n indexName: type,\n objectID: id\n })\n }\n\n partialUpdate(\n type: IndexType,\n entity: Partial<AlgoliaEntity> & { id: string }\n ) {\n return this.algolia_.partialUpdateObject({\n indexName: type,\n objectID: entity.id,\n attributesToUpdate: { ...entity }\n })\n }\n\n search<T = Record<string, unknown>>(\n indexName: IndexType,\n params: SearchParams\n ): Promise<SearchResponse<T>> {\n return this.algolia_.searchSingleIndex<T>({\n indexName,\n searchParams: params\n })\n }\n}\n\nexport default AlgoliaModuleService\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/modules/algolia/types.ts",
+ "content": "import { z } from 'zod'\n\nexport enum IndexType {\n PRODUCT = 'products'\n}\n\nexport enum AlgoliaEvents {\n PRODUCTS_CHANGED = 'algolia.products.changed',\n PRODUCTS_DELETED = 'algolia.products.deleted'\n}\n\nexport enum IntermediateEvents {\n FULFILLMENT_SET_CHANGED = 'algolia.intermediate.fulfillment_set.changed',\n SERVICE_ZONE_CHANGED = 'algolia.intermediate.service_zone.changed',\n SHIPPING_OPTION_CHANGED = 'algolia.intermediate.shipping_option.changed',\n STOCK_LOCATION_CHANGED = 'algolia.intermediate.stock_location.changed',\n INVENTORY_ITEM_CHANGED = 'algolia.intermediate.inventory_item.changed'\n}\n\nexport const AlgoliaVariantValidator = z.object({\n id: z.string(),\n title: z.string().nullish(),\n sku: z.string().nullish(),\n barcode: z.string().nullish(),\n ean: z.string().nullish(),\n ups: z.string().nullish(),\n allow_backorder: z.boolean(),\n manage_inventory: z.boolean(),\n hs_code: z.string().nullish(),\n origin_country: z.string().nullish(),\n mid_code: z.string().nullish(),\n material: z.string().nullish(),\n weight: z.number().nullish(),\n length: z.number().nullish(),\n height: z.number().nullish(),\n width: z.number().nullish(),\n variant_rank: z.number().nullish(),\n options: z.array(\n z.object({\n id: z.string(),\n value: z.string(),\n option: z.object({\n id: z.string(),\n title: z.string(),\n }),\n })\n ),\n prices: z.array(\n z.object({\n id: z.string(),\n title: z.string().nullish(),\n currency_code: z.string(),\n min_quantity: z.number().nullish(),\n max_quantity: z.number().nullish(),\n rules_count: z.number(),\n amount: z.number(),\n })\n ),\n})\n\nexport type AlgoliaProduct = z.infer<typeof AlgoliaProductValidator>\nexport const AlgoliaProductValidator = z.object({\n id: z.string(),\n title: z.string(),\n handle: z.string(),\n subtitle: z.string().nullable(),\n description: z.string().nullable(),\n thumbnail: z.string().nullable(),\n average_rating: z.coerce.number().nullable().default(null),\n supported_countries: z.array(z.string()).nullable().default([]),\n options: z.array(z.record(z.string())).nullable().default(null),\n images: z\n .array(\n z.object({\n id: z.string(),\n url: z.string(),\n rank: z.number(),\n })\n )\n .nullable()\n .optional(),\n collection: z\n .object({\n title: z.string(),\n })\n .nullable()\n .optional(),\n type: z\n .object({\n value: z.string(),\n })\n .nullable()\n .optional(),\n tags: z\n .array(\n z.object({\n value: z.string(),\n })\n )\n .optional(),\n categories: z\n .array(\n z.object({\n name: z.string(),\n id: z.string(),\n })\n )\n .optional(),\n variants: z.any().nullable().default(null),\n sku: z.string().nullable().optional(),\n ean: z.string().nullable().optional(),\n upc: z.string().nullable().optional(),\n barcode: z.string().nullable().optional(),\n hs_code: z.string().nullable().optional(),\n mid_code: z.string().nullable().optional(),\n weight: z.coerce.number().nullable().optional(),\n length: z.coerce.number().nullable().optional(),\n height: z.coerce.number().nullable().optional(),\n width: z.coerce.number().nullable().optional(),\n origin_country: z.string().nullable().optional(),\n material: z.string().nullable().optional(),\n seller: z\n .object({\n id: z.string(),\n handle: z.string().nullish(),\n status: z.string().nullish(),\n })\n .nullable(),\n})\n\nexport type AlgoliaEntity = AlgoliaProduct\n\nexport type AlgoliaSearchResult<T> = {\n hits: T[]\n nbHits: number\n page: number\n nbPages: number\n hitsPerPage: number\n facets?: Record<string, Record<string, number>>\n facets_stats?: Record<\n string,\n { min: number; max: number; avg: number; sum: number }\n >\n processingTimeMS: number\n query: string\n}\n\nexport interface IAlgoliaModuleService {\n search<T = Record<string, unknown>>(\n indexName: IndexType,\n params: Record<string, unknown>\n ): Promise<AlgoliaSearchResult<T>>\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/links/seller-fulfillment-set.ts",
+ "content": "import { defineLink } from '@medusajs/framework/utils'\nimport FulfillmentModule from '@medusajs/medusa/fulfillment'\nimport SellerModule from '@mercurjs/core-plugin/modules/seller'\n\nexport default defineLink(SellerModule.linkable.seller, {\n linkable: FulfillmentModule.linkable.fulfillmentSet,\n isList: true,\n})\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/links/seller-service-zone.ts",
+ "content": "import { defineLink } from '@medusajs/framework/utils'\nimport FulfillmentModule from '@medusajs/medusa/fulfillment'\nimport SellerModule from '@mercurjs/core-plugin/modules/seller'\n\nexport default defineLink(SellerModule.linkable.seller, {\n linkable: FulfillmentModule.linkable.serviceZone,\n isList: true,\n})\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/workflows/algolia/steps/sync-algolia-products.ts",
+ "content": "import { IEventBusModuleService, RemoteQueryFunction } from '@medusajs/framework/types'\nimport { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'\nimport { StepResponse, createStep } from '@medusajs/framework/workflows-sdk'\n\nimport { ALGOLIA_MODULE, AlgoliaModuleService } from '../../../modules/algolia'\nimport { AlgoliaEvents, IndexType } from '../../../modules/algolia/types'\n\nconst CHUNK_SIZE = 100\n\nexport const syncAlgoliaProductsStep = createStep(\n 'sync-algolia-products',\n async (_: void, { container }) => {\n const query = container.resolve<RemoteQueryFunction>(ContainerRegistrationKeys.QUERY)\n const algolia = container.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)\n\n const { data: productsToDelete } = await query.graph({\n entity: 'product',\n filters: {\n $or: [\n {\n deleted_at: {\n $ne: null,\n },\n },\n {\n status: {\n $ne: 'published',\n },\n },\n ],\n },\n fields: ['id'],\n })\n\n await algolia.batchDelete(\n IndexType.PRODUCT,\n productsToDelete.map((p) => p.id)\n )\n\n const { data: publishedProducts } = await query.graph({\n entity: 'product',\n filters: {\n status: 'published',\n },\n fields: ['id'],\n })\n\n const productsToInsert = publishedProducts.map((p) => p.id)\n const productChunks: string[][] = []\n for (let i = 0; i < productsToInsert.length; i += CHUNK_SIZE) {\n productChunks.push(productsToInsert.slice(i, i + CHUNK_SIZE))\n }\n\n const eventBus = container.resolve<IEventBusModuleService>(Modules.EVENT_BUS)\n for (const chunk of productChunks) {\n await eventBus.emit({\n name: AlgoliaEvents.PRODUCTS_CHANGED,\n data: {\n ids: chunk,\n },\n })\n }\n\n return new StepResponse()\n }\n)\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/workflows/algolia/workflows/sync-algolia.ts",
+ "content": "import { WorkflowResponse, createWorkflow } from '@medusajs/workflows-sdk'\n\nimport { syncAlgoliaProductsStep } from '../steps/sync-algolia-products'\n\nexport const syncAlgoliaWorkflow = createWorkflow(\n 'sync-algolia-workflow',\n function () {\n return new WorkflowResponse(syncAlgoliaProductsStep())\n }\n)\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-product-events-bridge.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { IEventBusModuleService } from '@medusajs/framework/types'\nimport {\n ContainerRegistrationKeys,\n Modules,\n} from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents } from '../modules/algolia/types'\n\nexport default async function algoliaProductEventsBridgeHandler({\n event,\n container\n}: SubscriberArgs<{ id: string }>) {\n const logger = container.resolve(ContainerRegistrationKeys.LOGGER)\n const eventBus = container.resolve<IEventBusModuleService>(Modules.EVENT_BUS)\n\n logger.info(`Algolia bridge: received event ${event.name} with data ${JSON.stringify(event.data)}`)\n\n const isDelete = event.name === 'product.product.deleted' || event.name === 'product.deleted'\n\n await eventBus.emit({\n name: isDelete\n ? AlgoliaEvents.PRODUCTS_DELETED\n : AlgoliaEvents.PRODUCTS_CHANGED,\n data: { ids: [event.data.id] },\n })\n}\n\nexport const config: SubscriberConfig = {\n event: [\n 'product.created',\n 'product.updated',\n 'product.deleted',\n 'product.product.created',\n 'product.product.updated',\n 'product.product.deleted',\n ],\n context: {\n subscriberId: 'algolia-product-events-bridge'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-products-changed.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IndexType } from '../modules/algolia/types'\nimport { ALGOLIA_MODULE, AlgoliaModuleService } from '../modules/algolia'\nimport {\n filterProductsByStatus,\n findAndTransformAlgoliaProducts\n} from './utils/algolia-product'\n\nexport default async function algoliaProductsChangedHandler({\n event,\n container\n}: SubscriberArgs<{ ids: string[] }>) {\n const logger = container.resolve(ContainerRegistrationKeys.LOGGER)\n\n try {\n const algolia = container.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)\n\n const { published, other } = await filterProductsByStatus(\n container,\n event.data.ids\n )\n\n logger.debug(\n `Algolia sync: Processing ${event.data.ids.length} products - ${published.length} to upsert, ${other.length} to delete`\n )\n\n const productsToInsert = published.length\n ? await findAndTransformAlgoliaProducts(container, published)\n : []\n\n await algolia.batch(IndexType.PRODUCT, productsToInsert, other)\n\n logger.debug(\n `Algolia sync: Successfully synced ${productsToInsert.length} products`\n )\n } catch (error: unknown) {\n logger.error(\n `Algolia sync failed for products ${event.data.ids.join(', ')}:`,\n error as Error\n )\n throw error\n }\n}\n\nexport const config: SubscriberConfig = {\n event: AlgoliaEvents.PRODUCTS_CHANGED,\n context: {\n subscriberId: 'algolia-products-changed-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-products-deleted.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IndexType } from '../modules/algolia/types'\nimport { ALGOLIA_MODULE, AlgoliaModuleService } from '../modules/algolia'\n\nexport default async function algoliaProductsDeletedHandler({\n event,\n container\n}: SubscriberArgs<{ ids: string[] }>) {\n const logger = container.resolve(ContainerRegistrationKeys.LOGGER)\n\n try {\n const algolia = container.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)\n\n logger.debug(\n `Algolia sync: Deleting ${event.data.ids.length} products from index`\n )\n\n await algolia.batchDelete(IndexType.PRODUCT, event.data.ids)\n\n logger.debug(\n `Algolia sync: Successfully deleted products ${event.data.ids.join(', ')}`\n )\n } catch (error: unknown) {\n logger.error(\n `Algolia delete failed for products ${event.data.ids.join(', ')}:`,\n error as Error\n )\n throw error\n }\n}\n\nexport const config: SubscriberConfig = {\n event: AlgoliaEvents.PRODUCTS_DELETED,\n context: {\n subscriberId: 'algolia-products-deleted-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/utils/algolia-product.ts",
+ "content": "import { z } from 'zod'\n\nimport { MedusaContainer } from '@medusajs/framework'\nimport {\n ContainerRegistrationKeys,\n arrayDifference,\n} from '@medusajs/framework/utils'\n\nimport {\n AlgoliaProductValidator,\n AlgoliaVariantValidator\n} from '../../modules/algolia/types'\n\nasync function selectProductVariantsSupportedCountries(\n container: MedusaContainer,\n product_id: string\n) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const { data: variants } = await query.graph({\n entity: 'product_variant',\n fields: ['inventory_items.inventory.location_levels.location_id'],\n filters: {\n product_id\n }\n })\n\n let location_ids: string[] = []\n\n for (const variant of variants) {\n const inventory_items =\n (variant as any).inventory_items?.map((item: any) => item.inventory) || []\n const locations = inventory_items\n .flatMap((inventory_item: any) => inventory_item.location_levels || [])\n .map((level: any) => level.location_id)\n\n location_ids = location_ids.concat(locations)\n }\n\n const { data: stock_locations } = await query.graph({\n entity: 'stock_location',\n fields: ['fulfillment_sets.service_zones.geo_zones.country_code'],\n filters: {\n id: location_ids\n }\n })\n\n let country_codes: string[] = []\n\n for (const location of stock_locations) {\n const fulfillmentSets =\n (location as any).fulfillment_sets?.flatMap((set: any) => set.service_zones || []) || []\n const codes = fulfillmentSets\n .flatMap((sz: any) => sz.geo_zones || [])\n .map((gz: any) => gz.country_code)\n\n country_codes = country_codes.concat(codes)\n }\n\n return [...new Set(country_codes)]\n}\n\nasync function selectProductSeller(\n container: MedusaContainer,\n product_id: string\n) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n\n const {\n data: [product]\n } = await query.graph({\n entity: 'product',\n fields: ['seller.id', 'seller.handle', 'seller.status'],\n filters: {\n id: product_id\n }\n })\n\n\n\n const p = product as any\n return p?.seller?.id\n ? {\n id: p.seller.id as string,\n handle: p.seller.handle as string | null,\n status: p.seller.status as string | null\n }\n : null\n}\n\nexport async function filterProductsByStatus(\n container: MedusaContainer,\n ids: string[] = []\n) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n\n const { data: products } = await query.graph({\n entity: 'product',\n fields: ['id', 'status'],\n filters: {\n id: ids\n }\n })\n\n const published = products.filter((p) => p.status === 'published')\n const notPublished = arrayDifference(products, published)\n\n const existingIds = new Set(products.map((p) => p.id))\n\n const deletedIds = ids.filter((id) => !existingIds.has(id))\n\n return {\n published: published.map((p) => p.id),\n other: [...notPublished.map((p) => p.id), ...deletedIds]\n }\n}\n\nexport async function findAndTransformAlgoliaProducts(\n container: MedusaContainer,\n ids: string[] = []\n) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n\n const { data: products } = await query.graph({\n entity: 'product',\n fields: [\n '*',\n 'categories.name',\n 'categories.id',\n 'collection.title',\n 'tags.value',\n 'type.value',\n 'variants.*',\n 'variants.options.*',\n 'variants.options.prices.*',\n 'variants.prices.*',\n 'options.*',\n 'options.values.*',\n 'images.*'\n ],\n filters: ids.length\n ? {\n id: ids,\n status: 'published'\n }\n : { status: 'published' }\n })\n\n for (const product of products as any[]) {\n product.average_rating = 0\n product.supported_countries = await selectProductVariantsSupportedCountries(\n container,\n product.id\n )\n product.seller = await selectProductSeller(container, product.id)\n\n product.options = (product.options ?? [])\n .filter((option: any) => option?.title && option?.values)\n .map((option: any) => {\n return option.values.map((value: any) => {\n const entry: Record<string, string> = {}\n entry[option.title.toLowerCase()] = value.value\n return entry\n })\n })\n .flat()\n\n product.variants = z\n .array(AlgoliaVariantValidator)\n .parse(product.variants ?? [])\n product.variants = (product.variants ?? [])\n .map((variant: any) => {\n return (variant.options ?? []).reduce((entry: any, item: any) => {\n if (item?.option?.title) {\n entry[item.option.title.toLowerCase()] = item.value\n }\n return entry\n }, variant)\n })\n .flat()\n\n }\n\n return z.array(AlgoliaProductValidator).parse(products)\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-fulfillment-set-changed.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'\nimport sellerFulfillmentSet from '../links/seller-fulfillment-set'\nimport productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'\n\nexport default async function fulfillmentSetChangedHandler({\n event,\n container\n}: SubscriberArgs<{ id: string }>) {\n const fulfillment_set_id = event.data.id\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const eventBus = container.resolve(Modules.EVENT_BUS)\n\n const {\n data: [seller]\n } = await query.graph({\n entity: sellerFulfillmentSet.entryPoint,\n fields: ['seller_id'],\n filters: {\n fulfillment_set_id\n },\n withDeleted: true\n })\n\n if (!seller) {\n return\n }\n\n const { data: products } = await query.graph({\n entity: productSellerLink.entryPoint,\n fields: ['product_id'],\n filters: {\n seller_id: seller.seller_id\n }\n })\n\n await eventBus.emit({\n name: AlgoliaEvents.PRODUCTS_CHANGED,\n data: { ids: products.map((p) => p.product_id) }\n })\n}\n\nexport const config: SubscriberConfig = {\n event: IntermediateEvents.FULFILLMENT_SET_CHANGED,\n context: {\n subscriberId: 'fulfillment-set-changed-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-inventory-item-changed.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'\n\nexport default async function inventoryItemChangedHandler({\n event,\n container\n}: SubscriberArgs<{ id: string | string[] }>) {\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const eventBus = container.resolve(Modules.EVENT_BUS)\n\n const { data: items } = await query.graph({\n entity: 'inventory_item',\n fields: ['variants.product_id'],\n filters: {\n id: event.data.id\n }\n })\n\n const products = items\n .flatMap((items) => items.variants)\n .map((variant) => variant.product_id)\n\n await eventBus.emit({\n name: AlgoliaEvents.PRODUCTS_CHANGED,\n data: { ids: [...new Set(products)] }\n })\n}\n\nexport const config: SubscriberConfig = {\n event: IntermediateEvents.INVENTORY_ITEM_CHANGED,\n context: {\n subscriberId: 'inventory-item-changed-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-service-zone-changed.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'\nimport productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'\nimport sellerServiceZone from '../links/seller-service-zone'\n\nexport default async function serviceZoneChangedHandler({\n event,\n container\n}: SubscriberArgs<{ id: string }>) {\n const service_zone_id = event.data.id\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const eventBus = container.resolve(Modules.EVENT_BUS)\n\n const {\n data: [seller]\n } = await query.graph({\n entity: sellerServiceZone.entryPoint,\n fields: ['seller_id'],\n filters: {\n service_zone_id\n },\n withDeleted: true\n })\n\n if (!seller) {\n return\n }\n\n const { data: products } = await query.graph({\n entity: productSellerLink.entryPoint,\n fields: ['product_id'],\n filters: {\n seller_id: seller.seller_id\n }\n })\n\n await eventBus.emit({\n name: AlgoliaEvents.PRODUCTS_CHANGED,\n data: { ids: products.map((p) => p.product_id) }\n })\n}\n\nexport const config: SubscriberConfig = {\n event: IntermediateEvents.SERVICE_ZONE_CHANGED,\n context: {\n subscriberId: 'service-zone-changed-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-shipping-option-changed.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'\nimport productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'\nimport shippingOptionSellerLink from '@mercurjs/core-plugin/links/shipping-option-seller-link'\n\nexport default async function shippingOptionChangedHandler({\n event,\n container\n}: SubscriberArgs<{ id: string }>) {\n const shipping_option_id = event.data.id\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const eventBus = container.resolve(Modules.EVENT_BUS)\n\n const {\n data: [seller]\n } = await query.graph({\n entity: shippingOptionSellerLink.entryPoint,\n fields: ['seller_id'],\n filters: {\n shipping_option_id\n },\n withDeleted: true\n })\n\n if (!seller) {\n return\n }\n\n const { data: products } = await query.graph({\n entity: productSellerLink.entryPoint,\n fields: ['product_id'],\n filters: {\n seller_id: seller.seller_id\n }\n })\n\n await eventBus.emit({\n name: AlgoliaEvents.PRODUCTS_CHANGED,\n data: { ids: products.map((p) => p.product_id) }\n })\n}\n\nexport const config: SubscriberConfig = {\n event: IntermediateEvents.SHIPPING_OPTION_CHANGED,\n context: {\n subscriberId: 'shipping-option-changed-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-stock-location-changed.ts",
+ "content": "import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'\nimport { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'\n\nimport { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'\nimport productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'\nimport stockLocationSellerLink from '@mercurjs/core-plugin/links/stock-location-seller-link'\n\nexport default async function stockLocationChangedHandler({\n event,\n container\n}: SubscriberArgs<{ id: string }>) {\n const stock_location_id = event.data.id\n const query = container.resolve(ContainerRegistrationKeys.QUERY)\n const eventBus = container.resolve(Modules.EVENT_BUS)\n\n const {\n data: [seller]\n } = await query.graph({\n entity: stockLocationSellerLink.entryPoint,\n fields: ['seller_id'],\n filters: {\n stock_location_id\n },\n withDeleted: true\n })\n\n if (!seller) {\n return\n }\n\n const { data: products } = await query.graph({\n entity: productSellerLink.entryPoint,\n fields: ['product_id'],\n filters: {\n seller_id: seller.seller_id\n }\n })\n\n await eventBus.emit({\n name: AlgoliaEvents.PRODUCTS_CHANGED,\n data: { ids: products.map((p) => p.product_id) }\n })\n}\n\nexport const config: SubscriberConfig = {\n event: IntermediateEvents.STOCK_LOCATION_CHANGED,\n context: {\n subscriberId: 'stock-location-changed-handler'\n }\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/middlewares.ts",
+ "content": "import { algoliaMiddlewares } from './store/products/search/middlewares'\n\nexport const allAlgoliaMiddlewares = [...algoliaMiddlewares]\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/admin/algolia/route.ts",
+ "content": "import { MedusaRequest, MedusaResponse } from '@medusajs/framework'\n\nimport { ALGOLIA_MODULE, AlgoliaModuleService } from '../../../modules/algolia'\nimport { IndexType } from '../../../modules/algolia/types'\nimport { syncAlgoliaWorkflow } from '../../../workflows/algolia/workflows/sync-algolia'\n\nexport const POST = async (req: MedusaRequest, res: MedusaResponse) => {\n await syncAlgoliaWorkflow.run({\n container: req.scope,\n })\n\n res.status(200).json({ message: 'Sync in progress' })\n}\n\nexport const GET = async (req: MedusaRequest, res: MedusaResponse) => {\n const algoliaService =\n req.scope.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)\n\n const appId = algoliaService.getAppId()\n const productIndex = await algoliaService.checkIndex(IndexType.PRODUCT)\n res.status(200).json({ appId, productIndex })\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/route.ts",
+ "content": "import { MedusaRequest, MedusaResponse } from '@medusajs/framework'\nimport {\n ContainerRegistrationKeys,\n QueryContext\n} from '@medusajs/framework/utils'\n\nimport { ALGOLIA_MODULE } from '../../../../modules/algolia'\nimport { IAlgoliaModuleService, IndexType } from '../../../../modules/algolia/types'\nimport { StoreSearchProductsType } from './validators'\n\ntype AlgoliaHit = {\n id: string\n}\n\nexport const POST = async (\n req: MedusaRequest<StoreSearchProductsType>,\n res: MedusaResponse\n) => {\n const algoliaService =\n req.scope.resolve<IAlgoliaModuleService>(ALGOLIA_MODULE)\n const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)\n\n const {\n query: searchQuery,\n page,\n hitsPerPage,\n filters,\n facets,\n maxValuesPerFacet,\n currency_code,\n region_id,\n customer_id,\n customer_group_id\n } = req.validatedBody\n\n const searchParams: Record<string, unknown> = {\n query: searchQuery,\n page,\n hitsPerPage,\n attributesToRetrieve: ['id'],\n attributesToHighlight: []\n }\n\n if (filters) {\n searchParams.filters = filters\n }\n if (facets) {\n searchParams.facets = facets\n }\n if (maxValuesPerFacet) {\n searchParams.maxValuesPerFacet = maxValuesPerFacet\n }\n\n const algoliaResult = await algoliaService.search<AlgoliaHit>(\n IndexType.PRODUCT,\n searchParams\n )\n\n const productIds = algoliaResult.hits.map((hit) => hit.id)\n\n if (productIds.length === 0) {\n return res.json({\n products: [],\n nbHits: algoliaResult.nbHits,\n page: algoliaResult.page,\n nbPages: algoliaResult.nbPages,\n hitsPerPage: algoliaResult.hitsPerPage,\n facets: algoliaResult.facets,\n facets_stats: algoliaResult.facets_stats,\n processingTimeMS: algoliaResult.processingTimeMS,\n query: algoliaResult.query\n })\n }\n\n const hasPricingContext =\n currency_code || region_id || customer_id || customer_group_id\n\n const contextParams: Record<string, unknown> = {}\n if (hasPricingContext) {\n contextParams.variants = {\n calculated_price: QueryContext({\n ...(currency_code && { currency_code }),\n ...(region_id && { region_id }),\n ...(customer_id && { customer_id }),\n ...(customer_group_id && { customer_group_id })\n })\n }\n }\n\n const { data: products } = await query.graph({\n entity: 'product',\n fields: [\n '*',\n 'images.*',\n 'options.*',\n 'options.values.*',\n 'variants.*',\n 'variants.options.*',\n 'variants.prices.*',\n ...(hasPricingContext ? ['variants.calculated_price.*'] : []),\n 'categories.*',\n 'collection.*',\n 'type.*',\n 'tags.*',\n 'seller.*'\n ],\n filters: {\n id: productIds\n },\n ...(Object.keys(contextParams).length > 0 && { context: contextParams })\n })\n\n const productMap = new Map(products.map((p) => [p.id, p]))\n const orderedProducts = productIds\n .map((id) => productMap.get(id))\n .filter(Boolean)\n\n res.json({\n products: orderedProducts,\n nbHits: algoliaResult.nbHits,\n page: algoliaResult.page,\n nbPages: algoliaResult.nbPages,\n hitsPerPage: algoliaResult.hitsPerPage,\n facets: algoliaResult.facets,\n facets_stats: algoliaResult.facets_stats,\n processingTimeMS: algoliaResult.processingTimeMS,\n query: algoliaResult.query\n })\n}\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/middlewares.ts",
+ "content": "import {\n MiddlewareRoute,\n validateAndTransformBody\n} from '@medusajs/framework'\n\nimport { StoreSearchProductsSchema } from './validators'\n\nexport const algoliaMiddlewares: MiddlewareRoute[] = [\n {\n methods: ['POST'],\n matcher: '/store/products/search',\n middlewares: [validateAndTransformBody(StoreSearchProductsSchema)]\n }\n]\n",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/validators.ts",
+ "content": "import { z } from 'zod'\n\nexport type StoreSearchProductsType = z.infer<typeof StoreSearchProductsSchema>\n\nexport const StoreSearchProductsSchema = z.object({\n query: z.string().default(''),\n page: z.coerce.number().int().min(0).default(0),\n hitsPerPage: z.coerce.number().int().min(1).max(100).default(12),\n filters: z.string().optional(),\n facets: z.array(z.string()).optional(),\n maxValuesPerFacet: z.coerce.number().int().min(1).max(1000).optional(),\n currency_code: z.string().length(3).optional(),\n region_id: z.string().optional(),\n customer_id: z.string().optional(),\n customer_group_id: z.array(z.string()).optional()\n})\n",
+ "type": "registry:api"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/packages/registry/r/registry.json b/packages/registry/r/registry.json
index a7d661995..4e67fcdbc 100644
--- a/packages/registry/r/registry.json
+++ b/packages/registry/r/registry.json
@@ -597,6 +597,107 @@
"api",
"vendor"
]
+ },
+ {
+ "name": "algolia",
+ "description": "Algolia search integration with product indexing, event-driven sync, and store search API.",
+ "dependencies": [
+ "algoliasearch"
+ ],
+ "registryDependencies": [],
+ "docs": "## Configuration\n\nAdd the algolia module to your `medusa-config.ts`:\n\n```ts\nmodules: [\n {\n resolve: './modules/algolia',\n options: {\n appId: process.env.ALGOLIA_APP_ID,\n apiKey: process.env.ALGOLIA_API_KEY,\n },\n },\n]\n```\n\n## Environment Variables\n\n```\nALGOLIA_APP_ID=your_algolia_app_id\nALGOLIA_API_KEY=your_algolia_admin_api_key\n```\n\n## Middlewares\n\nAdd algolia middlewares to your `api/middlewares.ts`:\n\n```ts\nimport { defineMiddlewares } from \"@medusajs/medusa\";\nimport { algoliaMiddlewares } from \"./store/products/search/middlewares\";\n\nexport default defineMiddlewares({\n routes: [...algoliaMiddlewares],\n});\n```\n\nIf you already have a `middlewares.ts` file, merge the algolia middleware imports and spread them into your existing `routes` array.\n\n## Database Migrations\n\nAfter installing the block, run migrations for the new links:\n\n```bash\nnpx medusa db:migrate\n```\n\n## Run codegen\n\nAfter installing, regenerate SDK types:\n\n```bash\nnpx @mercurjs/cli codegen\n```",
+ "files": [
+ {
+ "path": "algolia/modules/algolia/index.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/modules/algolia/service.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/modules/algolia/types.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/links/seller-fulfillment-set.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/links/seller-service-zone.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/workflows/algolia/steps/sync-algolia-products.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/workflows/algolia/workflows/sync-algolia.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-product-events-bridge.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-products-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-products-deleted.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/utils/algolia-product.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-fulfillment-set-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-inventory-item-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-service-zone-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-shipping-option-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-stock-location-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/middlewares.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/admin/algolia/route.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/route.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/middlewares.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/validators.ts",
+ "type": "registry:api"
+ }
+ ],
+ "categories": [
+ "module",
+ "workflow",
+ "api",
+ "link"
+ ]
}
]
}
\ No newline at end of file
diff --git a/packages/registry/registry.json b/packages/registry/registry.json
index a7d661995..4e67fcdbc 100644
--- a/packages/registry/registry.json
+++ b/packages/registry/registry.json
@@ -597,6 +597,107 @@
"api",
"vendor"
]
+ },
+ {
+ "name": "algolia",
+ "description": "Algolia search integration with product indexing, event-driven sync, and store search API.",
+ "dependencies": [
+ "algoliasearch"
+ ],
+ "registryDependencies": [],
+ "docs": "## Configuration\n\nAdd the algolia module to your `medusa-config.ts`:\n\n```ts\nmodules: [\n {\n resolve: './modules/algolia',\n options: {\n appId: process.env.ALGOLIA_APP_ID,\n apiKey: process.env.ALGOLIA_API_KEY,\n },\n },\n]\n```\n\n## Environment Variables\n\n```\nALGOLIA_APP_ID=your_algolia_app_id\nALGOLIA_API_KEY=your_algolia_admin_api_key\n```\n\n## Middlewares\n\nAdd algolia middlewares to your `api/middlewares.ts`:\n\n```ts\nimport { defineMiddlewares } from \"@medusajs/medusa\";\nimport { algoliaMiddlewares } from \"./store/products/search/middlewares\";\n\nexport default defineMiddlewares({\n routes: [...algoliaMiddlewares],\n});\n```\n\nIf you already have a `middlewares.ts` file, merge the algolia middleware imports and spread them into your existing `routes` array.\n\n## Database Migrations\n\nAfter installing the block, run migrations for the new links:\n\n```bash\nnpx medusa db:migrate\n```\n\n## Run codegen\n\nAfter installing, regenerate SDK types:\n\n```bash\nnpx @mercurjs/cli codegen\n```",
+ "files": [
+ {
+ "path": "algolia/modules/algolia/index.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/modules/algolia/service.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/modules/algolia/types.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/links/seller-fulfillment-set.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/links/seller-service-zone.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/workflows/algolia/steps/sync-algolia-products.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/workflows/algolia/workflows/sync-algolia.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-product-events-bridge.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-products-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-products-deleted.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/utils/algolia-product.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-fulfillment-set-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-inventory-item-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-service-zone-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-shipping-option-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/subscribers/algolia-stock-location-changed.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/middlewares.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/admin/algolia/route.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/route.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/middlewares.ts",
+ "type": "registry:api"
+ },
+ {
+ "path": "algolia/api/store/products/search/validators.ts",
+ "type": "registry:api"
+ }
+ ],
+ "categories": [
+ "module",
+ "workflow",
+ "api",
+ "link"
+ ]
}
]
}
\ No newline at end of file
diff --git a/packages/registry/src/algolia/api/admin/algolia/route.ts b/packages/registry/src/algolia/api/admin/algolia/route.ts
new file mode 100644
index 000000000..4ac4ec4e0
--- /dev/null
+++ b/packages/registry/src/algolia/api/admin/algolia/route.ts
@@ -0,0 +1,22 @@
+import { MedusaRequest, MedusaResponse } from '@medusajs/framework'
+
+import { ALGOLIA_MODULE, AlgoliaModuleService } from '../../../modules/algolia'
+import { IndexType } from '../../../modules/algolia/types'
+import { syncAlgoliaWorkflow } from '../../../workflows/algolia/workflows/sync-algolia'
+
+export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
+ await syncAlgoliaWorkflow.run({
+ container: req.scope,
+ })
+
+ res.status(200).json({ message: 'Sync in progress' })
+}
+
+export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
+ const algoliaService =
+ req.scope.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)
+
+ const appId = algoliaService.getAppId()
+ const productIndex = await algoliaService.checkIndex(IndexType.PRODUCT)
+ res.status(200).json({ appId, productIndex })
+}
diff --git a/packages/registry/src/algolia/api/middlewares.ts b/packages/registry/src/algolia/api/middlewares.ts
new file mode 100644
index 000000000..4724e23e8
--- /dev/null
+++ b/packages/registry/src/algolia/api/middlewares.ts
@@ -0,0 +1,3 @@
+import { algoliaMiddlewares } from './store/products/search/middlewares'
+
+export const allAlgoliaMiddlewares = [...algoliaMiddlewares]
diff --git a/packages/registry/src/algolia/api/store/products/search/middlewares.ts b/packages/registry/src/algolia/api/store/products/search/middlewares.ts
new file mode 100644
index 000000000..d57c1267c
--- /dev/null
+++ b/packages/registry/src/algolia/api/store/products/search/middlewares.ts
@@ -0,0 +1,14 @@
+import {
+ MiddlewareRoute,
+ validateAndTransformBody
+} from '@medusajs/framework'
+
+import { StoreSearchProductsSchema } from './validators'
+
+export const algoliaMiddlewares: MiddlewareRoute[] = [
+ {
+ methods: ['POST'],
+ matcher: '/store/products/search',
+ middlewares: [validateAndTransformBody(StoreSearchProductsSchema)]
+ }
+]
diff --git a/packages/registry/src/algolia/api/store/products/search/route.ts b/packages/registry/src/algolia/api/store/products/search/route.ts
new file mode 100644
index 000000000..a854e2b26
--- /dev/null
+++ b/packages/registry/src/algolia/api/store/products/search/route.ts
@@ -0,0 +1,129 @@
+import { MedusaRequest, MedusaResponse } from '@medusajs/framework'
+import {
+ ContainerRegistrationKeys,
+ QueryContext
+} from '@medusajs/framework/utils'
+
+import { ALGOLIA_MODULE } from '../../../../modules/algolia'
+import { IAlgoliaModuleService, IndexType } from '../../../../modules/algolia/types'
+import { StoreSearchProductsType } from './validators'
+
+type AlgoliaHit = {
+ id: string
+}
+
+export const POST = async (
+ req: MedusaRequest<StoreSearchProductsType>,
+ res: MedusaResponse
+) => {
+ const algoliaService =
+ req.scope.resolve<IAlgoliaModuleService>(ALGOLIA_MODULE)
+ const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
+
+ const {
+ query: searchQuery,
+ page,
+ hitsPerPage,
+ filters,
+ facets,
+ maxValuesPerFacet,
+ currency_code,
+ region_id,
+ customer_id,
+ customer_group_id
+ } = req.validatedBody
+
+ const searchParams: Record<string, unknown> = {
+ query: searchQuery,
+ page,
+ hitsPerPage,
+ attributesToRetrieve: ['id'],
+ attributesToHighlight: []
+ }
+
+ if (filters) {
+ searchParams.filters = filters
+ }
+ if (facets) {
+ searchParams.facets = facets
+ }
+ if (maxValuesPerFacet) {
+ searchParams.maxValuesPerFacet = maxValuesPerFacet
+ }
+
+ const algoliaResult = await algoliaService.search<AlgoliaHit>(
+ IndexType.PRODUCT,
+ searchParams
+ )
+
+ const productIds = algoliaResult.hits.map((hit) => hit.id)
+
+ if (productIds.length === 0) {
+ return res.json({
+ products: [],
+ nbHits: algoliaResult.nbHits,
+ page: algoliaResult.page,
+ nbPages: algoliaResult.nbPages,
+ hitsPerPage: algoliaResult.hitsPerPage,
+ facets: algoliaResult.facets,
+ facets_stats: algoliaResult.facets_stats,
+ processingTimeMS: algoliaResult.processingTimeMS,
+ query: algoliaResult.query
+ })
+ }
+
+ const hasPricingContext =
+ currency_code || region_id || customer_id || customer_group_id
+
+ const contextParams: Record<string, unknown> = {}
+ if (hasPricingContext) {
+ contextParams.variants = {
+ calculated_price: QueryContext({
+ ...(currency_code && { currency_code }),
+ ...(region_id && { region_id }),
+ ...(customer_id && { customer_id }),
+ ...(customer_group_id && { customer_group_id })
+ })
+ }
+ }
+
+ const { data: products } = await query.graph({
+ entity: 'product',
+ fields: [
+ '*',
+ 'images.*',
+ 'options.*',
+ 'options.values.*',
+ 'variants.*',
+ 'variants.options.*',
+ 'variants.prices.*',
+ ...(hasPricingContext ? ['variants.calculated_price.*'] : []),
+ 'categories.*',
+ 'collection.*',
+ 'type.*',
+ 'tags.*',
+ 'seller.*'
+ ],
+ filters: {
+ id: productIds
+ },
+ ...(Object.keys(contextParams).length > 0 && { context: contextParams })
+ })
+
+ const productMap = new Map(products.map((p) => [p.id, p]))
+ const orderedProducts = productIds
+ .map((id) => productMap.get(id))
+ .filter(Boolean)
+
+ res.json({
+ products: orderedProducts,
+ nbHits: algoliaResult.nbHits,
+ page: algoliaResult.page,
+ nbPages: algoliaResult.nbPages,
+ hitsPerPage: algoliaResult.hitsPerPage,
+ facets: algoliaResult.facets,
+ facets_stats: algoliaResult.facets_stats,
+ processingTimeMS: algoliaResult.processingTimeMS,
+ query: algoliaResult.query
+ })
+}
diff --git a/packages/registry/src/algolia/api/store/products/search/validators.ts b/packages/registry/src/algolia/api/store/products/search/validators.ts
new file mode 100644
index 000000000..1db062641
--- /dev/null
+++ b/packages/registry/src/algolia/api/store/products/search/validators.ts
@@ -0,0 +1,16 @@
+import { z } from 'zod'
+
+export type StoreSearchProductsType = z.infer<typeof StoreSearchProductsSchema>
+
+export const StoreSearchProductsSchema = z.object({
+ query: z.string().default(''),
+ page: z.coerce.number().int().min(0).default(0),
+ hitsPerPage: z.coerce.number().int().min(1).max(100).default(12),
+ filters: z.string().optional(),
+ facets: z.array(z.string()).optional(),
+ maxValuesPerFacet: z.coerce.number().int().min(1).max(1000).optional(),
+ currency_code: z.string().length(3).optional(),
+ region_id: z.string().optional(),
+ customer_id: z.string().optional(),
+ customer_group_id: z.array(z.string()).optional()
+})
diff --git a/packages/registry/src/algolia/links/seller-fulfillment-set.ts b/packages/registry/src/algolia/links/seller-fulfillment-set.ts
new file mode 100644
index 000000000..cbad64f88
--- /dev/null
+++ b/packages/registry/src/algolia/links/seller-fulfillment-set.ts
@@ -0,0 +1,8 @@
+import { defineLink } from '@medusajs/framework/utils'
+import FulfillmentModule from '@medusajs/medusa/fulfillment'
+import SellerModule from '@mercurjs/core-plugin/modules/seller'
+
+export default defineLink(SellerModule.linkable.seller, {
+ linkable: FulfillmentModule.linkable.fulfillmentSet,
+ isList: true,
+})
diff --git a/packages/registry/src/algolia/links/seller-service-zone.ts b/packages/registry/src/algolia/links/seller-service-zone.ts
new file mode 100644
index 000000000..651f4f104
--- /dev/null
+++ b/packages/registry/src/algolia/links/seller-service-zone.ts
@@ -0,0 +1,8 @@
+import { defineLink } from '@medusajs/framework/utils'
+import FulfillmentModule from '@medusajs/medusa/fulfillment'
+import SellerModule from '@mercurjs/core-plugin/modules/seller'
+
+export default defineLink(SellerModule.linkable.seller, {
+ linkable: FulfillmentModule.linkable.serviceZone,
+ isList: true,
+})
diff --git a/packages/registry/src/algolia/modules/algolia/index.ts b/packages/registry/src/algolia/modules/algolia/index.ts
new file mode 100644
index 000000000..dd67046b7
--- /dev/null
+++ b/packages/registry/src/algolia/modules/algolia/index.ts
@@ -0,0 +1,11 @@
+import { Module } from '@medusajs/framework/utils'
+
+import AlgoliaModuleService from './service'
+
+export const ALGOLIA_MODULE = 'algolia'
+export { AlgoliaModuleService }
+export { defaultProductSettings } from './service'
+
+export default Module(ALGOLIA_MODULE, {
+ service: AlgoliaModuleService,
+})
diff --git a/packages/registry/src/algolia/modules/algolia/service.ts b/packages/registry/src/algolia/modules/algolia/service.ts
new file mode 100644
index 000000000..0b9cf8d2e
--- /dev/null
+++ b/packages/registry/src/algolia/modules/algolia/service.ts
@@ -0,0 +1,150 @@
+import {
+ Action,
+ Algoliasearch,
+ BatchRequest,
+ IndexSettings,
+ SearchParams,
+ SearchResponse,
+ algoliasearch
+} from 'algoliasearch'
+
+import { AlgoliaEntity, IndexType } from './types'
+
+type ModuleOptions = {
+ appId: string
+ apiKey: string
+}
+
+export const defaultProductSettings: IndexSettings = {
+ searchableAttributes: [
+ 'title',
+ 'subtitle',
+ 'tags.value',
+ 'type.value',
+ 'categories.name',
+ 'collection.title',
+ 'variants.title'
+ ]
+}
+
+class AlgoliaModuleService {
+ private options_: ModuleOptions
+ private algolia_: Algoliasearch
+
+ constructor(_: unknown, options: ModuleOptions) {
+ this.options_ = options
+ this.algolia_ = algoliasearch(this.options_.appId, this.options_.apiKey)
+ }
+
+ getAppId() {
+ return this.options_.appId
+ }
+
+ checkIndex(index: IndexType) {
+ return this.algolia_.indexExists({
+ indexName: index
+ })
+ }
+
+ updateSettings(index: IndexType, settings: IndexSettings) {
+ return this.algolia_.setSettings({
+ indexName: index,
+ indexSettings: settings
+ })
+ }
+
+ batch(type: IndexType, toAdd: AlgoliaEntity[], toDelete: string[]) {
+ const addRequests: BatchRequest[] = toAdd.map((entity) => {
+ return {
+ action: 'addObject' as Action,
+ objectID: entity.id,
+ body: entity
+ }
+ })
+
+ const deleteRequests: BatchRequest[] = toDelete.map((id) => {
+ return {
+ action: 'deleteObject' as Action,
+ objectID: id,
+ body: {}
+ }
+ })
+
+ const requests = [...addRequests, ...deleteRequests]
+
+ return this.algolia_.batch({
+ indexName: type,
+ batchWriteParams: {
+ requests
+ }
+ })
+ }
+
+ batchUpsert(type: IndexType, entities: AlgoliaEntity[]) {
+ return this.algolia_.batch({
+ indexName: type,
+ batchWriteParams: {
+ requests: entities.map((entity) => {
+ return {
+ action: 'addObject',
+ objectID: entity.id,
+ body: entity
+ }
+ })
+ }
+ })
+ }
+
+ batchDelete(type: IndexType, ids: string[]) {
+ return this.algolia_.batch({
+ indexName: type,
+ batchWriteParams: {
+ requests: ids.map((id) => {
+ return {
+ action: 'deleteObject',
+ objectID: id,
+ body: {}
+ }
+ })
+ }
+ })
+ }
+
+ upsert(type: IndexType, entity: AlgoliaEntity) {
+ return this.algolia_.addOrUpdateObject({
+ indexName: type,
+ objectID: entity.id,
+ body: entity
+ })
+ }
+
+ delete(type: IndexType, id: string) {
+ return this.algolia_.deleteObject({
+ indexName: type,
+ objectID: id
+ })
+ }
+
+ partialUpdate(
+ type: IndexType,
+ entity: Partial<AlgoliaEntity> & { id: string }
+ ) {
+ return this.algolia_.partialUpdateObject({
+ indexName: type,
+ objectID: entity.id,
+ attributesToUpdate: { ...entity }
+ })
+ }
+
+ search<T = Record<string, unknown>>(
+ indexName: IndexType,
+ params: SearchParams
+ ): Promise<SearchResponse<T>> {
+ return this.algolia_.searchSingleIndex<T>({
+ indexName,
+ searchParams: params
+ })
+ }
+}
+
+export default AlgoliaModuleService
diff --git a/packages/registry/src/algolia/modules/algolia/types.ts b/packages/registry/src/algolia/modules/algolia/types.ts
new file mode 100644
index 000000000..814f91098
--- /dev/null
+++ b/packages/registry/src/algolia/modules/algolia/types.ts
@@ -0,0 +1,153 @@
+import { z } from 'zod'
+
+export enum IndexType {
+ PRODUCT = 'products'
+}
+
+export enum AlgoliaEvents {
+ PRODUCTS_CHANGED = 'algolia.products.changed',
+ PRODUCTS_DELETED = 'algolia.products.deleted'
+}
+
+export enum IntermediateEvents {
+ FULFILLMENT_SET_CHANGED = 'algolia.intermediate.fulfillment_set.changed',
+ SERVICE_ZONE_CHANGED = 'algolia.intermediate.service_zone.changed',
+ SHIPPING_OPTION_CHANGED = 'algolia.intermediate.shipping_option.changed',
+ STOCK_LOCATION_CHANGED = 'algolia.intermediate.stock_location.changed',
+ INVENTORY_ITEM_CHANGED = 'algolia.intermediate.inventory_item.changed'
+}
+
+export const AlgoliaVariantValidator = z.object({
+ id: z.string(),
+ title: z.string().nullish(),
+ sku: z.string().nullish(),
+ barcode: z.string().nullish(),
+ ean: z.string().nullish(),
+ ups: z.string().nullish(),
+ allow_backorder: z.boolean(),
+ manage_inventory: z.boolean(),
+ hs_code: z.string().nullish(),
+ origin_country: z.string().nullish(),
+ mid_code: z.string().nullish(),
+ material: z.string().nullish(),
+ weight: z.number().nullish(),
+ length: z.number().nullish(),
+ height: z.number().nullish(),
+ width: z.number().nullish(),
+ variant_rank: z.number().nullish(),
+ options: z.array(
+ z.object({
+ id: z.string(),
+ value: z.string(),
+ option: z.object({
+ id: z.string(),
+ title: z.string(),
+ }),
+ })
+ ),
+ prices: z.array(
+ z.object({
+ id: z.string(),
+ title: z.string().nullish(),
+ currency_code: z.string(),
+ min_quantity: z.number().nullish(),
+ max_quantity: z.number().nullish(),
+ rules_count: z.number(),
+ amount: z.number(),
+ })
+ ),
+})
+
+export type AlgoliaProduct = z.infer<typeof AlgoliaProductValidator>
+export const AlgoliaProductValidator = z.object({
+ id: z.string(),
+ title: z.string(),
+ handle: z.string(),
+ subtitle: z.string().nullable(),
+ description: z.string().nullable(),
+ thumbnail: z.string().nullable(),
+ average_rating: z.coerce.number().nullable().default(null),
+ supported_countries: z.array(z.string()).nullable().default([]),
+ options: z.array(z.record(z.string())).nullable().default(null),
+ images: z
+ .array(
+ z.object({
+ id: z.string(),
+ url: z.string(),
+ rank: z.number(),
+ })
+ )
+ .nullable()
+ .optional(),
+ collection: z
+ .object({
+ title: z.string(),
+ })
+ .nullable()
+ .optional(),
+ type: z
+ .object({
+ value: z.string(),
+ })
+ .nullable()
+ .optional(),
+ tags: z
+ .array(
+ z.object({
+ value: z.string(),
+ })
+ )
+ .optional(),
+ categories: z
+ .array(
+ z.object({
+ name: z.string(),
+ id: z.string(),
+ })
+ )
+ .optional(),
+ variants: z.any().nullable().default(null),
+ sku: z.string().nullable().optional(),
+ ean: z.string().nullable().optional(),
+ upc: z.string().nullable().optional(),
+ barcode: z.string().nullable().optional(),
+ hs_code: z.string().nullable().optional(),
+ mid_code: z.string().nullable().optional(),
+ weight: z.coerce.number().nullable().optional(),
+ length: z.coerce.number().nullable().optional(),
+ height: z.coerce.number().nullable().optional(),
+ width: z.coerce.number().nullable().optional(),
+ origin_country: z.string().nullable().optional(),
+ material: z.string().nullable().optional(),
+ seller: z
+ .object({
+ id: z.string(),
+ handle: z.string().nullish(),
+ status: z.string().nullish(),
+ })
+ .nullable(),
+})
+
+export type AlgoliaEntity = AlgoliaProduct
+
+export type AlgoliaSearchResult<T> = {
+ hits: T[]
+ nbHits: number
+ page: number
+ nbPages: number
+ hitsPerPage: number
+ facets?: Record<string, Record<string, number>>
+ facets_stats?: Record<
+ string,
+ { min: number; max: number; avg: number; sum: number }
+ >
+ processingTimeMS: number
+ query: string
+}
+
+export interface IAlgoliaModuleService {
+ search<T = Record<string, unknown>>(
+ indexName: IndexType,
+ params: Record<string, unknown>
+ ): Promise<AlgoliaSearchResult<T>>
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-fulfillment-set-changed.ts b/packages/registry/src/algolia/subscribers/algolia-fulfillment-set-changed.ts
new file mode 100644
index 000000000..eb66a1615
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-fulfillment-set-changed.ts
@@ -0,0 +1,50 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'
+import sellerFulfillmentSet from '../links/seller-fulfillment-set'
+import productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'
+
+export default async function fulfillmentSetChangedHandler({
+ event,
+ container
+}: SubscriberArgs<{ id: string }>) {
+ const fulfillment_set_id = event.data.id
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+ const eventBus = container.resolve(Modules.EVENT_BUS)
+
+ const {
+ data: [seller]
+ } = await query.graph({
+ entity: sellerFulfillmentSet.entryPoint,
+ fields: ['seller_id'],
+ filters: {
+ fulfillment_set_id
+ },
+ withDeleted: true
+ })
+
+ if (!seller) {
+ return
+ }
+
+ const { data: products } = await query.graph({
+ entity: productSellerLink.entryPoint,
+ fields: ['product_id'],
+ filters: {
+ seller_id: seller.seller_id
+ }
+ })
+
+ await eventBus.emit({
+ name: AlgoliaEvents.PRODUCTS_CHANGED,
+ data: { ids: products.map((p) => p.product_id) }
+ })
+}
+
+export const config: SubscriberConfig = {
+ event: IntermediateEvents.FULFILLMENT_SET_CHANGED,
+ context: {
+ subscriberId: 'fulfillment-set-changed-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-inventory-item-changed.ts b/packages/registry/src/algolia/subscribers/algolia-inventory-item-changed.ts
new file mode 100644
index 000000000..10c088eb0
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-inventory-item-changed.ts
@@ -0,0 +1,36 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'
+
+export default async function inventoryItemChangedHandler({
+ event,
+ container
+}: SubscriberArgs<{ id: string | string[] }>) {
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+ const eventBus = container.resolve(Modules.EVENT_BUS)
+
+ const { data: items } = await query.graph({
+ entity: 'inventory_item',
+ fields: ['variants.product_id'],
+ filters: {
+ id: event.data.id
+ }
+ })
+
+ const products = items
+ .flatMap((items) => items.variants)
+ .map((variant) => variant.product_id)
+
+ await eventBus.emit({
+ name: AlgoliaEvents.PRODUCTS_CHANGED,
+ data: { ids: [...new Set(products)] }
+ })
+}
+
+export const config: SubscriberConfig = {
+ event: IntermediateEvents.INVENTORY_ITEM_CHANGED,
+ context: {
+ subscriberId: 'inventory-item-changed-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-product-events-bridge.ts b/packages/registry/src/algolia/subscribers/algolia-product-events-bridge.ts
new file mode 100644
index 000000000..ba7487df2
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-product-events-bridge.ts
@@ -0,0 +1,41 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { IEventBusModuleService } from '@medusajs/framework/types'
+import {
+ ContainerRegistrationKeys,
+ Modules,
+} from '@medusajs/framework/utils'
+
+import { AlgoliaEvents } from '../modules/algolia/types'
+
+export default async function algoliaProductEventsBridgeHandler({
+ event,
+ container
+}: SubscriberArgs<{ id: string }>) {
+ const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
+ const eventBus = container.resolve<IEventBusModuleService>(Modules.EVENT_BUS)
+
+ logger.info(`Algolia bridge: received event ${event.name} with data ${JSON.stringify(event.data)}`)
+
+ const isDelete = event.name === 'product.product.deleted' || event.name === 'product.deleted'
+
+ await eventBus.emit({
+ name: isDelete
+ ? AlgoliaEvents.PRODUCTS_DELETED
+ : AlgoliaEvents.PRODUCTS_CHANGED,
+ data: { ids: [event.data.id] },
+ })
+}
+
+export const config: SubscriberConfig = {
+ event: [
+ 'product.created',
+ 'product.updated',
+ 'product.deleted',
+ 'product.product.created',
+ 'product.product.updated',
+ 'product.product.deleted',
+ ],
+ context: {
+ subscriberId: 'algolia-product-events-bridge'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-products-changed.ts b/packages/registry/src/algolia/subscribers/algolia-products-changed.ts
new file mode 100644
index 000000000..3a862b6b7
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-products-changed.ts
@@ -0,0 +1,52 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IndexType } from '../modules/algolia/types'
+import { ALGOLIA_MODULE, AlgoliaModuleService } from '../modules/algolia'
+import {
+ filterProductsByStatus,
+ findAndTransformAlgoliaProducts
+} from './utils/algolia-product'
+
+export default async function algoliaProductsChangedHandler({
+ event,
+ container
+}: SubscriberArgs<{ ids: string[] }>) {
+ const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
+
+ try {
+ const algolia = container.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)
+
+ const { published, other } = await filterProductsByStatus(
+ container,
+ event.data.ids
+ )
+
+ logger.debug(
+ `Algolia sync: Processing ${event.data.ids.length} products - ${published.length} to upsert, ${other.length} to delete`
+ )
+
+ const productsToInsert = published.length
+ ? await findAndTransformAlgoliaProducts(container, published)
+ : []
+
+ await algolia.batch(IndexType.PRODUCT, productsToInsert, other)
+
+ logger.debug(
+ `Algolia sync: Successfully synced ${productsToInsert.length} products`
+ )
+ } catch (error: unknown) {
+ logger.error(
+ `Algolia sync failed for products ${event.data.ids.join(', ')}:`,
+ error as Error
+ )
+ throw error
+ }
+}
+
+export const config: SubscriberConfig = {
+ event: AlgoliaEvents.PRODUCTS_CHANGED,
+ context: {
+ subscriberId: 'algolia-products-changed-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-products-deleted.ts b/packages/registry/src/algolia/subscribers/algolia-products-deleted.ts
new file mode 100644
index 000000000..a11abac55
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-products-deleted.ts
@@ -0,0 +1,39 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IndexType } from '../modules/algolia/types'
+import { ALGOLIA_MODULE, AlgoliaModuleService } from '../modules/algolia'
+
+export default async function algoliaProductsDeletedHandler({
+ event,
+ container
+}: SubscriberArgs<{ ids: string[] }>) {
+ const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
+
+ try {
+ const algolia = container.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)
+
+ logger.debug(
+ `Algolia sync: Deleting ${event.data.ids.length} products from index`
+ )
+
+ await algolia.batchDelete(IndexType.PRODUCT, event.data.ids)
+
+ logger.debug(
+ `Algolia sync: Successfully deleted products ${event.data.ids.join(', ')}`
+ )
+ } catch (error: unknown) {
+ logger.error(
+ `Algolia delete failed for products ${event.data.ids.join(', ')}:`,
+ error as Error
+ )
+ throw error
+ }
+}
+
+export const config: SubscriberConfig = {
+ event: AlgoliaEvents.PRODUCTS_DELETED,
+ context: {
+ subscriberId: 'algolia-products-deleted-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-service-zone-changed.ts b/packages/registry/src/algolia/subscribers/algolia-service-zone-changed.ts
new file mode 100644
index 000000000..a84d1cfe0
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-service-zone-changed.ts
@@ -0,0 +1,50 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'
+import productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'
+import sellerServiceZone from '../links/seller-service-zone'
+
+export default async function serviceZoneChangedHandler({
+ event,
+ container
+}: SubscriberArgs<{ id: string }>) {
+ const service_zone_id = event.data.id
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+ const eventBus = container.resolve(Modules.EVENT_BUS)
+
+ const {
+ data: [seller]
+ } = await query.graph({
+ entity: sellerServiceZone.entryPoint,
+ fields: ['seller_id'],
+ filters: {
+ service_zone_id
+ },
+ withDeleted: true
+ })
+
+ if (!seller) {
+ return
+ }
+
+ const { data: products } = await query.graph({
+ entity: productSellerLink.entryPoint,
+ fields: ['product_id'],
+ filters: {
+ seller_id: seller.seller_id
+ }
+ })
+
+ await eventBus.emit({
+ name: AlgoliaEvents.PRODUCTS_CHANGED,
+ data: { ids: products.map((p) => p.product_id) }
+ })
+}
+
+export const config: SubscriberConfig = {
+ event: IntermediateEvents.SERVICE_ZONE_CHANGED,
+ context: {
+ subscriberId: 'service-zone-changed-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-shipping-option-changed.ts b/packages/registry/src/algolia/subscribers/algolia-shipping-option-changed.ts
new file mode 100644
index 000000000..c5ec4782c
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-shipping-option-changed.ts
@@ -0,0 +1,50 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'
+import productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'
+import shippingOptionSellerLink from '@mercurjs/core-plugin/links/shipping-option-seller-link'
+
+export default async function shippingOptionChangedHandler({
+ event,
+ container
+}: SubscriberArgs<{ id: string }>) {
+ const shipping_option_id = event.data.id
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+ const eventBus = container.resolve(Modules.EVENT_BUS)
+
+ const {
+ data: [seller]
+ } = await query.graph({
+ entity: shippingOptionSellerLink.entryPoint,
+ fields: ['seller_id'],
+ filters: {
+ shipping_option_id
+ },
+ withDeleted: true
+ })
+
+ if (!seller) {
+ return
+ }
+
+ const { data: products } = await query.graph({
+ entity: productSellerLink.entryPoint,
+ fields: ['product_id'],
+ filters: {
+ seller_id: seller.seller_id
+ }
+ })
+
+ await eventBus.emit({
+ name: AlgoliaEvents.PRODUCTS_CHANGED,
+ data: { ids: products.map((p) => p.product_id) }
+ })
+}
+
+export const config: SubscriberConfig = {
+ event: IntermediateEvents.SHIPPING_OPTION_CHANGED,
+ context: {
+ subscriberId: 'shipping-option-changed-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/algolia-stock-location-changed.ts b/packages/registry/src/algolia/subscribers/algolia-stock-location-changed.ts
new file mode 100644
index 000000000..28acab67f
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/algolia-stock-location-changed.ts
@@ -0,0 +1,50 @@
+import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
+import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
+
+import { AlgoliaEvents, IntermediateEvents } from '../modules/algolia/types'
+import productSellerLink from '@mercurjs/core-plugin/links/product-seller-link'
+import stockLocationSellerLink from '@mercurjs/core-plugin/links/stock-location-seller-link'
+
+export default async function stockLocationChangedHandler({
+ event,
+ container
+}: SubscriberArgs<{ id: string }>) {
+ const stock_location_id = event.data.id
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+ const eventBus = container.resolve(Modules.EVENT_BUS)
+
+ const {
+ data: [seller]
+ } = await query.graph({
+ entity: stockLocationSellerLink.entryPoint,
+ fields: ['seller_id'],
+ filters: {
+ stock_location_id
+ },
+ withDeleted: true
+ })
+
+ if (!seller) {
+ return
+ }
+
+ const { data: products } = await query.graph({
+ entity: productSellerLink.entryPoint,
+ fields: ['product_id'],
+ filters: {
+ seller_id: seller.seller_id
+ }
+ })
+
+ await eventBus.emit({
+ name: AlgoliaEvents.PRODUCTS_CHANGED,
+ data: { ids: products.map((p) => p.product_id) }
+ })
+}
+
+export const config: SubscriberConfig = {
+ event: IntermediateEvents.STOCK_LOCATION_CHANGED,
+ context: {
+ subscriberId: 'stock-location-changed-handler'
+ }
+}
diff --git a/packages/registry/src/algolia/subscribers/utils/algolia-product.ts b/packages/registry/src/algolia/subscribers/utils/algolia-product.ts
new file mode 100644
index 000000000..ceca26a7d
--- /dev/null
+++ b/packages/registry/src/algolia/subscribers/utils/algolia-product.ts
@@ -0,0 +1,184 @@
+import { z } from 'zod'
+
+import { MedusaContainer } from '@medusajs/framework'
+import {
+ ContainerRegistrationKeys,
+ arrayDifference,
+} from '@medusajs/framework/utils'
+
+import {
+ AlgoliaProductValidator,
+ AlgoliaVariantValidator
+} from '../../modules/algolia/types'
+
+async function selectProductVariantsSupportedCountries(
+ container: MedusaContainer,
+ product_id: string
+) {
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+ const { data: variants } = await query.graph({
+ entity: 'product_variant',
+ fields: ['inventory_items.inventory.location_levels.location_id'],
+ filters: {
+ product_id
+ }
+ })
+
+ let location_ids: string[] = []
+
+ for (const variant of variants) {
+ const inventory_items =
+ (variant as any).inventory_items?.map((item: any) => item.inventory) || []
+ const locations = inventory_items
+ .flatMap((inventory_item: any) => inventory_item.location_levels || [])
+ .map((level: any) => level.location_id)
+
+ location_ids = location_ids.concat(locations)
+ }
+
+ const { data: stock_locations } = await query.graph({
+ entity: 'stock_location',
+ fields: ['fulfillment_sets.service_zones.geo_zones.country_code'],
+ filters: {
+ id: location_ids
+ }
+ })
+
+ let country_codes: string[] = []
+
+ for (const location of stock_locations) {
+ const fulfillmentSets =
+ (location as any).fulfillment_sets?.flatMap((set: any) => set.service_zones || []) || []
+ const codes = fulfillmentSets
+ .flatMap((sz: any) => sz.geo_zones || [])
+ .map((gz: any) => gz.country_code)
+
+ country_codes = country_codes.concat(codes)
+ }
+
+ return [...new Set(country_codes)]
+}
+
+async function selectProductSeller(
+ container: MedusaContainer,
+ product_id: string
+) {
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+
+ const {
+ data: [product]
+ } = await query.graph({
+ entity: 'product',
+ fields: ['seller.id', 'seller.handle', 'seller.status'],
+ filters: {
+ id: product_id
+ }
+ })
+
+
+
+ const p = product as any
+ return p?.seller?.id
+ ? {
+ id: p.seller.id as string,
+ handle: p.seller.handle as string | null,
+ status: p.seller.status as string | null
+ }
+ : null
+}
+
+export async function filterProductsByStatus(
+ container: MedusaContainer,
+ ids: string[] = []
+) {
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+
+ const { data: products } = await query.graph({
+ entity: 'product',
+ fields: ['id', 'status'],
+ filters: {
+ id: ids
+ }
+ })
+
+ const published = products.filter((p) => p.status === 'published')
+ const notPublished = arrayDifference(products, published)
+
+ const existingIds = new Set(products.map((p) => p.id))
+
+ const deletedIds = ids.filter((id) => !existingIds.has(id))
+
+ return {
+ published: published.map((p) => p.id),
+ other: [...notPublished.map((p) => p.id), ...deletedIds]
+ }
+}
+
+export async function findAndTransformAlgoliaProducts(
+ container: MedusaContainer,
+ ids: string[] = []
+) {
+ const query = container.resolve(ContainerRegistrationKeys.QUERY)
+
+ const { data: products } = await query.graph({
+ entity: 'product',
+ fields: [
+ '*',
+ 'categories.name',
+ 'categories.id',
+ 'collection.title',
+ 'tags.value',
+ 'type.value',
+ 'variants.*',
+ 'variants.options.*',
+ 'variants.options.prices.*',
+ 'variants.prices.*',
+ 'options.*',
+ 'options.values.*',
+ 'images.*'
+ ],
+ filters: ids.length
+ ? {
+ id: ids,
+ status: 'published'
+ }
+ : { status: 'published' }
+ })
+
+ for (const product of products as any[]) {
+ product.average_rating = 0
+ product.supported_countries = await selectProductVariantsSupportedCountries(
+ container,
+ product.id
+ )
+ product.seller = await selectProductSeller(container, product.id)
+
+ product.options = (product.options ?? [])
+ .filter((option: any) => option?.title && option?.values)
+ .map((option: any) => {
+ return option.values.map((value: any) => {
+ const entry: Record<string, string> = {}
+ entry[option.title.toLowerCase()] = value.value
+ return entry
+ })
+ })
+ .flat()
+
+ product.variants = z
+ .array(AlgoliaVariantValidator)
+ .parse(product.variants ?? [])
+ product.variants = (product.variants ?? [])
+ .map((variant: any) => {
+ return (variant.options ?? []).reduce((entry: any, item: any) => {
+ if (item?.option?.title) {
+ entry[item.option.title.toLowerCase()] = item.value
+ }
+ return entry
+ }, variant)
+ })
+ .flat()
+
+ }
+
+ return z.array(AlgoliaProductValidator).parse(products)
+}
diff --git a/packages/registry/src/algolia/workflows/algolia/steps/sync-algolia-products.ts b/packages/registry/src/algolia/workflows/algolia/steps/sync-algolia-products.ts
new file mode 100644
index 000000000..a437e9d77
--- /dev/null
+++ b/packages/registry/src/algolia/workflows/algolia/steps/sync-algolia-products.ts
@@ -0,0 +1,66 @@
+import { IEventBusModuleService, RemoteQueryFunction } from '@medusajs/framework/types'
+import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
+import { StepResponse, createStep } from '@medusajs/framework/workflows-sdk'
+
+import { ALGOLIA_MODULE, AlgoliaModuleService } from '../../../modules/algolia'
+import { AlgoliaEvents, IndexType } from '../../../modules/algolia/types'
+
+const CHUNK_SIZE = 100
+
+export const syncAlgoliaProductsStep = createStep(
+ 'sync-algolia-products',
+ async (_: void, { container }) => {
+ const query = container.resolve<RemoteQueryFunction>(ContainerRegistrationKeys.QUERY)
+ const algolia = container.resolve<AlgoliaModuleService>(ALGOLIA_MODULE)
+
+ const { data: productsToDelete } = await query.graph({
+ entity: 'product',
+ filters: {
+ $or: [
+ {
+ deleted_at: {
+ $ne: null,
+ },
+ },
+ {
+ status: {
+ $ne: 'published',
+ },
+ },
+ ],
+ },
+ fields: ['id'],
+ })
+
+ await algolia.batchDelete(
+ IndexType.PRODUCT,
+ productsToDelete.map((p) => p.id)
+ )
+
+ const { data: publishedProducts } = await query.graph({
+ entity: 'product',
+ filters: {
+ status: 'published',
+ },
+ fields: ['id'],
+ })
+
+ const productsToInsert = publishedProducts.map((p) => p.id)
+ const productChunks: string[][] = []
+ for (let i = 0; i < productsToInsert.length; i += CHUNK_SIZE) {
+ productChunks.push(productsToInsert.slice(i, i + CHUNK_SIZE))
+ }
+
+ const eventBus = container.resolve<IEventBusModuleService>(Modules.EVENT_BUS)
+ for (const chunk of productChunks) {
+ await eventBus.emit({
+ name: AlgoliaEvents.PRODUCTS_CHANGED,
+ data: {
+ ids: chunk,
+ },
+ })
+ }
+
+ return new StepResponse()
+ }
+)
diff --git a/packages/registry/src/algolia/workflows/algolia/workflows/sync-algolia.ts b/packages/registry/src/algolia/workflows/algolia/workflows/sync-algolia.ts
new file mode 100644
index 000000000..8c703beed
--- /dev/null
+++ b/packages/registry/src/algolia/workflows/algolia/workflows/sync-algolia.ts
@@ -0,0 +1,10 @@
+import { WorkflowResponse, createWorkflow } from '@medusajs/workflows-sdk'
+
+import { syncAlgoliaProductsStep } from '../steps/sync-algolia-products'
+
+export const syncAlgoliaWorkflow = createWorkflow(
+ 'sync-algolia-workflow',
+ function () {
+ return new WorkflowResponse(syncAlgoliaProductsStep())
+ }
+)