|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 4 | +import type { IDataEngine } from '@objectstack/spec/contracts'; |
| 5 | +import type { |
| 6 | + IEmailTransport, |
| 7 | + EmailAddress, |
| 8 | +} from '@objectstack/spec/contracts'; |
| 9 | +import { SysEmail } from '@objectstack/platform-objects/audit'; |
| 10 | +import { EmailService, LogTransport, type EmailPersistence } from './email-service.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Plugin configuration. |
| 14 | + */ |
| 15 | +export interface EmailServicePluginOptions { |
| 16 | + /** |
| 17 | + * Pluggable delivery transport. When omitted, a `LogTransport` is |
| 18 | + * installed which never sends mail — suitable for development. For |
| 19 | + * production, wire a concrete transport (nodemailer, Resend SDK, …). |
| 20 | + */ |
| 21 | + transport?: IEmailTransport; |
| 22 | + /** Default `From` address applied when `input.from` is omitted. */ |
| 23 | + defaultFrom?: EmailAddress; |
| 24 | + /** Persist each attempt to sys_email. Default true when ObjectQL engine present. */ |
| 25 | + persist?: boolean; |
| 26 | + /** Retry attempts on transport throw. Default 0. */ |
| 27 | + retries?: number; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * EmailServicePlugin — registers the `email` service. |
| 32 | + * |
| 33 | + * @example |
| 34 | + * ```ts |
| 35 | + * import { EmailServicePlugin } from '@objectstack/plugin-email'; |
| 36 | + * import nodemailer from 'nodemailer'; |
| 37 | + * |
| 38 | + * const smtp = nodemailer.createTransport({ host: 'smtp.example.com', port: 587 }); |
| 39 | + * const transport = { async send(msg) { const r = await smtp.sendMail(msg); return { messageId: r.messageId }; } }; |
| 40 | + * |
| 41 | + * kernel.use(new EmailServicePlugin({ |
| 42 | + * transport, |
| 43 | + * defaultFrom: { name: 'Acme CRM', address: 'no-reply@acme.com' }, |
| 44 | + * retries: 2, |
| 45 | + * })); |
| 46 | + * ``` |
| 47 | + */ |
| 48 | +export class EmailServicePlugin implements Plugin { |
| 49 | + name = 'com.objectstack.service.email'; |
| 50 | + version = '1.0.0'; |
| 51 | + type = 'standard'; |
| 52 | + dependencies = ['com.objectstack.engine.objectql']; |
| 53 | + |
| 54 | + private readonly options: EmailServicePluginOptions; |
| 55 | + private service?: EmailService; |
| 56 | + |
| 57 | + constructor(options: EmailServicePluginOptions = {}) { |
| 58 | + this.options = options; |
| 59 | + } |
| 60 | + |
| 61 | + async init(ctx: PluginContext): Promise<void> { |
| 62 | + // Register sys_email schema via manifest service. |
| 63 | + ctx.getService<{ register(m: any): void }>('manifest').register({ |
| 64 | + id: 'com.objectstack.service.email', |
| 65 | + name: 'Email Service', |
| 66 | + version: '1.0.0', |
| 67 | + type: 'plugin', |
| 68 | + scope: 'system', |
| 69 | + defaultDatasource: 'cloud', |
| 70 | + namespace: 'sys', |
| 71 | + objects: [SysEmail], |
| 72 | + }); |
| 73 | + |
| 74 | + const transport = this.options.transport ?? new LogTransport(ctx.logger); |
| 75 | + if (!this.options.transport) { |
| 76 | + ctx.logger.info( |
| 77 | + 'EmailServicePlugin: no transport configured — using LogTransport (mail will NOT be sent)', |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + // Persistence is wired in `start` once the ObjectQL engine is available; |
| 82 | + // here we register the service synchronously so dependents can resolve it. |
| 83 | + this.service = new EmailService({ |
| 84 | + transport, |
| 85 | + defaultFrom: this.options.defaultFrom, |
| 86 | + retries: this.options.retries, |
| 87 | + logger: ctx.logger, |
| 88 | + }); |
| 89 | + ctx.registerService('email', this.service); |
| 90 | + ctx.logger.info('EmailServicePlugin: email service registered'); |
| 91 | + } |
| 92 | + |
| 93 | + async start(ctx: PluginContext): Promise<void> { |
| 94 | + if (this.options.persist === false) return; |
| 95 | + ctx.hook('kernel:ready', async () => { |
| 96 | + let engine: IDataEngine | null = null; |
| 97 | + try { engine = ctx.getService<IDataEngine>('objectql'); } |
| 98 | + catch { try { engine = ctx.getService<IDataEngine>('data'); } catch { /* ignore */ } } |
| 99 | + if (!engine || !this.service) return; |
| 100 | + const persistence: EmailPersistence = { |
| 101 | + async insert(row) { |
| 102 | + const created = await (engine as any).insert('sys_email', row, { |
| 103 | + context: { isSystem: true, roles: [], permissions: [] }, |
| 104 | + }); |
| 105 | + return created?.id ? { id: String(created.id) } : { id: String(row.id) }; |
| 106 | + }, |
| 107 | + async update(id, patch) { |
| 108 | + await (engine as any).update('sys_email', id, patch, { |
| 109 | + context: { isSystem: true, roles: [], permissions: [] }, |
| 110 | + }); |
| 111 | + }, |
| 112 | + }; |
| 113 | + // Swap the service to persistence-enabled by re-constructing with same options. |
| 114 | + const upgraded = new EmailService({ |
| 115 | + transport: (this.service as any).options.transport, |
| 116 | + defaultFrom: (this.service as any).options.defaultFrom, |
| 117 | + retries: (this.service as any).options.retries, |
| 118 | + logger: ctx.logger, |
| 119 | + persistence, |
| 120 | + }); |
| 121 | + // Replace the registered instance via the same service name. |
| 122 | + ctx.registerService('email', upgraded); |
| 123 | + this.service = upgraded; |
| 124 | + ctx.logger.info('EmailServicePlugin: sys_email persistence enabled'); |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments