Updated website config endpoint

This commit is contained in:
DrMint 2024-04-05 17:42:37 +02:00
parent 39e2642c55
commit 8e36642715
4 changed files with 36 additions and 0 deletions

View File

@ -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,

View File

@ -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 [];

View File

@ -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;
};

View File

@ -136,6 +136,7 @@ export type EndpointWebsiteConfig = {
})[];
timeline: {
breaks: number[];
eventCount: number;
eras: {
startingYear: number;
endingYear: number;