Files
ProjectE/apps/web/app/api/time-summary/route.ts
T
mbatchelder 8f55626e03 refactor: migrate to monorepo structure with Docker, PocketBase, and e2e tests
- Reorganize into apps/, packages/, docs/, e2e/, pocketbase/ directories
- Add Dockerfiles for web, worker, and PocketBase services
- Add docker-compose.yml for local orchestration
- Add turbo.json for monorepo task management
- Add Playwright e2e test infrastructure
- Add PocketBase backend with migrations
- Remove Vite/Next.js/ESLint/PostCSS config files
- Update package.json with workspace dependencies
- Add .env.example and .dockerignore
2026-07-16 06:19:58 -04:00

56 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { withAuth } from '@/lib/auth';
import { createPocketBaseClient } from '@/lib/pocketbase';
// GET /api/time-summary — Aggregated time by domain/project/tag
export const GET = withAuth(async (request: NextRequest, _user) => {
const { searchParams } = new URL(request.url);
const startDate = searchParams.get('start') || new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString();
const endDate = searchParams.get('end') || new Date().toISOString();
const pb = createPocketBaseClient();
const entries = await pb.collection('task_time_entries').getFullList({
filter: `started_at >= "${startDate}" && started_at <= "${endDate}"`,
});
const byDomain: Record<string, number> = {};
const byProject: Record<string, number> = {};
const byTag: Record<string, number> = {};
let totalMinutes = 0;
for (const entry of entries) {
const duration = (entry.duration_minutes as number) || 0;
totalMinutes += duration;
// Get task for domain/project/tags
const task = await pb.collection('tasks').getOne(entry.task_id as string);
const domain = task.domain as string;
byDomain[domain] = (byDomain[domain] || 0) + duration;
const projectId = task.project_id as string | undefined;
if (projectId) {
byProject[projectId] = (byProject[projectId] || 0) + duration;
}
const tags = (task.tags as string[]) || [];
for (const tag of tags) {
byTag[tag] = (byTag[tag] || 0) + duration;
}
}
return NextResponse.json({
totalMinutes,
byDomain,
byProject,
byTag,
startDate,
endDate,
}, {
headers: {
'Cache-Control': 'private, max-age=300, stale-while-revalidate=600',
},
});
});