Implemented complete frontend for the Geofence MVP feature: **Pages:** - /admin/geofences - Management page with create/edit/delete modals - /admin/geofences/events - Event history with stats and filters - Dashboard widget showing active geofences and recent events **Features:** - Create/Edit geofences with device selection, coordinates, radius, and color - Toggle active/inactive status - View enter/exit events with notification status - Auto-refresh every 30 seconds - Zone limit enforcement (5 for users, unlimited for admins) - Stats cards showing total events, enters, exits, and notifications **API:** - GET /api/geofences/events - Fetch events with optional filters All frontend components follow the existing admin panel design system with gradient backgrounds, shadow effects, and responsive layouts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { auth } from "@/lib/auth";
|
|
import { geofenceDb } from "@/lib/geofence-db";
|
|
|
|
// GET /api/geofences/events - List all geofence events for the authenticated user
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const userId = (session.user as any).id;
|
|
|
|
// Get all geofences owned by this user
|
|
const userGeofences = geofenceDb.findByOwner(userId);
|
|
const geofenceIds = userGeofences.map((g) => g.id);
|
|
|
|
if (geofenceIds.length === 0) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
events: [],
|
|
total: 0,
|
|
});
|
|
}
|
|
|
|
// Parse query parameters
|
|
const { searchParams } = new URL(request.url);
|
|
const deviceId = searchParams.get("deviceId") || undefined;
|
|
const geofenceId = searchParams.get("geofenceId") || undefined;
|
|
const limit = parseInt(searchParams.get("limit") || "100");
|
|
|
|
// Get events (filtered by user's geofences)
|
|
let events = geofenceDb.findEvents({
|
|
deviceId,
|
|
geofenceId,
|
|
limit,
|
|
});
|
|
|
|
// Filter to only events from user's geofences
|
|
events = events.filter((event) => geofenceIds.includes(event.geofence_id));
|
|
|
|
// Enrich events with geofence and device names
|
|
const enrichedEvents = events.map((event) => {
|
|
const geofence = userGeofences.find((g) => g.id === event.geofence_id);
|
|
return {
|
|
...event,
|
|
geofenceName: geofence?.name || "Unknown",
|
|
geofenceColor: geofence?.color || "#gray",
|
|
};
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
events: enrichedEvents,
|
|
total: enrichedEvents.length,
|
|
});
|
|
} catch (error) {
|
|
console.error("[GET /api/geofences/events] Error:", error);
|
|
return NextResponse.json(
|
|
{
|
|
error: "Failed to fetch geofence events",
|
|
details: error instanceof Error ? error.message : "Unknown error",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|