import type { CollectionConfig } from 'payload'; import { lexicalEditor, BlocksFeature } from '@payloadcms/richtext-lexical'; import { StickyNarrative } from '../blocks/StickyNarrative'; import { ComparisonGrid } from '../blocks/ComparisonGrid'; import { VisualLinkPreview } from '../blocks/VisualLinkPreview'; import { TechnicalGrid } from '../blocks/TechnicalGrid'; import { HighlightBox } from '../blocks/HighlightBox'; import { AnimatedImage } from '../blocks/AnimatedImage'; import { ChatBubble } from '../blocks/ChatBubble'; import { PowerCTA } from '../blocks/PowerCTA'; import { Callout } from '../blocks/Callout'; import { Stats } from '../blocks/Stats'; import { SplitHeading } from '../blocks/SplitHeading'; export const Posts: CollectionConfig = { slug: 'posts', admin: { defaultColumns: ['featuredImage', 'title', 'date', 'updatedAt', '_status'], }, versions: { drafts: true, }, access: { read: ({ req: { user } }) => { if (process.env.NODE_ENV === 'development' || process.env.TARGET === 'staging') { return true; } if (user) { return true; } return { and: [ { _status: { equals: 'published', }, }, { date: { less_than_equal: new Date().toISOString(), }, }, ], }; }, }, hooks: { afterChange: [ async ({ doc, req }) => { // Run index sync asynchronously to not block the CMS save operation setTimeout(async () => { try { const { upsertProductVector, deleteProductVector } = await import('../../lib/qdrant'); // Check if post is published if (doc._status !== 'published') { await deleteProductVector(`post_${doc.id}`); req.payload.logger.info(`Removed drafted post ${doc.slug} from Qdrant`); } else { // Serialize payload const contentText = [ `Blog-Artikel: ${doc.title}`, doc.excerpt ? `Zusammenfassung: ${doc.excerpt}` : '', doc.category ? `Kategorie: ${doc.category}` : '', ] .filter(Boolean) .join('\n'); const payload = { type: 'knowledge', content: contentText, data: { title: doc.title, slug: doc.slug, }, }; await upsertProductVector(`post_${doc.id}`, contentText, payload); req.payload.logger.info(`Upserted post ${doc.slug} to Qdrant`); } } catch (error) { req.payload.logger.error({ msg: 'Error syncing post to Qdrant', err: error, postId: doc.id, }); } }, 0); return doc; }, ], afterDelete: [ async ({ id, req }) => { try { const { deleteProductVector } = await import('../../lib/qdrant'); await deleteProductVector(`post_${id}`); req.payload.logger.info(`Deleted post ${id} from Qdrant`); } catch (error) { req.payload.logger.error({ msg: 'Error deleting post from Qdrant', err: error, postId: id, }); } }, ], }, fields: [ { name: 'title', type: 'text', required: true, localized: true, }, { name: 'slug', type: 'text', required: true, localized: true, admin: { position: 'sidebar', description: 'Unique slug per locale (e.g. same slug can exist in DE and EN).', }, hooks: { beforeValidate: [ ({ value, data }) => { if (value || !data?.title) return value; return data.title .toLowerCase() .replace(/ /g, '-') .replace(/[^\w-]+/g, ''); }, ], }, }, { name: 'excerpt', type: 'text', localized: true, admin: { description: 'A short summary for blog feed cards and SEO.', }, }, { name: 'date', type: 'date', required: true, admin: { position: 'sidebar', description: 'Future dates will schedule the post to publish automatically.', }, defaultValue: () => new Date().toISOString(), }, { name: 'featuredImage', type: 'upload', relationTo: 'media', admin: { position: 'sidebar', description: 'The primary Hero image used for headers and OpenGraph previews.', }, }, { name: 'category', type: 'text', localized: true, admin: { position: 'sidebar', description: 'Used for tag bucketing (e.g. "Kabel Technologie").', }, }, { name: 'content', type: 'richText', localized: true, editor: lexicalEditor({ features: ({ defaultFeatures }) => [ ...defaultFeatures, BlocksFeature({ blocks: [ StickyNarrative, ComparisonGrid, VisualLinkPreview, TechnicalGrid, HighlightBox, AnimatedImage, ChatBubble, PowerCTA, Callout, Stats, SplitHeading, ], }), ], }), }, ], };