mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f04275d39 | ||
| 20afe30e88 | |||
| 02b3fecadf | |||
|
|
ea87f14308 | ||
| 6886027727 | |||
| 965569be06 | |||
|
|
ff788c1ecf | ||
| c12438aaa2 | |||
| 4dcbc1446a |
@@ -44,6 +44,8 @@ The plugin works with:
|
|||||||
- **String IDs**: `id: string`
|
- **String IDs**: `id: string`
|
||||||
- **Number IDs**: `id: number`
|
- **Number IDs**: `id: number`
|
||||||
- **Nullable fields**: Fields can be `null`, `undefined`, or have values
|
- **Nullable fields**: Fields can be `null`, `undefined`, or have values
|
||||||
|
- **Date fields**: Timestamp fields support both `Date` objects and `string` (ISO) formats
|
||||||
|
- **JSON variables**: Variables field supports any JSON-compatible value type
|
||||||
- **Generated types**: Works with `payload generate:types` output
|
- **Generated types**: Works with `payload generate:types` output
|
||||||
|
|
||||||
Your Payload configuration determines which types are used. The plugin automatically adapts to your setup.
|
Your Payload configuration determines which types are used. The plugin automatically adapts to your setup.
|
||||||
@@ -53,6 +55,9 @@ Your Payload configuration determines which types are used. The plugin automatic
|
|||||||
The base interfaces provided by the plugin:
|
The base interfaces provided by the plugin:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
// JSON value type that matches Payload's JSON field type
|
||||||
|
type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
||||||
|
|
||||||
interface BaseEmailDocument {
|
interface BaseEmailDocument {
|
||||||
id: string | number
|
id: string | number
|
||||||
template?: any
|
template?: any
|
||||||
@@ -64,16 +69,16 @@ interface BaseEmailDocument {
|
|||||||
subject: string
|
subject: string
|
||||||
html: string
|
html: string
|
||||||
text?: string | null
|
text?: string | null
|
||||||
variables?: Record<string, any> | null
|
variables?: JSONValue // Supports any JSON-compatible value
|
||||||
scheduledAt?: string | null
|
scheduledAt?: string | Date | null
|
||||||
sentAt?: string | null
|
sentAt?: string | Date | null
|
||||||
status?: 'pending' | 'processing' | 'sent' | 'failed' | null
|
status?: 'pending' | 'processing' | 'sent' | 'failed' | null
|
||||||
attempts?: number | null
|
attempts?: number | null
|
||||||
lastAttemptAt?: string | null
|
lastAttemptAt?: string | Date | null
|
||||||
error?: string | null
|
error?: string | null
|
||||||
priority?: number | null
|
priority?: number | null
|
||||||
createdAt?: string | null
|
createdAt?: string | Date | null
|
||||||
updatedAt?: string | null
|
updatedAt?: string | Date | null
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BaseEmailTemplateDocument {
|
interface BaseEmailTemplateDocument {
|
||||||
@@ -82,8 +87,8 @@ interface BaseEmailTemplateDocument {
|
|||||||
slug: string
|
slug: string
|
||||||
subject?: string | null
|
subject?: string | null
|
||||||
content?: any
|
content?: any
|
||||||
createdAt?: string | null
|
createdAt?: string | Date | null
|
||||||
updatedAt?: string | null
|
updatedAt?: string | Date | null
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-mailing",
|
"name": "@xtr-dev/payload-mailing",
|
||||||
"version": "0.1.14",
|
"version": "0.1.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",
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export interface SendEmailTaskInput {
|
|||||||
to: string | string[]
|
to: string | string[]
|
||||||
cc?: string | string[]
|
cc?: string | string[]
|
||||||
bcc?: string | string[]
|
bcc?: string | string[]
|
||||||
scheduledAt?: string // ISO date string
|
scheduledAt?: string | Date // ISO date string or Date object
|
||||||
priority?: number
|
priority?: number
|
||||||
|
|
||||||
// Allow any additional fields that users might have in their email collection
|
// Allow any additional fields that users might have in their email collection
|
||||||
|
|||||||
@@ -100,6 +100,23 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
|||||||
emailData.from = validated && validated.length > 0 ? validated[0] : undefined
|
emailData.from = validated && validated.length > 0 ? validated[0] : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize Date objects to ISO strings for consistent database storage
|
||||||
|
if (emailData.scheduledAt instanceof Date) {
|
||||||
|
emailData.scheduledAt = emailData.scheduledAt.toISOString()
|
||||||
|
}
|
||||||
|
if (emailData.sentAt instanceof Date) {
|
||||||
|
emailData.sentAt = emailData.sentAt.toISOString()
|
||||||
|
}
|
||||||
|
if (emailData.lastAttemptAt instanceof Date) {
|
||||||
|
emailData.lastAttemptAt = emailData.lastAttemptAt.toISOString()
|
||||||
|
}
|
||||||
|
if (emailData.createdAt instanceof Date) {
|
||||||
|
emailData.createdAt = emailData.createdAt.toISOString()
|
||||||
|
}
|
||||||
|
if (emailData.updatedAt instanceof Date) {
|
||||||
|
emailData.updatedAt = emailData.updatedAt.toISOString()
|
||||||
|
}
|
||||||
|
|
||||||
// Create the email in the collection with proper typing
|
// Create the email in the collection with proper typing
|
||||||
const email = await payload.create({
|
const email = await payload.create({
|
||||||
collection: collectionSlug,
|
collection: collectionSlug,
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import { Payload } from 'payload'
|
|||||||
import type { CollectionConfig, RichTextField } from 'payload'
|
import type { CollectionConfig, RichTextField } from 'payload'
|
||||||
import { Transporter } from 'nodemailer'
|
import { Transporter } from 'nodemailer'
|
||||||
|
|
||||||
|
// JSON value type that matches Payload's JSON field type
|
||||||
|
export type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
||||||
|
|
||||||
// Generic base interfaces that work with any ID type and null values
|
// Generic base interfaces that work with any ID type and null values
|
||||||
export interface BaseEmailDocument {
|
export interface BaseEmailDocument {
|
||||||
id: string | number
|
id: string | number
|
||||||
@@ -14,16 +17,16 @@ export interface BaseEmailDocument {
|
|||||||
subject: string
|
subject: string
|
||||||
html: string
|
html: string
|
||||||
text?: string | null
|
text?: string | null
|
||||||
variables?: Record<string, any> | null
|
variables?: JSONValue
|
||||||
scheduledAt?: string | null
|
scheduledAt?: string | Date | null
|
||||||
sentAt?: string | null
|
sentAt?: string | Date | null
|
||||||
status?: 'pending' | 'processing' | 'sent' | 'failed' | null
|
status?: 'pending' | 'processing' | 'sent' | 'failed' | null
|
||||||
attempts?: number | null
|
attempts?: number | null
|
||||||
lastAttemptAt?: string | null
|
lastAttemptAt?: string | Date | null
|
||||||
error?: string | null
|
error?: string | null
|
||||||
priority?: number | null
|
priority?: number | null
|
||||||
createdAt?: string | null
|
createdAt?: string | Date | null
|
||||||
updatedAt?: string | null
|
updatedAt?: string | Date | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BaseEmailTemplateDocument {
|
export interface BaseEmailTemplateDocument {
|
||||||
@@ -32,8 +35,8 @@ export interface BaseEmailTemplateDocument {
|
|||||||
slug: string
|
slug: string
|
||||||
subject?: string | null
|
subject?: string | null
|
||||||
content?: any
|
content?: any
|
||||||
createdAt?: string | null
|
createdAt?: string | Date | null
|
||||||
updatedAt?: string | null
|
updatedAt?: string | Date | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BaseEmail<TEmail extends BaseEmailDocument = BaseEmailDocument, TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmail, 'id' | 'template'> & {template: Omit<TEmailTemplate, 'id'> | TEmailTemplate['id'] | undefined | null}
|
export type BaseEmail<TEmail extends BaseEmailDocument = BaseEmailDocument, TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmail, 'id' | 'template'> & {template: Omit<TEmailTemplate, 'id'> | TEmailTemplate['id'] | undefined | null}
|
||||||
@@ -84,12 +87,12 @@ export interface QueuedEmail {
|
|||||||
subject: string
|
subject: string
|
||||||
html: string
|
html: string
|
||||||
text?: string | null
|
text?: string | null
|
||||||
variables?: Record<string, any> | null
|
variables?: JSONValue
|
||||||
scheduledAt?: string | null
|
scheduledAt?: string | Date | null
|
||||||
sentAt?: string | null
|
sentAt?: string | Date | null
|
||||||
status: 'pending' | 'processing' | 'sent' | 'failed'
|
status: 'pending' | 'processing' | 'sent' | 'failed'
|
||||||
attempts: number
|
attempts: number
|
||||||
lastAttemptAt?: string | null
|
lastAttemptAt?: string | Date | null
|
||||||
error?: string | null
|
error?: string | null
|
||||||
priority?: number | null
|
priority?: number | null
|
||||||
createdAt: string
|
createdAt: string
|
||||||
|
|||||||
Reference in New Issue
Block a user