From 5aa3283dc097691d802c2a11b92c54ecff02ba56 Mon Sep 17 00:00:00 2001 From: Jarrod Flesch <30633324+JarrodMFlesch@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:47:39 -0400 Subject: [PATCH] fix: search plugin localized fields (#7292) --- .../src/Search/hooks/syncWithSearch.ts | 11 +++++- packages/plugin-search/src/index.ts | 4 +- test/plugin-search/collections/Posts.ts | 5 +++ test/plugin-search/config.ts | 17 ++++++-- test/plugin-search/int.spec.ts | 39 +++++++++++++++++++ test/plugin-search/payload-types.ts | 10 ++++- 6 files changed, 78 insertions(+), 8 deletions(-) diff --git a/packages/plugin-search/src/Search/hooks/syncWithSearch.ts b/packages/plugin-search/src/Search/hooks/syncWithSearch.ts index 7e32e67228..e439a68181 100644 --- a/packages/plugin-search/src/Search/hooks/syncWithSearch.ts +++ b/packages/plugin-search/src/Search/hooks/syncWithSearch.ts @@ -23,8 +23,17 @@ export const syncWithSearch: SyncWithSearch = async (args) => { } if (typeof beforeSync === 'function') { + let docToSyncWith = doc + if (payload.config?.localization) { + docToSyncWith = await payload.findByID({ + id, + collection, + locale: 'all', + req, + }) + } dataToSave = await beforeSync({ - originalDoc: doc, + originalDoc: docToSyncWith, payload, req, searchDoc: dataToSave, diff --git a/packages/plugin-search/src/index.ts b/packages/plugin-search/src/index.ts index fe8a026bf8..3ab321902f 100644 --- a/packages/plugin-search/src/index.ts +++ b/packages/plugin-search/src/index.ts @@ -13,10 +13,10 @@ export const searchPlugin = if (collections) { const pluginConfig: SearchPluginConfig = { - ...incomingPluginConfig, + // write any config defaults here deleteDrafts: true, syncDrafts: false, - // write any config defaults here + ...incomingPluginConfig, } // add afterChange and afterDelete hooks to every search-enabled collection diff --git a/test/plugin-search/collections/Posts.ts b/test/plugin-search/collections/Posts.ts index 22af461264..e653688b64 100644 --- a/test/plugin-search/collections/Posts.ts +++ b/test/plugin-search/collections/Posts.ts @@ -26,5 +26,10 @@ export const Posts: CollectionConfig = { label: 'Excerpt', type: 'text', }, + { + type: 'text', + name: 'slug', + localized: true, + }, ], } diff --git a/test/plugin-search/config.ts b/test/plugin-search/config.ts index 89f26f1b37..caed478b6f 100644 --- a/test/plugin-search/config.ts +++ b/test/plugin-search/config.ts @@ -31,10 +31,13 @@ export default buildConfigWithDefaults({ }, plugins: [ searchPlugin({ - beforeSync: ({ originalDoc, searchDoc }) => ({ - ...searchDoc, - excerpt: originalDoc?.excerpt || 'This is a fallback excerpt', - }), + beforeSync: ({ originalDoc, searchDoc }) => { + return { + ...searchDoc, + excerpt: originalDoc?.excerpt || 'This is a fallback excerpt', + slug: originalDoc.slug, + } + }, collections: ['pages', 'posts'], defaultPriorities: { pages: 10, @@ -50,6 +53,12 @@ export default buildConfigWithDefaults({ position: 'sidebar', }, }, + { + name: 'slug', + required: false, + type: 'text', + localized: true, + }, ], }, }), diff --git a/test/plugin-search/int.spec.ts b/test/plugin-search/int.spec.ts index fe06d0b7b3..32a29f6647 100644 --- a/test/plugin-search/int.spec.ts +++ b/test/plugin-search/int.spec.ts @@ -184,4 +184,43 @@ describe('@payloadcms/plugin-search', () => { expect(deletedResults).toHaveLength(0) }) + + it('should sync localized data', async () => { + const createdDoc = await payload.create({ + collection: 'posts', + data: { + _status: 'draft', + title: 'test title', + slug: 'es', + }, + locale: 'es', + }) + + await payload.update({ + collection: 'posts', + id: createdDoc.id, + data: { + _status: 'published', + title: 'test title', + slug: 'en', + }, + locale: 'en', + }) + + const syncedSearchData = await payload.find({ + collection: 'search', + locale: 'es', + where: { + and: [ + { + 'doc.value': { + equals: createdDoc.id, + }, + }, + ], + }, + }) + + expect(syncedSearchData.docs[0].slug).toEqual('es') + }) }) diff --git a/test/plugin-search/payload-types.ts b/test/plugin-search/payload-types.ts index ac50a50f2e..9d1b14a69b 100644 --- a/test/plugin-search/payload-types.ts +++ b/test/plugin-search/payload-types.ts @@ -18,6 +18,9 @@ export interface Config { 'payload-preferences': PayloadPreference; 'payload-migrations': PayloadMigration; }; + db: { + defaultIDType: string; + }; globals: {}; locale: 'en' | 'es' | 'de'; user: User & { @@ -29,13 +32,16 @@ export interface UserAuthOperations { email: string; }; login: { - password: string; email: string; + password: string; }; registerFirstUser: { email: string; password: string; }; + unlock: { + email: string; + }; } /** * This interface was referenced by `Config`'s JSON-Schema @@ -74,6 +80,7 @@ export interface Post { id: string; title: string; excerpt?: string | null; + slug?: string | null; updatedAt: string; createdAt: string; _status?: ('draft' | 'published') | null; @@ -96,6 +103,7 @@ export interface Search { value: string | Post; }; excerpt?: string | null; + slug?: string | null; updatedAt: string; createdAt: string; }