From 116a6225816e003317d7ca9edec1e5518e62d2bd Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sun, 28 Jul 2024 13:55:37 +0200 Subject: [PATCH] Simplify conversion from change to meilidoc --- src/convert.ts | 93 +++++++++++++------------------------------------- src/shared | 2 +- src/synchro.ts | 20 +++-------- src/utils.ts | 6 ---- src/webhook.ts | 10 ++---- 5 files changed, 30 insertions(+), 101 deletions(-) delete mode 100644 src/utils.ts diff --git a/src/convert.ts b/src/convert.ts index 8c37752..d31e447 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -1,8 +1,5 @@ import { payload } from "src/services"; -import type { - MeiliDocument, - MeiliDocumentRequest, -} from "src/shared/meilisearch/types"; +import type { MeiliDocument } from "src/shared/meilisearch/types"; import { Collections } from "src/shared/payload/constants"; import type { EndpointAudio, @@ -40,7 +37,6 @@ const convertPageToDocument = ({ }${formatRichTextContentToString(content)}`, updatedAt: Date.parse(data.updatedAt), type: Collections.Pages, - slug: data.slug, endpointCalled, data, }) @@ -61,7 +57,6 @@ const convertCollectibleToDocument = ({ : {}), updatedAt: Date.parse(data.updatedAt), type: Collections.Collectibles, - slug: data.slug, endpointCalled, data, }) @@ -80,7 +75,6 @@ const convertFolderToDocument = ({ ? { description: formatRichTextContentToString(description) } : {}), type: Collections.Folders, - slug: data.slug, endpointCalled, data, })); @@ -99,7 +93,6 @@ const convertAudioToDocument = ({ : {}), updatedAt: Date.parse(data.updatedAt), type: Collections.Audios, - id: data.id, endpointCalled, data, })); @@ -120,7 +113,6 @@ const convertImageToDocument = ({ : {}), updatedAt: Date.parse(data.updatedAt), type: Collections.Images, - id: data.id, endpointCalled, data, }) @@ -134,7 +126,6 @@ const convertImageToDocument = ({ title: data.filename, updatedAt: Date.parse(data.updatedAt), type: Collections.Images, - id: data.id, endpointCalled, data, }, @@ -156,7 +147,6 @@ const convertVideoToDocument = ({ : {}), updatedAt: Date.parse(data.updatedAt), type: Collections.Videos, - id: data.id, endpointCalled, data, })); @@ -175,7 +165,6 @@ const convertRecorderToDocument = ({ ? { description: formatRichTextContentToString(biography) } : {}), type: Collections.Recorders, - id: data.id, endpointCalled, data, })); @@ -187,7 +176,6 @@ const convertRecorderToDocument = ({ languages: [], title: data.username, type: Collections.Recorders, - id: data.id, endpointCalled, data, }, @@ -211,7 +199,6 @@ const convertFileToDocument = ({ : {}), updatedAt: Date.parse(data.updatedAt), type: Collections.Files, - id: data.id, endpointCalled, data, }) @@ -225,7 +212,6 @@ const convertFileToDocument = ({ title: data.filename, updatedAt: Date.parse(data.updatedAt), type: Collections.Files, - id: data.id, endpointCalled, data, }, @@ -252,81 +238,48 @@ const convertChronologyEventToDocument = ({ } : {}), type: Collections.ChronologyEvents, - id: data.id, endpointCalled, data: { date: data.date, event }, }) ) ); -export const getMeiliDocumentsFromRequest = async ( - request: MeiliDocumentRequest +export const convertEndpointChangeToMeiliDocuments = async ( + request: EndpointChange ): Promise => { switch (request.type) { - case Collections.Audios: - return convertAudioToDocument(await payload.getAudioByID(request.id)); + case SDKEndpointNames.getFolder: + return convertFolderToDocument(await payload.getFolder(request.slug)); - case Collections.ChronologyEvents: - return convertChronologyEventToDocument( - await payload.getChronologyEventByID(request.id) - ); + case SDKEndpointNames.getPage: + return convertPageToDocument(await payload.getPage(request.slug)); - case Collections.Collectibles: + case SDKEndpointNames.getCollectible: return convertCollectibleToDocument( await payload.getCollectible(request.slug) ); - case Collections.Files: - return convertFileToDocument(await payload.getFileByID(request.id)); + case SDKEndpointNames.getChronologyEventByID: + return convertChronologyEventToDocument( + await payload.getChronologyEventByID(request.id) + ); - case Collections.Folders: - return convertFolderToDocument(await payload.getFolder(request.slug)); - - case Collections.Images: + case SDKEndpointNames.getImageByID: return convertImageToDocument(await payload.getImageByID(request.id)); - case Collections.Pages: - return convertPageToDocument(await payload.getPage(request.slug)); + case SDKEndpointNames.getAudioByID: + return convertAudioToDocument(await payload.getAudioByID(request.id)); - case Collections.Recorders: + case SDKEndpointNames.getVideoByID: + return convertVideoToDocument(await payload.getVideoByID(request.id)); + + case SDKEndpointNames.getFileByID: + return convertFileToDocument(await payload.getFileByID(request.id)); + + case SDKEndpointNames.getRecorderByID: return convertRecorderToDocument( await payload.getRecorderByID(request.id) ); - case Collections.Videos: - return convertVideoToDocument(await payload.getVideoByID(request.id)); - } -}; - -export const convertChangeToMeiliDocumentRequest = ( - change: EndpointChange -): MeiliDocumentRequest | undefined => { - switch (change.type) { - case SDKEndpointNames.getFolder: - return { type: Collections.Folders, slug: change.slug }; - - case SDKEndpointNames.getPage: - return { type: Collections.Pages, slug: change.slug }; - - case SDKEndpointNames.getCollectible: - return { type: Collections.Pages, slug: change.slug }; - - case SDKEndpointNames.getChronologyEventByID: - return { type: Collections.ChronologyEvents, id: change.id }; - - case SDKEndpointNames.getImageByID: - return { type: Collections.Images, id: change.id }; - - case SDKEndpointNames.getAudioByID: - return { type: Collections.Images, id: change.id }; - - case SDKEndpointNames.getVideoByID: - return { type: Collections.Images, id: change.id }; - - case SDKEndpointNames.getFileByID: - return { type: Collections.Images, id: change.id }; - - case SDKEndpointNames.getRecorderByID: - return { type: Collections.Images, id: change.id }; case SDKEndpointNames.getWebsiteConfig: case SDKEndpointNames.getLanguages: @@ -338,6 +291,6 @@ export const convertChangeToMeiliDocumentRequest = ( case SDKEndpointNames.getCollectibleGalleryImage: case SDKEndpointNames.getChronologyEvents: default: - return undefined; + return []; } }; diff --git a/src/shared b/src/shared index caa79de..3208347 160000 --- a/src/shared +++ b/src/shared @@ -1 +1 @@ -Subproject commit caa79dee9eca5b9b6959e6f5a721245202423612 +Subproject commit 32083473e9799ffddada87b979b0e4b546722be7 diff --git a/src/synchro.ts b/src/synchro.ts index be9885c..0c249d1 100644 --- a/src/synchro.ts +++ b/src/synchro.ts @@ -1,14 +1,6 @@ -import { - convertChangeToMeiliDocumentRequest, - getMeiliDocumentsFromRequest, -} from "src/convert"; +import { convertEndpointChangeToMeiliDocuments } from "src/convert"; import { meili, payload } from "src/services"; import { MeiliIndexes } from "src/shared/meilisearch/constants"; -import type { - MeiliDocument, - MeiliDocumentRequest, -} from "src/shared/meilisearch/types"; -import { isDefined } from "src/utils"; export const synchronizeMeiliDocs = async () => { const version = await meili.getVersion(); @@ -39,13 +31,9 @@ export const synchronizeMeiliDocs = async () => { const allChanges = (await payload.getAll()).data; - const documentRequests: MeiliDocumentRequest[] = allChanges - .map(convertChangeToMeiliDocumentRequest) - .filter(isDefined); - const documents: MeiliDocument[] = []; - for (const request of documentRequests) { - documents.push(...(await getMeiliDocumentsFromRequest(request))); - } + const documents = ( + await Promise.all(allChanges.map(convertEndpointChangeToMeiliDocuments)) + ).flat(); console.log("Adding", documents.length, "documents to Meilisearch"); diff --git a/src/utils.ts b/src/utils.ts deleted file mode 100644 index 0bab86f..0000000 --- a/src/utils.ts +++ /dev/null @@ -1,6 +0,0 @@ -export const isDefined = (value: T | null | undefined): value is T => - value !== null && value !== undefined; - -export const isUndefined = ( - value: T | null | undefined -): value is null | undefined => !isDefined(value); diff --git a/src/webhook.ts b/src/webhook.ts index d9fd930..07bc3d3 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -1,7 +1,4 @@ -import { - convertChangeToMeiliDocumentRequest, - getMeiliDocumentsFromRequest, -} from "src/convert"; +import { convertEndpointChangeToMeiliDocuments } from "src/convert"; import { meili } from "src/services"; import { MeiliIndexes } from "src/shared/meilisearch/constants"; import type { MeiliDocument } from "src/shared/meilisearch/types"; @@ -15,10 +12,7 @@ export const webhookHandler = async (changes: EndpointChange[]) => { await index.deleteDocuments({ filter: `endpointCalled = "${change.url}"`, }); - - const meiliDocRequest = convertChangeToMeiliDocumentRequest(change); - if (!meiliDocRequest) continue; - documents.push(...(await getMeiliDocumentsFromRequest(meiliDocRequest))); + documents.push(...(await convertEndpointChangeToMeiliDocuments(change))); } console.log("[Webhook] Adding", documents.length, "documents to Meilisearch");