Implemented complete MVP for geofencing functionality with database, backend logic, MQTT integration, and API endpoints. **Phase 1: Database & Core Logic** - scripts/init-geofence-db.js: Database initialization for Geofence tables - lib/types.ts: TypeScript types for Geofence, GeofenceEvent, GeofenceStatus - lib/geofence-engine.ts: Core geofencing logic (Haversine distance, state tracking) - lib/geofence-db.ts: Database layer with CRUD operations - package.json: Added db:init:geofence script **Phase 2: MQTT Integration & Email Notifications** - emails/geofence-enter.tsx: React Email template for enter events - emails/geofence-exit.tsx: React Email template for exit events - lib/email-renderer.ts: Added geofence email rendering functions - lib/geofence-notifications.ts: Notification service for geofence events - lib/mqtt-subscriber.ts: Integrated automatic geofence checking on location updates **Phase 3: Minimal API** - app/api/geofences/route.ts: GET (list) and POST (create) endpoints - app/api/geofences/[id]/route.ts: DELETE endpoint - All endpoints with authentication and ownership checks **MVP Simplifications:** - No zone limit enforcement (unlimited for all users) - No notification flags (always send Enter + Exit emails) - Device assignment required (no NULL device logic) - Circular geofences only **Features:** ✅ Automatic geofence detection on MQTT location updates ✅ Email notifications for enter/exit events ✅ State tracking to prevent duplicate events ✅ REST API for geofence management ✅ Non-blocking async processing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
/**
|
|
* Renders React Email templates to HTML
|
|
*/
|
|
import { render } from '@react-email/components';
|
|
import WelcomeEmail from '@/emails/welcome';
|
|
import PasswordResetEmail from '@/emails/password-reset';
|
|
import MqttCredentialsEmail from '@/emails/mqtt-credentials';
|
|
import GeofenceEnterEmail from '@/emails/geofence-enter';
|
|
import GeofenceExitEmail from '@/emails/geofence-exit';
|
|
|
|
export interface WelcomeEmailData {
|
|
username: string;
|
|
loginUrl: string;
|
|
temporaryPassword?: string;
|
|
}
|
|
|
|
export interface PasswordResetEmailData {
|
|
username: string;
|
|
resetUrl: string;
|
|
expiresIn?: string;
|
|
}
|
|
|
|
export interface MqttCredentialsEmailData {
|
|
deviceName: string;
|
|
deviceId: string;
|
|
mqttUsername: string;
|
|
mqttPassword: string;
|
|
brokerUrl: string;
|
|
brokerHost?: string;
|
|
brokerPort?: string;
|
|
}
|
|
|
|
export interface GeofenceEmailData {
|
|
username: string;
|
|
deviceName: string;
|
|
geofenceName: string;
|
|
timestamp: string;
|
|
latitude: number | string;
|
|
longitude: number | string;
|
|
distanceFromCenter: number;
|
|
mapUrl?: string;
|
|
}
|
|
|
|
export async function renderWelcomeEmail(data: WelcomeEmailData): Promise<string> {
|
|
return render(WelcomeEmail(data));
|
|
}
|
|
|
|
export async function renderPasswordResetEmail(data: PasswordResetEmailData): Promise<string> {
|
|
return render(PasswordResetEmail(data));
|
|
}
|
|
|
|
export async function renderMqttCredentialsEmail(data: MqttCredentialsEmailData): Promise<string> {
|
|
return render(MqttCredentialsEmail(data));
|
|
}
|
|
|
|
export async function renderGeofenceEnterEmail(data: GeofenceEmailData): Promise<string> {
|
|
return render(GeofenceEnterEmail(data));
|
|
}
|
|
|
|
export async function renderGeofenceExitEmail(data: GeofenceEmailData): Promise<string> {
|
|
return render(GeofenceExitEmail(data));
|
|
}
|
|
|
|
export async function renderEmailTemplate(
|
|
template: string,
|
|
data: any
|
|
): Promise<string> {
|
|
switch (template) {
|
|
case 'welcome':
|
|
return renderWelcomeEmail(data);
|
|
case 'password-reset':
|
|
return renderPasswordResetEmail(data);
|
|
case 'mqtt-credentials':
|
|
return renderMqttCredentialsEmail(data);
|
|
case 'geofence-enter':
|
|
return renderGeofenceEnterEmail(data);
|
|
case 'geofence-exit':
|
|
return renderGeofenceExitEmail(data);
|
|
default:
|
|
throw new Error(`Unknown email template: ${template}`);
|
|
}
|
|
}
|