From c266e2f09dda043fbc6161bb870a1e177f9a02f1 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 2 Feb 2024 16:48:33 +0800 Subject: [PATCH] refactor: algolia search operations Signed-off-by: Innei --- .../core/src/modules/search/search.service.ts | 94 ++++++++++--------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/apps/core/src/modules/search/search.service.ts b/apps/core/src/modules/search/search.service.ts index 080ae5d4..7c5d3585 100644 --- a/apps/core/src/modules/search/search.service.ts +++ b/apps/core/src/modules/search/search.service.ts @@ -4,6 +4,7 @@ import { omit } from 'lodash' import type { SearchResponse } from '@algolia/client-search' import type { SearchDto } from '~/modules/search/search.dto' import type { Pagination } from '~/shared/interface/paginator.interface' +import type { SearchIndex } from 'algoliasearch' import { BadRequestException, @@ -285,74 +286,75 @@ export class SearchService { @OnEvent(BusinessEvents.POST_CREATE) async onPostCreate(post: PostModel) { - const configs = await this.configs.waitForConfigReady() - if (!configs.algoliaSearchOptions.enable || isDev) { - return - } - const index = await this.getAlgoliaSearchIndex() - const data = await this.postService.model.findById(post.id).lean() if (!data) return - this.logger.log( - 'detect post create, save to algolia, data: ', - inspect(data), - ) - await index.saveObject( - { - ...omit(data, '_id'), - objectID: data.id, - id: data.id, - type: 'post', - }, - { - autoGenerateObjectIDIfNotExist: false, - }, - ) + this.executeAlgoliaSearchOperationIfEnabled(async (index) => { + this.logger.log( + 'detect post create, save to algolia, data: ', + inspect(data), + ) + await index.saveObject( + { + ...omit(data, '_id'), + objectID: data.id, + id: data.id, + + type: 'post', + }, + { + autoGenerateObjectIDIfNotExist: false, + }, + ) + }) } @OnEvent(BusinessEvents.NOTE_CREATE) async onNoteCreate(note: NoteModel) { - const configs = await this.configs.waitForConfigReady() - if (!configs.algoliaSearchOptions.enable || isDev) { - return - } - const index = await this.getAlgoliaSearchIndex() - const data = await this.noteService.model.findById(note.id).lean() if (!data) return - this.logger.log( - 'detect note create, save to algolia, data: ', - inspect(data), - ) - await index.saveObject( - { - ...omit(data, '_id'), - objectID: data.id, + this.executeAlgoliaSearchOperationIfEnabled(async (index) => { + this.logger.log( + 'detect note create, save to algolia, data: ', + inspect(data), + ) + await index.saveObject( + { + ...omit(data, '_id'), + objectID: data.id, - id: data.id, + id: data.id, - type: 'note', - }, - { - autoGenerateObjectIDIfNotExist: false, - }, - ) + type: 'note', + }, + { + autoGenerateObjectIDIfNotExist: false, + }, + ) + }) } @OnEvent(BusinessEvents.POST_DELETE) @OnEvent(BusinessEvents.NOTE_DELETE) - async onPostDelete(id: string) { + async onPostDelete({ data: id }: { data: string }) { + await this.executeAlgoliaSearchOperationIfEnabled(async (index) => { + this.logger.log('detect data delete, save to algolia, data: ', id) + + await index.deleteObject(id) + }) + } + + private async executeAlgoliaSearchOperationIfEnabled( + caller: (index: SearchIndex) => Promise, + ) { const configs = await this.configs.waitForConfigReady() if (!configs.algoliaSearchOptions.enable || isDev) { return } - - this.logger.log('detect data delete, save to algolia, data: ', id) const index = await this.getAlgoliaSearchIndex() - await index.deleteObject(id) + return caller(index) } }