From 0a4fe750f89804fd7f97b6c15eceacc555412000 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Wed, 26 Jun 2024 06:38:49 +0200 Subject: [PATCH] Improved getAllSDKUrls --- src/endpoints/getAllPathsEndpoint.ts | 112 ------------- src/endpoints/getAllSDKUrlsEndpoint.ts | 209 +++++++++++++++++++++++++ src/hooks/afterOperationWebhook.ts | 79 +--------- src/payload.config.ts | 4 +- src/sdk.ts | 18 +-- 5 files changed, 220 insertions(+), 202 deletions(-) delete mode 100644 src/endpoints/getAllPathsEndpoint.ts create mode 100644 src/endpoints/getAllSDKUrlsEndpoint.ts diff --git a/src/endpoints/getAllPathsEndpoint.ts b/src/endpoints/getAllPathsEndpoint.ts deleted file mode 100644 index 07ab58f..0000000 --- a/src/endpoints/getAllPathsEndpoint.ts +++ /dev/null @@ -1,112 +0,0 @@ -import payload from "payload"; -import { Endpoint } from "payload/config"; -import { Collections } from "../constants"; -import { EndpointAllPaths } from "../sdk"; - -export const getAllPathsEndpoint: Endpoint = { - method: "get", - path: "/all-paths", - 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 result: EndpointAllPaths = { - collectibles: collectibles.docs.map(({ slug }) => slug), - pages: pages.docs.map(({ slug }) => slug), - folders: folders.docs.map(({ slug }) => slug), - videos: videos.docs.map(({ id }) => id), - audios: audios.docs.map(({ id }) => id), - images: images.docs.map(({ id }) => id), - files: files.docs.map(({ id }) => id), - recorders: recorders.docs.map(({ id }) => id), - chronologyEvents: chronologyEvents.docs.map(({ id }) => id), - }; - - return res.status(200).send(result); - }, -}; diff --git a/src/endpoints/getAllSDKUrlsEndpoint.ts b/src/endpoints/getAllSDKUrlsEndpoint.ts new file mode 100644 index 0000000..85620d8 --- /dev/null +++ b/src/endpoints/getAllSDKUrlsEndpoint.ts @@ -0,0 +1,209 @@ +import payload from "payload"; +import { Endpoint } from "payload/config"; +import { Collections } from "../constants"; +import { EndpointAllSDKUrls, getSDKEndpoint } from "../sdk"; +import { Collectible } from "../types/collections"; + +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(), + + ...collectibles.docs.flatMap((doc) => + getSDKUrlsForDocument(Collections.Collectibles, doc) + ), + ...pages.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Pages, doc)), + ...folders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Folders, 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)), + ...recorders.docs.flatMap((doc) => getSDKUrlsForDocument(Collections.Recorders, doc)), + ...chronologyEvents.docs.flatMap((doc) => + getSDKUrlsForDocument(Collections.ChronologyEvents, doc) + ), + ]); + + const result: EndpointAllSDKUrls = { + urls: [...urls], + }; + + return res.status(200).send(result); + }, +}; + +export 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 []; + } + } +}; diff --git a/src/hooks/afterOperationWebhook.ts b/src/hooks/afterOperationWebhook.ts index 5fbec06..e6f6c2e 100644 --- a/src/hooks/afterOperationWebhook.ts +++ b/src/hooks/afterOperationWebhook.ts @@ -4,79 +4,8 @@ import { } from "payload/dist/collections/config/types"; import { AfterChangeHook as GlobalAfterChangeHook } from "payload/dist/globals/config/types"; import { AfterOperationWebHookMessage, Collections } from "../constants"; +import { getSDKUrlsForDocument } from "../endpoints/getAllSDKUrlsEndpoint"; import { getAddedBackPropagationRelationships } from "../fields/backPropagationField/backPropagationUtils"; -import { getSDKEndpoint } from "../sdk"; -import { Collectible } from "../types/collections"; - -const getURLs = (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)]; - - default: { - console.warn("Unrecognized collection", collection, "when sending webhook. No URL."); - return []; - } - } -}; export const globalAfterChangeWebhook: GlobalAfterChangeHook = async ({ global, @@ -87,7 +16,7 @@ export const globalAfterChangeWebhook: GlobalAfterChangeHook = async ({ await sendWebhookMessage({ collection, addedDependantIds: await getAddedBackPropagationRelationships(global, doc, previousDoc), - urls: getURLs(collection, doc), + urls: getSDKUrlsForDocument(collection, doc), }); return doc; }; @@ -113,7 +42,7 @@ export const collectionAfterChangeWebhook: CollectionAfterChangeHook = async ({ collection: collectionSlug, id: doc.id, addedDependantIds: await getAddedBackPropagationRelationships(collection, doc, previousDoc), - urls: getURLs(collectionSlug, doc), + urls: getSDKUrlsForDocument(collectionSlug, doc), }); return doc; @@ -131,7 +60,7 @@ export const afterDeleteWebhook: AfterDeleteHook = async ({ collection, doc }) = collection: collectionSlug, id: doc.id, addedDependantIds: [], - urls: getURLs(collectionSlug, doc), + urls: getSDKUrlsForDocument(collectionSlug, doc), }); return doc; diff --git a/src/payload.config.ts b/src/payload.config.ts index 76978e4..1b44d7b 100644 --- a/src/payload.config.ts +++ b/src/payload.config.ts @@ -28,7 +28,7 @@ import { Wordings } from "./collections/Wordings/Wordings"; import { Icon } from "./components/Icon"; import { Logo } from "./components/Logo"; import { Collections } from "./constants"; -import { getAllPathsEndpoint } from "./endpoints/getAllPathsEndpoint"; +import { getAllSDKUrlsEndpoint } from "./endpoints/getAllSDKUrlsEndpoint"; import { createEditor } from "./utils/editor"; const configuredSftpAdapter = sftpAdapter({ @@ -87,7 +87,7 @@ export default buildConfig({ typescript: { outputFile: path.resolve(__dirname, "types/collections.ts"), }, - endpoints: [getAllPathsEndpoint], + endpoints: [getAllSDKUrlsEndpoint], graphQL: { disable: true, }, diff --git a/src/sdk.ts b/src/sdk.ts index e4291cc..ee4062f 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -531,16 +531,8 @@ export type PayloadImage = PayloadMedia & { height: number; }; -export type EndpointAllPaths = { - collectibles: string[]; - pages: string[]; - folders: string[]; - videos: string[]; - audios: string[]; - images: string[]; - files: string[]; - recorders: string[]; - chronologyEvents: string[]; +export type EndpointAllSDKUrls = { + urls: string[]; }; // SDK @@ -583,7 +575,7 @@ export const getSDKEndpoint = { getVideoByIDEndpoint: (id: string) => `/${Collections.Videos}/id/${id}`, getFileByIDEndpoint: (id: string) => `/${Collections.Files}/id/${id}`, getRecorderByIDEndpoint: (id: string) => `/${Collections.Recorders}/id/${id}`, - getAllPathsEndpoint: () => `/all-paths`, + getAllSDKUrlsEndpoint: () => `/all-sdk-urls`, getLoginEndpoint: () => `/${Collections.Recorders}/login`, }; @@ -680,8 +672,8 @@ export const getPayloadSDK = ({ await request(getSDKEndpoint.getFileByIDEndpoint(id)), getRecorderByID: async (id: string): Promise => await request(getSDKEndpoint.getRecorderByIDEndpoint(id)), - getAllPaths: async (): Promise => - await request(getSDKEndpoint.getAllPathsEndpoint()), + getAllSdkUrls: async (): Promise => + await request(getSDKEndpoint.getAllSDKUrlsEndpoint()), request: async (pathname: string): Promise => await request(pathname), }; };