Add comprehensive test suite and update configuration
- Add Jest testing framework configuration - Add test files for services, types, and utilities - Update package.json with Jest dependencies and test scripts - Enhance pre-commit checks to include testing - Add proper environment validation and error handling in mailgun service
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
// Mailgun Email Service
|
||||
// This service handles email sending via Mailgun API
|
||||
/**
|
||||
* Mailgun Email Service
|
||||
* This service handles email sending via Mailgun API
|
||||
*/
|
||||
|
||||
import {
|
||||
getMailgunConfig,
|
||||
isMailgunConfigured,
|
||||
isDevelopmentMode,
|
||||
type MailgunConfig,
|
||||
} from './mailgun.config';
|
||||
import { getMailgunConfig, type MailgunConfig } from './mailgun.config';
|
||||
|
||||
interface EmailTemplate {
|
||||
subject: string;
|
||||
@@ -97,19 +94,6 @@ export class MailgunService {
|
||||
|
||||
async sendEmail(to: string, template: EmailTemplate): Promise<boolean> {
|
||||
try {
|
||||
// In development mode or when Mailgun is not configured, just log the email
|
||||
if (isDevelopmentMode()) {
|
||||
console.warn('📧 Mock Email Sent (Development Mode):', {
|
||||
to,
|
||||
subject: template.subject,
|
||||
from: `${this.config.fromName} <${this.config.fromEmail}>`,
|
||||
html: template.html,
|
||||
text: template.text,
|
||||
note: 'To enable real emails, configure Mailgun credentials in environment variables',
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
// Production Mailgun API call
|
||||
const formData = new FormData();
|
||||
formData.append(
|
||||
@@ -147,7 +131,7 @@ export class MailgunService {
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
console.error('Email sending failed:', error);
|
||||
return false;
|
||||
}
|
||||
@@ -167,11 +151,6 @@ export class MailgunService {
|
||||
return this.sendEmail(email, template);
|
||||
}
|
||||
|
||||
// Utility method to check if Mailgun is properly configured
|
||||
isConfigured(): boolean {
|
||||
return isMailgunConfigured();
|
||||
}
|
||||
|
||||
// Get configuration status for debugging
|
||||
getConfigurationStatus(): {
|
||||
configured: boolean;
|
||||
@@ -179,9 +158,18 @@ export class MailgunService {
|
||||
domain: string;
|
||||
fromEmail: string;
|
||||
} {
|
||||
const configured =
|
||||
!!this.config.apiKey &&
|
||||
!!this.config.domain &&
|
||||
!!this.config.baseUrl &&
|
||||
!!this.config.fromEmail &&
|
||||
!!this.config.fromName;
|
||||
const mode: 'development' | 'production' = configured
|
||||
? 'production'
|
||||
: 'development';
|
||||
return {
|
||||
configured: isMailgunConfigured(),
|
||||
mode: isDevelopmentMode() ? 'development' : 'production',
|
||||
configured,
|
||||
mode,
|
||||
domain: this.config.domain,
|
||||
fromEmail: this.config.fromEmail,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user