Simplify conversion from change to meilidoc

This commit is contained in:
DrMint 2024-07-28 13:55:37 +02:00
parent 326c3393ca
commit 116a622581
5 changed files with 30 additions and 101 deletions

View File

@ -1,8 +1,5 @@
import { payload } from "src/services"; import { payload } from "src/services";
import type { import type { MeiliDocument } from "src/shared/meilisearch/types";
MeiliDocument,
MeiliDocumentRequest,
} from "src/shared/meilisearch/types";
import { Collections } from "src/shared/payload/constants"; import { Collections } from "src/shared/payload/constants";
import type { import type {
EndpointAudio, EndpointAudio,
@ -40,7 +37,6 @@ const convertPageToDocument = ({
}${formatRichTextContentToString(content)}`, }${formatRichTextContentToString(content)}`,
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Pages, type: Collections.Pages,
slug: data.slug,
endpointCalled, endpointCalled,
data, data,
}) })
@ -61,7 +57,6 @@ const convertCollectibleToDocument = ({
: {}), : {}),
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Collectibles, type: Collections.Collectibles,
slug: data.slug,
endpointCalled, endpointCalled,
data, data,
}) })
@ -80,7 +75,6 @@ const convertFolderToDocument = ({
? { description: formatRichTextContentToString(description) } ? { description: formatRichTextContentToString(description) }
: {}), : {}),
type: Collections.Folders, type: Collections.Folders,
slug: data.slug,
endpointCalled, endpointCalled,
data, data,
})); }));
@ -99,7 +93,6 @@ const convertAudioToDocument = ({
: {}), : {}),
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Audios, type: Collections.Audios,
id: data.id,
endpointCalled, endpointCalled,
data, data,
})); }));
@ -120,7 +113,6 @@ const convertImageToDocument = ({
: {}), : {}),
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Images, type: Collections.Images,
id: data.id,
endpointCalled, endpointCalled,
data, data,
}) })
@ -134,7 +126,6 @@ const convertImageToDocument = ({
title: data.filename, title: data.filename,
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Images, type: Collections.Images,
id: data.id,
endpointCalled, endpointCalled,
data, data,
}, },
@ -156,7 +147,6 @@ const convertVideoToDocument = ({
: {}), : {}),
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Videos, type: Collections.Videos,
id: data.id,
endpointCalled, endpointCalled,
data, data,
})); }));
@ -175,7 +165,6 @@ const convertRecorderToDocument = ({
? { description: formatRichTextContentToString(biography) } ? { description: formatRichTextContentToString(biography) }
: {}), : {}),
type: Collections.Recorders, type: Collections.Recorders,
id: data.id,
endpointCalled, endpointCalled,
data, data,
})); }));
@ -187,7 +176,6 @@ const convertRecorderToDocument = ({
languages: [], languages: [],
title: data.username, title: data.username,
type: Collections.Recorders, type: Collections.Recorders,
id: data.id,
endpointCalled, endpointCalled,
data, data,
}, },
@ -211,7 +199,6 @@ const convertFileToDocument = ({
: {}), : {}),
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Files, type: Collections.Files,
id: data.id,
endpointCalled, endpointCalled,
data, data,
}) })
@ -225,7 +212,6 @@ const convertFileToDocument = ({
title: data.filename, title: data.filename,
updatedAt: Date.parse(data.updatedAt), updatedAt: Date.parse(data.updatedAt),
type: Collections.Files, type: Collections.Files,
id: data.id,
endpointCalled, endpointCalled,
data, data,
}, },
@ -252,81 +238,48 @@ const convertChronologyEventToDocument = ({
} }
: {}), : {}),
type: Collections.ChronologyEvents, type: Collections.ChronologyEvents,
id: data.id,
endpointCalled, endpointCalled,
data: { date: data.date, event }, data: { date: data.date, event },
}) })
) )
); );
export const getMeiliDocumentsFromRequest = async ( export const convertEndpointChangeToMeiliDocuments = async (
request: MeiliDocumentRequest request: EndpointChange
): Promise<MeiliDocument[]> => { ): Promise<MeiliDocument[]> => {
switch (request.type) { switch (request.type) {
case Collections.Audios: case SDKEndpointNames.getFolder:
return convertAudioToDocument(await payload.getAudioByID(request.id)); return convertFolderToDocument(await payload.getFolder(request.slug));
case Collections.ChronologyEvents: case SDKEndpointNames.getPage:
return convertChronologyEventToDocument( return convertPageToDocument(await payload.getPage(request.slug));
await payload.getChronologyEventByID(request.id)
);
case Collections.Collectibles: case SDKEndpointNames.getCollectible:
return convertCollectibleToDocument( return convertCollectibleToDocument(
await payload.getCollectible(request.slug) await payload.getCollectible(request.slug)
); );
case Collections.Files: case SDKEndpointNames.getChronologyEventByID:
return convertFileToDocument(await payload.getFileByID(request.id)); return convertChronologyEventToDocument(
await payload.getChronologyEventByID(request.id)
);
case Collections.Folders: case SDKEndpointNames.getImageByID:
return convertFolderToDocument(await payload.getFolder(request.slug));
case Collections.Images:
return convertImageToDocument(await payload.getImageByID(request.id)); return convertImageToDocument(await payload.getImageByID(request.id));
case Collections.Pages: case SDKEndpointNames.getAudioByID:
return convertPageToDocument(await payload.getPage(request.slug)); 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( return convertRecorderToDocument(
await payload.getRecorderByID(request.id) 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.getWebsiteConfig:
case SDKEndpointNames.getLanguages: case SDKEndpointNames.getLanguages:
@ -338,6 +291,6 @@ export const convertChangeToMeiliDocumentRequest = (
case SDKEndpointNames.getCollectibleGalleryImage: case SDKEndpointNames.getCollectibleGalleryImage:
case SDKEndpointNames.getChronologyEvents: case SDKEndpointNames.getChronologyEvents:
default: default:
return undefined; return [];
} }
}; };

@ -1 +1 @@
Subproject commit caa79dee9eca5b9b6959e6f5a721245202423612 Subproject commit 32083473e9799ffddada87b979b0e4b546722be7

View File

@ -1,14 +1,6 @@
import { import { convertEndpointChangeToMeiliDocuments } from "src/convert";
convertChangeToMeiliDocumentRequest,
getMeiliDocumentsFromRequest,
} from "src/convert";
import { meili, payload } from "src/services"; import { meili, payload } from "src/services";
import { MeiliIndexes } from "src/shared/meilisearch/constants"; 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 () => { export const synchronizeMeiliDocs = async () => {
const version = await meili.getVersion(); const version = await meili.getVersion();
@ -39,13 +31,9 @@ export const synchronizeMeiliDocs = async () => {
const allChanges = (await payload.getAll()).data; const allChanges = (await payload.getAll()).data;
const documentRequests: MeiliDocumentRequest[] = allChanges const documents = (
.map(convertChangeToMeiliDocumentRequest) await Promise.all(allChanges.map(convertEndpointChangeToMeiliDocuments))
.filter(isDefined); ).flat();
const documents: MeiliDocument[] = [];
for (const request of documentRequests) {
documents.push(...(await getMeiliDocumentsFromRequest(request)));
}
console.log("Adding", documents.length, "documents to Meilisearch"); console.log("Adding", documents.length, "documents to Meilisearch");

View File

@ -1,6 +0,0 @@
export const isDefined = <T>(value: T | null | undefined): value is T =>
value !== null && value !== undefined;
export const isUndefined = <T>(
value: T | null | undefined
): value is null | undefined => !isDefined(value);

View File

@ -1,7 +1,4 @@
import { import { convertEndpointChangeToMeiliDocuments } from "src/convert";
convertChangeToMeiliDocumentRequest,
getMeiliDocumentsFromRequest,
} from "src/convert";
import { meili } from "src/services"; import { meili } from "src/services";
import { MeiliIndexes } from "src/shared/meilisearch/constants"; import { MeiliIndexes } from "src/shared/meilisearch/constants";
import type { MeiliDocument } from "src/shared/meilisearch/types"; import type { MeiliDocument } from "src/shared/meilisearch/types";
@ -15,10 +12,7 @@ export const webhookHandler = async (changes: EndpointChange[]) => {
await index.deleteDocuments({ await index.deleteDocuments({
filter: `endpointCalled = "${change.url}"`, filter: `endpointCalled = "${change.url}"`,
}); });
documents.push(...(await convertEndpointChangeToMeiliDocuments(change)));
const meiliDocRequest = convertChangeToMeiliDocumentRequest(change);
if (!meiliDocRequest) continue;
documents.push(...(await getMeiliDocumentsFromRequest(meiliDocRequest)));
} }
console.log("[Webhook] Adding", documents.length, "documents to Meilisearch"); console.log("[Webhook] Adding", documents.length, "documents to Meilisearch");