import type pg from 'pg'; import type { NotificationsRepository } from '../domain/ports.js'; import type { EmailMessage } from '../domain/notification.js'; interface MessageRow { event_id: string; template: string; recipient: string; status: string; } export class PgNotificationsRepository implements NotificationsRepository { constructor(private readonly pool: pg.Pool) {} async findByEventId( eventId: string, ): Promise<{ template: string; recipient: string; status: string } | undefined> { const result = await this.pool.query( 'SELECT event_id, template, recipient, status FROM notifications_messages WHERE event_id = $1', [eventId], ); const row = result.rows[0]; return row ? { template: row.template, recipient: row.recipient, status: row.status } : undefined; } async record(message: EmailMessage): Promise<{ created: boolean }> { const result = await this.pool.query( `INSERT INTO notifications_messages (event_id, template, recipient, subject, body) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (event_id) DO NOTHING RETURNING id`, [message.eventId, message.template, message.recipient, message.subject, message.body], ); return { created: (result.rowCount ?? 0) > 0 }; } }