Removed unused endpoints and use EndpointChanges for getAll

This commit is contained in:
DrMint 2024-07-26 08:13:43 +02:00
parent 4c467089dc
commit 9e7d020ad7
5 changed files with 84 additions and 269 deletions

View File

@ -1,11 +1,27 @@
import payload from "payload"; import payload from "payload";
import { Endpoint } from "payload/config"; import { Endpoint } from "payload/config";
import { Collections } from "../shared/payload/constants"; import { Collections } from "../shared/payload/constants";
import { EndpointAllIds } from "../shared/payload/endpoint-types"; import { EndpointChange } from "../shared/payload/webhooks";
import {
getEndpointChangesForAudio,
getEndpointChangesForChronologyEvent,
getEndpointChangesForCollectible,
getEndpointChangesForCurrency,
getEndpointChangesForFile,
getEndpointChangesForFolder,
getEndpointChangesForImage,
getEndpointChangesForLanguage,
getEndpointChangesForPage,
getEndpointChangesForRecorder,
getEndpointChangesForVideo,
getEndpointChangesForWebsiteConfig,
getEndpointChangesForWording,
} from "../hooks/afterOperationSendChangesWebhook";
import { uniqueBy } from "../utils/array";
export const getAllIds: Endpoint = { export const getAllEndpoint: Endpoint = {
method: "get", method: "get",
path: "/all-ids", path: "/all",
handler: async (req, res) => { handler: async (req, res) => {
if (!req.user) { if (!req.user) {
return res.status(403).send({ return res.status(403).send({
@ -95,18 +111,22 @@ export const getAllIds: Endpoint = {
}, },
}); });
const result: EndpointAllIds = { const result: EndpointChange[] = [
collectibles: { slugs: collectibles.docs.map(({ slug }) => slug) }, ...getEndpointChangesForWebsiteConfig(),
pages: { slugs: pages.docs.map(({ slug }) => slug) }, ...getEndpointChangesForLanguage(),
folders: { slugs: folders.docs.map(({ slug }) => slug) }, ...getEndpointChangesForCurrency(),
videos: { ids: videos.docs.map(({ id }) => id) }, ...getEndpointChangesForWording(),
audios: { ids: audios.docs.map(({ id }) => id) }, ...folders.docs.flatMap(getEndpointChangesForFolder),
images: { ids: images.docs.map(({ id }) => id) }, ...pages.docs.flatMap(getEndpointChangesForPage),
files: { ids: files.docs.map(({ id }) => id) }, ...collectibles.docs.flatMap(getEndpointChangesForCollectible),
recorders: { ids: recorders.docs.map(({ id }) => id) }, ...audios.docs.flatMap(getEndpointChangesForAudio),
chronologyEvents: { ids: chronologyEvents.docs.map(({ id }) => id) }, ...images.docs.flatMap(getEndpointChangesForImage),
}; ...videos.docs.flatMap(getEndpointChangesForVideo),
...files.docs.flatMap(getEndpointChangesForFile),
...recorders.docs.flatMap(getEndpointChangesForRecorder),
...chronologyEvents.docs.flatMap(getEndpointChangesForChronologyEvent),
];
return res.status(200).send(result); return res.status(200).send(uniqueBy(result, ({ url }) => url));
}, },
}; };

View File

@ -1,208 +0,0 @@
import payload from "payload";
import { Endpoint } from "payload/config";
import { Collectible } from "../types/collections";
import { Collections } from "../shared/payload/constants";
import { EndpointAllSDKUrls } from "../shared/payload/endpoint-types";
import { getSDKEndpoint } from "../shared/payload/sdk";
export const getAllSDKUrlsEndpoint: Endpoint = {
method: "get",
path: "/all-sdk-urls",
handler: async (req, res) => {
if (!req.user) {
return res.status(403).send({
errors: [
{
message: "You are not allowed to perform this action.",
},
],
});
}
const collectibles = await payload.find({
collection: Collections.Collectibles,
depth: 0,
pagination: false,
user: req.user,
where: {
_status: {
equals: "published",
},
},
});
const pages = await payload.find({
collection: Collections.Pages,
depth: 0,
pagination: false,
user: req.user,
where: {
_status: {
equals: "published",
},
},
});
const folders = await payload.find({
collection: Collections.Folders,
depth: 0,
pagination: false,
user: req.user,
});
const videos = await payload.find({
collection: Collections.Videos,
depth: 0,
pagination: false,
user: req.user,
});
const audios = await payload.find({
collection: Collections.Audios,
depth: 0,
pagination: false,
user: req.user,
});
const images = await payload.find({
collection: Collections.Images,
depth: 0,
pagination: false,
user: req.user,
});
const files = await payload.find({
collection: Collections.Files,
depth: 0,
pagination: false,
user: req.user,
});
const recorders = await payload.find({
collection: Collections.Recorders,
depth: 0,
pagination: false,
user: req.user,
});
const chronologyEvents = await payload.find({
collection: Collections.ChronologyEvents,
depth: 0,
pagination: false,
user: req.user,
where: {
_status: {
equals: "published",
},
},
});
const urls = new Set([
getSDKEndpoint.getConfigEndpoint(),
getSDKEndpoint.getLanguagesEndpoint(),
getSDKEndpoint.getCurrenciesEndpoint(),
getSDKEndpoint.getWordingsEndpoint(),
...folders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Folders, doc)),
...pages.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Pages, doc)),
...chronologyEvents.docs.flatMap((doc) =>
getSDKUrlsForDocument(Collections.ChronologyEvents, doc)
),
...videos.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Videos, doc)),
...audios.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Audios, doc)),
...images.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Images, doc)),
...files.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Files, doc)),
...collectibles.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Collectibles, doc)),
...recorders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Recorders, doc)),
]);
const result: EndpointAllSDKUrls = {
urls: [...urls],
};
return res.status(200).send(result);
},
};
const getSDKUrlsForDocument = (collection: Collections, doc: any): string[] => {
switch (collection) {
case Collections.WebsiteConfig:
return [getSDKEndpoint.getConfigEndpoint()];
case Collections.Folders:
return [getSDKEndpoint.getFolderEndpoint(doc.slug)];
case Collections.Languages:
return [getSDKEndpoint.getLanguagesEndpoint()];
case Collections.Currencies:
return [getSDKEndpoint.getCurrenciesEndpoint()];
case Collections.Wordings:
return [getSDKEndpoint.getWordingsEndpoint()];
case Collections.Pages:
return [getSDKEndpoint.getPageEndpoint(doc.slug)];
case Collections.Collectibles: {
const { slug, gallery, scans, scansEnabled } = doc as Collectible;
const urls: string[] = [getSDKEndpoint.getCollectibleEndpoint(slug)];
if (gallery && gallery.length > 0) {
urls.push(getSDKEndpoint.getCollectibleGalleryEndpoint(slug));
urls.push(
...gallery.map((_, index) =>
getSDKEndpoint.getCollectibleGalleryImageEndpoint(slug, index.toString())
)
);
}
if (scans && scansEnabled) {
urls.push(getSDKEndpoint.getCollectibleScansEndpoint(slug));
// TODO: Add other pages for cover, obi, dustjacket...
if (scans.pages) {
urls.push(
...scans.pages.map(({ page }) =>
getSDKEndpoint.getCollectibleScanPageEndpoint(slug, page.toString())
)
);
}
}
return urls;
}
case Collections.ChronologyEvents:
return [
getSDKEndpoint.getChronologyEventsEndpoint(),
getSDKEndpoint.getChronologyEventByIDEndpoint(doc.id),
];
case Collections.Images:
return [getSDKEndpoint.getImageByIDEndpoint(doc.id)];
case Collections.Audios:
return [getSDKEndpoint.getAudioByIDEndpoint(doc.id)];
case Collections.Videos:
return [getSDKEndpoint.getVideoByIDEndpoint(doc.id)];
case Collections.Recorders:
return [getSDKEndpoint.getRecorderByIDEndpoint(doc.id)];
case Collections.Files:
return [getSDKEndpoint.getFileByIDEndpoint(doc.id)];
case Collections.Attributes:
case Collections.CreditsRole:
case Collections.GenericContents:
case Collections.MediaThumbnails:
case Collections.Scans:
case Collections.Tags:
case Collections.VideosChannels:
case Collections.VideosSubtitles:
return [];
default: {
console.warn("Unrecognized collection", collection);
return [];
}
}
};

View File

@ -1,20 +1,17 @@
import { Collections } from "../shared/payload/constants"; import { Collections } from "../shared/payload/constants";
import { getSDKEndpoint } from "../shared/payload/sdk"; import { SDKEndpointNames, getSDKEndpoint } from "../shared/payload/sdk";
import { EndpointChange } from "../shared/payload/webhooks"; import { EndpointChange } from "../shared/payload/webhooks";
import { import {
Audio, Audio,
ChronologyEvent, ChronologyEvent,
Collectible, Collectible,
Currency,
File, File,
Folder, Folder,
Image, Image,
Language,
Page, Page,
Recorder, Recorder,
Relationship, Relationship,
Video, Video,
Wording,
} from "../types/collections"; } from "../types/collections";
import { isPayloadType } from "../utils/asserts"; import { isPayloadType } from "../utils/asserts";
import { AfterChangeHook, AfterDeleteHook } from "payload/dist/collections/config/types"; import { AfterChangeHook, AfterDeleteHook } from "payload/dist/collections/config/types";
@ -55,7 +52,7 @@ export const globalAfterChangeSendChangesWebhook: GlobalAfterChangeHook = async
switch (global.slug as keyof GeneratedTypes["globals"]) { switch (global.slug as keyof GeneratedTypes["globals"]) {
case Collections.WebsiteConfig: case Collections.WebsiteConfig:
changes.push({ type: "getConfig", url: getSDKEndpoint.getConfigEndpoint() }); changes.push(...getEndpointChangesForWebsiteConfig());
break; break;
default: default:
@ -133,13 +130,13 @@ const getEndpointChangesFromDocument = ({
return getEndpointChangesForChronologyEvent(value); return getEndpointChangesForChronologyEvent(value);
case Collections.Languages: case Collections.Languages:
return getEndpointChangesForLanguage(value); return getEndpointChangesForLanguage();
case Collections.Currencies: case Collections.Currencies:
return getEndpointChangesForCurrency(value); return getEndpointChangesForCurrency();
case Collections.Wordings: case Collections.Wordings:
return getEndpointChangesForWording(value); return getEndpointChangesForWording();
case Collections.Attributes: case Collections.Attributes:
case Collections.CreditsRole: case Collections.CreditsRole:
@ -248,27 +245,34 @@ const getEndpointChangesFromOutgoingRelation = ({
} }
}; };
const getEndpointChangesForFolder = ({ slug }: Folder): EndpointChange[] => [ export const getEndpointChangesForWebsiteConfig = (): EndpointChange[] => [
{ type: "getFolder", slug, url: getSDKEndpoint.getFolderEndpoint(slug) }, {
type: SDKEndpointNames.getWebsiteConfig,
url: getSDKEndpoint.getWebsiteConfig(),
},
]; ];
const getEndpointChangesForLanguage = (_: Language): EndpointChange[] => [ export const getEndpointChangesForFolder = ({ slug }: Folder): EndpointChange[] => [
{ type: "getLanguages", url: getSDKEndpoint.getLanguagesEndpoint() }, { type: SDKEndpointNames.getFolder, slug, url: getSDKEndpoint.getFolder(slug) },
]; ];
const getEndpointChangesForCurrency = (_: Currency): EndpointChange[] => [ export const getEndpointChangesForLanguage = (): EndpointChange[] => [
{ type: "getCurrencies", url: getSDKEndpoint.getCurrenciesEndpoint() }, { type: SDKEndpointNames.getLanguages, url: getSDKEndpoint.getLanguages() },
]; ];
const getEndpointChangesForWording = (_: Wording): EndpointChange[] => [ export const getEndpointChangesForCurrency = (): EndpointChange[] => [
{ type: "getWordings", url: getSDKEndpoint.getWordingsEndpoint() }, { type: SDKEndpointNames.getCurrencies, url: getSDKEndpoint.getCurrencies() },
]; ];
const getEndpointChangesForPage = ({ slug }: Page): EndpointChange[] => [ export const getEndpointChangesForWording = (): EndpointChange[] => [
{ type: "getPage", slug, url: getSDKEndpoint.getPageEndpoint(slug) }, { type: SDKEndpointNames.getWordings, url: getSDKEndpoint.getWordings() },
]; ];
const getEndpointChangesForCollectible = ({ export const getEndpointChangesForPage = ({ slug }: Page): EndpointChange[] => [
{ type: SDKEndpointNames.getPage, slug, url: getSDKEndpoint.getPage(slug) },
];
export const getEndpointChangesForCollectible = ({
slug, slug,
gallery, gallery,
scans, scans,
@ -278,26 +282,26 @@ const getEndpointChangesForCollectible = ({
if (gallery && gallery.length > 0) { if (gallery && gallery.length > 0) {
changes.push({ changes.push({
type: "getCollectibleGallery", type: SDKEndpointNames.getCollectibleGallery,
slug, slug,
url: getSDKEndpoint.getCollectibleGalleryEndpoint(slug), url: getSDKEndpoint.getCollectibleGallery(slug),
}); });
gallery.forEach((_, indexNumber) => { gallery.forEach((_, indexNumber) => {
const index = indexNumber.toString(); const index = indexNumber.toString();
changes.push({ changes.push({
type: "getCollectibleGalleryImage", type: SDKEndpointNames.getCollectibleGalleryImage,
slug, slug,
index: index, index: index,
url: getSDKEndpoint.getCollectibleGalleryImageEndpoint(slug, index), url: getSDKEndpoint.getCollectibleGalleryImage(slug, index),
}); });
}); });
} }
if (scans && scansEnabled) { if (scans && scansEnabled) {
changes.push({ changes.push({
type: "getCollectibleScans", type: SDKEndpointNames.getCollectibleScans,
slug, slug,
url: getSDKEndpoint.getCollectibleScansEndpoint(slug), url: getSDKEndpoint.getCollectibleScans(slug),
}); });
// TODO: Add other changes for cover, obi, dustjacket... // TODO: Add other changes for cover, obi, dustjacket...
@ -305,10 +309,10 @@ const getEndpointChangesForCollectible = ({
scans.pages?.forEach((_, indexNumber) => { scans.pages?.forEach((_, indexNumber) => {
const index = indexNumber.toString(); const index = indexNumber.toString();
changes.push({ changes.push({
type: "getCollectibleScanPage", type: SDKEndpointNames.getCollectibleScanPage,
slug, slug,
index: index, index: index,
url: getSDKEndpoint.getCollectibleScanPageEndpoint(slug, index), url: getSDKEndpoint.getCollectibleScanPage(slug, index),
}); });
}); });
} }
@ -316,35 +320,35 @@ const getEndpointChangesForCollectible = ({
return changes; return changes;
}; };
const getEndpointChangesForAudio = ({ id }: Audio): EndpointChange[] => [ export const getEndpointChangesForAudio = ({ id }: Audio): EndpointChange[] => [
{ type: "getAudioByID", id, url: getSDKEndpoint.getAudioByIDEndpoint(id) }, { type: SDKEndpointNames.getAudioByID, id, url: getSDKEndpoint.getAudioByID(id) },
]; ];
const getEndpointChangesForImage = ({ id }: Image): EndpointChange[] => [ export const getEndpointChangesForImage = ({ id }: Image): EndpointChange[] => [
{ type: "getImageByID", id, url: getSDKEndpoint.getImageByIDEndpoint(id) }, { type: SDKEndpointNames.getImageByID, id, url: getSDKEndpoint.getImageByID(id) },
]; ];
const getEndpointChangesForVideo = ({ id }: Video): EndpointChange[] => [ export const getEndpointChangesForVideo = ({ id }: Video): EndpointChange[] => [
{ type: "getVideoByID", id, url: getSDKEndpoint.getVideoByIDEndpoint(id) }, { type: SDKEndpointNames.getVideoByID, id, url: getSDKEndpoint.getVideoByID(id) },
]; ];
const getEndpointChangesForFile = ({ id }: File): EndpointChange[] => [ export const getEndpointChangesForFile = ({ id }: File): EndpointChange[] => [
{ type: "getFileByID", id, url: getSDKEndpoint.getFileByIDEndpoint(id) }, { type: SDKEndpointNames.getFileByID, id, url: getSDKEndpoint.getFileByID(id) },
]; ];
const getEndpointChangesForRecorder = ({ id }: Recorder): EndpointChange[] => [ export const getEndpointChangesForRecorder = ({ id }: Recorder): EndpointChange[] => [
{ type: "getRecorderByID", id, url: getSDKEndpoint.getRecorderByIDEndpoint(id) }, { type: SDKEndpointNames.getRecorderByID, id, url: getSDKEndpoint.getRecorderByID(id) },
]; ];
const getEndpointChangesForChronologyEvent = ({ id }: ChronologyEvent): EndpointChange[] => [ export const getEndpointChangesForChronologyEvent = ({ id }: ChronologyEvent): EndpointChange[] => [
{ {
type: "getChronologyEventByID", type: SDKEndpointNames.getChronologyEventByID,
id, id,
url: getSDKEndpoint.getChronologyEventByIDEndpoint(id), url: getSDKEndpoint.getChronologyEventByID(id),
}, },
{ {
type: "getChronologyEvents", type: SDKEndpointNames.getChronologyEvents,
url: getSDKEndpoint.getChronologyEventsEndpoint(), url: getSDKEndpoint.getChronologyEvents(),
}, },
]; ];

View File

@ -27,8 +27,7 @@ import { WebsiteConfig } from "./collections/WebsiteConfig/WebsiteConfig";
import { Wordings } from "./collections/Wordings/Wordings"; import { Wordings } from "./collections/Wordings/Wordings";
import { Icon } from "./components/Icon"; import { Icon } from "./components/Icon";
import { Logo } from "./components/Logo"; import { Logo } from "./components/Logo";
import { getAllIds } from "./endpoints/getAllIdsEndpoint"; import { getAllEndpoint } from "./endpoints/getAllEndpoint";
import { getAllSDKUrlsEndpoint } from "./endpoints/getAllSDKUrlsEndpoint";
import { createEditor } from "./utils/editor"; import { createEditor } from "./utils/editor";
import { Collections } from "./shared/payload/constants"; import { Collections } from "./shared/payload/constants";
import { relationshipsPlugin } from "payloadcms-relationships"; import { relationshipsPlugin } from "payloadcms-relationships";
@ -92,7 +91,7 @@ export default buildConfig({
typescript: { typescript: {
outputFile: path.resolve(__dirname, "types/collections.ts"), outputFile: path.resolve(__dirname, "types/collections.ts"),
}, },
endpoints: [getAllSDKUrlsEndpoint, getAllIds], endpoints: [getAllEndpoint],
graphQL: { graphQL: {
disable: true, disable: true,
}, },

@ -1 +1 @@
Subproject commit 3582a48bd12a66e99121d9fbc01681971a7d943a Subproject commit caa79dee9eca5b9b6959e6f5a721245202423612