From 8e3664271595d5edb9d55325079268fe2613dce4 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Fri, 5 Apr 2024 17:42:37 +0200 Subject: [PATCH] Updated website config endpoint --- .../WebsiteConfig/WebsiteConfig.ts | 4 ++++ .../endpoints/getConfigEndpoint.ts | 19 +++++++++++++++++++ src/hooks/afterChangeWebhook.ts | 12 ++++++++++++ src/sdk.ts | 1 + 4 files changed, 36 insertions(+) create mode 100644 src/hooks/afterChangeWebhook.ts diff --git a/src/collections/WebsiteConfig/WebsiteConfig.ts b/src/collections/WebsiteConfig/WebsiteConfig.ts index 656bbf3..dde85fc 100644 --- a/src/collections/WebsiteConfig/WebsiteConfig.ts +++ b/src/collections/WebsiteConfig/WebsiteConfig.ts @@ -3,6 +3,7 @@ import { mustBeAdmin } from "../../accesses/collections/mustBeAdmin"; import { CollectionGroups, Collections } from "../../constants"; import { imageField } from "../../fields/imageField/imageField"; import { rowField } from "../../fields/rowField/rowField"; +import { afterChangeWebhook } from "../../hooks/afterChangeWebhook"; import { getConfigEndpoint } from "./endpoints/getConfigEndpoint"; const fields = { @@ -27,6 +28,9 @@ export const WebsiteConfig: GlobalConfig = { }, access: { update: mustBeAdmin, read: mustBeAdmin }, endpoints: [getConfigEndpoint], + hooks: { + afterChange: [afterChangeWebhook], + }, fields: [ { name: fields.homeFolders, diff --git a/src/collections/WebsiteConfig/endpoints/getConfigEndpoint.ts b/src/collections/WebsiteConfig/endpoints/getConfigEndpoint.ts index 2514afa..b0cb6a5 100644 --- a/src/collections/WebsiteConfig/endpoints/getConfigEndpoint.ts +++ b/src/collections/WebsiteConfig/endpoints/getConfigEndpoint.ts @@ -23,6 +23,24 @@ export const getConfigEndpoint: CollectionEndpoint = { slug: Collections.WebsiteConfig, }); + const events = ( + await payload.find({ + collection: Collections.ChronologyEvents, + pagination: false, + draft: false, + where: { + _status: { + equals: "published", + }, + }, + }) + ).docs; + + let eventCount = 0; + events.forEach(({ events }) => { + eventCount += events.length; + }); + const endpointWebsiteConfig: EndpointWebsiteConfig = { homeFolders: homeFolders?.flatMap(({ folder, darkThumbnail, lightThumbnail }) => { @@ -35,6 +53,7 @@ export const getConfigEndpoint: CollectionEndpoint = { }) ?? [], timeline: { breaks: timeline?.breaks ?? [], + eventCount, eras: timeline?.eras?.flatMap(({ endingYear, name, startingYear }) => { if (!isPayloadType(name)) return []; diff --git a/src/hooks/afterChangeWebhook.ts b/src/hooks/afterChangeWebhook.ts new file mode 100644 index 0000000..31c2c94 --- /dev/null +++ b/src/hooks/afterChangeWebhook.ts @@ -0,0 +1,12 @@ +import { AfterChangeHook } from "payload/dist/globals/config/types"; + +export const afterChangeWebhook: AfterChangeHook = async ({ doc, global }) => { + const url = `${process.env.WEB_HOOK_URI}/collection-operation`; + await fetch(url, { + headers: { + Authorization: `Bearer ${process.env.WEB_HOOK_TOKEN}`, + Collection: global.slug, + }, + }); + return doc; +}; diff --git a/src/sdk.ts b/src/sdk.ts index 07fcb96..023c98c 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -136,6 +136,7 @@ export type EndpointWebsiteConfig = { })[]; timeline: { breaks: number[]; + eventCount: number; eras: { startingYear: number; endingYear: number;