Merge pull request #64 from xtr-dev/dev

Add templateSlug field auto-populated from template relationship and …
This commit is contained in:
Bas
2025-10-06 23:38:11 +02:00
committed by GitHub
3 changed files with 32 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@xtr-dev/payload-mailing", "name": "@xtr-dev/payload-mailing",
"version": "0.4.16", "version": "0.4.17",
"description": "Template-based email system with scheduling and job processing for PayloadCMS", "description": "Template-based email system with scheduling and job processing for PayloadCMS",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -12,7 +12,7 @@ const Emails: CollectionConfig = {
description: 'Email delivery and status tracking', description: 'Email delivery and status tracking',
}, },
defaultPopulate: { defaultPopulate: {
template: true, templateSlug: true,
to: true, to: true,
cc: true, cc: true,
bcc: true, bcc: true,
@@ -40,6 +40,14 @@ const Emails: CollectionConfig = {
description: 'Email template used (optional if custom content provided)', description: 'Email template used (optional if custom content provided)',
}, },
}, },
{
name: 'templateSlug',
type: 'text',
admin: {
description: 'Slug of the email template (auto-populated from template relationship)',
readOnly: true,
},
},
{ {
name: 'to', name: 'to',
type: 'text', type: 'text',
@@ -208,6 +216,27 @@ const Emails: CollectionConfig = {
}, },
], ],
hooks: { hooks: {
beforeChange: [
async ({ data, req }) => {
// Auto-populate templateSlug from template relationship
if (data.template) {
try {
const template = await req.payload.findByID({
collection: 'email-templates',
id: typeof data.template === 'string' ? data.template : data.template.id,
})
data.templateSlug = template.slug
} catch (error) {
// If template lookup fails, clear the slug
data.templateSlug = undefined
}
} else {
// Clear templateSlug if template is removed
data.templateSlug = undefined
}
return data
}
],
// Simple approach: Only use afterChange hook for job management // Simple approach: Only use afterChange hook for job management
// This avoids complex interaction between hooks and ensures document ID is always available // This avoids complex interaction between hooks and ensures document ID is always available
afterChange: [ afterChange: [

View File

@@ -14,6 +14,7 @@ export type JSONValue = string | number | boolean | { [k: string]: unknown } | u
export interface BaseEmailDocument { export interface BaseEmailDocument {
id: string | number id: string | number
template?: any template?: any
templateSlug?: string | null
to: string[] to: string[]
cc?: string[] | null cc?: string[] | null
bcc?: string[] | null bcc?: string[] | null