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",
"version": "0.4.16",
"version": "0.4.17",
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
"type": "module",
"main": "dist/index.js",

View File

@@ -12,7 +12,7 @@ const Emails: CollectionConfig = {
description: 'Email delivery and status tracking',
},
defaultPopulate: {
template: true,
templateSlug: true,
to: true,
cc: true,
bcc: true,
@@ -40,6 +40,14 @@ const Emails: CollectionConfig = {
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',
type: 'text',
@@ -208,6 +216,27 @@ const Emails: CollectionConfig = {
},
],
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
// This avoids complex interaction between hooks and ensures document ID is always available
afterChange: [

View File

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