From 107f67e22b4bf15602e6b5a01f120301db6fc9ee Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Mon, 6 Oct 2025 23:37:17 +0200 Subject: [PATCH] Add templateSlug field auto-populated from template relationship and bump version to 0.4.17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added templateSlug text field to Emails collection that is automatically populated via beforeChange hook when template relationship is set, making template slug accessible in beforeSend hook. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- package.json | 2 +- src/collections/Emails.ts | 31 ++++++++++++++++++++++++++++++- src/types/index.ts | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index cd1ce08..52a6ea7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/collections/Emails.ts b/src/collections/Emails.ts index fcd2a5c..f31b145 100644 --- a/src/collections/Emails.ts +++ b/src/collections/Emails.ts @@ -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: [ diff --git a/src/types/index.ts b/src/types/index.ts index 4826286..0a0d010 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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