Skip to content

Commit 7957e33

Browse files
committed
feat(meilisearch-connector): update getContentTypesWithSameIndex
to handle multiple indexes
1 parent 60d81bc commit 7957e33

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

server/__tests__/meilisearch.test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ describe('Tests content types', () => {
358358
})
359359

360360
expect(
361-
customStrapi.plugin().service().actionInBatches
361+
customStrapi.plugin().service().actionInBatches,
362362
).toHaveBeenCalledWith({
363363
contentType: 'restaurant',
364364
callback: expect.anything(),
@@ -373,7 +373,7 @@ describe('Tests content types', () => {
373373
})
374374
expect(customStrapi.log.info).toHaveBeenCalledTimes(1)
375375
expect(customStrapi.log.info).toHaveBeenCalledWith(
376-
'A task to update the settings to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).'
376+
'A task to update the settings to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).',
377377
)
378378
})
379379

@@ -401,7 +401,7 @@ describe('Tests content types', () => {
401401
})
402402

403403
expect(
404-
customStrapi.plugin().service().actionInBatches
404+
customStrapi.plugin().service().actionInBatches,
405405
).toHaveBeenCalledWith({
406406
contentType: 'restaurant',
407407
callback: expect.anything(),
@@ -416,10 +416,10 @@ describe('Tests content types', () => {
416416
})
417417
expect(customStrapi.log.info).toHaveBeenCalledTimes(2)
418418
expect(customStrapi.log.info).toHaveBeenCalledWith(
419-
'A task to update the settings to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).'
419+
'A task to update the settings to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).',
420420
)
421421
expect(customStrapi.log.info).toHaveBeenCalledWith(
422-
'A task to update the settings to the Meilisearch index "anotherIndex" has been enqueued (Task uid: undefined).'
422+
'A task to update the settings to the Meilisearch index "anotherIndex" has been enqueued (Task uid: undefined).',
423423
)
424424
})
425425

@@ -587,4 +587,25 @@ describe('Tests content types', () => {
587587

588588
expect(result).toEqual(['api::restaurant.restaurant'])
589589
})
590+
591+
test('Test to get content types with same index, edge case with multiple indexes', async () => {
592+
const customStrapi = createStrapiMock({
593+
restaurantConfig: {
594+
indexName: ['customIndex'],
595+
},
596+
aboutConfig: {
597+
indexName: ['customIndex', 'anotherIndex'],
598+
},
599+
})
600+
601+
const meilisearchService = createMeilisearchService({
602+
strapi: customStrapi,
603+
})
604+
605+
const result = await meilisearchService.getContentTypesWithSameIndex({
606+
contentType: 'restaurant',
607+
})
608+
609+
expect(result).toEqual(['api::restaurant.restaurant', 'api::about.about'])
610+
})
590611
})

server/services/meilisearch/connector.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,30 @@ module.exports = ({ strapi, adapter, config }) => {
364364
* @returns {Promise<string[]>} - ContentTypes names.
365365
*/
366366
getContentTypesWithSameIndex: async function ({ contentType }) {
367-
const indexUid = config.getIndexNameOfContentType({ contentType })
367+
const indexUids = config.getIndexNamesOfContentType({ contentType })
368+
369+
// Initialize an empty array to hold contentTypes with the same index names
370+
let contentTypesWithSameIndex = []
371+
372+
// Iterate over each indexUid to fetch and accumulate contentTypes that have the same indexName
373+
for (const indexUid of indexUids) {
374+
const contentTypesForCurrentIndex = await config
375+
.listContentTypesWithCustomIndexName({ indexName: indexUid })
376+
.map(contentTypeName => `api::${contentTypeName}.${contentTypeName}`)
377+
378+
contentTypesWithSameIndex = [
379+
...contentTypesWithSameIndex,
380+
...contentTypesForCurrentIndex,
381+
]
382+
}
368383

369-
// Fetch contentTypes that has the same indexName as the provided contentType
370-
const contentTypesWithSameIndex = await config
371-
.listContentTypesWithCustomIndexName({ indexName: indexUid })
372-
.map(contentTypeName => `api::${contentTypeName}.${contentTypeName}`)
384+
// Remove duplicates
385+
contentTypesWithSameIndex = [...new Set(contentTypesWithSameIndex)]
373386

374-
// get all contentTypes (not indexes) indexed in Meilisearch.
387+
// Get all contentTypes (not indexes) indexed in Meilisearch.
375388
const indexedContentTypes = await store.getIndexedContentTypes()
376389

377-
// Take intersection of both array
390+
// Take intersection of both arrays
378391
const indexedContentTypesWithSameIndex = indexedContentTypes.filter(
379392
contentType => contentTypesWithSameIndex.includes(contentType),
380393
)

0 commit comments

Comments
 (0)