medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.83k stars 2.58k forks source link

Algolia plugin - `transformer` function used only for the products index #5556

Open lukawski opened 1 year ago

lukawski commented 1 year ago

Bug report

Describe the bug

When using the Algolia search plugin, only the products index will use the transformer function. I believe it should not be hardcoded and should work for any index.

System information

Medusa version (including plugins): 1.16.1, Algolia plugin 0.2.20 Node.js version: 20.8.1 Database: PostgreSQL Operating system: macOS 14.1

Steps to reproduce the behavior

  1. Define 2 indexes, 1 for product, 1 for another entity
    {
        resolve: `medusa-plugin-algolia`,
        options: {
            applicationId: process.env.ALGOLIA_APP_ID,
            adminApiKey: process.env.ALGOLIA_ADMIN_API_KEY,
            settings: {
                products: {
                    indexSettings: {
                        searchableAttributes: ['title', 'description', 'material', 'tags'],
                        attributesToRetrieve: ['id', 'title', 'handle', 'thumbnail', 'subtitle', 'tags'],
                    },
                    transformer: (item) => ({
                        objectID: item.id,
                        title: item.title,
                        handle: item.handle,
                        thumbnail: item.thumbnail,
                        subtitle: item.subtitle,
                        tags: item.tags,
                        description: item.description,
                        material: item.material,
                    }),
                },
                categories: {
                    indexSettings: {
                        searchableAttributes: ['name', 'description'],
                        attributesToRetrieve: ['id', 'name', 'handle', 'thumbnail'],
                    },
                    transformer: (item) => {
                        return {
                            objectID: item.id,
                            name: item.name,
                            handle: item.handle,
                            thumbnail: item.thumbnail,
                            description: item.description,
                        };
                    },
                },
            },
        },
    }
  2. Create entity subscriber e.g. ProductCategorySubscriber and subscribe to entity relevant event e.g. ProductCategoryService.Events.CREATED
    this.eventBusService_
            .subscribe(ProductCategoryService.Events.CREATED, this.handleCategoryCreation)
    handleCategoryCreation = async (data) => {
        const category = await this.productCategoryService_.retrieve(data.id);
        await this.searchService_.addDocuments(
            CATEGORY_INDEX_NAME,
            [this.transformCategory(category)],
            'categories',
        );
    };
  3. Create a relevant entity

    Expected behavior

Transformer defined in the index settings is used and data is correctly transformed and indexed.

Screenshots

image
lukawski commented 1 year ago

I was able to find the cause for this issue and implement a fix. PR: https://github.com/medusajs/medusa/pull/5558.