Updated website config endpoint
This commit is contained in:
		
							parent
							
								
									39e2642c55
								
							
						
					
					
						commit
						8e36642715
					
				| @ -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, | ||||
|  | ||||
| @ -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 []; | ||||
|  | ||||
							
								
								
									
										12
									
								
								src/hooks/afterChangeWebhook.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/hooks/afterChangeWebhook.ts
									
									
									
									
									
										Normal 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; | ||||
| }; | ||||
| @ -136,6 +136,7 @@ export type EndpointWebsiteConfig = { | ||||
|   })[]; | ||||
|   timeline: { | ||||
|     breaks: number[]; | ||||
|     eventCount: number; | ||||
|     eras: { | ||||
|       startingYear: number; | ||||
|       endingYear: number; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 DrMint
						DrMint