|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { getCurrentUser } from '@/lib/users'; |
| 3 | +import { prisma } from '@/lib/db'; |
| 4 | +import { |
| 5 | + listProjectSyncStatus, |
| 6 | + listRecentSyncChanges, |
| 7 | +} from '@/lib/sync/sync.service'; |
| 8 | + |
| 9 | +export async function GET(request: NextRequest) { |
| 10 | + try { |
| 11 | + const user = await getCurrentUser(); |
| 12 | + if (!user) { |
| 13 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); |
| 14 | + } |
| 15 | + |
| 16 | + const projectId = request.nextUrl.searchParams.get('projectId'); |
| 17 | + if (!projectId) { |
| 18 | + return NextResponse.json({ error: 'projectId is required' }, { status: 400 }); |
| 19 | + } |
| 20 | + |
| 21 | + const project = await prisma.project.findFirst({ |
| 22 | + where: { id: projectId, userId: user.id }, |
| 23 | + select: { id: true }, |
| 24 | + }); |
| 25 | + if (!project) { |
| 26 | + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); |
| 27 | + } |
| 28 | + |
| 29 | + const [status, recentChanges] = await Promise.all([ |
| 30 | + listProjectSyncStatus(projectId, user.id), |
| 31 | + listRecentSyncChanges(projectId, user.id), |
| 32 | + ]); |
| 33 | + |
| 34 | + return NextResponse.json({ |
| 35 | + success: true, |
| 36 | + connected: status.connected, |
| 37 | + syncProject: status.syncProject |
| 38 | + ? { |
| 39 | + id: status.syncProject.id, |
| 40 | + repoName: status.syncProject.repoName, |
| 41 | + framework: status.syncProject.framework, |
| 42 | + privacyMode: status.syncProject.privacyMode, |
| 43 | + lastSyncedCommit: status.syncProject.lastSyncedCommit, |
| 44 | + } |
| 45 | + : null, |
| 46 | + lastScanTime: status.lastScanTime, |
| 47 | + recentChangesCount: status.recentChangesCount, |
| 48 | + pendingSuggestionsCount: status.pendingSuggestionsCount, |
| 49 | + generatedDocumentation: status.generatedDocumentation, |
| 50 | + recentChanges: recentChanges.map((change) => ({ |
| 51 | + id: change.id, |
| 52 | + fromCommit: change.fromCommit, |
| 53 | + toCommit: change.toCommit, |
| 54 | + branch: change.branch, |
| 55 | + status: change.status, |
| 56 | + changedFiles: change.changedFiles, |
| 57 | + createdAt: change.createdAt, |
| 58 | + })), |
| 59 | + }); |
| 60 | + } catch (error) { |
| 61 | + console.error('Error getting project sync status:', error); |
| 62 | + return NextResponse.json( |
| 63 | + { error: 'Failed to fetch sync status' }, |
| 64 | + { status: 500 } |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
0 commit comments