"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.emailService = exports.EmailService = void 0; const logger_1 = require("../utils/logger"); const nodemailer_1 = __importDefault(require("nodemailer")); class EmailService { static instance; transporter; constructor() { this.transporter = nodemailer_1.default.createTransport({ host: process.env['SMTP_HOST'] || 'smtp.gmail.com', port: parseInt(process.env['SMTP_PORT'] || '587'), secure: process.env['SMTP_SECURE'] === 'true', auth: { user: process.env['GOOGLE_EMAIL'], pass: process.env['GOOGLE_APP_PASSWORD'], }, }); this.verifyConnection(); } static getInstance() { if (!EmailService.instance) { EmailService.instance = new EmailService(); } return EmailService.instance; } async verifyConnection() { try { await this.transporter.verify(); logger_1.logger.info('SMTP connection verified successfully'); } catch (error) { logger_1.logger.error('SMTP connection failed:', error); } } async sendEmail(options) { try { const mailOptions = { from: { name: 'Court Docket System', address: process.env['GOOGLE_EMAIL'] || 'system@deafgain.org', }, to: options.to, subject: options.subject, html: options.html, }; const result = await this.transporter.sendMail(mailOptions); logger_1.logger.info('Email sent successfully:', { to: options.to, subject: options.subject, messageId: result.messageId, }); return true; } catch (error) { logger_1.logger.error('Failed to send email:', error); return false; } } async notifySubscribersOfNewEntry(entryTitle, entryDate, entrySummary) { try { const { prisma } = await Promise.resolve().then(() => __importStar(require('../index'))); const subscribers = await prisma.subscription.findMany({ where: { isActive: true }, }); if (subscribers.length === 0) { logger_1.logger.info('No active subscribers to notify'); return; } const subject = `New Court Filing: ${entryTitle}`; const html = this.generateNotificationEmail(entryTitle, entryDate, entrySummary); const emailPromises = subscribers.map((subscriber) => this.sendEmail({ to: subscriber.email, subject, html, })); const results = await Promise.allSettled(emailPromises); const successful = results.filter((result) => result.status === 'fulfilled').length; const failed = results.filter((result) => result.status === 'rejected').length; logger_1.logger.info(`Email notifications sent: ${successful} successful, ${failed} failed`); } catch (error) { logger_1.logger.error('Failed to notify subscribers:', error); } } generateNotificationEmail(title, date, summary) { const formattedDate = new Date(date).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', }); return ` New Court Filing Notification

Eliza Kragh v. Montana Association of the Deaf

New Court Filing Notification

A new filing has been added to the court docket:

${formattedDate}
${title}
${summary}

You can view the complete docket and any associated documents by visiting the court docket website:

View Court Docket
`; } } exports.EmailService = EmailService; exports.emailService = EmailService.getInstance(); //# sourceMappingURL=emailService.js.map