feat: implement email notification worker with Mailgun integration
- Add MongoDB connection module for database access - Implement Mailgun email service for sending notifications - Add shareChecksum utility for generating secure share links - Implement three email sender functions: - Email verification requests (highest priority) - Rent due notifications (CET timezone) - Utility bills due notifications - Create main email worker with budget-based email sending - Add environment variables for configuration - Install dependencies: mongodb, mailgun.js, form-data - Update package.json description to reflect email worker purpose - Add .env.example with all required configuration The worker processes emails in priority order and respects a configurable budget to prevent overwhelming the mail server. All database operations are atomic and updates are performed immediately after each email send. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
53
email-worker/src/lib/dbClient.ts
Normal file
53
email-worker/src/lib/dbClient.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { MongoClient, Db } from 'mongodb';
|
||||
import { createLogger } from './logger';
|
||||
|
||||
const log = createLogger("db:client");
|
||||
|
||||
let client: MongoClient | null = null;
|
||||
let db: Db | null = null;
|
||||
|
||||
/**
|
||||
* Connect to MongoDB
|
||||
* @returns Database instance
|
||||
*/
|
||||
export async function connectToDatabase(): Promise<Db> {
|
||||
if (!process.env.MONGODB_URI) {
|
||||
throw new Error('MONGODB_URI environment variable is not set');
|
||||
}
|
||||
|
||||
if (db) {
|
||||
log('Reusing existing database connection');
|
||||
return db;
|
||||
}
|
||||
|
||||
log('Creating new database connection');
|
||||
client = new MongoClient(process.env.MONGODB_URI);
|
||||
await client.connect();
|
||||
db = client.db("utility-bills");
|
||||
|
||||
log('Connected to database');
|
||||
return db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from MongoDB
|
||||
*/
|
||||
export async function disconnectFromDatabase(): Promise<void> {
|
||||
if (client) {
|
||||
log('Disconnecting from database');
|
||||
await client.close();
|
||||
client = null;
|
||||
db = null;
|
||||
log('Disconnected from database');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current database instance (must call connectToDatabase first)
|
||||
*/
|
||||
export function getDatabase(): Db {
|
||||
if (!db) {
|
||||
throw new Error('Database not connected. Call connectToDatabase() first.');
|
||||
}
|
||||
return db;
|
||||
}
|
||||
Reference in New Issue
Block a user